-
Notifications
You must be signed in to change notification settings - Fork 6
/
Pareto8020
367 lines (335 loc) · 10 KB
/
Pareto8020
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
This Program is used to conduct simulations of 80/20 distribution by GRA.
It needs the IBM CPLEX ILOG Optimization Package.
Please cite:
[1] H. Zhu, “Social Development Paradox: An E-CARGO Perspective on the Formation of the Pareto 80/20 Distribution,” IEEE Trans. on Computational Social Systems,2022 (In Press), avail: https://ieeexplore.ieee.org/document/9576894 .
[2] H. Zhu, E-CARGO and Role-Based Collaboration: Modeling and Solving Problems in the Complex World, Wiley-IEEE Press, NJ, USA, Dec. 2021.
[3] 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.
[4] 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, July. 2006, pp. 578-589.
*/
import ilog.concert.*;
import ilog.cplex.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.io.File;
import java.text.DecimalFormat;
import java.util.*;
class TestResult
{
public double sigma;
public double q20;
public double per;
public TestResult()
{
sigma=0;
q20=0;
per=0;
}
}
class TeRe
{
public int max;
public int min;
public int ave;
public TeRe()
{
max=0;
ave=0;
min=0;
}
}
class GRA_ILOG {
private int m; //number of agents
private int n; //number of roles
private double[] Q; //Qualification matrix
private int[] L; //Role Requirement array
private int[][] A; //Assignment array
DecimalFormat df = new DecimalFormat("0.00");
double optimized_result = 0;
boolean bILOG_result;
public GRA_ILOG(int nagent, int nrole, double[][] QM, 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++; }
L = new int[n];
for(int j=0; j<n; j++) {L[j] = RA[j]; }
A = new int[m][n];
for(int r=0; r<m; r++) for (int c=0; c<n; c++) A[r][c] = 0;
}
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.addMaximize(cplex.scalProd(x, Q)); //add the optimize objective to cplex.
//Add Constraint 1: L[j];
for (int j = 0; j<n; j++)
{
IloLinearNumExpr exprReqConstraint = cplex.linearNumExpr();
for (int i = 0; i<m; i++)
{
exprReqConstraint.addTerm(1, x[j+i*n]);
}
cplex.addEq(exprReqConstraint, L[j]);
}
//Constrain type 2: LA[i] The agent limit constrains.
for (int i=0; i<m; i++) // for each agent
{
IloLinearNumExpr exprAgentLimitConstraint = cplex.linearNumExpr();
for (int j = 0; j<n; j++)
{
exprAgentLimitConstraint.addTerm(1, x[j+i*n]);
}
cplex.addLe(exprAgentLimitConstraint, 1);
}
//Solve LP
if (cplex.solve())
{
bILOG_result = true;
optimized_result = cplex.getObjValue();
double[] val = cplex.getValues(x);
int ncols = cplex.getNcols();
for (int j=0; j<ncols; j++)
{
A[j/n][j%n] = (int)val[j];
TR[j/n][j%n] = A[j/n][j%n];
}
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;
}
};
class roleValue{
int i;
int j;
double q;
roleValue(){
i=0; j =0; q =0;
}
roleValue(int x, int y, double z){
i =x; j = y; q =z;
}
roleValue(roleValue rv){
i =rv.i; j =rv.j; q =rv.q;
}
}
public class P8020m {
static DecimalFormat df = new DecimalFormat("0.00");
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");
System.out.print("{");
for (int i = 0; i < m; i++)
{ System.out.print("{");
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 double getNewRV(roleValue []rv, roleValue []newrv, int m1, int m2) {
//Get the first 20% of agents with the largest assigned Q values to newrv (m2) from rv (m1)
double q20=0;
int i3=0;
for (int i2 =0; i2< m1; i2++) {
if (rv[i2]!=null) {
newrv[i3]=new roleValue(rv[i2]);
for (int i1 = i2+1; i1< m1; i1++) {
if (rv[i1]!=null) {
if (rv[i1].q>newrv[i3].q) {
roleValue temp =rv[i1];
rv[i1]=newrv[i3];
newrv[i3]=temp;
rv[i2]=newrv[i3];//Keep rv the same as new rv for the first 20%.
}
}
}
if (newrv[i3]!=null) {
q20+=newrv[i3].q;
i3++;
if (i3>=m2) break;
}
else System.out.println("Error!!!!!!!!!!!!!!!!");
}
}
// for (int i = 0; i < m2; i++)//Suppose
// System.out.print ("<"+newrv[i].j+", "+ df.format(newrv[i].q)+"> ");
// System.out.println();
return q20;
}
public static double CollectTwenty(double [][]Q, int [][]T, int m, int n, roleValue [] newRV) {
double res =0;
int m_20 = (int)(m*0.2);
roleValue []RV=new roleValue[m];
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if (T[i][j]==1) RV[i]=new roleValue(i, j, Q[i][j]);
}
}
// for (int i = 0; i < m; i++)//Suppose
// System.out.print ("<"+RV[i].j+", "+ df.format(RV[i].q)+"> ");
System.out.println();
res=getNewRV (RV, newRV, m, m_20);
// for (int i = 0; i < m_20; i++)//Suppose
// System.out.print ("<"+RV[i].j+", "+ df.format(RV[i].q)+"> ");
// System.out.println();
return res;
}
public static void ReviseQ(double [][]Q, int [][]T, double []le, int m, int n, double rate) {
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if (1==T[i][j]) Q[i][j]=Q[i][j]*(1+le[i])*rate;
else Q[i][j]=Q[i][j]*(1+le[i]);
}
}
}
public static void ReviseQ1(double [][]Q, int [][]T, double []le, int m, int n, double rate) {
//A new way to adjust the Q matrix, i.e., delta impacts both roles assigned or not.
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
Q[i][j]=Q[i][j]*(1+le[i])*rate;
}
}
}
//private static TestResult randamtest(int m, int n, int r_limit, int a_limit) {
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("0.00");
Random generator = new Random();
TeRe tere[]=new TeRe[10];//To store the 10 results for 10 different ms.
double rate = 1.1; //The change rate of Qs when re-assignment is conducted.
try
{
File myObj = new File("Result");
myObj.delete();
BufferedWriter out = new BufferedWriter(new FileWriter(("Result"), true));
for (int ll =0; ll<10; ll++) {
int m=172+ll*16;//16 = 156*10%.
int n = 4;
int L[]= {1, 5, 25, 125};
int [] ta=new int [100];
for (int kk=0;kk<100;kk++) {//For 100 random initial Qs and LEs.
System.out.println("Q"+kk+" is created!");
double [][]Q=new double [m][n];
double []LE=new double [m];
out.write("Initial Q"+kk+": \n"); // Random Q
for(int r=0; r<m; r++){ for(int c=0; c<n; c++){
Q[r][c] = generator.nextDouble();//Determines the range of Q
out.write(df.format(Q[r][c]) + " ");
}
out.write("\n");
}
// Random LE
out.write("LE:\n");
for(int i = 0; i<m; i++) {
LE[i]=generator.nextDouble()-0.5;//Determines the range of LE
out.write(""+LE[i]);
}
out.write("\n");
int[][] T = new int[m][n];
//Init ILOG and resolve
double v=0.0f;
int RA = 200;//Assume there are at most RA times of re-assignment.
TestResult []tr = new TestResult[RA];
for (int i = 0; i < m; i++)//Suppose
System.out.print (" "+df.format(LE[i]));
System.out.println ();
int k=0;
//Role re-assignment to see when 80/20 happens.
do {
tr[k]=new TestResult();
GRA_ILOG ILOG = new GRA_ILOG(m, n, Q, L);
v = ILOG.resolve(T);
out.write("T: \n");
for(int r=0; r<m; r++){ for(int c=0; c<n; c++){
out.write(" "+T[r][c] + " ");
}
out.write("\n");
}
out.write("\n");
int m_20=(int)(m*0.2);
roleValue [] newRV = new roleValue[m_20];
double q20=CollectTwenty(Q, T, m, n, newRV);
out.write("Top 20 agents ("+(k+1)+"):\n");
for(int r=0; r<m_20; r++){
out.write(" " + newRV[r].i+" "+ newRV[r].j+" "+ df.format(newRV[r].q)+"\n");
}
out.write("\n");
tr[k].sigma=v;
tr[k].q20=q20;
tr[k].per=q20/v;
ReviseQ1(Q, T, LE, m, n, rate);
out.write("Q"+(k+1)+":\n");
for(int r=0; r<m; r++){ for(int c=0; c<n; c++){
out.write(df.format(Q[r][c]) + " ");
}
out.write("\n");
}
k++;
System.out.println ("Re-Assignment "+k);
} while (tr[k-1].per<0.8);
for ( int k1 = 0; k1 < k; k1++)//Suppose
System.out.println (" "+k1+": "+df.format(tr[k1].sigma)+" "+df.format(tr[k1].q20)+" "+df.format(tr[k1].per));
System.out.println();
ta[kk]=k-1;
}
//Collect the data for 100 random initial Qs
int min =1000, max=-100;
int ave=0;
for ( int kk = 0; kk < 100; kk++) {
System.out.println (" "+ta[kk]);
if (ta[kk]>max) max=ta[kk];
if (ta[kk]<min) min=ta[kk];
ave+=ta[kk];
}
tere[ll]=new TeRe();
tere[ll].max=max;
tere[ll].ave=ave/100;
tere[ll].min=min;
}
for ( int ll = 0; ll < 10; ll++)//Collect all the data for 10 different ms.
out.write (" "+(172+ll*16)+" "+tere[ll].max+" "+tere[ll].ave+" "+tere[ll].min+"\n");
out.write("\n");
out.close();
}
catch (IOException e) {System.out.println ("Error in writing into a file!");}
}
}