-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathpiecewise.py
302 lines (251 loc) · 11.6 KB
/
piecewise.py
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
##@file piecewise.py
# @brief several approaches for solving problems with piecewise linear functions.
"""
Approaches:
- mult_selection: multiple selection model
- convex_comb_sos: model with SOS2 constraints
- convex_comb_dis: convex combination with binary variables (disaggregated model)
- convex_comb_dis_log: convex combination with a logarithmic number of binary variables
- convex_comb_agg: convex combination with binary variables (aggregated model)
- convex_comb_agg_log: convex combination with a logarithmic number of binary variables
Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
"""
import math
from pyscipopt import Model, quicksum
def mult_selection(model, a, b):
"""mult_selection -- add piecewise relation with multiple selection formulation
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
w, z = {}, {}
for k in range(K):
w[k] = model.addVar(lb=-model.infinity()) # do not name variables for avoiding clash
z[k] = model.addVar(vtype="B")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity())
for k in range(K):
model.addCons(w[k] >= a[k] * z[k])
model.addCons(w[k] <= a[k + 1] * z[k])
model.addCons(quicksum(z[k] for k in range(K)) == 1)
model.addCons(X == quicksum(w[k] for k in range(K)))
c = [float(b[k + 1] - b[k]) / (a[k + 1] - a[k]) for k in range(K)]
d = [b[k] - c[k] * a[k] for k in range(K)]
model.addCons(Y == quicksum(d[k] * z[k] + c[k] * w[k] for k in range(K)))
return X, Y, z
def convex_comb_sos(model, a, b):
"""convex_comb_sos -- add piecewise relation with gurobi's SOS constraints
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
z = {}
for k in range(K + 1):
z[k] = model.addVar(lb=0, ub=1, vtype="C")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity(), vtype="C")
model.addCons(X == quicksum(a[k] * z[k] for k in range(K + 1)))
model.addCons(Y == quicksum(b[k] * z[k] for k in range(K + 1)))
model.addCons(quicksum(z[k] for k in range(K + 1)) == 1)
model.addConsSOS2([z[k] for k in range(K + 1)])
return X, Y, z
def convex_comb_dis(model, a, b):
"""convex_comb_dis -- add piecewise relation with convex combination formulation
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
wL, wR, z = {}, {}, {}
for k in range(K):
wL[k] = model.addVar(lb=0, ub=1, vtype="C")
wR[k] = model.addVar(lb=0, ub=1, vtype="C")
z[k] = model.addVar(vtype="B")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity(), vtype="C")
model.addCons(X == quicksum(a[k] * wL[k] + a[k + 1] * wR[k] for k in range(K)))
model.addCons(Y == quicksum(b[k] * wL[k] + b[k + 1] * wR[k] for k in range(K)))
for k in range(K):
model.addCons(wL[k] + wR[k] == z[k])
model.addCons(quicksum(z[k] for k in range(K)) == 1)
return X, Y, z
def gray(i):
"""returns i^int(i/2)"""
return i ^ (int(i / 2))
def convex_comb_dis_log(model, a, b):
"""convex_comb_dis_log -- add piecewise relation with a logarithmic number of binary variables
using the convex combination formulation.
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
G = int(math.ceil((math.log(K) / math.log(2)))) # number of required bits
N = 1 << G # number of required variables
# print("K,G,N:",K,G,N
wL, wR, z = {}, {}, {}
for k in range(N):
wL[k] = model.addVar(lb=0, ub=1, vtype="C")
wR[k] = model.addVar(lb=0, ub=1, vtype="C")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity(), vtype="C")
g = {}
for j in range(G):
g[j] = model.addVar(vtype="B")
model.addCons(X == quicksum(a[k] * wL[k] + a[k + 1] * wR[k] for k in range(K)))
model.addCons(Y == quicksum(b[k] * wL[k] + b[k + 1] * wR[k] for k in range(K)))
model.addCons(quicksum(wL[k] + wR[k] for k in range(K)) == 1)
# binary variables setup
for j in range(G):
ones = []
zeros = []
for k in range(K):
if k & (1 << j):
ones.append(k)
else:
zeros.append(k)
model.addCons(quicksum(wL[k] + wR[k] for k in ones) <= g[j])
model.addCons(quicksum(wL[k] + wR[k] for k in zeros) <= 1 - g[j])
return X, Y, wL, wR
def convex_comb_agg(model, a, b):
"""convex_comb_agg -- add piecewise relation convex combination formulation -- non-disaggregated.
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
w, z = {}, {}
for k in range(K + 1):
w[k] = model.addVar(lb=0, ub=1, vtype="C")
for k in range(K):
z[k] = model.addVar(vtype="B")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity(), vtype="C")
model.addCons(X == quicksum(a[k] * w[k] for k in range(K + 1)))
model.addCons(Y == quicksum(b[k] * w[k] for k in range(K + 1)))
model.addCons(w[0] <= z[0])
model.addCons(w[K] <= z[K - 1])
for k in range(1, K):
model.addCons(w[k] <= z[k - 1] + z[k])
model.addCons(quicksum(w[k] for k in range(K + 1)) == 1)
model.addCons(quicksum(z[k] for k in range(K)) == 1)
return X, Y, z
def convex_comb_agg_log(model, a, b):
"""convex_comb_agg_log -- add piecewise relation with a logarithmic number of binary variables
using the convex combination formulation -- non-disaggregated.
Parameters:
- model: a model where to include the piecewise linear relation
- a[k]: x-coordinate of the k-th point in the piecewise linear relation
- b[k]: y-coordinate of the k-th point in the piecewise linear relation
Returns the model with the piecewise linear relation on added variables X, Y, and z.
"""
K = len(a) - 1
G = int(math.ceil((math.log(K) / math.log(2)))) # number of required bits
w, g = {}, {}
for k in range(K + 1):
w[k] = model.addVar(lb=0, ub=1, vtype="C")
for j in range(G):
g[j] = model.addVar(vtype="B")
X = model.addVar(lb=a[0], ub=a[K], vtype="C")
Y = model.addVar(lb=-model.infinity(), vtype="C")
model.addCons(X == quicksum(a[k] * w[k] for k in range(K + 1)))
model.addCons(Y == quicksum(b[k] * w[k] for k in range(K + 1)))
model.addCons(quicksum(w[k] for k in range(K + 1)) == 1)
# binary variables setup
for j in range(G):
zeros, ones = [0], []
# print(j,"\tinit zeros:",zeros,"ones:",ones
for k in range(1, K + 1):
# print(j,k,"\t>zeros:",zeros,"ones:",ones
if (1 & gray(k) >> j) == 1 and (1 & gray(k - 1) >> j) == 1:
ones.append(k)
if (1 & gray(k) >> j) == 0 and (1 & gray(k - 1) >> j) == 0:
zeros.append(k)
# print(j,k,"\tzeros>:",zeros,"ones:",ones
# print(j,"\tzeros:",zeros,"ones:",ones
model.addCons(quicksum(w[k] for k in ones) <= g[j])
model.addCons(quicksum(w[k] for k in zeros) <= 1 - g[j])
return X, Y, w
if __name__ == "__main__":
# random.seed(1)
a = [-10, 10, 15, 25, 30, 35, 40, 45, 50, 55, 60, 70]
b = [-20, -20, 15, -21, 0, 50, 18, 0, 15, 24, 10, 15]
print("\n\n\npiecewise: multiple selection")
model = Model("multiple selection")
X, Y, z = mult_selection(model, a, b) # X,Y --> piecewise linear replacement of x,f(x) based on points a,b
# model using X and Y (and possibly other variables)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))
print("\n\n\npiecewise: disaggregated convex combination")
model = Model("disaggregated convex combination")
X, Y, z = convex_comb_dis(model, a, b)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))
print("\n\n\npiecewise: disaggregated convex combination, logarithmic number of variables")
model = Model("disaggregated convex combination (log)")
X, Y, z = convex_comb_dis(model, a, b)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))
print("\n\n\npiecewise: SOS2 constraint")
model = Model("SOS2")
X, Y, w = convex_comb_sos(model, a, b)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))
print("\n\n\npiecewise: aggregated convex combination")
model = Model("aggregated convex combination")
X, Y, z = convex_comb_agg(model, a, b)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))
print("\n\n\npiecewise: aggregated convex combination, logarithmic number of variables")
model = Model("aggregated convex combination (log)")
X, Y, w = convex_comb_agg_log(model, a, b)
u = model.addVar(vtype="C", name="u")
A = model.addCons(3 * X + 4 * Y <= 250, "A")
B = model.addCons(7 * X - 2 * Y + 3 * u == 170, "B")
model.setObjective(2 * X + 15 * Y + 5 * u, "maximize")
model.optimize()
print("X:", model.getVal(X))
print("Y:", model.getVal(Y))
print("u:", model.getVal(u))