-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecuitSimuleAgenda.java
More file actions
296 lines (249 loc) · 7.61 KB
/
Copy pathRecuitSimuleAgenda.java
File metadata and controls
296 lines (249 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.util.ArrayList;
public class RecuitSimuleAgenda {
//Matrice graphe
static int[][] matrice = new int[][]{
//A B C D E F G H I J K
{0,1,1,0,1,0,1,1,0,1,0}, //A
{1,0,1,1,0,0,1,1,1,0,1}, //B
{1,1,0,0,1,1,0,1,0,0,0}, //C
{0,1,0,0,0,1,0,1,0,1,0}, //D
{1,0,1,0,0,0,1,0,1,0,1}, //E
{0,0,1,1,0,0,1,0,0,1,0}, //F
{1,1,0,0,1,1,0,1,1,0,0}, //G
{1,1,1,1,0,0,1,0,1,0,1}, //H
{0,1,0,0,1,0,1,1,0,1,0}, //I
{1,0,0,1,0,1,0,0,1,0,0}, //J
{0,1,0,0,1,0,0,1,0,0,0} //K
};
//Constraints Couple of predecessor
static int[][] predecessorConstraint = new int[][]{
{4 ,9},
{3,10},
{5,10}
};
//Coloration initial
static int[] colorInitial = new int[]{
//A B C D E F G H I J K
0,1,2,3,0,1,2,3,0,1,2
};
static int numberOfColor = 4;
static int maxNumberOfVerticePerColor = 3;
//Some const for playing :)
static int weightColorationConflict = 1;
static int weightPredescessorConflict = 5;
static int weightMaxVerticePerColorConflict = 5;
//Const for Temperature
static double temperature = 30;
static double coolingGraduatation = 0.95; //[0.9 - 0.99]
//Number of step used by algorihtm
static int step = 0;
static int stepBeforeCooling = 100;
public static void main(String[] args) {
/** Algo recuit simule**/
int[] coloration = colorInitial;
int scoreCurrent = countScoreOfColoration(coloration);
int bufferStepForCooling = 0;
int maxStep = 1000000;
while(scoreCurrent > 0 && step < maxStep){
//Pick One algo for neighbour search
int[] colorationNext = rand2Change(coloration);
//int[] colorationNext = rand3Change(coloration);
//int[] colorationNext = alg2OptChange(coloration);
//int[] colorationNext = alg3OptChange(coloration);
int scoreNext = countScoreOfColoration(colorationNext);
if(bufferStepForCooling > stepBeforeCooling)
temperature *= coolingGraduatation;
if(shouldAccept( temperature, (scoreNext - scoreCurrent)) ){
scoreCurrent = scoreNext;
coloration = colorationNext;
bufferStepForCooling = 0;
}
bufferStepForCooling++;
step++;
}
printAgenda(coloration);
}
/**
*
* @param temperature
* @param deltaE difference between newScore - score
* @return
*/
public static boolean shouldAccept(double temperature, double deltaE) {
return (deltaE > 0.0) || (Math.random() <= probabilityOfAcceptance(temperature, deltaE));
}
/**
* Recuit acceptance
* @param temperature
* @param deltaE
* @return
*/
public static double probabilityOfAcceptance(double temperature, double deltaE) {
return Math.exp(deltaE / temperature);
}
/**
* Randomize two color
* @param color
* @return
*/
public static int[] rand2Change(int[] color){
int[] colorBuffer = color;
int id1 = (int)((Math.random() * colorBuffer.length));
int id2 = (int)((Math.random() * colorBuffer.length));
while(id1 == id2){
id2 = (int)((Math.random() * colorBuffer.length));
}
colorBuffer[id1] = (int)((Math.random() * numberOfColor));
colorBuffer[id2] = (int)((Math.random() * numberOfColor));
return colorBuffer;
}
/**
* Randomize three color
* @param color
* @return
*/
public static int[] rand3Change(int[] color){
int[] colorBuffer = color;
int id1 = (int)((Math.random() * colorBuffer.length));
int id2 = (int)((Math.random() * colorBuffer.length));
int id3 = (int)((Math.random() * colorBuffer.length));
while(id1 != id2 && id2 != id3){
id2 = (int)((Math.random() * colorBuffer.length));
}
while(id3 != id2 && id1 != id3){
id3 = (int)((Math.random() * colorBuffer.length));
}
colorBuffer[id1] = (int)((Math.random() * numberOfColor));
colorBuffer[id2] = (int)((Math.random() * numberOfColor));
colorBuffer[id3] = (int)((Math.random() * numberOfColor));
return color;
}
/**
* Switch two color
* @param color
* @return
*/
public static int[] alg2OptChange(int[] color){
int[] colorBuffer = color;
int id1 = (int)((Math.random() * colorBuffer.length));
int id2 = (int)((Math.random() * colorBuffer.length));
while(id1 == id2){
id2 = (int)((Math.random() * colorBuffer.length));
}
int buffer = colorBuffer[id1];
colorBuffer[id1] = colorBuffer[id2];
colorBuffer[id2] = buffer;
return colorBuffer;
}
/**
* Switch three color
* @param color
* @return
*/
public static int[] alg3OptChange(int[] color){
int[] colorBuffer = color;
int id1 = (int)((Math.random() * colorBuffer.length));
int id2 = (int)((Math.random() * colorBuffer.length));
int id3 = (int)((Math.random() * colorBuffer.length));
while(id1 != id2 && id2 != id3){
id2 = (int)((Math.random() * colorBuffer.length));
}
while(id3 != id2 && id1 != id3){
id3 = (int)((Math.random() * colorBuffer.length));
}
int buffer = colorBuffer[id1];
colorBuffer[id1] = colorBuffer[id2];
colorBuffer[id2] = colorBuffer[id3];
colorBuffer[id3] = buffer;
return color;
}
/**
* @return the total number of conflict on the current coloration
*/
public static int countScoreOfColoration(int[] color){
int conflict = 0;
//Coloration Conflict
for(int i=0; i<matrice.length;i++)
conflict += weightColorationConflict * countConflictOnAVertice(i, color);
//Predecessor Constraint
for(int[] couple : predecessorConstraint)
conflict += weightPredescessorConflict * additionalConstraintOnPredecessor(couple[0], couple[1], color);
//MaxVerticesPerColor Constraint
conflict += weightMaxVerticePerColorConflict * additionalConstraintOnMaxVerticePerColor(3, color);
return conflict;
}
/**
* @return the number of conflict of a specific vertice on the curent coloration
*/
public static int countConflictOnAVertice(int verticeId, int[] color){
int conflict = 0;
for(int j=0; j<matrice[verticeId].length;j++)
if(matrice[verticeId][j] == 1 && color[verticeId] == color[j])
conflict++;
return conflict;
}
/**
* Additional constraint Predecessor
* @return
*/
public static int additionalConstraintOnPredecessor(int idOfFirstVertice, int idOfSecondVertice, int[] color){
int conflict = 0;
if(color[idOfFirstVertice] > color[idOfSecondVertice])
conflict++;
return conflict;
}
/**
* Additional constraint on max vertices/color
* @return
*/
public static int additionalConstraintOnMaxVerticePerColor(int maxNumber, int[] color){
int conflict = 0;
int[] colorOccurence = new int[numberOfColor];
for(int i = 0; i < color.length ; i ++){
colorOccurence[color[i]]++;
if(colorOccurence[color[i]] > maxNumber)
conflict++;
}
return conflict;
}
/**
* Print a console formating Agenda from the coloration
*/
public static void printAgenda(int[] color){
ArrayList<ArrayList<Integer>> sortedVerticeByColor = new ArrayList<>();
for(int i=0;i<numberOfColor;i++)
sortedVerticeByColor.add(new ArrayList<Integer>());
for(int i=0;i<color.length;i++)
sortedVerticeByColor.get(color[i]).add(i);
System.out.println("Score Total : " + countScoreOfColoration(color));
System.out.println("Step number : " + step);
System.out.println("#############");
for(ArrayList<Integer> seance : sortedVerticeByColor){
for(Integer session : seance)
System.out.print(charPicker(session) + " ");
System.out.println();
System.out.println("########");
}
}
/**
* Translate the vertice ID into a Character
* @param index of the vertice
* @return
*/
public static String charPicker(Integer index){
switch(index){
case 0 : return "A";
case 1 : return "B";
case 2 : return "C";
case 3 : return "D";
case 4 : return "E";
case 5 : return "F";
case 6 : return "G";
case 7 : return "H";
case 8 : return "I";
case 9 : return "J";
case 10 : return "K";
default : return "unknow";
}
}
}