Permalink
Browse files

The heuristic fonction like hell git add -A + optimize some imports

  • Loading branch information...
PereWoulpy committed Dec 6, 2017
1 parent 7a4935e commit 05af34026598c2f3d725ece357e37266c37c7a50
@@ -5,7 +5,7 @@
import java.util.ArrayList;
import java.util.Iterator;
public class ShortestLessCostTSP extends ShortestTSP{
public class NNHLessCostTSP extends NNHTSP {
@Override
protected Iterator<Integer> iterator(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows) {
return new LessCostIterator(crtNode, nonView, timeCost);
@@ -4,23 +4,23 @@
import java.util.ArrayList;
public class ShortestTSP extends NaiveTSP{
public class NNHTSP extends NaiveTSP {
@Override
protected float bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
protected boolean bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
ArrayList<Integer> toVisit = new ArrayList<>(nonView);
int nextIndex;
int index = crtNode;
float sum = 0.f;
for (int i = 0; i < nonView.size(); i++) {
nextIndex = getShortestRoute(timeCost[index], toVisit);
sum += timeCost[index][nextIndex];
sum += timeCost[index][nextIndex] + duration[nextIndex];
index = nextIndex;
}
//ajout du chemin le plus court vers le warehouse
sum += timeCost[index][timeCost.length - 1];
sum += shortestToWarehouse(nonView, timeCost);
return sum;
return (sum + crtCost) < bestSolutionCost;
}
protected int getShortestRoute(float[] timeCostCrtNode, ArrayList<Integer> nonView) {
@@ -40,4 +40,13 @@ protected int getShortestRoute(float[] timeCostCrtNode, ArrayList<Integer> nonVi
nonView.remove(indexNonView);
return index;
}
protected float shortestToWarehouse(ArrayList<Integer> nonView, float[][] timeCost) {
float min = Float.MAX_VALUE;
for (Integer i : nonView) {
if(min > timeCost[i][timeCost.length - 1])
min = timeCost[i][timeCost.length - 1];
}
return min;
}
}
@@ -0,0 +1,13 @@
package lsbdp.agile.algorithm;
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.Iterator;
public class NNHTimeLessCostTSP extends NNHTimeTSP {
@Override
protected Iterator<Integer> iterator(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows) {
return new LessCostIterator(crtNode, nonView, timeCost);
}
}
@@ -4,39 +4,27 @@
import java.util.ArrayList;
public class NNHTimeTSP extends ShortestLessCostTSP{
public class NNHTimeTSP extends NNHTSP {
@Override
protected float bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
protected boolean bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
ArrayList<Integer> toVisit = new ArrayList<>(nonView);
int nextIndex;
int index = crtNode;
if( !checkTimePossible(crtNode,nonView,timeWindows,crtCost) ){
return Float.MAX_VALUE; //infinite
if( !checkTimePossible(nonView,timeWindows,crtCost) ){
return false; //infinite
}
float sum = 0.f;
for (int i = 0; i < nonView.size(); i++) {
nextIndex = getShortestRoute(timeCost[index], toVisit);
sum += timeCost[index][nextIndex];
sum += timeCost[index][nextIndex] + duration[nextIndex];
index = nextIndex;
}
//ajout du chemin le plus court vers le warehouse
sum += timeCost[index][timeCost.length - 1];
sum += shortestToWarehouse(nonView, timeCost);
return sum;
}
public boolean checkTimePossible(int crtNode, ArrayList<Integer> nonView, Pair<Float, Float>[] timeWindows, float crtCost) {
float crtTime = crtCost;
for (Integer s : nonView) {
if(timeWindows[s] == null){}
else if (timeWindows[s].getValue() < crtTime) {
return false;
}
}
return true;
return (sum + crtCost) < bestSolutionCost;
}
}
@@ -5,10 +5,10 @@
import java.util.ArrayList;
import java.util.Iterator;
public class NaiveTSP extends TemplateTSP{
public class NaiveTSP extends TemplateTSP {
@Override
protected float bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
return 0;
protected boolean bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost) {
return true;
}
@Override

This file was deleted.

Oops, something went wrong.
@@ -3,7 +3,6 @@
import javafx.util.Pair;
import lsbdp.agile.model.*;
import java.lang.reflect.Array;
import java.util.*;
public abstract class TemplateTSP implements TSP {
@@ -14,12 +13,9 @@
protected float bestSolutionCost;
private boolean solutionFound;
private int count;
@Override
public void findSolution(DeliverySchedule schedule, StreetMap map, DeliveriesRequest req) {
schedule.setStartingTime(req.getStartingTime());
count = 0;
Intersection warehouse = req.getWarehouse();
List<Delivery> deliveries = req.getDeliveryList();
Route[][] graphTSP = Dijkstra.createTSPGraph(map, warehouse, deliveries);
@@ -65,15 +61,14 @@ public void findSolution(DeliverySchedule schedule, StreetMap map, DeliveriesReq
}
protected abstract float bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost);
protected abstract boolean bound(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows, float crtCost);
protected abstract Iterator<Integer> iterator(int crtNode, ArrayList<Integer> nonView, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows);
/*
When i visit a node, it means that i manage to get to the node and that it is possible to get there. the crtCost has been increment by its duration
*/
private void branchAndBound(int crtNode, ArrayList<Integer> nonView, ArrayList<Integer> view, float crtCost, float[][] timeCost, float[] duration, Pair<Float, Float>[] timeWindows) {
count++;
if (nonView.isEmpty()) { //this is the last node that has been visited
crtCost += timeCost[crtNode][timeCost.length - 1]; //we have to go back to the warehouse
if (crtCost < bestSolutionCost) { //we find a better solution
@@ -84,7 +79,7 @@ private void branchAndBound(int crtNode, ArrayList<Integer> nonView, ArrayList<I
solutionFound = true;
}
} else {
if (crtCost + bound(crtNode, nonView, timeCost, duration, timeWindows, crtCost) < bestSolutionCost) { //there are still nodes to visit
if (bound(crtNode, nonView, timeCost, duration, timeWindows, crtCost)) { //there are still nodes to visit
Iterator<Integer> it = iterator(crtNode, nonView, timeCost, duration, timeWindows);
while (it.hasNext()) {
Integer nextNode = it.next();
@@ -154,4 +149,14 @@ private void branchAndBound(int crtNode, ArrayList<Integer> nonView, ArrayList<I
return timeWindows;
}
protected boolean checkTimePossible(ArrayList<Integer> nonView, Pair<Float, Float>[] timeWindows, float crtCost) {
for (Integer s : nonView) {
if(timeWindows[s] == null){}
else if (timeWindows[s].getValue() < crtCost) {
return false;
}
}
return true;
}
}
@@ -1,7 +1,6 @@
package lsbdp.agile.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Date;
@@ -23,7 +22,7 @@
public Controller() {
Controller.schedule = new DeliverySchedule();
Controller.cmdList = new CommandList();
Controller.algo = new ShortestLessCostTSP();
Controller.algo = new NNHTimeTSP();
}
//Gérer Map
@@ -1,35 +1,24 @@
package lsbdp.agile.view;
import java.io.File;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.HBox;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.FileChooser;
import javafx.stage.Popup;
import lsbdp.agile.algorithm.Dijkstra;
import lsbdp.agile.algorithm.Scheduler;
import lsbdp.agile.controller.Controller;
import lsbdp.agile.data.SerializerXML;
import lsbdp.agile.model.Delivery;
import lsbdp.agile.model.DeliverySchedule;
import lsbdp.agile.model.Intersection;
import lsbdp.agile.model.Route;
import lsbdp.agile.model.StreetMap;
import java.io.File;
import java.text.ParseException;
import java.util.Date;
public class EventHandlers {
@@ -2,34 +2,30 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.SplitPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.FileChooser;
import javafx.util.Pair;
import lsbdp.agile.algorithm.Dijkstra;
import lsbdp.agile.algorithm.Scheduler;
import lsbdp.agile.controller.Controller;
import lsbdp.agile.data.SerializerXML;
import lsbdp.agile.model.*;
import java.io.File;
import java.text.ParseException;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class WindowManager{
@@ -19,7 +19,7 @@ public static void setUp() {
File f = new File("./Data/fichiersXML/planLyonGrand.xml");
map = SerializerXML.deserializeMapXML(f);
f = new File("./Data/fichiersXML/DLgrand10TW2.xml");
f = new File("./Data/fichiersXML/DLgrand20TW.xml");
req = SerializerXML.deserializeDeliveryXML(f, map);
}
@@ -40,7 +40,6 @@ public void testNaiveTSP() {
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
System.out.println(System.currentTimeMillis() - start);
}
@Test
@@ -50,37 +49,41 @@ public void testNaiveLessCostTSP() {
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
System.out.println(System.currentTimeMillis() - start);
}
@Test
public void testShortestTSP() {
TSP test = new ShortestTSP();
public void testNNHTSP() {
TSP test = new NNHTSP();
DeliverySchedule s = new DeliverySchedule();
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
System.out.println(System.currentTimeMillis() - start);
}
@Test
public void testShortestLessCostTSP() {
TSP test = new ShortestLessCostTSP();
public void testNNHLessCostTSP() {
TSP test = new NNHLessCostTSP();
DeliverySchedule s = new DeliverySchedule();
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
System.out.println(System.currentTimeMillis() - start);
}
@Test
public void testNNHTimeTSP() {
TSP test = new NNHTimeTSP();
DeliverySchedule s = new DeliverySchedule();
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
System.out.println(System.currentTimeMillis() - start);
}
@Test
public void testNNHTimeLessCostTSP() {
TSP test = new NNHTimeLessCostTSP();
DeliverySchedule s = new DeliverySchedule();
long start = System.currentTimeMillis();
test.findSolution(s, map, req);
}
}

0 comments on commit 05af340

Please sign in to comment.