-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
50 lines (39 loc) · 1.33 KB
/
Main.java
File metadata and controls
50 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.company;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
int numberOfCities = 8;
int[] X = new int[] { 10, 15, 12, 18, 8, 9, 11, 5 };
int[] Y = new int[] { 5, 20, 14, 6, 18, 13, 24, 19 };
double[][] travelPrices = new double[numberOfCities][numberOfCities];
for (int i = 0; i < numberOfCities; i++) {
for (int j = 0; j <= i; j++) {
if (i == j)
travelPrices[i][j] = 0.00;
else {
DecimalFormat df = new DecimalFormat("#.##");
travelPrices[i][j] = Double.parseDouble(df.format(Point2D.distance(X[i], Y[i], X[j], Y[j])));
travelPrices[j][i] = travelPrices[i][j];
}
}
}
PrintDistanceOfCities(travelPrices, numberOfCities);
SaleMan geneticAlgorithm = new SaleMan(numberOfCities, travelPrices, 0);
Chromosome result = geneticAlgorithm.optimize();
System.out.println("--------------\n final: \n ");
System.out.println(result);
}
public static void PrintDistanceOfCities(double[][] travelPrices, int numberOfCities) {
for (int i = 0; i < numberOfCities; i++) {
for (int j = 0; j < numberOfCities; j++) {
System.out.print(travelPrices[i][j] + " ");
if (travelPrices[i][j] / 10 == 0)
System.out.print(" ");
else
System.out.print(' ');
}
System.out.print("\n\n");
}
}
}