-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneration.java
More file actions
81 lines (66 loc) · 2.52 KB
/
Generation.java
File metadata and controls
81 lines (66 loc) · 2.52 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
package simulator;
import java.util.ArrayList;
import java.util.Random;
import org.jfree.ui.RefineryUtilities;
//Defines all methods and members for implementing Genetic Algorithm
public abstract class Generation {
protected ArrayList<Exp> constraints = new ArrayList<Exp>();// list of
// expressions
// containing
// constraints
protected int populationSize = 200;// population size default 100
protected double variationRate = 10; // variation or mutation rate default
// 0%
protected Point[] population = new Point[populationSize];
protected Boolean populationInitFlag = false;
protected XYLineChart_AWT chart;
protected Point[] matingPool;
protected Exp objectiveFun;
protected int windowX, windowY;
Point solution = null;
// display present generation on graph
public void displayGraph(String title, String genName, String xAxesName, String yAxesName, int windowX,
int windowY) {
chart = new XYLineChart_AWT(title, genName, xAxesName, yAxesName, constraints, windowX, windowY);
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
chart.dispConstraints();
chart.initPopulation(population, populationInitFlag);
this.windowX = windowX;
this.windowY = windowY;
}
public void dispSolutionOnGraph() {
chart.dispSoln(solution);
}
public void updateGraph(String title) {
chart.setTitle(title);
chart.initPopulation(population, populationInitFlag);
}
public void dispMatingPool() {
chart.initPopulation(population, populationInitFlag);
}
// calculate fitness for all individual or Points in the population
protected abstract void calFitness();
// generating mating pool for population
protected abstract void genMatingPool();
// breed Points in mating pool to generate new population
protected abstract void breed();
// initialize population
protected abstract void initPopulation(Point[] p);
// generate offspring from 2 parent Points.
protected abstract Point[] genOffspring(Point p1);
// generate population with random genotype
public Point[] genRandomPopulation() {
Point[] randomPopulation = new Point[populationSize];
Random rand = new Random();
for (int i = 0; i < populationSize; i++) {
Point p = new Point(rand.nextDouble() * windowX, rand.nextDouble() * windowY);
randomPopulation[i] = p;
// randomPopulation[i].setY(rand.nextDouble() * 15);
// xydataset.getSeries(PopulationSeriesIndex).add(x,y);
// System.out.println("indivisual added:"+x+","+y);
}
return randomPopulation;
}
}