-
Notifications
You must be signed in to change notification settings - Fork 0
/
L_shape_algo.py
122 lines (113 loc) · 3.41 KB
/
L_shape_algo.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.3.3
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
from gurobipy import *
import numpy as np
# +
#Function to print results of an optimization problem
def printResult(m):
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('\nMinimal cost %g \n' % m.objVal)
##L-shaped algorithm
#remaining things to address
# 1. m2 == 0, n2 == 0, n1 == 0
#
def LShapedAlgo(c, q, A, b, W, T, h, p):
n1 = len(c)
m1 = len(b)
n2 = len(q)
m2 = W.shape[0]
K = len(p)
#Initialization
optimal = False
r = 0
s = 0
nu = 0
#Create master problem
master = Model('master problem')
master.setParam('OutPutFlag', False)
x = master.addMVar(n1, lb=0, name='x')
master.setObjective(c @ x, GRB.MINIMIZE)
if m1 > 0:
master.addConstr(A @ x == b)
theta_val = float("-inf")
#L-shaped iterations
while(not optimal):
infease = False
nu += 1
master.optimize()
if s >= 1:
theta_val = theta.x
print('\nmaster problem %d solved' % nu)
print('x = ')
print(master.x)
print('theta = ')
print(theta_val)
x_cur = x.x
#Feasibility cuts
for k in range(K):
hk = h[k]
Tk = T[k]
fcut = Model('feasibilityCut')
fcut.setParam('OutPutFlag', False)
y = fcut.addMVar(n2, lb=0, name='y')
vp = fcut.addMVar(m2, lb=0, name='vplus')
vm = fcut.addMVar(m2, lb=0, name='vminus')
e = np.ones(m2)
fcut.setObjective(e @ vp + e @ vm)
fcut.addConstr(W @ y + vp - vm == hk - np.dot(Tk, x_cur))
fcut.optimize()
if fcut.objVal > 0:
r += 1
pi = np.array(fcut.pi)
D = np.dot(pi,T)
d = np.dot(pi,hk)
print('feasibility cut %d created with\n D =' % r)
print(D)
print('d=%f'%d)
master.addConstr(D @ x >= d)
infease = True
break
if infease:
continue
##Optimality cuts
E = np.zeros(n1)
e = 0
for k in range(K):
hk = h[k]
Tk = T[k]
ocut = Model('optimalityCut')
ocut.setParam('OutPutFlag', False)
y = ocut.addMVar(n2,lb=0, name='y')
ocut.setObjective(q @ y, GRB.MINIMIZE)
ocut.addConstr(W @ y == hk - np.dot(Tk,x_cur))
ocut.optimize()
pi = np.array(ocut.pi)
E += p[k]*np.dot(pi,Tk)
e += p[k]*np.dot(pi,hk)
if theta_val >= e - np.dot(E,x_cur):
optimal = True
print('theta: %f >= e-E\'X = %f ' % (theta_val, e - np.dot(E,x_cur)))
print('optimality reached')
printResult(master)
else:
s += 1
if s == 1:
theta = master.addMVar(1,lb=-GRB.INFINITY, name='theta')
master.setObjective(c @ x + theta, GRB.MINIMIZE)
master.addConstr(E @ x + theta >= e)
print('optimality cut %d created with \n E = ' % s)
print(E)
print('e = %f' % e)