1
2 from scop2 import *
3
5 """
6 assign1.py:
7 Using SCOP for solving an assignment problem
8 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2011
9
10 Example 1 (Assignment Problem):
11 Three jobs (0,1,2) must be assigned to three workers (A,B,C)
12 so that each job is assigned to exactly one worker.
13 The cost matrix is represented by the list of lists
14 Cost=[[15, 20, 30],
15 [7, 15, 12],
16 [25,10,13]],
17 where rows of the matrix are workers, and columns are jobs.
18 Find the minimum cost assignment of workers to jobs.
19 """
20
21 m=Model()
22 workers=["A","B","C"]
23 Cost=[[15, 20, 30],[7, 15, 12],[25,10,13]]
24
25 varlist=m.addVariables(workers,range(3))
26
27
28 con1=Alldiff("AD",varlist)
29 con1.setWeight("inf")
30 con2=Linear("L",weight=1,rhs=0,direction="<=")
31 for i in range(len(workers)):
32 for j in range(3):
33 con2.addTerms(Cost[i][j],varlist[i],j)
34 m.addConstraint(con1)
35 m.addConstraint(con2)
36 m.Params.TimeLimit=1
37 sol,violated=m.optimize()
38
39 print m
40
41 print "solution"
42 for x in sol:
43 print x,sol[x]
44 print "violated constraint(s)"
45 for v in violated:
46 print v,violated[v]
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
66 """
67 assign2.py:
68 Using SCOP for solving an assignment problem under linear constraints.
69 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2011
70
71 Example 2 (Generalized Assignment Problem):
72 Three jobs (0,1,2) must be assigned to five workers (A,B,C,D,E).
73 The numbers of workers that must be assigned to jobs 0,1 and 2 are 1,1 and 2, respectively.
74 The cost matrix is represented by the list of lists
75 Cost=[[15, 20, 30],
76 [7, 15, 12],
77 [25,10,13],
78 [15,18,3],
79 [5,12,17]]
80 where rows are workers, and columns are jobs.
81 Find the minimum cost assignment of workers to jobs.
82
83 """
84
85 m=Model()
86 workers=["A","B","C","D","E"]
87 Cost=[[15, 20, 30],[7, 15, 12],[25,10,13],[15,18,3],[5,12,17]]
88 LB=[1,2,2]
89 varlist=m.addVariables(workers,range(3))
90 LBC={}
91 for j in range(len(LB)):
92 LBC[j]=Linear("LB%s"%j,"inf",LB[j],">=")
93 coeffs=[1 for i in range(5)]
94 values=[j for i in range(5)]
95 LBC[j].addTerms(coeffs,varlist,values)
96 m.addConstraint(LBC[j])
97 con1=Linear("L")
98 for i in range(len(workers)):
99 for j in range(3):
100 con1.addTerms(Cost[i][j],varlist[i],j)
101 m.addConstraint(con1)
102 m.Params.TimeLimit=1
103 sol,violated=m.optimize()
104 print m
105 print "solution"
106 for x in sol:
107 print x,sol[x]
108 print "violated constraint(s)"
109 for v in violated: print v,violated[v]
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
138 """
139 assign3.py:
140 Using SCOP for solving an assignment problem under a quadratic constraint.
141 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2011
142
143 Example 3 (Variation of Generalized Assignment Problem):
144 Three jobs (0,1,2) must be assigned to five workers (A,B,C,D,E).
145 The numbers of workers that must be assigned to jobs 0,1 and 2 are 1,1 and 2, respectively.
146 The cost matrix is represented by the list of lists
147 Cost=[[15, 20, 30],
148 [7, 15, 12],
149 [25,10,13],
150 [15,18,3],
151 [5,12,17]]
152 where rows are workers, and columns are jobs.
153 We add an additional condition: worker A cannot do the job with worker C.
154 Find the minimum cost assignment of workers to jobs.
155
156 """
157
158 m=Model()
159
160 workers=["A","B","C","D","E"]
161 Cost=[[15, 20, 30],[7, 15, 12],[25,10,13],[15,18,3],[5,12,17]]
162 LB=[1,2,2]
163
164 varlist=m.addVariables(workers,range(3))
165
166 LBC={}
167 for j in range(len(LB)):
168 LBC[j]=Linear("LB%s"%j,"inf",LB[j],">=")
169 for i in range(len(workers)):
170 LBC[j].addTerms(1,varlist[i],j)
171 m.addConstraint(LBC[j])
172
173 con1=Linear("L")
174 for i in range(len(workers)):
175 for j in range(3):
176 con1.addTerms(Cost[i][j],varlist[i],j)
177 m.addConstraint(con1)
178
179
180
181
182
183
184
185
186
187
188
189 con2=Quadratic("Q",100)
190 for j in range(3):
191 con2.addTerms(1,varlist[0],j,varlist[2],j)
192 m.addConstraint(con2)
193
194 m.Params.TimeLimit=1
195 sol,violated=m.optimize()
196
197 print m
198
199 print "solution"
200 for x in sol:
201 print x,sol[x]
202 print "violated constraint(s)"
203 for v in violated:
204 print v,violated[v]
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