File src/salesman/algorithms/SimAnn.java changed (mode: 100644) (index 2d88787..3c13013) |
1 |
1 |
package salesman.algorithms; |
package salesman.algorithms; |
2 |
2 |
|
|
|
3 |
|
import javafx.application.Platform; |
3 |
4 |
import org.joda.time.DateTime; |
import org.joda.time.DateTime; |
|
5 |
|
import org.joda.time.Period; |
|
6 |
|
import org.joda.time.format.PeriodFormatter; |
|
7 |
|
import org.joda.time.format.PeriodFormatterBuilder; |
4 |
8 |
import salesman.data.Order; |
import salesman.data.Order; |
5 |
9 |
import salesman.ui.MainWindowController; |
import salesman.ui.MainWindowController; |
6 |
10 |
|
|
|
... |
... |
public class SimAnn extends Thread { |
25 |
29 |
MainWindowController.data.setAlgorithmRunning(false); |
MainWindowController.data.setAlgorithmRunning(false); |
26 |
30 |
return; |
return; |
27 |
31 |
} |
} |
28 |
|
|
|
29 |
|
sigma-= sigma*0.001; |
|
30 |
|
// System.out.println("Sigma: "+sigma); |
|
|
32 |
|
double test = (new Double(1)/(currentOrder.getSize()*currentOrder.getSize())); |
|
33 |
|
sigma-= sigma*test; |
|
34 |
|
System.out.println("Sigma: "+sigma); |
31 |
35 |
int a = r.nextInt(currentOrder.getSize()); |
int a = r.nextInt(currentOrder.getSize()); |
32 |
36 |
int b = r.nextInt(currentOrder.getSize()); |
int b = r.nextInt(currentOrder.getSize()); |
33 |
37 |
|
|
|
... |
... |
public class SimAnn extends Thread { |
35 |
39 |
currentOrder.swapValues(a, b); |
currentOrder.swapValues(a, b); |
36 |
40 |
|
|
37 |
41 |
int length = currentOrder.getTotalLength(MainWindowController.data.getCities()); |
int length = currentOrder.getTotalLength(MainWindowController.data.getCities()); |
|
42 |
|
System.out.println(bestLength-length); |
38 |
43 |
if (length< bestLength) { |
if (length< bestLength) { |
39 |
44 |
MainWindowController.data.setBestEver(currentOrder.copy()); |
MainWindowController.data.setBestEver(currentOrder.copy()); |
40 |
45 |
MainWindowController.data.getCurrentOrder().setOrder(currentOrder.getOrder()); |
MainWindowController.data.getCurrentOrder().setOrder(currentOrder.getOrder()); |
|
... |
... |
public class SimAnn extends Thread { |
57 |
62 |
Thread.sleep(10); |
Thread.sleep(10); |
58 |
63 |
} catch (InterruptedException e) { |
} catch (InterruptedException e) { |
59 |
64 |
e.printStackTrace(); |
e.printStackTrace(); |
|
65 |
|
interrupt(); |
60 |
66 |
} |
} |
61 |
67 |
} |
} |
62 |
68 |
if(sigma<1){ |
if(sigma<1){ |
63 |
69 |
running=false; |
running=false; |
64 |
70 |
} |
} |
65 |
|
if(cancel>40){ |
|
|
71 |
|
/* if(cancel>40){ |
66 |
72 |
running=false; |
running=false; |
67 |
|
} |
|
|
73 |
|
}*/ |
68 |
74 |
|
|
69 |
75 |
} |
} |
70 |
76 |
DateTime end = new DateTime(); |
DateTime end = new DateTime(); |
|
77 |
|
Period p = new Period(start,end); |
|
78 |
|
PeriodFormatter mmssFormat = new PeriodFormatterBuilder() |
|
79 |
|
/*.printZeroAlways()*/.minimumPrintedDigits(2).appendMinutes().appendSuffix("min").appendSeparator(" : ") |
|
80 |
|
.appendSeconds().appendSuffix("sec").appendMillis().appendSuffix("ms").toFormatter(); |
|
81 |
|
|
|
82 |
|
Platform.runLater(MainWindowController.getNewSetDataonWindow(mmssFormat.print(p), bestLength, bestLength)); |
71 |
83 |
MainWindowController.data.setAlgorithmRunning(false); |
MainWindowController.data.setAlgorithmRunning(false); |
|
84 |
|
MainWindowController.finishedAlgorithm(); |
72 |
85 |
|
|
73 |
86 |
} |
} |
74 |
87 |
} |
} |
File src/salesman/ui/MainWindowController.java changed (mode: 100644) (index 349f16b..d3ffd5b) |
1 |
1 |
package salesman.ui; |
package salesman.ui; |
2 |
2 |
|
|
3 |
3 |
import javafx.application.Platform; |
import javafx.application.Platform; |
4 |
|
import javafx.beans.value.ChangeListener; |
|
5 |
|
import javafx.beans.value.ObservableValue; |
|
6 |
4 |
import javafx.collections.FXCollections; |
import javafx.collections.FXCollections; |
7 |
5 |
import javafx.collections.ObservableList; |
import javafx.collections.ObservableList; |
8 |
6 |
import javafx.fxml.FXML; |
import javafx.fxml.FXML; |
|
... |
... |
import javafx.scene.layout.VBox; |
16 |
14 |
import javafx.scene.paint.Color; |
import javafx.scene.paint.Color; |
17 |
15 |
import salesman.data.City; |
import salesman.data.City; |
18 |
16 |
import salesman.data.Data; |
import salesman.data.Data; |
19 |
|
import salesman.data.ObservableBoolean; |
|
20 |
17 |
import salesman.data.Order; |
import salesman.data.Order; |
21 |
18 |
|
|
22 |
19 |
import java.io.IOException; |
import java.io.IOException; |
|
20 |
|
import java.util.Timer; |
|
21 |
|
import java.util.TimerTask; |
23 |
22 |
|
|
24 |
23 |
|
|
25 |
24 |
public class MainWindowController { |
public class MainWindowController { |
|
... |
... |
public class MainWindowController { |
36 |
35 |
@FXML |
@FXML |
37 |
36 |
private CheckBox checkBoxMeasure; |
private CheckBox checkBoxMeasure; |
38 |
37 |
@FXML |
@FXML |
39 |
|
protected Label lblCurrentLength, lblShortestLength,lblTime; |
|
|
38 |
|
protected Label lblCurrentLength, lblShortestLength, lblTime; |
40 |
39 |
|
|
41 |
40 |
private static MainWindowController actuallController; |
private static MainWindowController actuallController; |
42 |
41 |
|
|
43 |
|
public static SetDataOnWindow getNewSetDataonWindow(String time, double sl,double cl){ |
|
44 |
|
return new SetDataOnWindow(actuallController,time,sl,cl); |
|
|
42 |
|
public static SetDataOnWindow getNewSetDataonWindow(String time, double sl, double cl) { |
|
43 |
|
return new SetDataOnWindow(actuallController, time, sl, cl); |
|
44 |
|
} |
|
45 |
|
|
|
46 |
|
public static void finishedAlgorithm() { |
|
47 |
|
Platform.runLater(new Runnable() { |
|
48 |
|
@Override |
|
49 |
|
public void run() { |
|
50 |
|
Alert a = new Alert(Alert.AlertType.INFORMATION); |
|
51 |
|
a.setTitle("Fertig"); |
|
52 |
|
a.setContentText("Fertig"); |
|
53 |
|
a.show(); |
|
54 |
|
} |
|
55 |
|
}); |
45 |
56 |
} |
} |
46 |
57 |
|
|
47 |
58 |
public int getNumOfCities() { |
public int getNumOfCities() { |
|
... |
... |
public class MainWindowController { |
50 |
61 |
} |
} |
51 |
62 |
|
|
52 |
63 |
public static Data data = new Data(10); |
public static Data data = new Data(10); |
53 |
|
public static ObservableBoolean repaint = new ObservableBoolean(); |
|
54 |
64 |
|
|
55 |
65 |
private GridPane actualMenu; |
private GridPane actualMenu; |
56 |
66 |
|
|
|
... |
... |
public class MainWindowController { |
59 |
69 |
public void initialize() { |
public void initialize() { |
60 |
70 |
ObservableList<String> items = FXCollections.observableArrayList(); |
ObservableList<String> items = FXCollections.observableArrayList(); |
61 |
71 |
cBalgorithm.setItems(items); |
cBalgorithm.setItems(items); |
62 |
|
cBalgorithm.getItems().addAll("BrutForce","2-Opt","Simulated Annealing"); |
|
63 |
|
|
|
|
72 |
|
cBalgorithm.getItems().addAll("BrutForce", "2-Opt", "Simulated Annealing"); |
64 |
73 |
|
|
65 |
|
repaint.addListener(new RepaintListener()); |
|
66 |
|
repaint.set(true); |
|
67 |
|
actuallController=this; |
|
68 |
74 |
|
|
|
75 |
|
actuallController = this; |
69 |
76 |
|
|
|
77 |
|
new Timer(true).scheduleAtFixedRate(new TimerTask() { |
|
78 |
|
@Override |
|
79 |
|
public void run() { |
|
80 |
|
Platform.runLater(new Repaint()); |
|
81 |
|
} |
|
82 |
|
}, 0, 17); |
70 |
83 |
}//initialize() |
}//initialize() |
71 |
84 |
|
|
72 |
85 |
@FXML |
@FXML |
73 |
|
private void cBalgorithmOnAction(){ |
|
|
86 |
|
private void cBalgorithmOnAction() { |
74 |
87 |
|
|
75 |
88 |
try { |
try { |
76 |
89 |
loadNewMenu(cBalgorithm.getSelectionModel().getSelectedItem()); |
loadNewMenu(cBalgorithm.getSelectionModel().getSelectedItem()); |
|
... |
... |
public class MainWindowController { |
82 |
95 |
} |
} |
83 |
96 |
|
|
84 |
97 |
@FXML |
@FXML |
85 |
|
private void checkBoxMeasureOnAction(){ |
|
|
98 |
|
private void checkBoxMeasureOnAction() { |
86 |
99 |
data.setMeasurement(checkBoxMeasure.isSelected()); |
data.setMeasurement(checkBoxMeasure.isSelected()); |
87 |
100 |
} |
} |
88 |
101 |
|
|
89 |
102 |
|
|
90 |
|
|
|
91 |
103 |
private void loadNewMenu(String menuName) throws IOException { |
private void loadNewMenu(String menuName) throws IOException { |
92 |
104 |
GridPane newMenu; |
GridPane newMenu; |
93 |
105 |
switch (menuName) { |
switch (menuName) { |
94 |
106 |
case "BrutForce": |
case "BrutForce": |
95 |
|
newMenu= FXMLLoader.load(getClass().getResource("MenuBrutForce.fxml")); |
|
|
107 |
|
newMenu = FXMLLoader.load(getClass().getResource("MenuBrutForce.fxml")); |
96 |
108 |
break; |
break; |
97 |
109 |
case "2-Opt": |
case "2-Opt": |
98 |
|
newMenu=FXMLLoader.load(getClass().getResource("Menu2Opt.fxml")); |
|
|
110 |
|
newMenu = FXMLLoader.load(getClass().getResource("Menu2Opt.fxml")); |
99 |
111 |
break; |
break; |
100 |
112 |
case "Simulated Annealing": |
case "Simulated Annealing": |
101 |
|
newMenu=FXMLLoader.load(getClass().getResource("MenuSimulatedAnnealing.fxml")); |
|
|
113 |
|
newMenu = FXMLLoader.load(getClass().getResource("MenuSimulatedAnnealing.fxml")); |
102 |
114 |
break; |
break; |
103 |
115 |
default: |
default: |
104 |
116 |
throw new IllegalArgumentException("String 'menuName' didn't match any menu available"); |
throw new IllegalArgumentException("String 'menuName' didn't match any menu available"); |
|
... |
... |
public class MainWindowController { |
112 |
124 |
@FXML |
@FXML |
113 |
125 |
public void paneCitiesMouseClick(MouseEvent event) { |
public void paneCitiesMouseClick(MouseEvent event) { |
114 |
126 |
System.out.println("Mouse"); |
System.out.println("Mouse"); |
115 |
|
City newCity = new City(new Double(event.getX()).intValue(),new Double(event.getY()).intValue(),MainWindowController.data.getCitiesSize()+1); |
|
|
127 |
|
City newCity = new City(new Double(event.getX()).intValue(), new Double(event.getY()).intValue(), MainWindowController.data.getCitiesSize() + 1); |
116 |
128 |
MainWindowController.data.addCity(newCity); |
MainWindowController.data.addCity(newCity); |
117 |
129 |
} |
} |
118 |
130 |
|
|
119 |
131 |
@FXML |
@FXML |
120 |
|
private void btnNewCities(){ |
|
121 |
|
if(!data.isAlgorithmRunning()) { |
|
|
132 |
|
private void btnNewCities() { |
|
133 |
|
if (!data.isAlgorithmRunning()) { |
122 |
134 |
data.setBestEver(null); |
data.setBestEver(null); |
123 |
135 |
data.generateNewCities(Integer.valueOf(txtFieldNumCities.getText())); |
data.generateNewCities(Integer.valueOf(txtFieldNumCities.getText())); |
124 |
136 |
|
|
|
... |
... |
public class MainWindowController { |
126 |
138 |
} |
} |
127 |
139 |
|
|
128 |
140 |
|
|
129 |
|
private class RepaintListener extends Thread implements ChangeListener<Boolean> { |
|
130 |
|
boolean newValue; |
|
|
141 |
|
private class Repaint extends Thread { |
131 |
142 |
|
|
132 |
143 |
@Override |
@Override |
133 |
|
public void run(){ |
|
134 |
|
// System.out.println("paint: "+System.currentTimeMillis()); |
|
|
144 |
|
public void run() { |
|
145 |
|
// System.out.println("paint: "+System.currentTimeMillis()); |
135 |
146 |
int numCities = getNumOfCities(); |
int numCities = getNumOfCities(); |
136 |
147 |
Order currentOrder = data.getCurrentOrder(); |
Order currentOrder = data.getCurrentOrder(); |
137 |
|
if (newValue) { |
|
138 |
|
GraphicsContext gc = canvasCities.getGraphicsContext2D(); |
|
139 |
|
gc.clearRect(0, 0, canvasCities.getWidth(), canvasCities.getHeight()); |
|
140 |
|
if(!(currentOrder.getOrder()==null)) { |
|
141 |
|
gc.setStroke(Color.BLACK); |
|
142 |
|
gc.setLineWidth(1); |
|
143 |
|
gc.setFill((Color.GRAY)); |
|
144 |
|
for (int i = 0; i < currentOrder.getSize(); i++) { |
|
145 |
|
City current = data.getCities().get(currentOrder.getValue(i)); |
|
146 |
|
City next; |
|
147 |
|
if ((i + 1) > numCities - 1) { |
|
148 |
|
next = data.getCities().get(currentOrder.getValue(0)); |
|
149 |
|
} else { |
|
150 |
|
next = data.getCities().get(currentOrder.getValue(i + 1)); |
|
151 |
|
} |
|
152 |
148 |
|
|
|
149 |
|
GraphicsContext gc = canvasCities.getGraphicsContext2D(); |
|
150 |
|
gc.clearRect(0, 0, canvasCities.getWidth(), canvasCities.getHeight()); |
|
151 |
|
if (!(currentOrder.getOrder() == null)) { |
|
152 |
|
gc.setStroke(Color.BLACK); |
|
153 |
|
gc.setLineWidth(1); |
|
154 |
|
gc.setFill((Color.GRAY)); |
|
155 |
|
for (int i = 0; i < currentOrder.getSize(); i++) { |
|
156 |
|
City current = data.getCities().get(currentOrder.getValue(i)); |
|
157 |
|
City next; |
|
158 |
|
if ((i + 1) > numCities - 1) { |
|
159 |
|
next = data.getCities().get(currentOrder.getValue(0)); |
|
160 |
|
} else { |
|
161 |
|
next = data.getCities().get(currentOrder.getValue(i + 1)); |
|
162 |
|
} |
153 |
163 |
|
|
154 |
|
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10); |
|
155 |
|
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY()); |
|
156 |
164 |
|
|
157 |
|
} |
|
|
165 |
|
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10); |
|
166 |
|
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY()); |
|
167 |
|
|
158 |
168 |
} |
} |
159 |
169 |
|
|
160 |
170 |
|
|
|
... |
... |
public class MainWindowController { |
174 |
184 |
} |
} |
175 |
185 |
|
|
176 |
186 |
|
|
177 |
|
// gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10); |
|
|
187 |
|
// gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10); |
178 |
188 |
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY()); |
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY()); |
179 |
189 |
|
|
180 |
190 |
} |
} |
181 |
|
repaint.set(false); |
|
182 |
191 |
} |
} |
183 |
192 |
} |
} |
184 |
193 |
} |
} |
185 |
194 |
} |
} |
186 |
195 |
|
|
187 |
|
@Override |
|
188 |
|
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { |
|
189 |
|
this.newValue=newValue; |
|
190 |
|
Platform.runLater(this); |
|
191 |
|
} |
|
192 |
|
} |
|
193 |
196 |
|
|
|
197 |
|
} |
194 |
198 |
|
|
195 |
199 |
|
|
196 |
200 |
} |
} |