Etape 5 terminee

This commit is contained in:
Nabzzz 2020-05-03 12:18:39 +02:00
parent a90cae2805
commit b48d1f16ec
7 changed files with 121 additions and 14 deletions

View file

@ -1,9 +1,38 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
public class AStarAlgorithm extends DijkstraAlgorithm { import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) { public AStarAlgorithm(ShortestPathData data) {
super(data); super(data);
} }
@Override protected void init(Label[] tabLabel, ShortestPathData data)
{ double maxspeed=data.getMaximumSpeed();
System.out.println("Max speed avant: "+maxspeed+"\n");
if(maxspeed==-1)
{
maxspeed=(data.getGraph().getGraphInformation().getMaximumSpeed());
//System.out.println("Max speed 2: "+maxspeed+"\n");
maxspeed=maxspeed/3.6;
}
System.out.println("Max speed 3: "+maxspeed+"\n");
if(data.getMode()==Mode.LENGTH)
{
maxspeed=1.0;
}
System.out.println("Max speed 4: "+maxspeed+"\n");
//Initialization
for(Node n: data.getGraph().getNodes())
{
tabLabel[n.getId()]=new LabelStar(n,Point.distance(n.getPoint(),data.getDestination().getPoint())/maxspeed);
}
//tabLabel[data.getOrigin().getId()].setCout(Point.distance(data.getOrigin().getPoint(),data.getDestination().getPoint())/maxspeed);
}
} }

View file

@ -16,7 +16,21 @@ 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 org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm { public class DijkstraAlgorithm extends ShortestPathAlgorithm {
protected void init(Label[] tabLabel, ShortestPathData data)
{
//Initialization
for(Node n:data.getGraph().getNodes())
{
tabLabel[n.getId()]=new Label(n);
}
//tabLabel[data.getOrigin().getId()].setCout(0);
}
public DijkstraAlgorithm(ShortestPathData data) { public DijkstraAlgorithm(ShortestPathData data) {
super(data); super(data);
} }
@ -28,12 +42,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Graph graph = data.getGraph(); Graph graph = data.getGraph();
final int nbNodes=graph.size(); final int nbNodes=graph.size();
BinaryHeap<Label> tas=new BinaryHeap<Label>(); BinaryHeap<Label> tas=new BinaryHeap<Label>();
double coutPrec=0.0;
Label []tabLabel=new Label[nbNodes]; Label []tabLabel=new Label[nbNodes];
init(tabLabel, data);
//Initialization //Initialization
for(Node n:graph.getNodes()) /*for(Node n:graph.getNodes())
{ {
tabLabel[n.getId()]=new Label(n); tabLabel[n.getId()]=new Label(n);
} }*/
tabLabel[data.getOrigin().getId()].setCout(0); tabLabel[data.getOrigin().getId()].setCout(0);
tas.insert(tabLabel[data.getOrigin().getId()]); tas.insert(tabLabel[data.getOrigin().getId()]);
this.notifyOriginProcessed(data.getOrigin()); this.notifyOriginProcessed(data.getOrigin());
@ -54,7 +70,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
tabLabel[X.getId()].setMarque(true); tabLabel[X.getId()].setMarque(true);
this.notifyNodeMarked(X); this.notifyNodeMarked(X);
System.out.println("Coût: "+tas.findMin().getCout()+"\n");
if(tabLabel[X.getId()].getCoutTotal()<coutPrec)
{
System.out.println("Problème! Les coûts ne sont pas croissants \n");
System.out.println("Coût: "+tabLabel[X.getId()].getCoutTotal()+"\n");
System.out.println("Coût précédent: "+ coutPrec +"\n");
}
coutPrec=tabLabel[X.getId()].getCoutTotal();
//nbMarque++; //nbMarque++;
try try
{ {
@ -89,7 +112,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
if(nbSuccesseurs==X.getNumberOfSuccessors()) if(nbSuccesseurs==X.getNumberOfSuccessors())
{ {
System.out.println("Nombre de successeurs OK \n"); //System.out.println("Nombre de successeurs OK \n");
} }
else else
{ {
@ -124,14 +147,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
if(p.getLength()==cout_total) if(p.getLength()==cout_total)
{ {
System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à Dijkstra \n"); System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à l'algo \n");
} }
else else
{ {
System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à Dijkstra \n"); System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à l'algo \n");
System.out.println("Cout Dijktra="+cout_total); System.out.println("Cout pcc="+cout_total);
System.out.println("Cout Dijktra="+p.getLength()); System.out.println("Cout algo="+p.getLength());
} }
/*noeuds.add(data.getDestination()); /*noeuds.add(data.getDestination());

View file

@ -47,13 +47,24 @@ public class Label implements java.lang.Comparable<Label>{
public int compareTo(Label B) public int compareTo(Label B)
{ {
if(this.getCout()<B.getCout()) if(this.getCoutTotal()<B.getCoutTotal())
{ {
return -1; return -1;
} }
else if(this.getCout()==B.getCout()) else if(this.getCoutTotal()==B.getCoutTotal())
{ {
return 0; if(this.getCoutEstime()<B.getCoutEstime())
{
return -1;
}
else if(this.getCoutEstime()>B.getCoutEstime())
{
return 1;
}
else
{
return 0;
}
} }
else else
{ {
@ -65,4 +76,13 @@ public class Label implements java.lang.Comparable<Label>{
this.cout = cout; this.cout = cout;
} }
public double getCoutEstime()
{
return 0;
}
public double getCoutTotal()
{
return cout+this.getCoutEstime();
}
} }

View file

@ -0,0 +1,35 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
public class LabelStar extends Label{
private double coutestime;
public LabelStar(Node sommetcourant, double coutestime)
{
super(sommetcourant);
this.coutestime=coutestime;
}
public double getCoutEstime()
{
return this.coutestime;
}
public double getCoutTotal()
{
return super.getCout()+this.coutestime;
}
public void setCoutEstime(double coutestime)
{
this.coutestime=coutestime;
}
}