-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndividual.java
181 lines (133 loc) · 3.99 KB
/
Individual.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
package Rumana;
/**
*
* @author Quazi
*/
import java.util.Random;
public class Individual implements Constants, Comparable {
private int Dimension;
private double Parameters[];
private double Score;
double sc;
//float cR[];
Random Rnd;
//double Violation; // for constrained optimization
// private double error;
public Individual(double param[], Random rnd) {
Parameters = (double[]) param.clone();
Dimension = Parameters.length; // dimension of each individual
Rnd = rnd;
//Violation = 0;
}
public double fitness(float cReturn[],float cRisk[],float covM[][])
{
int i,j;
//double f_score=0;
float TotalReturn,TotalRisk;
TotalReturn=0;
TotalRisk=0;
double Std;
double term1=0;
double term2=0;
for(i=0;i<Dimension;i++)
{
if(Parameters[i]!=0){
TotalReturn+=Parameters[i]*cReturn[i];
}
}
for(i=0;i<Dimension;i++){
for(j=0;j<Dimension;j++){
term1+=Parameters[i]*Parameters[j]*covM[i][j];
}
}
for(i=0;i<Dimension;i++){
term2+=((Parameters[i]*Parameters[i])*(cRisk[i]*cRisk[i]));
}
double sum=(term1+term2);
Std=Math.sqrt(sum);
Score=(TotalReturn-1*Std);
return Score;
}
/*
public void setFitness (double score){
this.Score = score;//Math.abs(score);
}
*/
public int getDimension() {
return (Dimension);
}
public double getScore() {
return (Score);
}
public double getParamAt(int index) {
return (Parameters[index]);
}
public double[] getParameters() {
return (Parameters);
}
/// DE/RAND/1/EXP
public Individual reproduction(Individual A, Individual B, Individual C) {
double a[], b[], c[], offSpring[];
//Random rnd = new Random((long)System.currentTimeMillis());
//Random rnd = new Random((new Random()).nextLong());
int i, j;
offSpring = new double[Dimension];
// get the parameters of A, B, C for calculation efficiency
a = A.getParameters();
b = B.getParameters();
c = C.getParameters();
for (i = 0; i < Dimension; ++i) {
offSpring[i] = this.Parameters[i];
}
j = (int) (this.Rnd.nextDouble() * Dimension); // randomly pick the first parameter
// Exponential crossover
i = 0;
do {
offSpring[j] = c[j] + gAmpFact * (a[j] - b[j]);
j = (j + 1) % Dimension;
} while ((this.Rnd.nextDouble() < gCrossRate) && (++i < Dimension));
/*
// binomial crossover
for(i=0; i<Dimension; ++i){
if( ( this.Rnd.nextDouble() < gCrossRate) || (i == (Dimension -1))){
offSpring[j] = c[j] + gAmpFact*(a[j] - b[j] );
}
else{
offSpring[j] = Parameters[j];
}
j = (j+1) %Dimension;
}
*/
// Keep solutions feasible: with in the search range
for (i = 0; i < Dimension; i++) {
if (offSpring[i] < 0)
offSpring[i] = 0;
}
double offSum = 0;
for (i = 0; i < Dimension; i++) {
offSum += offSpring[i];
//System.out.println("The population is now");
}
// System.out.println("The efficient portfolios are");
for(i=0;i<Dimension;i++){
if (offSum >= .999) {
offSpring[i] = offSpring[i] / offSum;
//System.out.println("The sum is"+offSum);
//System.out.print(offSpring[i]+" ");
}
//System.out.println(" ");
}
return (new Individual(offSpring, this.Rnd));
}
public int compareTo(Object indiv) throws ClassCastException {
if (!(indiv instanceof Individual))
throw new ClassCastException("An Individual object expected.");
if (this.getScore() < ((Individual) indiv).getScore()) {
return (-1);
} else if (this.getScore() > ((Individual) indiv).getScore()) {
return (1);
} else {
return (0);
}
}
}