refactor(marathon): find next arc to remove to own function

This commit is contained in:
Paul Alnet 2024-05-25 15:15:35 +02:00
parent 26a662bb1b
commit 63da86ab0f

View file

@ -12,6 +12,8 @@ import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph; import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import java.util.Random; import java.util.Random;
@ -42,26 +44,17 @@ public class MarathonAlgorithm extends ShortestPathAlgorithm {
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution path = dijkstra.run(); ShortestPathSolution path = dijkstra.run();
while (path.getPath().getLength() < getDistance()) { if (!path.isFeasible())
// Recuperation indices return path;
// TODO : A MODIFIER POUR AMELIORER LA COHERENCE DU CHEMIN
List<Arc> pathMax = path.getPath().getArcs();
float max = 0;
int indiceArcToRemove = 0; //Math.abs(rand.nextInt() % (longueurPath - 1));
for (int i = 0; i < pathMax.size(); i++) {
if (pathMax.get(i).getLength() > max) {
max = pathMax.get(i).getLength();
indiceArcToRemove = i;
}
}
while (path.getPath().getLength() < getDistance()) {
// Recuperation Arc à supprimer // Recuperation Arc à supprimer
Arc arcToRemove = path.getPath().getArcs().get(indiceArcToRemove); Arc arcToRemove = getLongestArc(path.getPath());
Node originArcToRemove = arcToRemove.getOrigin(); Node originArcToRemove = arcToRemove.getOrigin();
Node destinationArcToRemove = arcToRemove.getDestination(); Node destinationArcToRemove = arcToRemove.getDestination();
// On le supprime dans une copie du tableau (getter Collections.unmodifiable dans Path.java) // On le supprime dans une copie du tableau (getter Collections.unmodifiable dans Path.java)
path.getPath().getArcs().remove(indiceArcToRemove); path.getPath().getArcs().remove(arcToRemove);
originArcToRemove.removeArc(arcToRemove); originArcToRemove.removeArc(arcToRemove);
// Creations du chemin entre les 2 noeuds dont l'arc a été supprimé // Creations du chemin entre les 2 noeuds dont l'arc a été supprimé
@ -88,5 +81,24 @@ public class MarathonAlgorithm extends ShortestPathAlgorithm {
public static int getDistance() { public static int getDistance() {
return distanceMarathon; return distanceMarathon;
} }
private Arc getLongestArc(Path path) {
// TODO : A MODIFIER POUR AMELIORER LA COHERENCE DU CHEMIN
final List<Arc> arcs = path.getArcs();
// we checked that the path is feasible in run
// so there should be at least one Arc (unless start = destination ?)
if (arcs.size() == 0)
return null;
Arc longestArc = arcs.get(0);
for (Arc arc : arcs) {
if (arc.getLength() > longestArc.getLength()) {
longestArc = arc;
}
}
return longestArc;
//int indiceArcToRemove = 0; //Math.abs(rand.nextInt() % (longueurPath - 1));
}
} }