Skip to content
This repository was archived by the owner on Jul 28, 2019. It is now read-only.

Commit 0f96b8f

Browse files
committed
Update RobustOptim.jl
1 parent 1b4409e commit 0f96b8f

File tree

1 file changed

+125
-10
lines changed

1 file changed

+125
-10
lines changed

RobustOptim.jl

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ using GLPKMathProgInterface
1212
# Function for MILP with Box Uncertainty and Budget of Uncertainty
1313
function milpBoxBudget(; num_x, num_y, vec_min_y, vec_max_y, vec_c, vec_f, vec_b, mat_a, mat_b,
1414
mat_a_bar, mat_a_hat, mat_b_bar, vec_b_bar, vec_gammaCap)
15-
num_row = length(mat_a_bar[:,1])
16-
num_col = length(mat_a_bar[1,:])
17-
for i = 1: num_row
18-
if vec_gammaCap[i] > num_col
19-
println("Error: vec_gammaCap[$i] is greater than num_row in nominal and hat data!")
15+
m = length(mat_a_bar[:,1])
16+
n = length(mat_a_bar[1,:])
17+
for i = 1: m
18+
if vec_gammaCap[i] > n
19+
println("Error: vec_gammaCap[$i] is greater than m in nominal and hat data!")
2020
end
2121
end
2222
println("---------------------------------------------------------\n",
@@ -32,12 +32,12 @@ function milpBoxBudget(; num_x, num_y, vec_min_y, vec_max_y, vec_c, vec_f, vec_b
3232
@constraint(model, vec_y[1: num_y] .>= vec_min_y)
3333
@constraint(model, mat_a * vec_x + mat_b * vec_y .>= vec_b)
3434
# Transformation of Box Uncertainty.
35-
@variable(model, vec_lambda[1: num_row] >= 0)
36-
@variable(model, mat_mu[1: num_row, 1: num_x] >= 0)
35+
@variable(model, vec_lambda[1: m] >= 0)
36+
@variable(model, mat_mu[1: m, 1: num_x] >= 0)
3737
@variable(model, vec_z[1: num_x] >= 0)
3838
@constraint(model, - vec_z .<= vec_x)
3939
@constraint(model, vec_x .<= vec_z)
40-
for i = 1: num_row
40+
for i = 1: m
4141
# sum() of variables without coefficients can be used directly
4242
@constraint(model, transpose(mat_a_bar[i, :]) * vec_x + vec_gammaCap[i] * vec_lambda[i] +
4343
sum(mat_mu[i, :]) + transpose(mat_b_bar[i, :]) * vec_y <= vec_b_bar[i])
@@ -55,10 +55,52 @@ function milpBoxBudget(; num_x, num_y, vec_min_y, vec_max_y, vec_c, vec_f, vec_b
5555
println("---------------------------------------------------------\n",
5656
" 2/2. Nominal Ending\n",
5757
"---------------------------------------------------------\n")
58-
return obj_result, vec_result_y, vec_result_x, vec_result_z, mat_result_mu, vec_result_lambda
58+
return (obj_result, vec_result_y, vec_result_x, vec_result_z, mat_result_mu, vec_result_lambda)
5959
end
6060

61-
# Fucntion for Two-Stage Stochastic LP with Box Uncertainty
61+
# Function for LP with Box Uncertainty and Budget of Uncertainty
62+
function lpBoxBudget(; num_x, vec_c, vec_b, mat_a, mat_a_bar, mat_a_hat, vec_b_bar, vec_gammaCap)
63+
m = length(mat_a_bar[:,1])
64+
n = length(mat_a_bar[1,:])
65+
for i = 1: m
66+
if vec_gammaCap[i] > n
67+
println("Error: vec_gammaCap[$i] is greater than m in nominal and hat data!")
68+
end
69+
end
70+
println("---------------------------------------------------------\n",
71+
" 1/2. Robust MILP with Box Uncertainty and Budget\n",
72+
"---------------------------------------------------------\n")
73+
println("Input: vec_gammaCap = $vec_gammaCap.")
74+
#
75+
model = Model(solver = GLPKSolverMIP())
76+
@variable(model, vec_x[1: num_x] >= 0)
77+
@objective(model, Min, (transpose(vec_c) * vec_x)[1])
78+
# Transformation of Box Uncertainty.
79+
@variable(model, vec_lambda[1: m] >= 0)
80+
@variable(model, mat_mu[1: m, 1: num_x] >= 0)
81+
@variable(model, vec_z[1: num_x] >= 0)
82+
@constraint(model, - vec_z .<= vec_x)
83+
@constraint(model, vec_x .<= vec_z)
84+
for i = 1: m
85+
# sum() of variables without coefficients can be used directly
86+
@constraint(model, transpose(mat_a_bar[i, :]) * vec_x + vec_gammaCap[i] * vec_lambda[i] +
87+
sum(mat_mu[i, :]) <= vec_b_bar[i])
88+
@constraint(model, vec_lambda[i] .+ hcat(mat_mu[i, :]) .>= hcat(mat_a_hat[i, :]) .* vec_z[:])
89+
end
90+
solve(model)
91+
obj_result = getobjectivevalue(model)
92+
vec_result_x = getvalue(vec_x)
93+
# println("Result: vec_x = $vec_result_x.")
94+
vec_result_z = getvalue(vec_z)
95+
mat_result_mu = getvalue(mat_mu)
96+
vec_result_lambda = getvalue(vec_lambda)
97+
println("---------------------------------------------------------\n",
98+
" 2/2. Nominal Ending\n",
99+
"---------------------------------------------------------\n")
100+
return (obj_result, vec_result_x, vec_result_z, mat_result_mu, vec_result_lambda)
101+
end
102+
103+
# Fucntion for Two-Stage Stochastic MILP with Box Uncertainty
62104
function milpAdjustBox(; num_x, num_y, num_z, vec_min_y, vec_max_y, vec_c, vec_f, vec_g, vec_b,
63105
mat_a, mat_b, mat_d, mat_a_bar, mat_a_hat, mat_b_bar, mat_d_bar, vec_b_bar)
64106
println("---------------------------------------------------------\n",
@@ -67,6 +109,8 @@ function milpAdjustBox(; num_x, num_y, num_z, vec_min_y, vec_max_y, vec_c, vec_f
67109
model = Model(solver = GLPKSolverMIP())
68110
# 1. Standard LP
69111
@variable(model, vec_y[1: num_y], Int)
112+
@constraint(model, vec_y >= vec_min_y)
113+
@constraint(model, vec_max_y >= vec_y)
70114
@variable(model, vec_x[1: num_x] >= 0)
71115
@variable(model, gamma)
72116
@variable(model, vec_alpha[1: num_z])
@@ -108,4 +152,75 @@ function milpAdjustBox(; num_x, num_y, num_z, vec_min_y, vec_max_y, vec_c, vec_f
108152
return (obj_result, vec_result_y, vec_result_x, vec_result_gamma, vec_result_theta1, vec_result_theta2)
109153
end
110154

155+
# Fucntion for Two-Stage Stochastic LP with Box Uncertainty
156+
function lpAdjustBox(; num_x, m, num_z, vec_c, vec_g, vec_b,
157+
mat_a, mat_d, mat_a_bar, mat_a_hat, mat_d_bar, vec_b_bar)
158+
println("---------------------------------------------------------\n",
159+
" 1/2. Adjustable Robust LP with Box Uncertainty\n",
160+
"---------------------------------------------------------\n")
161+
model = Model(solver = GLPKSolverMIP())
162+
# 1. Standard LP
163+
@variable(model, vec_x[1: num_x] >= 0)
164+
@variable(model, gamma)
165+
@variable(model, vec_alpha[1: num_z])
166+
@variable(model, vec_theta1[1: num_z])
167+
@objective(model, Min, (transpose(vec_c) * vec_x)[1] + gamma)
168+
@constraint(model, mat_a * vec_x + mat_d * (vec_alpha + vec_theta1) .>= vec_b)
169+
# 2. Get rid of the uncertainty in objective function
170+
@variable(model, vec_beta[1: num_z])
171+
@constraint(model, gamma >= (transpose(vec_g) * (vec_alpha + vec_theta1))[1])
172+
@constraint(model, vec_theta1 .<= vec_beta)
173+
@constraint(model, - vec_beta .<= vec_theta1)
174+
@constraint(model, vec_alpha .>= - vec_theta1) # z(zeta) must be greater than 0
175+
# 3. Transformation of Box Uncertainty.
176+
@variable(model, vec_theta2[1: num_z])
177+
vec_theta2_rep = (ones(m, num_z) .* vec_theta2')[:] # Repeat every theta2 num_y times and then concatenate.
178+
@constraint(model, mat_a_bar * vec_x + mat_a_hat * vec_theta2_rep +
179+
mat_d_bar * (vec_alpha + vec_theta1) .<= vec_b_bar)
180+
@constraint(model, vec_theta2_rep .<= vec_x)
181+
@constraint(model, - vec_x .<= vec_theta2_rep)
182+
# Solve the model
183+
solve(model)
184+
obj_result = getobjectivevalue(model)
185+
# println("Result: obj = $(obj).")
186+
vec_result_x = getvalue(vec_x)
187+
# println("Result: vec_x = $vec_result_x.")
188+
vec_result_theta1 = getvalue(vec_theta1)
189+
vec_result_theta2 = getvalue(vec_theta2)
190+
vec_result_gamma = getvalue(gamma)
191+
println("---------------------------------------------------------\n",
192+
" 2/2. Nominal Ending\n",
193+
"---------------------------------------------------------\n")
194+
return (obj_result, vec_result_x, vec_result_gamma, vec_result_theta1, vec_result_theta2)
195+
end
196+
197+
# Fucntion for LP with Box Uncertainty
198+
function lpBox(; num_x, m, num_z, vec_c, vec_b, mat_a, mat_a_bar, mat_a_hat, vec_b_bar)
199+
println("---------------------------------------------------------\n",
200+
" 1/2. Adjustable Robust LP with Box Uncertainty\n",
201+
"---------------------------------------------------------\n")
202+
model = Model(solver = GLPKSolverMIP())
203+
# 1. Standard LP
204+
@variable(model, vec_x[1: num_x] >= 0)
205+
@objective(model, Min, (transpose(vec_c) * vec_x)[1])
206+
@constraint(model, mat_a * vec_x .>= vec_b)
207+
# 2. Transformation of Box Uncertainty.
208+
@variable(model, vec_theta2[1: m])
209+
vec_theta2_rep = (ones(m, num_z) .* vec_theta2')[:] # Repeat every theta2 num_y times and then concatenate.
210+
@constraint(model, mat_a_bar * vec_x + mat_a_hat * vec_theta2_rep .<= vec_b_bar)
211+
@constraint(model, vec_theta2_rep .<= vec_x)
212+
@constraint(model, - vec_x .<= vec_theta2_rep)
213+
# Solve the model
214+
solve(model)
215+
obj_result = getobjectivevalue(model)
216+
# println("Result: obj = $(obj).")
217+
vec_result_x = getvalue(vec_x)
218+
# println("Result: vec_x = $vec_result_x.")
219+
vec_result_theta2 = getvalue(vec_theta2)
220+
println("---------------------------------------------------------\n",
221+
" 2/2. Nominal Ending\n",
222+
"---------------------------------------------------------\n")
223+
return (obj_result, vec_result_x, vec_result_theta2)
224+
end
225+
111226
end

0 commit comments

Comments
 (0)