-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPython_Pulp_GRA.txt
146 lines (141 loc) · 4.41 KB
/
Python_Pulp_GRA.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
"""
This is a Python Implementation for GRA and WGRA using Pulp, i.e., Problem 2 in [1].
Please cite:
[1] H. Zhu, "Group Role Assignment with Constraints (GRA+): A New Category of Assignment Problems," IEEE Trans. on Systems, Man, and Cybernetics: Systems (In Press), 2022, DOI: 10.1109/TSMC.2022.3199096.
[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.
Authors: Haibin Zhu, Aug. 2022
"""
import pulp
import time
class GRA:
def __init__(self, nagent, nrole, QM, RA):
self.m = nagent
self.n = nrole
self.L = RA
self.Q = QM
@property
def resolve(self):
Agents = range(self.m)
Roles = range(self.n)
gra = pulp.LpProblem("GRA Model", pulp.LpMaximize)
Assignments = [i*n+j for i in Agents for j in Roles]
mn=self.m*self.n
vars = pulp.LpVariable.dicts("Assignment", range (mn), 0, 1, pulp.LpInteger)
# The objective function is added to 'prob' first
gra += (
pulp.lpSum([vars[index] * self.Q[int(index / n)][index % n] for index in Assignments]),
"Sum_of_Assignments",
)
for j in Roles:
gra += (
pulp.lpSum([vars[i*n+j] for i in Agents]) == self.L[j],
"each_role%s" % j,
)
for i in Agents:
gra += (
pulp.lpSum([vars[i*n+j] for j in Roles]) <= 1,
"each_agent%s" % i,
)
gra.solve()
T = [None]*mn
for v in gra.variables():
print(v.name, " ", v.varValue)
ind = int(v.name[11:len(v.name)])
print(ind)
if abs(1 - v.varValue) < 0.0001:
T[ind]=1
else:
T[ind]=0
return T
def printDMatrix(x, m, n):
txt = "{:.2f}"
for i in range(m):
for j in range(n):
print(txt.format(x[i][j]), " ", end='')
print()
def printIMatrix(x, m, n):
txt = "{:2}"
for i in range(m):
for j in range(n):
print(txt.format(x[i][j]), " ", end='')
print()
def sigmaL(L):
total = 0
for j in range(len(L)):
total += L[j]
return total
import copy
def getWQ(m, n, Q, W):
maxQ = 1
WQ = copy.deepcopy(Q)
for i in range(m):
for j in range(n):
WQ[i][j] = Q[i][j] * W[j]
if WQ[i][j] > maxQ:
maxQ = WQ[i][j]
for i in range(m):
for j in range(n):
WQ[i][j] = WQ[i][j] / maxQ
return WQ
m = 16
n = 4
L = [2, 3, 5, 2]
W = [4, 3, 2, 1]
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]]
"""
m = 9
n = 4
L = [2, 3, 2, 2]
# W = [4, 3, 2, 1]
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.78, 0.67, 0.80, 0.62]]
"""
t1 = int(round(time.time() * 1000))
# Th Next statement is for GRA
PGRA = GRA(m, n, Q, L)
# Replace the above line by the following three lines, you will get WGRA
#WQ=getWQ (m ,n, Q, W)
#PGRA = GRA(m, n, WQ, L)
T = PGRA.resolve
t2 = int(round(time.time() * 1000))
diff1 = t2 - t1
print("Q=")
printDMatrix(Q, m, n);
mat = []
while T != []:
mat.append(T[:n])
T = T[n:]
printIMatrix(mat, m,n)
print("L=", L)
print("W=", W)
v1 = 0
for i in range(m):
for j in range(n):
v1+= Q[i][j] * mat[i][j]
print("Total (W)GRA =", v1, " ", "Time = ", diff1, "ms")
del GRA