-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueens.java
More file actions
338 lines (280 loc) · 10.1 KB
/
NQueens.java
File metadata and controls
338 lines (280 loc) · 10.1 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import java.util.*;
public class NQueens{
public static void main(String[] args){
//accept in 3 parameters:
// size, ps, pss
// size is the N in n queens look it up
// ps is the size of the population
// pss is the parent sample size
int size = Integer.parseInt(args[0]);
int ps = Integer.parseInt(args[1]);
int pss = Integer.parseInt(args[2]);
int al = Integer.parseInt(args[3]);
//construct our population array list
ArrayList<QObject> population = popBuilder(ps, size);
//setup the qfinder
QFinder qf = new QFinder(population, ps, pss, al);
//System.out.println(qf.population);
//run the qfinder
qf.run();
}
//randomizer mixes up the array inorder to have a random population
public static void randomizer(int[] target){
Random rand = new Random();
//swap things randomly
for(int i = 0; i<target.length *2; i++){
//grab random positions in the array
int a = rand.nextInt(target.length);
int b = rand.nextInt(target.length);
//grab the values from each position
int first = target[a];
int second = target[b];
//swap the values
target[a] = second;
target[b] = first;
}
}
//this just creates the inital populatiion
public static ArrayList<QObject> popBuilder(int ps, int size){
ArrayList<QObject> result = new ArrayList<>();
for(int i = 1; i <= ps; i++){
int [] temp = genotypeBuilder(size);
result.add(new QObject(temp, fitness(temp)));
}
return result;
}
//creates the single individual and randomizes it
public static int[] genotypeBuilder(int size){
int[] result = new int[size];
for(int i = 0; i < size; i ++){
result[i] = i + 1;
}
randomizer(result);
return result;
}
//this is the fitness function it tells us how good our thingy does
public static int fitness(int[] a){
int collisions = 0;
for(int i = 0; i < a.length; i ++){
//check the first half
for(int j = 0 ; j <= i - 1 ; j++){
if(a[i] + (i-j) == a[j])
collisions++;
if(a[i] - (i -j) == a[j])
collisions++;
}
for(int j = i+1; j < a.length; j++){
if(a[i] + (i- j) == a[j])
collisions++;
if(a[i] - (i -j) == a[j])
collisions++;
}
}
return collisions;
}
}
//q object is an individual specimen
//it holds the fitness score and the genotype
class QObject{
int fitness;
int[] genotype;
int age;
public QObject(int[] a, int fitness){
this.fitness = fitness;
genotype = a;
age = 0;
}
public int fitness(){
return fitness;
}
public int[] genotype(){
return genotype;
}
@Override
public String toString(){
return fitness + " " + Arrays.toString(genotype);
}
}
//qfinder is the meat of the algorithm this does the work
//creates and find the solution to a NQueen board
class QFinder{
ArrayList<QObject> population;
int size;
int numberOfParents;
int ageLimit;
public QFinder(ArrayList<QObject> a, int s, int parents, int al){
population = a;
size = s;
numberOfParents = parents;
ageLimit = al;
}
public void run(){
boolean s = true;
int c = 0;
while(s){
//sort arraylist by fitness
this.fitnessSort();
//easy enough to find a winning solution
if(population.get(0).fitness() == 0){
System.out.println("FOUND A MATCH");
System.out.println(population.get(0));
System.exit(0);
}
//update the age of all subjects
for(int i = 0; i < population.size(); i++){
population.get(i).age++;
if(population.get(i).age > this.ageLimit)
population.remove(i);
}
//kill of any items that do not meet the fitness cut off
//only keep the initial population size amount
while(population.size() > this.size){
//remove things until we have the size
population.remove(population.size()-1);
}
//keeps track of how many times we loop
c++;
System.out.print(population.get(0).fitness() + " ");
System.out.print(population.size() + " ");
System.out.println(c);
//find parents
QObject[] parents = new QObject[numberOfParents];
int[] picks = new int[numberOfParents];
//patent pending parent finding algorithm this alforithm pits 2
//solutiosn against each other, the higher one wins, if there is a
// tie a always wins
//a and b are randomly selected
for(int i = 0; i < numberOfParents; i++){
//doh, just make the array full of -1's
for(int j = 0; j < picks.length; j ++)
picks[i]--;
//wooo random pickings (LOTTERY TIME
Random rand = new Random();
int a = rand.nextInt(this.size);
int b = rand.nextInt(this.size);
//make sure the individual isn't inside the array already
for(int j = 0; j < picks.length; j++){
while(picks[j] == a)
a = rand.nextInt(this.size);
while(picks[j] == b)
b = rand.nextInt(this.size);
}
//fitness fighting
if( a >= b)
picks[i] = a;
else
picks[i] = b;
}
//once we have the parents we can do recombination
for(int i = 1; i < picks.length; i = i + 2){
combine(population.get(picks[i-1]),population.get( picks[i]));
}
}
}
//the combination function woooo
private void combine(QObject a, QObject b){
//pick a combination point
Random r = new Random();
int cp = r.nextInt(a.genotype().length - 2) + 1;
//create the new individuals
int[] baby1 = new int[a.genotype().length];
int[] baby2 = new int[a.genotype().length];
for(int i = 0; i < cp; i++){
baby1[i] = a.genotype()[i];
baby2[i] = b.genotype()[i];
}
//from the cp point go through each element in the other array and check
//if the baby contains it already if not add it in at the cp point
int newCP = cp;
while(newCP != baby1.length){
for(int i = 0; i < b.genotype.length; i++){
if(!contains(baby1, b.genotype[i])){
baby1[newCP] = b.genotype[i];
newCP++;
}
}
}
newCP = cp;
while(newCP != baby2.length){
for(int i = 0; i < a.genotype.length; i++){
if(!contains(baby2, a.genotype[i])){
baby2[newCP] = a.genotype[i];
newCP++;
}
}
}
//System.out.println(Arrays.toString(baby1));
//random mutation
mutate(baby1);
mutate(baby2);
population.add(new QObject(baby1, fitness(baby1)));
population.add(new QObject(baby1, fitness(baby2)));
}
private boolean contains(int[] a, int value){
for(int i = 0; i < a.length; i++){
if(a[i] == value)
return true;
}
return false;
}
public void mutate(int[] a){
Random r = new Random();
int b = r.nextInt(a.length);
int c = r.nextInt(a.length);
if(r.nextInt(100) > 50){
int temp = a[b];
a[b] = a[c];
a[c] = temp;
}
}
private void fitnessSort(){
QObject[] a = new QObject[this.population.size()];
QObject insert = this.population.remove(population.size()-1);
a[0] = insert;
int aSize = 1;
while(!population.isEmpty()){
insert = this.population.remove(population.size()-1);
int insertPos = aSize;
for(int i = 0; i < aSize; i++){
if(insert.fitness() < a[i].fitness()){
insertPos = i;
break;
}
}
if(insertPos == aSize){
a[insertPos] = insert;
aSize++;
} else {
insert(a, insert, insertPos, aSize);
aSize++;
}
}
for(int i = 0; i < a.length; i++)
this.population.add(a[i]);
}
private void insert(QObject[] a, QObject b, int pos, int aSize){
for(int i = aSize; i > pos ; i --){
a[i]= a[i - 1];
}
a[pos ] = b;
}
public int fitness(int[] a){
int collisions = 0;
for(int i = 0; i < a.length; i ++){
//check the first half
for(int j = 0 ; j <= i - 1 ; j++){
if(a[i] + (i-j) == a[j])
collisions++;
if(a[i] - (i -j) == a[j])
collisions++;
}
for(int j = i+1; j < a.length; j++){
if(a[i] + (i- j) == a[j])
collisions++;
if(a[i] - (i -j) == a[j])
collisions++;
}
}
return collisions;
}
}