File src/salesman/algorithms/Opt2.java changed (mode: 100644) (index d401461..db68845) |
... |
... |
package salesman.algorithms; |
3 |
3 |
|
|
4 |
4 |
import javafx.application.Platform; |
import javafx.application.Platform; |
5 |
5 |
import org.joda.time.DateTime; |
import org.joda.time.DateTime; |
6 |
|
import org.joda.time.Duration; |
|
7 |
6 |
import org.joda.time.Period; |
import org.joda.time.Period; |
8 |
|
import org.joda.time.format.PeriodFormat; |
|
9 |
7 |
import org.joda.time.format.PeriodFormatter; |
import org.joda.time.format.PeriodFormatter; |
10 |
8 |
import org.joda.time.format.PeriodFormatterBuilder; |
import org.joda.time.format.PeriodFormatterBuilder; |
11 |
9 |
import salesman.data.Order; |
import salesman.data.Order; |
12 |
10 |
import salesman.ui.MainWindowController; |
import salesman.ui.MainWindowController; |
13 |
|
import salesman.ui.SetDataOnWindow; |
|
14 |
|
|
|
15 |
11 |
|
|
16 |
12 |
import java.util.Random; |
import java.util.Random; |
17 |
13 |
|
|
|
... |
... |
public class Opt2 extends Thread { |
29 |
25 |
int count = 0; |
int count = 0; |
30 |
26 |
double cancelCondition= MainWindowController.data.getCities().length*MainWindowController.data.getCities().length-1; |
double cancelCondition= MainWindowController.data.getCities().length*MainWindowController.data.getCities().length-1; |
31 |
27 |
currentOrder = Order.getRandomOrder(MainWindowController.data.getCities()); |
currentOrder = Order.getRandomOrder(MainWindowController.data.getCities()); |
|
28 |
|
|
|
29 |
|
|
32 |
30 |
while (count<cancelCondition) { |
while (count<cancelCondition) { |
33 |
31 |
if(isInterrupted()){ |
if(isInterrupted()){ |
34 |
32 |
MainWindowController.data.setAlgorithmRunning(false); |
MainWindowController.data.setAlgorithmRunning(false); |
35 |
33 |
return; |
return; |
36 |
34 |
} |
} |
|
35 |
|
|
37 |
36 |
currentLength = currentOrder.getTotalLength(); |
currentLength = currentOrder.getTotalLength(); |
38 |
37 |
int index1 = r.nextInt(currentOrder.getSize()), index2 = r.nextInt(currentOrder.getSize()); |
int index1 = r.nextInt(currentOrder.getSize()), index2 = r.nextInt(currentOrder.getSize()); |
39 |
38 |
currentOrder.swapValues(index1, index2); |
currentOrder.swapValues(index1, index2); |
40 |
39 |
MainWindowController.data.getCurrentOrder().setOrder(currentOrder.getOrder()); |
MainWindowController.data.getCurrentOrder().setOrder(currentOrder.getOrder()); |
|
40 |
|
|
41 |
41 |
System.out.println("change: "+System.currentTimeMillis()); |
System.out.println("change: "+System.currentTimeMillis()); |
|
42 |
|
|
42 |
43 |
if (currentOrder.getTotalLength() < currentLength) { |
if (currentOrder.getTotalLength() < currentLength) { |
43 |
44 |
MainWindowController.data.setBestEver(currentOrder); |
MainWindowController.data.setBestEver(currentOrder); |
44 |
45 |
count =0; |
count =0; |
File src/salesman/algorithms/SimAnn.java added (mode: 100644) (index 0000000..0d96966) |
|
1 |
|
package salesman.algorithms; |
|
2 |
|
|
|
3 |
|
import org.joda.time.DateTime; |
|
4 |
|
import salesman.data.Order; |
|
5 |
|
import salesman.ui.MainWindowController; |
|
6 |
|
|
|
7 |
|
import java.util.Random; |
|
8 |
|
|
|
9 |
|
public class SimAnn extends Thread { |
|
10 |
|
private Order currentOrder = Order.getRandomOrder(MainWindowController.data.getCities()); |
|
11 |
|
private Random r = new Random(); |
|
12 |
|
private int bestLength =currentOrder.getTotalLength(); |
|
13 |
|
private double sigma = 1000000; |
|
14 |
|
|
|
15 |
|
@Override |
|
16 |
|
public void run(){ |
|
17 |
|
|
|
18 |
|
boolean running = true; |
|
19 |
|
|
|
20 |
|
MainWindowController.data.setAlgorithmRunning(true); |
|
21 |
|
DateTime start =new DateTime(); |
|
22 |
|
|
|
23 |
|
while(running) { |
|
24 |
|
|
|
25 |
|
sigma-=sigma*0.01; |
|
26 |
|
int a = r.nextInt(currentOrder.getSize() - 1); |
|
27 |
|
int b = r.nextInt(currentOrder.getSize() - 1); |
|
28 |
|
|
|
29 |
|
|
|
30 |
|
currentOrder.swapValues(a, b); |
|
31 |
|
MainWindowController.data.getCurrentOrder().setOrder(currentOrder.getOrder()); |
|
32 |
|
if (currentOrder.getTotalLength() < bestLength) { |
|
33 |
|
MainWindowController.data.setBestEver(currentOrder); |
|
34 |
|
} else { |
|
35 |
|
//TODO implement probability decision |
|
36 |
|
} |
|
37 |
|
|
|
38 |
|
if (!MainWindowController.data.isAlgorithmRunning()) { |
|
39 |
|
try { |
|
40 |
|
Thread.sleep(10); |
|
41 |
|
} catch (InterruptedException e) { |
|
42 |
|
e.printStackTrace(); |
|
43 |
|
} |
|
44 |
|
} |
|
45 |
|
|
|
46 |
|
} |
|
47 |
|
DateTime end = new DateTime(); |
|
48 |
|
MainWindowController.data.setAlgorithmRunning(false); |
|
49 |
|
} |
|
50 |
|
} |