on a commencé à implémenter l'algo du problème ouvert
This commit is contained in:
parent
a68e27f24d
commit
8de2ae972b
6 changed files with 114 additions and 13 deletions
|
|
@ -88,7 +88,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// Create the path from the array of predecessors...
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
Label nCourant = listLabel[dataInput.getDestination().getId()];
|
||||
while (nCourant.pere != null) {
|
||||
while (nCourant.getPere() != null) {
|
||||
arcs.add(nCourant.getPere());
|
||||
nCourant = listLabel[nCourant.getPere().getOrigin().getId()];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,103 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
public class Marathon extends ShortestPathAlgorithm {
|
||||
|
||||
public Marathon(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
public Label newLabelMarathon(Node s) {
|
||||
return new LabelMarathon(s, false, Double.MAX_VALUE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShortestPathSolution doRun() {
|
||||
|
||||
// retrieve data from the input problem (getInputData() is inherited from the
|
||||
// parent class ShortestPathAlgorithm)
|
||||
final ShortestPathData dataInput = getInputData();
|
||||
Graph graph = dataInput.getGraph();
|
||||
final int nbNodes = graph.size();
|
||||
Label[] listLabel = new Label[nbNodes];
|
||||
BinaryHeap<Label> tas = new BinaryHeap<>();
|
||||
|
||||
// variable that will contain the solution of the shortest path problem
|
||||
ShortestPathSolution solution;
|
||||
|
||||
// initialisation
|
||||
for (Node nod : graph.getNodes()) {
|
||||
listLabel[nod.getId()] = null;
|
||||
if (nod.equals(dataInput.getOrigin())) {
|
||||
listLabel[nod.getId()] = new Label(nod, false, 0, null);
|
||||
tas.insert(listLabel[nod.getId()]);
|
||||
notifyOriginProcessed(nod);
|
||||
}
|
||||
}
|
||||
|
||||
Node dest = dataInput.getOrigin().getSuccessors().get(0).getDestination() ;
|
||||
|
||||
Label xl = tas.findMin();
|
||||
boolean possible = True ;
|
||||
boolean fini = False ;
|
||||
|
||||
// iterations
|
||||
while (!tas.isEmpty() && fini && possible) {
|
||||
xl = tas.findMin();
|
||||
Node x = xl.getSommetCourant();
|
||||
notifyNodeMarked(x);
|
||||
xl.marque = true;
|
||||
possible = False ;
|
||||
for (Arc a : x.getSuccessors()) {
|
||||
if (dataInput.isAllowed(a)) {
|
||||
Node n = a.getDestination();
|
||||
if (listLabel[n.getId()] == null) {
|
||||
listLabel[n.getId()] = newLabelMarathon(n);
|
||||
}
|
||||
if (!listLabel[n.getId()].getMarque()) {
|
||||
possible = True ;
|
||||
listLabel[n.getId()].update(x,x.getCost());
|
||||
|
||||
if ((!n.equals(dest)) && (listLabel[n.getId()].getCost()) < 43){
|
||||
tas.insert(listLabel[n.getId()]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tas.remove(xl);
|
||||
}
|
||||
|
||||
// when the algorithm terminates, return the solution that has been found
|
||||
if (listLabel[dataInput.getDestination().getId()].getCost() == Double.MAX_VALUE) {
|
||||
solution = new ShortestPathSolution(dataInput, Status.INFEASIBLE);
|
||||
}
|
||||
else {
|
||||
// The destination has been found, notify the observers.
|
||||
notifyDestinationReached(dataInput.getDestination());
|
||||
|
||||
// Create the path from the array of predecessors...
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
Label nCourant = listLabel[dataInput.getDestination().getId()];
|
||||
while (nCourant.getPere() != null) {
|
||||
arcs.add(nCourant.getPere());
|
||||
nCourant = listLabel[nCourant.getPere().getOrigin().getId()];
|
||||
}
|
||||
// Reverse the path...
|
||||
Collections.reverse(arcs);
|
||||
|
||||
// Create the final solution.
|
||||
solution = new ShortestPathSolution(dataInput, Status.OPTIMAL,
|
||||
new Path(graph, arcs));
|
||||
}
|
||||
return solution;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -61,24 +61,22 @@ public class Launch {
|
|||
|
||||
// visit these directory to see the list of available files on commetud.
|
||||
final String[] mapName ={
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haiti-and-domrep.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/french-polynesia.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/fractal.mapgr"
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/france.mapgr"
|
||||
};
|
||||
final String[] pathName = {
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path"
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_bikini_motorcar.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_bikini_canal.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr_insa_tour.path"
|
||||
};
|
||||
|
||||
final Graph[] graph = new Graph[5];
|
||||
final Path[] path = new Path[5];
|
||||
final Graph[] graph = new Graph[4];
|
||||
final Path[] path = new Path[4];
|
||||
|
||||
for (int i = 0 ; i<5 ; i++) {
|
||||
for (int i = 0 ; i<4 ; i++) {
|
||||
// create a graph reader
|
||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName[i]))))) {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in a new issue