/
GRACAR_CPLEX.txt
298 lines (258 loc) · 8.63 KB
/
GRACAR_CPLEX.txt
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
297
298
/**
* A CPLEX implementation of Group Role Assignment with Conflicting Agents on Roles (GRACAR);
* @author Haibin Zhu, 2016, revised 2020.
* * Please cite:
* [1] H. Zhu, “Avoiding Conflicts by Group Role Assignment”, IEEE Trans. on Systems, Man, and Cybernetics: Systems, vol. 46, no. 4, April 2016, pp. 535-547.
* [2] H. Zhu, M.C. Zhou, and R. Alkins, “Group Role Assignment via a Kuhn-Munkres Algorithm-based Solution”, IEEE Trans. on Systems, Man, and Cybernetics, Part A: Systems and Humans, vol. 42, no. 3, May 2012, pp. 739-750.
* [3] H. Zhu, and M. Zhou, “Role-Based Collaboration and its Kernel Mechanisms,” IEEE Trans. on Systems, Man, and Cybernetics, Part C: Applications and Reviews, vol. 36, no. 4, 2006, pp. 578-589.
* [4] H. Zhu, E-CARGO and Role-Based Collaboration: Modeling and Solving Problems in the Complex World, Wiley-IEEE Press, NJ, USA, Dec. 2021.
*/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.Integer;
import ilog.concert.*;
import ilog.cplex.*;
class GRACAR_ILOG {
int m; //number of agents
int n; //number of roles
double[] Q; //Qualification matrix
int[] C; //Conflict matrix
int[] L; //Requirement array
int[][] A; //Assignment array
DecimalFormat df = new DecimalFormat("0.00");
double optimized_result = 0;
boolean bILOG_result;
public GRACAR_ILOG(int nagent, int nrole, double[][] QM, int[][] CM, int[]RA)
{
m = nagent;
n = nrole;
Q = new double[m*n];
for(int i=0, r=0; r<m; r++) for (int c=0; c<n; c++){Q[i] = QM[r][c]; i++; }
C = new int[m*m];
for(int i=0, r=0; r<m; r++) for (int c=0; c<m; c++){C[i] = CM[r][c]; i++; }
L = new int[n];
L = RA;
A = new int[m][n];
for(int r=0; r<m; r++) for (int c=0; c<n; c++) A[r][c] = 0;
//LOG:
System.out.println("Qualification Matrix: ");
for (int i=0;i<m*n;i++)
{
System.out.print(df.format(Q[i])+" ");
if ((i+1)%(n) == 0) System.out.print("\n");
}
System.out.print("\n");
System.out.println("Requirement Array: ");
for(int i=0; i<n; i++)
{
System.out.print(L[i]+" ");
}
System.out.print("\n");
System.out.println("Conflict Matrix: ");
for (int i=0; i<m*m;i++)
{
System.out.print(C[i]+" ");
if ((i+1)%m == 0) System.out.print("\n");
}
System.out.print("\n");
}
public double resolve(int[][]TR)
{
try
{
//Creat cplex obj
IloCplex cplex = new IloCplex(); //initialize the cplex object
IloIntVar[]x = cplex.intVarArray(m*n, 0, 1); //initialize the variables array under cplex.
//cplex.addMinimize(cplex.scalProd(x, Q)); //add the optimize objective to cplex.
cplex.addMaximize(cplex.scalProd(x, Q)); //add the optimize objective to cplex.
//Add Constrains:
//Constrain type 1: unique constrains here, one person can only be assigned on one role at one time,
//thus there are number of 'm' constrains here need to be inserted into the cplex obj.
for(int i=0; i<m; i++)
{
IloLinearNumExpr exprUniConstrain = cplex.linearNumExpr();
for(int j = 0; j<n; j++)
{
exprUniConstrain.addTerm(1, x[n*i+j]);
}
cplex.addLe(exprUniConstrain, 1.0);
}
//Constrain type 2: Add role requirement constrains,
//the number of people assigned on each role should meet the requirement on that role.
//Hence, n constrains will be added.
for (int i = 0; i<n; i++)
{
IloLinearNumExpr exprReqConstrain = cplex.linearNumExpr();
for (int j = 0; j<m; j++)
{
exprReqConstrain.addTerm(1, x[i+j*n]);
}
cplex.addEq(exprReqConstrain, L[i]);
}
//Constrain type 3: The conflict constrains.
//On each role which require more than one people, all the constrains may occur on that role should be added
//Constrain type 3: The conflict constrains.
for (int r=0; r<n; r++) // Scan the cost matrix by column
{
if ( 1 < L[r] )
{
//Find out all the index of x on that column
int index[] = new int[m]; //number of person
int indexcounter = 0;
for(int i=0; i<m*n; i++)
{
if(i%n==r)
{
index[indexcounter]=i;
indexcounter++;
}
}
//Add conflicts constrains on that role.
for(int i=0; i<m*m; i++) //i size of the conflict chart
{
int row = i/m;
int col = i%m;
if (1 == C[i])
{
IloLinearNumExpr conflict = cplex.linearNumExpr();
conflict.addTerm(1, x[index[col]]);
conflict.addTerm(1, x[index[row]]);
cplex.addLe(conflict, 1);
}
}
}
}
//Solve LP
//long t1 = System.nanoTime();
if (cplex.solve())
{
bILOG_result = true;
optimized_result = cplex.getObjValue();
//cplex.output().println("Solution status = " + cplex.getStatus());
//cplex.output().println("Solution value = " + cplex.getObjValue());
double[] val = cplex.getValues(x);
int ncols = cplex.getNcols();
//cplex.output().println("Num COL: " + ncols);
cplex.output().println("Result Table: " );
for (int j=0; j<ncols; j++)
{
A[j/n][j%n] = (int)val[j];
System.out.print(A[j/n][j%n] + " ");
TR[j/n][j%n] = A[j/n][j%n];
//System.out.print(val[j]+ " ");
if ((j+1)%(n) == 0) {System.out.print("\n");}
}
//TR = A;
cplex.end();
}
else
{
cplex.end();
bILOG_result = true;
}
//long t2 = System.nanoTime();
//time[0] = (t2-t1)/1000000;
}
catch (IloException e){System.err.println("Concert exception" + e + " caught");}
return(optimized_result);
}
public double getOptimizedResult()
{
return optimized_result;
}
}
public class GRACARCPLEX {
public static void printDMatrix (double [][]x, int m, int n){
DecimalFormat tw = new DecimalFormat("0.00");
for (int i = 0; i < m; i++)
{ for (int j =0; j< n; j++)
{
System.out.print (tw.format(x[i][j])); System.out.print (" ");
}
System.out.println ();
}
System.out.println ();
}
public static void printIMatrix (int [][]x, int m, int n){
DecimalFormat tw = new DecimalFormat("0");
for (int i = 0; i < m; i++)
{ for (int j =0; j< n; j++)
{
System.out.print (tw.format(x[i][j])); System.out.print (" ");
}
System.out.println ();
}
System.out.println ();
}
public static int sigmaL(int []L){
int total=0;
for(int j=0; j<L.length; j++)
total+=L[j];
return total;
}
public static void main(String[] args)
{
Random generator = new Random();
DecimalFormat df = new DecimalFormat("0.00");
int m = 16;
int n =4;
int L[]={2,3,5,2};
int W[]={4,3,2,1};
double [][]Q={
{0.35,0.82,0.58,0.45},
{0.84,0.88,0.86,0.36},
{0.96,0.51,0.45,0.64},
{0.22,0.33,0.68,0.33},
{0.35,0.80,0.58,0.35},
{0.84,0.85,0.86,0.36},
{0.96,0.90,0.88,0.87},
{0.55,0.23,0.45,0.58},
{0.65,0.34,0.78,0.18},
{0.62,0.78,0.68,0.31},
{0.96,0.50,0.10,0.73},
{0.20,0.50,0.80,0.96},
{0.38,0.54,0.72,0.20},
{0.91,0.31,0.34,0.15},
{0.45,0.68,0.53,0.49},
{0.78,0.67,0.80,0.62},};
int [][]C ={
{0,0,0,0,1, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 0},
{0,0,0,0,0, 0,1,0,0,0, 0,0,0,0,0, 0},
{1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, 0},
{0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,1,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 1,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0},};
//TEST parameters:
int[][] T = new int[m][n];
long t11 = System.nanoTime();
//Init ILOG and resolve
System.out.print("[");
for (int i =0; i< L.length; i++)
System.out.print(""+L[i]+" ");
System.out.println("]");
GRACAR_ILOG ILOG = new GRACAR_ILOG(m, n, Q, C, L);
double v1 = ILOG.resolve(T);//ILOG.resolve(TR, time);
long t12 = System.nanoTime();
double diff1 = (double)(t12-t11)/1000000;
printDMatrix (Q, m, n);
printIMatrix (T, m, n);
System.out.print("L=[");
for (int j=0; j<n; j++) {System.out.print(L[j]+" ");} System.out.println("]");
System.out.println ("Total GRA ="+v1+" "+"Time = "+diff1+"ms");
System.out.println();
return;
}
}