version à jour sauf TestDijkstra

This commit is contained in:
Matteo Sabben 2025-05-26 18:43:36 +02:00
parent 9de19549ae
commit 27c1f7f5dc
3 changed files with 3 additions and 198 deletions

View file

@ -2,29 +2,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
<<<<<<< HEAD
public class LabelProblemeOuvert extends Label {
private double batteryLeft; //double pour la cohérence avec les autres couts qui sont en double eux aussi
public LabelProblemeOuvert(Node sommet, boolean marque, double cout, Arc pere, double batteryLeft) {
super(sommet, marque, cout, pere);
this.batteryLeft = batteryLeft;
}
public double getBatteryLeft() {
return batteryLeft;
}
public void setBatteryLeft(double batteryLeft) {
this.batteryLeft = batteryLeft;
}
@Override
public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo
return this.getCoutRealise();
}
=======
public class LabelProblemeOuvert extends Label {
private double autonomieRestante; //double pour la cohérence avec les autres couts qui sont en double eux aussi
@ -50,5 +28,4 @@ public class LabelProblemeOuvert extends Label {
public int compareTo(LabelProblemeOuvert other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}*/
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
}

View file

@ -1,3 +1,6 @@
/* je considère que les 200km d'autonomie atteint (200km avec le 22 kWh - Q90 Renault ZOE chargé à 90%)
cette autonomie est considéré en ville à 200km (à 30km/h de moyenne)
en mixte elle devient 154 km (47km/h de moyenne) ~3/4 de l'autonmie
@ -105,25 +108,17 @@ gérer le problème de la recharge, quand la faire ?
package org.insa.graphs.algorithm.shortestpath;
<<<<<<< HEAD
=======
import java.util.ArrayList;
import java.util.Collections;
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
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;
<<<<<<< HEAD
import org.insa.graphs.model.RoadInformation.RoadType;
=======
import org.insa.graphs.model.Path;
import org.insa.graphs.model.RoadInformation.RoadType;
//import org.insa.graphs.algorithm.shortestpath.ProblemeOuvert;
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
public class ProblemeOuvert extends DijkstraAlgorithm {
@ -139,75 +134,17 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
return new LabelProblemeOuvert(node, false, Double.POSITIVE_INFINITY, null, MAX_BATTERY);
}
<<<<<<< HEAD
// Surcharge pour compatibilité avec DijkstraAlgorithm
@Override
protected void tryUpdateLabel(Label[] labels, BinaryHeap<Label> heap, Node node,
double newCost, Arc pere) {
// Si c'est bien un label de probleme ouvert, on applique la logique spécifique
if (labels instanceof LabelProblemeOuvert[]) {
LabelProblemeOuvert[] plabels = (LabelProblemeOuvert[]) labels;
double batteryLeft = plabels[node.getId()].getBatteryLeft();
tryUpdateLabelSpecifique(plabels, heap, node, newCost, batteryLeft, pere);
} else {
// Sinon, on garde le comportement normal du Dijkstra
super.tryUpdateLabel(labels, heap, node, newCost, pere);
}
}
// Version spécifique pour le problème ouvert (pas d'@Override ici)
protected void tryUpdateLabelSpecifique(LabelProblemeOuvert[] labels, BinaryHeap<Label> heap, Node node,
double newCost, double newBatteryLeft, Arc pere) {
LabelProblemeOuvert label = labels[node.getId()];
// On ne met à jour que si on a assez de batterie
if (newBatteryLeft < 0) return;
if (newCost < label.getCoutRealise() || (Math.abs(newCost - label.getCoutRealise()) < 1e-6 && newBatteryLeft > label.getBatteryLeft())) {
=======
protected void tryUpdateLabel(LabelProblemeOuvert[] labels, BinaryHeap<Label> heap, Node node,
double newCost, double newBatteryLeft, Arc pere, Arc[] tableauArcs) {
LabelProblemeOuvert label = labels[node.getId()];
if (newCost < label.getCoutRealise()) {
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
if (label.getCoutRealise() != Double.POSITIVE_INFINITY) {
heap.remove(label);
}
label.setCoutRealise(newCost);
<<<<<<< HEAD
label.setBatteryLeft(newBatteryLeft);
label.setPere(pere);
heap.insert(label);
notifyNodeReached(node);
}
}
// Surcharge pour compatibilité avec DijkstraAlgorithm
// (Pas d'@Override ici car il n'y a pas de méthode à surcharger dans la superclasse)
protected void fonctionProblemeOuvert(RoadType typeDeRoute, Label[] labels, BinaryHeap<Label> heap, Node node,
double newCost, Arc pere) {
// Si c'est bien un label de probleme ouvert, on applique la logique spécifique
if (labels instanceof LabelProblemeOuvert[]) {
fonctionProblemeOuvertSpecifique(typeDeRoute, (LabelProblemeOuvert[]) labels, heap, node, newCost, pere);
} else {
// Sinon, on garde le comportement normal du Dijkstra
super.fonctionProblemeOuvert(typeDeRoute, labels, heap, node, newCost, pere);
}
}
// Version spécifique pour le problème ouvert (pas d'@Override ici)
protected void fonctionProblemeOuvertSpecifique(RoadType typeDeRoute, LabelProblemeOuvert[] labels, BinaryHeap<Label> heap, Node node,
double newCost, Arc pere) {
if (typeDeRoute == RoadType.MOTORWAY) {
tryUpdateLabelSpecifique(labels, heap, node, newCost, MAX_BATTERY, pere);
}
}
=======
label.setAutonomieRestante(newBatteryLeft);
label.setPere(pere);
@ -229,16 +166,12 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
}*/
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
<<<<<<< HEAD
=======
// accès au graphe
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
Graph graph = data.getGraph();
int nbNodes = graph.size();
@ -249,18 +182,10 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
// Initialisation des labels
for (Node node : graph.getNodes()) {
<<<<<<< HEAD
labels[node.getId()] = createLabel(node);
}
// Origine : coût 0, batterie pleine
labels[data.getOrigin().getId()].setCoutRealise(0);
labels[data.getOrigin().getId()].setBatteryLeft(MAX_BATTERY);
=======
labels[node.getId()] =createLabel(node);
}
// Origine : coût 0, non marqué, pas de père
labels[data.getOrigin().getId()].setCoutRealise(0);
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
BinaryHeap<Label> heap = new BinaryHeap<>();
heap.insert(labels[data.getOrigin().getId()]);
@ -268,17 +193,6 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
notifyOriginProcessed(data.getOrigin());
while (!heap.isEmpty()) {
<<<<<<< HEAD
LabelProblemeOuvert currentLabel = (LabelProblemeOuvert) heap.deleteMin();
Node currentNode = currentLabel.getSommetCourant();
// sommet traité
currentLabel.setMarque(true);
// On a atteint la destination on s'arrête
if (currentNode.equals(data.getDestination())) {
notifyDestinationReached(currentNode);
=======
Label LabelActuel = heap.deleteMin();
Node NodeActuel = LabelActuel.getSommetCourant();
@ -288,60 +202,10 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
// On a atteint la destination on s'arrête
if (NodeActuel.equals(data.getDestination())) {
notifyDestinationReached(NodeActuel);
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
break;
}
// Pour chaque successeur
<<<<<<< HEAD
for (Arc arc : currentNode.getSuccessors()) {
if (!data.isAllowed(arc)) continue;
Node succ = arc.getDestination();
LabelProblemeOuvert succLabel = labels[succ.getId()];
if (succLabel.getMarque()) continue;
double arcCost = data.getCost(arc);
double batteryLeft = currentLabel.getBatteryLeft();
// Calcul de la batterie restante après avoir parcouru l'arc
double newBatteryLeft = batteryLeft - arcCost;
double newCost = currentLabel.getCoutRealise() + arcCost;
// Si pas assez de batterie, on ne continue pas
if (newBatteryLeft < 0) continue;
// Mise à jour du label si meilleur coût ou plus de batterie à coût égal
if (newCost < succLabel.getCoutRealise() ||
(Math.abs(newCost - succLabel.getCoutRealise()) < 1e-6 && newBatteryLeft > succLabel.getBatteryLeft())) {
if (succLabel.getCoutRealise() != Double.POSITIVE_INFINITY) {
heap.remove(succLabel);
}
succLabel.setCoutRealise(newCost);
succLabel.setBatteryLeft(newBatteryLeft);
succLabel.setPere(arc);
heap.insert(succLabel);
notifyNodeReached(succ);
}
// Si on arrive sur une autoroute, possibilité de recharger
if (arc.getRoadInformation().getType() == RoadType.MOTORWAY) {
double rechargeCost = newCost; // ici, pas de coût supplémentaire pour la recharge
double rechargeBattery = MAX_BATTERY;
if (rechargeCost < succLabel.getCoutRealise() ||
(Math.abs(rechargeCost - succLabel.getCoutRealise()) < 1e-6 && rechargeBattery > succLabel.getBatteryLeft())) {
if (succLabel.getCoutRealise() != Double.POSITIVE_INFINITY) {
heap.remove(succLabel);
}
succLabel.setCoutRealise(rechargeCost);
succLabel.setBatteryLeft(rechargeBattery);
succLabel.setPere(arc);
heap.insert(succLabel);
notifyNodeReached(succ);
}
}
=======
for (Arc arc : NodeActuel.getSuccessors()) {
if (!data.isAllowed(arc)) continue; // ici c'est pour l'amélioration voiture ou a pied
@ -367,30 +231,15 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
if (arc.getRoadInformation().getType() == RoadType.MOTORWAY) {
tryUpdateLabel(labels, heap, succ, newCost, MAX_BATTERY, arc,predecessorArcs);
}
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
}
}
// Construction de la solution
<<<<<<< HEAD
if (labels[data.getDestination().getId()].getPere() == null) {
=======
if (predecessorArcs[data.getDestination().getId()] == null) {
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
// Reconstruire le chemin
<<<<<<< HEAD
java.util.ArrayList<Arc> arcs = new java.util.ArrayList<>();
Arc arc = labels[data.getDestination().getId()].getPere();
while (arc != null) {
arcs.add(arc);
arc = labels[arc.getOrigin().getId()].getPere();
}
java.util.Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new org.insa.graphs.model.Path(graph, arcs));
=======
ArrayList<Arc> arcs = new ArrayList<>();
Arc arc = predecessorArcs[data.getDestination().getId()];
while (arc != null) {
@ -399,7 +248,6 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
}
Collections.reverse(arcs);// on inverse le chemin
solution = new ShortestPathSolution(data, Status.OPTIMAL,new Path(graph, arcs));
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
}
return solution;

View file

@ -12,11 +12,6 @@ import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
<<<<<<< HEAD:be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/TestDijkstra.java
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.simple.Launch;
=======
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0:be-graphes-gui/src/test/java/TestDijkstra.java
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
@ -162,26 +157,11 @@ public class TestDijkstra {
// System.out.println("== Test bug dijkstra temps ==");
// testScenario("paris.mapgr", 27361, 36108, Mode.TIME, true,false,false); // ce test mettait en lumière un problème d'arrondi qui est mtn résolu.
<<<<<<< HEAD:be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/TestDijkstra.java
//System.out.println("== Trajet impossible (piste cyclable) ==");
//testScenario("insa.mapgr",90,922 , Mode.LENGTH, false,false,true);
//System.out.println("== Trajet impossible (piste cyclable) == sans restriction");
//testScenario("insa.mapgr",90,922 , Mode.LENGTH, true,false,false); //marche pas
//scénarios de comparaison Dijkstra vs Astar
System.out.println("== Dijkstra vs Astar (en temps) ==");
testScenario("paris.mapgr", 27361, 36108, Mode.TIME, true,false,false);
testScenario("paris.mapgr", 27361, 36108, Mode.TIME, true,true,false);
=======
System.out.println("== Trajet impossible (piste cyclable) ==");
testScenario("insa.mapgr",90,922 , Mode.LENGTH, false,false,true);
System.out.println("== Trajet impossible (piste cyclable) == sans restriction");
testScenario("insa.mapgr",90,922 , Mode.LENGTH, false,false,false); //marche pas
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0:be-graphes-gui/src/test/java/TestDijkstra.java
}
}