-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChromosome.java
More file actions
74 lines (59 loc) · 1.88 KB
/
Copy pathChromosome.java
File metadata and controls
74 lines (59 loc) · 1.88 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
// ********************************************************
// Class: CS225
// Name: Lucien Hammond
// Date: 12/2/22
//
// Purpose: Is an individual with a given number of values that
// can change its values and calculate its fitness given
// an array of points
//
//
// Attributes: -values: double[]
// -fitness: double
//
// Methods: +Chromosome(int)
// +calcFitness(double[][]): void
// +getFitness(): double
// +setRandomValue(int): void
// +setValue(int): void
// +getValue(int): double
//
// ********************************************************
public class Chromosome {
private double[] values;
private double fitness = 0;
Chromosome(int polynomialSize) {
values = new double[polynomialSize + 1];
}
public void calcFitness(double[][] points) {
double num = 0;
double yPrediction = 0;
double yMean = 0;
double dem = 0;
for(int i = 0; i < points.length; i++) {
yMean = yMean + points[i][1];
}
yMean = yMean / points.length;
for(int i = 0; i < points.length; i++) {
yPrediction = 0;
for(int j = 0; j < values.length; j++) {
yPrediction = yPrediction + values[j] * Math.pow(points[i][0], j);
}
num = num + Math.pow(points[i][1] - yPrediction, 2);
dem = dem + Math.pow(points[i][1] - yMean, 2);
}
fitness = 1 - (num/dem);
}
public double getFitness() {
return fitness;
}
public void setRandomValue(int position) {
values[position] = Math.floor((Math.random() * 41) - 20);
}
public void setValue(int position, double value) {
values[position] = value;
}
public double getValue(int position) {
return values[position];
}
}