-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueenGene2.java
217 lines (199 loc) · 5.69 KB
/
QueenGene2.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
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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
import java.awt.Point;
public class QueenGene2 extends Gene {
// This constructor is only for the initial.
public QueenGene2(ArrayList<Point> queens, boolean _aging) {
super(queens, 0, _aging);
// TODO Auto-generated constructor stub
fitness_calculation();
}
@Override
public Gene mate(Gene gene) {
// TODO Auto-generated method stub
return super.mate(gene);
}
@Override
public void mutate() {
// TODO Auto-generated method sayList<Point>)tub
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
int i = 0;
Random r = new Random();
int queen = r.nextInt(nqueens);
int y;
int x;
Point newPoint;
do {
y = r.nextInt(nqueens);
x = r.nextInt(nqueens);
newPoint = new Point(x, y);
} while (queens.contains(newPoint));
queens.get(queen).setLocation(x, y);
fitness_calculation();
}
@Override
public Gene getRandom() {
// TODO Auto-generated method stub
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
ArrayList<Point> rand_queens = new ArrayList<Point>(nqueens);
int i = 0;
Random r = new Random();
while (i < nqueens) {
int y = r.nextInt(nqueens);
int x = r.nextInt(nqueens);
Point newPoint = new Point(x, y);
if (!rand_queens.contains(newPoint)) {
rand_queens.add(newPoint);
i++;
} else
;
}
rand_queens.sort(Comparator.comparing(Point::getX));
QueenGene randQg = new QueenGene(rand_queens, this.aging);
randQg.setDeathAge(this.getDeathAge());
randQg.setMatureAge(this.getMatureAge());
return randQg;
}
@Override
public Gene mate(Gene gene, Crossover option) {
// TODO Auto-generated method stub
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
ArrayList<Point> mate_queens = (ArrayList<Point>) gene.value;
QueenGene newSG = null;
switch (option) {
case DEFULAT: {
newSG = new QueenGene(singlePointCO(queens, mate_queens), this.aging);
break;
}
case SINGLEPOINT: {
newSG = new QueenGene(singlePointCO(queens, mate_queens), this.aging);
break;
}
case TWOPOINT: {
newSG = new QueenGene(twoPointCO(queens, mate_queens), this.aging);
break;
}
case UINFORM: {
newSG = new QueenGene(uniformCO(queens, mate_queens), this.aging);
break;
}
default: {
System.out.println("Error - Choose Crossover Option");
break;
}
}
if (newSG == null) {
System.out.println("Error");
System.exit(1);
}
newSG.setDeathAge(this.getDeathAge());
newSG.setMatureAge(this.getMatureAge());
return newSG;
}
@Override
public void fitness_calculation() {
// TODO Auto-generated method stub
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
this.fitness = nqueens * nqueens;
for (int i = 0; i < nqueens; i++) {
// add here call to is safe queen
if(isPlaceSafe(queens.get(i).getLocation()))
{
fitness-=nqueens;
}
}
}
public ArrayList<Point> twoPointCO(ArrayList<Point> queens, ArrayList<Point> mate_queens) {
int nqueens = queens.size();
Random r = new Random();
int i1 = r.nextInt(nqueens);
int i2 = r.nextInt(nqueens - i1) + i1;
int spos = r.nextInt(nqueens);
ArrayList<Point> newQueens = new ArrayList<Point>(nqueens);
for (int i = 0; i < i1; i++) {
newQueens.add(i, queens.get(i));
}
for (int i = i1; i < i2; i++) {
newQueens.add(i, mate_queens.get(i));
}
for (int i = i2; i < nqueens; i++) {
newQueens.add(i, queens.get(i));
}
return newQueens;
}
public ArrayList<Point> singlePointCO(ArrayList<Point> queens, ArrayList<Point> mate_queens) {
Random r = new Random();
int nqueens = queens.size();
int spos = r.nextInt(nqueens);
ArrayList<Point> newQueens = new ArrayList<Point>(nqueens);
for (int i = 0; i < nqueens; i++) {
if (i < spos)
newQueens.add(i, queens.get(i));
else
newQueens.add(i, mate_queens.get(i));
}
return newQueens;
}
public ArrayList<Point> uniformCO(ArrayList<Point> queens, ArrayList<Point> mate_queens) {
Random r = new Random();
int nqueens = queens.size();
ArrayList<Point> newQueens = new ArrayList<Point>(nqueens);
for (int i = 0; i < nqueens; i++) {
if (r.nextInt(2) == 1) // either 1 or 0 - 50% chance for each
{
newQueens.add(i, queens.get(i));
} else {
newQueens.add(i, mate_queens.get(i));
}
}
return newQueens;
}
private boolean isPlaceSafe(Point place) {
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
/* Check this row on left side */
for(int i=0;i<nqueens;i++)
{
Point current = queens.get(i);
//check row
if(place.getX()==current.getX())
return false;
// check column
if(place.getY()==current.getY())
return false;
//check diagonal
if((Math.abs(place.getY()-current.getY()))==(Math.abs(place.getX()-current.getX())))
return false;
}
System.out.println("Safe");
return true;
}
@Override
public String toString() {
// TODO Auto-generated method stub
print_board();
return super.toString();
}
public void print_board() {
System.out.print("\n\n");
ArrayList<Point> queens = (ArrayList<Point>) this.value;
int nqueens = queens.size();
for (int i = 0; i < nqueens; i++) {
System.out.print("***** ");
for (int j = 0; j < nqueens; j++) {
Point current = new Point(j, i);
if(queens.contains(current))
System.out.print("Q");
else
System.out.print("O");
}
System.out.println("");
}
System.out.print("\n\n");
}
}