forked from mkaouer/JavaCodeExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparator.java
More file actions
259 lines (215 loc) · 7.61 KB
/
Comparator.java
File metadata and controls
259 lines (215 loc) · 7.61 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
253
254
255
256
257
258
259
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MWM
*/
public class Comparator
{
ArrayList<Solution> solutions ;
ArrayList<ArrayList<Solution>> fronts ;
// Backup solutions
public Comparator(Population pop)
{
this.solutions = new ArrayList<Solution>();
this.fronts = new ArrayList<ArrayList<Solution>>();
for (int i = 0; i < pop.solutions.size(); i++)
{
this.solutions.add(pop.solutions.get(i));
}
//solutionSet_ = solutionSet ;
// dominateMe[i] contains the number of solutions dominating i
int [] dominateMe = new int[this.solutions.size()];
// iDominate[k] contains the list of solutions dominated by k
List<Integer> [] iDominate = new List[this.solutions.size()];
// front[i] contains the list of individuals belonging to the front i
List<Integer> [] front = new List[this.solutions.size()+1];
// flagDominate is an auxiliar variable
int flagDominate;
// Initialize the fronts
for (int i = 0; i < front.length; i++)
front[i] = new LinkedList<Integer>();
//-> Fast non dominated sorting algorithm
for (int p = 0; p < this.solutions.size(); p++)
{
// Initialice the list of individuals that i dominate and the number
// of individuals that dominate me
iDominate[p] = new LinkedList<Integer>();
dominateMe[p] = 0;
// For all q individuals , calculate if p dominates q or vice versa
for (int q = 0; q < this.solutions.size(); q++)
{
flagDominate =pop.compare_dominance(solutions.get(p), solutions.get(q));
if (flagDominate == -1)
{
iDominate[p].add(new Integer(q));
}
else if (flagDominate == 1)
{
dominateMe[p]++;
}
}
// If nobody dominates p, p belongs to the first front
if (dominateMe[p] == 0)
{
front[0].add(new Integer(p));
solutions.get(p).rank = 0 ;
}
}
//Obtain the rest of fronts
int i = 0;
Iterator<Integer> it1, it2 ; // Iterators
while (front[i].size()!= 0)
{
i++;
it1 = front[i-1].iterator();
while (it1.hasNext())
{
it2 = iDominate[it1.next().intValue()].iterator();
while (it2.hasNext())
{
int index = it2.next().intValue();
dominateMe[index]--;
if (dominateMe[index]==0)
{
front[i].add(new Integer(index));
solutions.get(index).rank =i;
}
}
}
}
//<-
//0,1,2,....,i-1 are front, then i fronts
for (int j = 0; j < i; j++)
{
it1 = front[j].iterator();
fronts.add(j, new ArrayList<Solution>());
while (it1.hasNext())
{
fronts.get(j).add(solutions.get(it1.next().intValue()));
}
}
for (int j = 0; j < fronts.size(); j++)
{
for (int k = 0; k < fronts.get(j).size(); k++)
{
fronts.get(j).get(k).rank = j ;
}
}
} // Ranking
/**
* Returns a <code>SolutionSet</code> containing the solutions of a given rank.
* @param rank The rank
* @return Object representing the <code>SolutionSet</code>.
*/
public ArrayList<Solution> getSubfront(int rank) {
return fronts.get(rank);
} // getSubFront
/**
* Returns the total number of subFronts founds.
*/
public int getNumberOfSubfronts() {
return fronts.size();
} // getNumberOfSubfronts
public void print_fronts()
{
System.out.println("done! and number of fronts is : "+this.getNumberOfSubfronts());
for (int j = 0; j < fronts.size(); j++)
{
System.out.println("\n Front number : "+j+" has solutions size : "+fronts.get(j).size());
for (int k = 0; k < fronts.get(j).size(); k++)
{
fronts.get(j).get(k).print_metrics();
}
}
}
void export_population()
{
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("yyyy.MM.dd'-'hh.mm.ss");
String file_name = new String("./output/result_population_");
file_name = file_name.concat(ft.format(dNow));
file_name = file_name.concat(".csv");
try
{
FileWriter writer = new FileWriter(file_name);
for (int j = 0; j < fronts.size(); j++)
{
writer.append("--- Front number "+Integer.toString(j)+" has "+fronts.get(j).size()+" solutions ---\n\n");
for(int i=0;i<fronts.get(0).get(0).objectives_names.size();i++)
{
writer.append(fronts.get(0).get(0).objectives_names.get(i));
if(i == (fronts.get(0).get(0).objectives_names.size()-1))
{
writer.append('\n');writer.append('\n');
}
else
{
writer.append(',');
}
}
System.out.println("\n Front number : "+j+" has solutions size : "+fronts.get(j).size());
for (int k = 0; k < fronts.get(j).size(); k++)
{
writer.append(fronts.get(j).get(k).objectives_values_to_string()+"\n");
if(k == (fronts.get(j).size()-1))
{
writer.append('\n');
}
}
}
//generate whatever data you want
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
// parameters for local testing for class Comparator
double aspiration_values[] = {0.4,0.6};
ReferencePoint ref = new ReferencePoint(aspiration_values) ;
Sigma s = new Sigma(0.5);
Population p = new Population(10,s,ref,50);
p.create_poplulation();
p.print_popluation_metrics(0);
/* testing non-dominated sorting and crowding distance
if(true)
{
System.out.println("\n\n--- Testing non-dominated sorting -- ");
for (int j = 0; j < p.solutions.size(); j++)
{
p.solutions.get(j).objectives.set(0,j+1);
p.solutions.get(j).objectives.get(1) = j+1 ;
}
Comparator c = new Comparator(p);
p.print_popluation_metrics(0);
}
* /
// testing crossover
/*
if(true)
{
ArrayList<Solution> test = new ArrayList<Solution>();
test = p.crossover(p.solutions.get(0), p.solutions.get(1));
System.out.println("--- Size of resulting crossover : "+test.size());
test.get(0).print_solution();
test.get(1).print_solution();
}
**/
}
}