This commit is contained in:
bezza 2025-05-26 18:31:55 +02:00
commit 9de19549ae
6 changed files with 208 additions and 9 deletions

View file

@ -21,7 +21,13 @@ public class MyArcInspector implements ArcInspector {
boolean result=true;
if (this.restreint){
RoadInformation.RoadType type=arc.getRoadInformation().getType();
if (type==RoadInformation.RoadType.PEDESTRIAN || type==RoadInformation.RoadType.CYCLEWAY ) {
if (type != RoadInformation.RoadType.MOTORWAY &&
type != RoadInformation.RoadType.TRUNK &&
type != RoadInformation.RoadType.PRIMARY &&
type != RoadInformation.RoadType.SECONDARY &&
type != RoadInformation.RoadType.TERTIARY &&
type != RoadInformation.RoadType.RESIDENTIAL &&
type != RoadInformation.RoadType.UNCLASSIFIED ) {
result=false;
}
}

View file

@ -9,7 +9,8 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
@Override
protected LabelStar createLabel(Node node) {
return new LabelStar(node, false, Double.POSITIVE_INFINITY, null, getInputData().getDestination());
ShortestPathData data = getInputData(); //pour récupérer la vitesse maximale du graphe, la destination et si on est en fastestpath ou shortestpath
return new LabelStar(node, false, Double.POSITIVE_INFINITY,null,data);
}

View file

@ -2,6 +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
@ -23,4 +24,31 @@ public class LabelProblemeOuvert extends Label {
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
public LabelProblemeOuvert(Node sommet, boolean marque, double cout, Arc pere, double autonomieRestante) {
super(sommet, marque, cout, pere);
this.autonomieRestante = autonomieRestante;
}
public double getAutonomieRestante() {
return autonomieRestante;
}
public void setAutonomieRestante(double autonomieRestante) {
this.autonomieRestante = autonomieRestante;
}
@Override
public double getTotalCost() { //normalement pas besoin
return this.getCoutRealise();
}
/*@Override
public int compareTo(LabelProblemeOuvert other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}*/
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
}

View file

@ -1,5 +1,6 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
@ -7,30 +8,70 @@ import org.insa.graphs.model.Point;
public class LabelStar extends Label {
private Node destination;
public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere,Node destination) {
;
private ShortestPathData pathData;
public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere, ShortestPathData data) {
super(sommetCourant, marque, coutRealise, pere);
this.destination=destination;
this.pathData = data; //pour récupérer la vitesse maximale du graphe, la destination et si on est en fastestpath ou shortestpath
}
//pour optimiser l'algo : ATTENTION PAS à L'initialisation pas le faire sinon ça va le faire pr tout les pts
//on rajoute un paremètre distance ds le label et quand on passe sur le label si c'est linfini on le calcule
@Override
/*@Override
public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo
//System.out.println("cout realise : " + getCoutRealise()+ "\n distance : "distance(getSommetCourant().getPoint(),this.destination.getPoint()));
double cout=getCoutRealise();
Point calcul = this.destination.getPoint();
Point calcul = this.pathData.getDestination().getPoint();
cout+=calcul.distanceTo(this.getSommetCourant().getPoint());
return (cout);
}
}*/
//pas nécessaire normalement
/*public int compareTo(LabelStar other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}*/
/**
* Calcule le coût total estimé (f = g + h) pour l'algorithme A*.
* g (getCoutRealise()) est le coût actuel depuis le départ.
* h est l'heuristique :
* - En mode DISTANCE: distance à vol d'oiseau (via distanceTo) jusqu'à la destination.
* - En mode TEMPS: distance à vol d'oiseau (via distanceTo) / vitesse maximale sur le graphe.
* @return Le coût total estimé.
*/
@Override
public double getTotalCost() {
double gCost = getCoutRealise(); // Coût actuel depuis l'origine (distance ou temps)
double hCost = 0.0;
Node current = getSommetCourant();
Node destinationNode = this.pathData.getDestination(); // Obtention de la destination depuis pathData
Point currentPoint = current.getPoint();
Point destinationPoint = destinationNode.getPoint();
if (currentPoint == null || destinationPoint == null) {
return gCost; // Heuristique nulle si points non valides
}
//calcul vol d'oiseau
double distanceToDestinationMeters = currentPoint.distanceTo(destinationPoint);
//différencier TIME et DISTANCE
if (this.pathData.getMode() == AbstractInputData.Mode.TIME) {
int maxSpeedKmH = this.pathData.getGraph().getGraphInformation().getMaximumSpeed();
// Conversion de la vitesse max en mètres par seconde (m/s)
double maxSpeedMpS = maxSpeedKmH / 3.6;
hCost = distanceToDestinationMeters / maxSpeedMpS; // hCost est maintenant en secondes
} else {
// Heuristique pour le mode DISTANCE (ou par défaut)
hCost = distanceToDestinationMeters; // hCost est en mètres
}
return gCost + hCost;
}
}

View file

@ -105,13 +105,25 @@ 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 {
@ -127,6 +139,7 @@ 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,
@ -151,11 +164,20 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
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);
@ -185,11 +207,38 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
}
}
=======
label.setAutonomieRestante(newBatteryLeft);
label.setPere(pere);
tableauArcs[pere.getDestination().getId()] = pere;
heap.insert(label);
notifyNodeReached(node);
}
}
/*@Override
protected void fonctionProblemeOuvert(RoadType typeDeRoute,LabelProblemeOuvert[] labels, BinaryHeap<LabelProblemeOuvert> heap, Node node,
double newCost, Arc pere){
if (typeDeRoute == RoadType.MOTORWAY) {
tryUpdateLabel(labels, heap,node, newCost, MAX_BATTERY, pere);
}
}*/
>>>>>>> 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();
@ -200,11 +249,18 @@ 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()]);
@ -212,6 +268,7 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
notifyOriginProcessed(data.getOrigin());
while (!heap.isEmpty()) {
<<<<<<< HEAD
LabelProblemeOuvert currentLabel = (LabelProblemeOuvert) heap.deleteMin();
Node currentNode = currentLabel.getSommetCourant();
@ -221,10 +278,22 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
// On a atteint la destination on s'arrête
if (currentNode.equals(data.getDestination())) {
notifyDestinationReached(currentNode);
=======
Label LabelActuel = heap.deleteMin();
Node NodeActuel = LabelActuel.getSommetCourant();
// sommet traité
LabelActuel.setMarque(true);
// 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;
@ -272,15 +341,47 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
notifyNodeReached(succ);
}
}
=======
for (Arc arc : NodeActuel.getSuccessors()) {
if (!data.isAllowed(arc)) continue; // ici c'est pour l'amélioration voiture ou a pied
Node succ = arc.getDestination();
LabelProblemeOuvert succLabel = labels[succ.getId()];
if (succLabel.getMarque()) continue;
double arcCost = data.getCost(arc);
double batteryLeft = ((LabelProblemeOuvert) LabelActuel).getAutonomieRestante();
if (batteryLeft < arcCost) continue; // Pas assez de batterie pour ce chemin
double newCost = LabelActuel.getCoutRealise() + arcCost;
double newBatteryLeft = batteryLeft - arcCost;
// 1. On fais le test d'avant qui vérfie si le coût avec ce chemin est meilleur
tryUpdateLabel(labels, heap, succ, newCost, newBatteryLeft, arc,predecessorArcs);
// 2. On fait aussi le test avec une autonmie pleine pour voir si la solution est meilleure (il faudra rajouter 2mn au temps de trajet)
//si on recharge effectivement et détecter cette recharge
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) {
@ -289,6 +390,16 @@ public class ProblemeOuvert extends DijkstraAlgorithm {
}
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) {
arcs.add(arc);
arc = predecessorArcs[arc.getOrigin().getId()];
}
Collections.reverse(arcs);// on inverse le chemin
solution = new ShortestPathSolution(data, Status.OPTIMAL,new Path(graph, arcs));
>>>>>>> 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0
}
return solution;

View file

@ -12,8 +12,11 @@ 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;
@ -159,6 +162,7 @@ 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);
@ -171,5 +175,13 @@ public class TestDijkstra {
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
}
}