-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lshape.py
54 lines (40 loc) · 1.27 KB
/
Lshape.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
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 1 14:48:26 2018
@author: Rahman Khorramfar
L-Shape Method for stochastic knapsack problem
"""
import numpy as np;
import SP;
from ProblemData import M,N,K,Omega,a,c,W,b,p,q;
import time;
current = time.time();
#%% Get the initial solution (x)
xs,initialOBj = SP.InitialSol(); EQ = []; #
Thetas = 10e+8; # theta is set to a large number for this problem at the beginning
# get H and T matrices
ht = SP.Get_h_T();
H = ht[0]; T = ht[1];
cuts = []; MP_obj =[]; MP_obj.append(initialOBj);
#%% Main Loop
while True: #abs(Thetas - EQ)>10e-2
rts = [];
for w in range(Omega):
# rt[0]= objective function, rt[1] dual of cons 3, rt[2] dual of cons 4
rt = SP.SolveSP(xs,w);
rts.append(rt); rt=[];
#EQ += p[w]*rt[0];
EQ.append(SP.Recourse_Expected_Value(xs,H,T,rts));
if abs(EQ[len(EQ)-1]- Thetas) <= 1:
print('\n\n Objective Function: ', MP_obj[len(MP_obj)-1]);
break
cuts.append(rts);
# add the optimality cut solve the MP and get values of x and theta
rt = SP.SolveMP(cuts,H,T);
MP_obj.append(rt[0]);
xs = rt[2];
Thetas = rt[1];
#print(Thetas, '\t', EQ);
#print('*****************');
#%% Plot the results
print('Elapsed Time: ', time.time()-current);