Skip to content

Commit 102580b

Browse files
authored
Add files via upload
0 parents  commit 102580b

9 files changed

+504
-0
lines changed

TSP_GeneticAlgorithm/Berlin52.txt

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
565.0,575.0
2+
25.0,185.0
3+
345.0,750.0
4+
945.0,685.0
5+
845.0,655.0
6+
880.0,660.0
7+
25.0,230.0
8+
525.0,1000.0
9+
580.0,1175.0
10+
650.0,1130.0
11+
1605.0,620.0
12+
1220.0,580.0
13+
1465.0,200.0
14+
1530.0,5.0
15+
845.0,680.0
16+
725.0,370.0
17+
145.0,665.0
18+
415.0,635.0
19+
510.0,875.0
20+
560.0,365.0
21+
300.0,465.0
22+
520.0,585.0
23+
480.0,415.0
24+
835.0,625.0
25+
975.0,580.0
26+
1215.0,245.0
27+
1320.0,315.0
28+
1250.0,400.0
29+
660.0,180.0
30+
410.0,250.0
31+
420.0,555.0
32+
575.0,665.0
33+
1150.0,1160.0
34+
700.0,580.0
35+
685.0,595.0
36+
685.0,610.0
37+
770.0,610.0
38+
795.0,645.0
39+
720.0,635.0
40+
760.0,650.0
41+
475.0,960.0
42+
95.0,260.0
43+
875.0,920.0
44+
700.0,500.0
45+
555.0,815.0
46+
830.0,485.0
47+
1170.0,65.0
48+
830.0,610.0
49+
605.0,625.0
50+
595.0,360.0
51+
1340.0,725.0
52+
1740.0,245.0

TSP_GeneticAlgorithm/Cities.csv

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
275.9280868,358.9084991
2+
935.9036437,371.9184489
3+
385.4965861,275.3342348
4+
337.3112117,440.2848436
5+
370.8866932,233.8955856
6+
652.177041,43.11978117
7+
312.6802095,248.0312791
8+
845.3195242,141.7787516
9+
540.8694427,403.4439935
10+
456.1251612,171.2703718
11+
501.8960844,52.00966753
12+
698.11845,124.3340383
13+
78.64692094,583.7036937
14+
319.144423,137.5752244
15+
354.3473235,140.8338038
16+
449.1575217,76.41948003
17+
897.9505625,332.5980927
18+
355.600505,158.4155755
19+
722.8356039,467.8275442
20+
553.6933135,125.7939894
21+
853.6961742,506.3917922
22+
203.8112707,314.3068728
23+
343.92983,472.0550228
24+
100.6730291,306.2708499
25+
316.6240771,260.9135096
26+
438.8390213,299.5092555
27+
599.4967054,283.7580848
28+
149.5003049,39.48742755
29+
182.5057392,452.049342
30+
135.8439024,281.6154937
31+
284.814109,64.32597576
32+
476.940857,236.0554327
33+
653.0268919,438.1049689
34+
417.8698871,236.5317369
35+
350.6397122,396.0379539
36+
821.4457462,71.04369083
37+
37.83493169,252.4688617
38+
546.585188,56.87175794
39+
798.8083819,549.8697304
40+
101.027704,443.9685779
41+
7.873897929,385.54372
42+
575.4366491,401.5106192
43+
522.248753,57.31481864
44+
721.6946634,144.706047
45+
643.834298,546.933896
46+
217.4669193,371.4692548
47+
33.01671214,382.436976
48+
722.6631054,571.4180518
49+
470.1378096,469.5838065
50+
214.4340921,327.9578734
51+
216.7551374,563.5797884
52+
0.022003239,251.6002298
53+
151.0955699,155.6020736
54+
618.7089082,538.1272396
55+
966.3394617,135.7341984
56+
3.063628162,108.5166966
57+
726.8631273,466.6634653
58+
215.1471798,335.6538241
59+
864.8255653,2.146931896
60+
101.638141,154.7929288
61+
189.3662626,429.7430641
62+
600.1143531,13.09862274
63+
467.986001,198.6277013
64+
170.991836,517.7523958
65+
11.36329669,599.4064416
66+
454.9643683,285.7729904
67+
314.4379231,555.0298184
68+
50.91188639,380.7100349
69+
793.6984297,79.57510348
70+
889.0353505,297.6746923
71+
210.4806224,107.9530632
72+
974.3897881,566.393234
73+
681.0399865,216.6876873
74+
360.0410701,398.0174287
75+
327.8779199,110.0739236
76+
350.3695294,426.4013403
77+
685.4114423,137.0859779
78+
619.1751356,283.2606814
79+
454.0749185,23.50502646
80+
245.2973731,196.4812562
81+
14.04748345,543.1446923
82+
553.9858826,557.3384487
83+
71.59453506,339.6716974
84+
821.8105179,67.04148917
85+
506.3885487,85.49643966
86+
811.9106958,585.3054495
87+
63.25034418,422.189731
88+
326.2672885,427.787811
89+
239.7343938,591.0817182
90+
593.3634051,471.3770414
91+
708.2598888,238.111482
92+
310.3214242,267.0661332
93+
148.4753425,414.5478382
94+
347.1201009,453.9071578
95+
122.4881315,395.395845
96+
486.2195951,355.538639
97+
402.1475972,362.8194227
98+
188.869998,152.3612214
99+
646.7939931,403.6342508
100+
541.1495762,155.104456

TSP_GeneticAlgorithm/City.pde

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class City {
2+
private float x;
3+
private float y;
4+
City(float x, float y) {
5+
this.x = x;
6+
this.y = y;
7+
}
8+
public float getX() {
9+
return x;
10+
}
11+
public float getY() {
12+
return y;
13+
}
14+
}

TSP_GeneticAlgorithm/DNA.pde

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
public class DNA {
2+
private City[] genes;
3+
private double fitness;
4+
private double distance;
5+
6+
private double getFitness() {return this.fitness;}
7+
private double getDistance() {return this.distance;}
8+
9+
private City[] getGenes() {
10+
return genes;
11+
}
12+
13+
public DNA(City[] cities, boolean firstGeneration) {
14+
this.genes = deepCopy(cities);
15+
if (firstGeneration) {
16+
shuffleGenes(genes.length);
17+
}
18+
}
19+
20+
private void shuffleGenes(int n) {
21+
for (int i = n-1; i > 0; i--) {
22+
int j = floor(random(i));
23+
swap(genes, i, j);
24+
}
25+
}
26+
27+
private double calcFitness() {
28+
double distance = dist(genes[0].getX(), genes[0].getY(), genes[genes.length-1].getX(), genes[genes.length-1].getY());
29+
for (int i = 1; i < genes.length; i++) {
30+
distance += dist(genes[i-1].getX(), genes[i-1].getY(), genes[i].getX(), genes[i].getY());
31+
}
32+
this.distance = distance;
33+
this.fitness = pow((float)(10000 / distance), 4);
34+
return this.fitness;
35+
}
36+
37+
private DNA crossover(DNA parent2, int start, int end, float crossoverRate) {
38+
if (random(1) < crossoverRate) {
39+
final int count = end - start + 1;
40+
City[] child = new City[cityNum];
41+
arrayCopy(subset(this.genes, start, count), 0, child, start, count);
42+
int insertIndex = end + 1;
43+
for (int k = 0; k < cityNum; k++) {
44+
int index = (k + end + 1) % cityNum;
45+
City city = parent2.getGenes()[index];
46+
if (contains(child, city)) {
47+
continue;
48+
} else {
49+
child[insertIndex % cityNum] = city;
50+
insertIndex++;
51+
}
52+
}
53+
return new DNA(child, false);
54+
} else {
55+
return new DNA(this.genes, false);
56+
}
57+
}
58+
59+
private DNA mutate(float mutationRate) {
60+
City[] newGenes;
61+
if (random(1) < mutationRate) {
62+
int i = floor(random(genes.length));
63+
int j = floor(random(genes.length));
64+
int start = min(i,j);
65+
int end = max(i,j);
66+
int count = end - start + 1;
67+
City[] before = (City[]) subset(genes,0,start);
68+
City[] after = (City[]) subset(genes, (end+1));
69+
City[] selectedSection = (City[]) subset(genes, start, count);
70+
selectedSection = (City[]) reverse(selectedSection);
71+
newGenes = (City[]) concat(before, selectedSection);
72+
newGenes = (City[]) concat(newGenes, after);
73+
} else {
74+
newGenes = this.genes;
75+
}
76+
return new DNA(newGenes, false);
77+
}
78+
79+
void swap(City[] cities, int i, int j) {
80+
City temp = cities[i];
81+
cities[i] = cities[j];
82+
cities[j] = temp;
83+
}
84+
85+
public City[] deepCopy(City[] genes) {
86+
City[] newGenes = new City[TSP_GeneticAlgorithm.cityNum];
87+
for (int i = 0; i < genes.length; i++) {
88+
newGenes[i] = genes[i];
89+
}
90+
return newGenes;
91+
}
92+
93+
public boolean contains(City[] genes, City city) {
94+
boolean contains = false;
95+
for (City gene: genes) {
96+
if (city.equals(gene)) {
97+
contains = true;
98+
break;
99+
}
100+
}
101+
return contains;
102+
}
103+
}

TSP_GeneticAlgorithm/Population.pde

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
public class Population {
2+
private City[] cities;
3+
private int populationNum;
4+
private float mutationRate;
5+
private float crossoverRate;
6+
private DNA[] population = new DNA[populationNum];
7+
private DNA bestOne;
8+
private double shortestDistance;
9+
private double bestFitness;
10+
private double sumFitness;
11+
private int generationCount = 1;
12+
13+
City[] getBestRoute() {
14+
return bestOne.getGenes();
15+
}
16+
17+
public double getShortestDistance() {
18+
return this.shortestDistance;
19+
}
20+
public int getGenerationCount() {
21+
return this.generationCount;
22+
}
23+
24+
Population(City[] cities, int populationNum, float crossoverRate, float mutationRate) {
25+
this.cities = cities;
26+
this.populationNum = populationNum;
27+
this.population = new DNA[populationNum];
28+
this.crossoverRate = crossoverRate;
29+
this.mutationRate = mutationRate;
30+
println("Generation " + generationCount);
31+
println("-----------------------------");
32+
for (int i = 0; i < populationNum; i++) {
33+
DNA dna = new DNA(this.cities, true);
34+
population[i] = dna;
35+
}
36+
}
37+
38+
void calcFitness() {
39+
sumFitness = 0;
40+
sumFitness += population[0].calcFitness();
41+
shortestDistance = population[0].getDistance();
42+
bestFitness = population[0].getFitness();
43+
bestOne = population[0];
44+
for (int i = 1; i < populationNum; i++) {
45+
double currentFitness = population[i].calcFitness();
46+
sumFitness += currentFitness;
47+
if (currentFitness > bestOne.getFitness()) {
48+
shortestDistance = population[i].getDistance();
49+
bestFitness = currentFitness;
50+
bestOne = population[i];
51+
}
52+
}
53+
println("Shortest Distance: " + shortestDistance);
54+
println("Best Fitness: " + bestFitness);
55+
println();
56+
}
57+
58+
void naturalSelection() {
59+
DNA[] nextGeneration = new DNA[populationNum];
60+
nextGeneration[0] = new DNA(bestOne.getGenes(),false);
61+
for (int i = 1; i < populationNum; i += 2) {
62+
DNA[] parents = new DNA[2];
63+
for (int j = 0; j < 2; j++) {
64+
double randomFitness = random((float)(sumFitness));
65+
int index = -1;
66+
while (randomFitness >= 0) {
67+
randomFitness -= population[++index].getFitness();
68+
}
69+
parents[j] = population[index];
70+
}
71+
final int k = floor(random(cityNum)); //<>//
72+
final int j = floor(random(cityNum));
73+
final int start = min(k,j);
74+
final int end = max(k,j);
75+
DNA child1 = parents[0].crossover(parents[1], start, end, crossoverRate);
76+
DNA child2 = parents[1].crossover(parents[0], start, end, crossoverRate);
77+
child1 = child1.mutate(mutationRate);
78+
child2 = child2.mutate(mutationRate);
79+
nextGeneration[i] = child1;
80+
nextGeneration[i+1] = child2;
81+
}
82+
this.population = nextGeneration;
83+
println("Generation " + ++generationCount);
84+
println("-----------------------------");
85+
}
86+
}

0 commit comments

Comments
 (0)