-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGA.java
167 lines (128 loc) · 3.7 KB
/
GA.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.io.PrintWriter;
import java.util.*;
public class GA {
final int GENERATIONS = 300;
int POPULATION_SIZE = 200;
final int MAX_ALLOWED_REPRODUCTIONS = (2 * POPULATION_SIZE) / 3;
final int pm = 10; // probability of mutation
List<edge> adj[]; // graph
int N; // #nodes
int edgeLen[][];
PrintWriter pw ;
public GA(List<edge> adj[], int n, PrintWriter arg){
N = n;
this.adj = adj;
this.pw = arg;
}
Random r = new Random();
void solveTask(){
edgeLen = new int[N+1][N+1];
for(int i= 1; i<=N; i++){
for(edge x : adj[i])
edgeLen[i][x.end] = x.weight;
}
int i, j, k;
// initialse population
Chromosome initial[] = new Chromosome[POPULATION_SIZE];
Chromosome currGen[] = new Chromosome[POPULATION_SIZE];
for(int it = 0; it < POPULATION_SIZE; it ++ )
{
cnt = 0;
int path [] = generateRandomPath();
Chromosome c = new Chromosome(path, edgeLen);
initial[it] = c;
}
Arrays.sort(initial);
pw.println("Initial population has path length : " + initial[0].pathLen+"\n");
for(i = 0; i < POPULATION_SIZE; i ++)
currGen[i] = initial[i];
int prev = initial[0].pathLen;
for(int genIt = 1; genIt <= GENERATIONS; genIt ++ ){
// parent selection for crossover
// number of offsprings generated will be randomly chosen and will be less than MAX_ALLOWED_REPRODUCTION
int num_offs = r.nextInt(MAX_ALLOWED_REPRODUCTIONS) + 1;
Chromosome children [] = new Chromosome[num_offs];
double sumFit = 0;
for(Chromosome it : currGen)
sumFit += it.fitness;
// reproduction stage
for(j = 0; j < num_offs; j ++){
double u = r.nextDouble() * sumFit;
double v = r.nextDouble() * (sumFit);
double p = 0;
int x = -1, y = -1;
for(i = 0; i < POPULATION_SIZE; i ++){
p += currGen[i].fitness;
if(p >= u){
x = i;
break;
}
}
p = 0;
for(i = 0; i < POPULATION_SIZE; i ++){
p += currGen[i].fitness;
if(p >= v){
y = i;
break;
}
}
if(y == x)
{
j -- ;
continue;
}
Chromosome cx = currGen[x], cy = currGen[y];
Chromosome c1 = cx.crossover(r, cx, cy, edgeLen, 0), c2 = cx.crossover(r, cy, cx, edgeLen, 0), fc=null;
if(c1 == null && c2 == null)
j -- ;
else{
if(c1 == null ) fc = c2;
else if(c2 == null) fc = c1;
else if(c1.fitness > c2.fitness) fc = c1;
else fc = c2;
}
if(fc != null){
children[j] = fc;
}
}
// replacement of less fit individuals
for(i = 0; i < num_offs ; i++){
currGen[POPULATION_SIZE - i - 1] = children[i];
}
Arrays.sort(currGen);
// MUTATION STAGE, avoid mutating the elite member
for(i = 1; i < POPULATION_SIZE ; i++){
k = r.nextInt(101);
if( k <= pm){
currGen[i].mutate(r, edgeLen);
currGen[i].computeFitness(edgeLen);
}
}
Arrays.sort(currGen);
}
pw.println("The path length produced is : "+ currGen[0].pathLen );
for(int cities : currGen[0].path)
pw.print(cities + " ");
}
int cnt = 0;
int[] generateRandomPath(){
int ret[] = new int [N];
int s = r.nextInt(N)+1, max = N*(N-1)/2;
ret[0] = s;
boolean is[] = new boolean[N+1];
is[s] = true;
for(int i=1, j = 0; i<N && j < max; i++, j++){
int size = adj[ret[i-1]].size();
s = r.nextInt(size);
ret[i] = adj[ret[i-1]].get(s).end;
if(is[ret[i]])
i--;
else
is[ret[i]] = true;
}
if(edgeLen[ret[N-1]][ret[0]] > 0)
return ret;
else
return generateRandomPath();
}
}