-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinAlgebra.java
More file actions
252 lines (217 loc) · 6.64 KB
/
LinAlgebra.java
File metadata and controls
252 lines (217 loc) · 6.64 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
package simulator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
public class LinAlgebra extends Generation {
// Exp e1,e2,e3,e4;
ArrayList<Exp> expressions = new ArrayList<Exp>();
// here is where you give constraints and obj fun
final String objectiveFunString = "x*x+y";
final String constraint1String = "Math.sin(3*x)";
final String constraint2String = "7";
final String constraint3String = "3";
final String constraint4String = "6";
final int Scale = 3;
final Boolean Maximization = true;
final double matingPoolSelectionRatio = 0.75;
// Point solution = null;
LinAlgebra() {
// init values
variationRate = 0.1;
objectiveFun = new Exp();
objectiveFun.setExp(objectiveFunString, false, true, "");
Exp e;
e = new Exp();
e.setExp(constraint1String, true, false, "<=");
constraints.add(e);
e = new Exp();
e.setExp(constraint2String, false, false, "<=");
constraints.add(e);
/*
* e = new Exp(); e.setExp(constraint3String,true,false,">=");
* constraints.add(e);
*
* e = new Exp(); e.setExp(constraint4String,true,false,"<=");
* constraints.add(e);
*/
}
protected Boolean satisfyAll(double x, double y) {
Boolean flag = true;
for (Exp e : constraints) {
if (!e.satisfy(x, y)) {
flag = false;
break;
}
}
return flag;
}
@Override
protected void calFitness() {
// TODO Auto-generated method stub
for (int i = 0; i < populationSize; i++) {
try {
this.population[i].setFitness(0);
if (this.population[i].getX() >= 0 && this.population[i].getY() >= 0
&& satisfyAll(this.population[i].getX(), this.population[i].getY())) {
this.population[i]
.setFitness(objectiveFun.solve(this.population[i].getX(), this.population[i].getY()));
}
} catch (PointUsageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this.initPopulation(this.population);
}
@Override
protected void genMatingPool() {
ArrayList<Point> tempFeasible = new ArrayList<Point>();
for (int i = 0; i < populationSize; i++) {
try {
// System.out.println("!"+this.population[i].getFitness());
if (this.population[i].getFitness() > 0)
tempFeasible.add(this.population[i]);
} catch (PointUsageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Collections.sort(tempFeasible, new Comparator<Point>() {
@Override
public int compare(Point arg0, Point arg1) {
// TODO Auto-generated method stub
double arg0Fitness = 0;
double arg1Fitness = 0;
try {
arg0Fitness = arg0.getFitness() * 100;
arg1Fitness = arg1.getFitness() * 100;
} catch (Exception e) {
e.printStackTrace();
}
if (Maximization)
return (int) (arg1Fitness - arg0Fitness);
else
return (int) (arg0Fitness - arg1Fitness);
}
});
// System.out.println(tempFeasible);
solution = tempFeasible.get(0);
dispSolutionOnGraph();
int tempfittestsize = (int) (tempFeasible.size() * (0.50));
Point[] tempfittest = new Point[tempfittestsize];
int frontcount = 0;
int endcount = tempfittestsize - 1;
int count = 0;
Random randomvar = new Random();
while (count < tempfittestsize) {
int r = randomvar.nextInt(100);
if (r > variationRate) {
tempfittest[count] = tempFeasible.get(frontcount);
frontcount++;
} else {
tempfittest[count] = tempFeasible.get(endcount);
endcount--;
}
count++;
}
population = tempfittest;
}
public double printSolution() {
if (this.solution != null) {
try {
double objFunSoln = objectiveFun.solve(solution.getX(), solution.getY());
System.out
.println("Solution:(" + this.solution.getX() + "," + this.solution.getY() + "):" + objFunSoln);
return objFunSoln;
} catch (PointUsageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
@Override
protected void breed() {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Point> temp = new ArrayList<Point>();
while (temp.size() < populationSize) {
int randomChoice = rand.nextInt(population.length);
// Point parent = population[randomChoice];
Point parent = population[0];
// randomChoice = rand.nextInt(population.length);
// Point parent2 = population[randomChoice];
// temp.add(genOffspring(parent1, parent2));
Point children[] = genOffspring(parent);
for (int z = 0; z < 9; z++)
temp.add(children[z]);
}
population = temp.toArray(new Point[populationSize]);
}
protected double crossover(double v1, double v2) {
Random r = new Random();
return v1 > v2 ? ((v1 - v2) * r.nextDouble() + v1) : ((v2 - v1) * r.nextDouble() + v2);
}
@Override
protected Point[] genOffspring(Point p) {
// TODO Auto-generated method stub
Point[] children = new Point[9];
try {
Random rv = new Random();
double r = rv.nextDouble();
double scaledval = Scale * r;
children[0] = new Point(p.getX(), p.getY());
r = rv.nextDouble();
scaledval = Scale * r;
children[1] = new Point(p.getX() - scaledval, p.getY());
r = rv.nextDouble();
scaledval = Scale * r;
children[2] = new Point(p.getX(), p.getY() + scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[3] = new Point(p.getX(), p.getY() - scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[4] = new Point(p.getX() + scaledval, p.getY() + scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[5] = new Point(p.getX() - scaledval, p.getY() - scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[6] = new Point(p.getX() - scaledval, p.getY() + scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[7] = new Point(p.getX() + scaledval, p.getY() - scaledval);
r = rv.nextDouble();
scaledval = Scale * r;
children[8] = new Point(p.getX() + scaledval, p.getY());
} catch (Exception e) {
e.printStackTrace();
}
return children;
/*
* double childx = 0; double childy = 0; try { childx =
* crossover(p1.getX(),p2.getX()); childy =
* crossover(p1.getY(),p2.getY()); } catch(Exception e)
* {e.printStackTrace();} Point offSpring = new Point(childx,childy);
* return offSpring;
*/
}
@Override
protected void initPopulation(Point[] p) {
// TODO Auto-generated method stub
populationInitFlag = true;
for (int i = 0; i < populationSize; i++) {
try {
this.population[i] = new Point(p[i].getX(), p[i].getY());
double r = p[i].getFitness();
// System.out.println("****"+r);
this.population[i].setFitness(r);
} catch (PointUsageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}