Skip to content

Commit cfdd952

Browse files
authored
Merge pull request #111 from JuliaOpt/fix_sdp_bad_constraint_handling
Fix sdp bad constraint handling
2 parents 3473e8d + 930b188 commit cfdd952

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

examples/battery_storage_parallel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ println("library loaded")
7777
end
7878

7979
function constraint(t, x, u, xi)
80-
return( (x[1] <= s_bounds[1][2] )&(x[1] >= s_bounds[1][1]))
80+
return true
8181
end
8282

8383
function finalCostFunction(x)

examples/benchmark.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function benchmark_sdp()
259259
end
260260

261261
function constraints(t, x, u, w)
262-
return (VOLUME_MIN<=x[1]<=VOLUME_MAX)&(VOLUME_MIN<=x[2]<=VOLUME_MAX)
262+
return true
263263
end
264264

265265
function finalCostFunction(x)

src/SDPoptimize.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ function build_sdpmodel_from_spmodel(model::SPModel)
7878

7979
if isa(model,PiecewiseLinearCostSPmodel)||isa(model,LinearDynamicLinearCostSPmodel)
8080
function cons_fun(t,x,u,w)
81-
for i in 1:model.dimStates
82-
if (x[i]<model.xlim[i][1]) || (x[i]>model.xlim[i][2])
83-
return false
84-
end
85-
end
8681
return true
8782
end
8883
if in(:finalCostFunction,fieldnames(model))
@@ -418,7 +413,7 @@ function get_control(model::SPModel,param::SDPparameters,V, t::Int64, x::Array)
418413

419414
next_state = SDPmodel.dynamics(t, x, u, w_sample)
420415

421-
if SDPmodel.constraints(t, next_state, u, w_sample)
416+
if SDPmodel.constraints(t, x, u, w_sample)&&SDPutils.is_next_state_feasible(next_state, model.dimStates, model.xlim)
422417
ind_next_state = SDPutils.real_index_from_variable(next_state, x_bounds, x_steps)
423418
next_V = Vitp[ind_next_state...]
424419
current_V += proba *(SDPmodel.costFunctions(t, x, u, w_sample) + next_V)
@@ -481,7 +476,7 @@ function get_control(model::SPModel,param::SDPparameters,V, t::Int64, x::Array,
481476

482477
next_state = SDPmodel.dynamics(t, x, u, w)
483478

484-
if SDPmodel.constraints(t, next_state, u, w)
479+
if SDPmodel.constraints(t, x, u, w)&&SDPutils.is_next_state_feasible(next_state, model.dimStates, model.xlim)
485480
ind_next_state = SDPutils.real_index_from_variable(next_state, x_bounds, x_steps)
486481
next_V = Vitp[ind_next_state...]
487482
current_V = SDPmodel.costFunctions(t, x, u, w) + next_V
@@ -586,7 +581,7 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
586581

587582
next_state = model.dynamics(t, x, u, w_sample)
588583

589-
if model.constraints(t, next_state, u, w_sample)
584+
if model.constraints(t, x, u, w_sample)&&SDPutils.is_next_state_feasible(next_state, model.dimStates, model.xlim)
590585
ind_next_state = SDPutils.real_index_from_variable(next_state, x_bounds, x_steps)
591586
next_V = Vitp[ind_next_state...]
592587
current_V += proba *(model.costFunctions(t, x, u, w_sample) + next_V)
@@ -629,7 +624,7 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
629624

630625
next_state = model.dynamics(t, x, u, scenario[t,1,:])
631626

632-
if model.constraints(t, next_state, u, scenario[t])
627+
if model.constraints(t, x, u, scenario[t])&&SDPutils.is_next_state_feasible(next_state, model.dimStates, model.xlim)
633628
ind_next_state = SDPutils.real_index_from_variable(next_state, x_bounds, x_steps)
634629
next_V = Vitp[ind_next_state...]
635630
current_V = model.costFunctions(t, x, u, scenario[t,1,:]) + next_V

src/SDPutils.jl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ function real_index_from_variable(variable, bounds::Array, variable_steps::Array
4949
return tuple([1 + ( variable[i] - bounds[i][1] )/variable_steps[i] for i in 1:length(variable)]...)
5050
end
5151

52+
"""
53+
Check if next state x_{t+1} satisfies state bounds constraints
54+
55+
# Parameters
56+
* `next_stae::Array`:
57+
the state we want to check
58+
* `x_dim::Int`:
59+
the number of state variables
60+
* `x_bounds::Array`:
61+
the state variables bounds
62+
63+
# Returns
64+
* `index::Tuple{Float64}`:
65+
the indexes of the variable
66+
67+
"""
68+
function is_next_state_feasible(next_state, x_dim, x_bounds)
69+
70+
next_state_box_const = true
71+
72+
for i in 1:x_dim
73+
next_state_box_const = (next_state_box_const&&
74+
(next_state[i]>=x_bounds[i][1])&&
75+
(next_state[i]<=x_bounds[i][2]))
76+
end
77+
78+
return next_state_box_const
79+
end
5280

5381
"""
5482
Computes the value function at time t evaluated at state x in a decision
@@ -104,7 +132,7 @@ function compute_V_given_x_t_DH(sampling_size, samples, probas, u_bounds,
104132
proba = probas[w]
105133
next_state = dynamics(t, x, u, w_sample)
106134

107-
if constraints(t, next_state, u, w_sample)
135+
if constraints(t, x, u, w_sample)&&is_next_state_feasible(next_state, x_dim, x_bounds)
108136

109137
count_admissible_w = count_admissible_w + proba
110138
ind_next_state = real_index_from_variable(next_state, x_bounds,
@@ -195,7 +223,8 @@ function compute_V_given_x_t_HD(sampling_size, samples, probas, u_bounds,
195223

196224
next_state = dynamics(t, x, u, w_sample)
197225

198-
if constraints(t, next_state, u, w_sample)
226+
227+
if constraints(t, x, u, w_sample)&&is_next_state_feasible(next_state, x_dim, x_bounds)
199228
admissible_u_w_count += 1
200229
current_cost = cost(t, x, u, w_sample)
201230
ind_next_state = real_index_from_variable(next_state, x_bounds,

test/sdp.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ facts("Indexation for SDP") do
1414
ind = SDPutils.index_from_variable(var, bounds, steps)
1515
ind2 = SDPutils.real_index_from_variable(vart, bounds, steps)
1616

17+
checkFalse = SDPutils.is_next_state_feasible([0,1,2],3,bounds)
18+
checkTrue = SDPutils.is_next_state_feasible([0.12,1.3,1.3],3,bounds)
19+
1720

1821
@fact ind --> (4,51,141)
1922
@fact ind2[1] --> roughly(4.2)
2023
@fact ind2[2] --> roughly(52.6)
2124
@fact ind2[3] --> roughly(144.2)
22-
25+
@fact checkFalse --> false
26+
@fact checkTrue --> true
2327

2428
end
2529

@@ -67,7 +71,7 @@ facts("SDP algorithm") do
6771
end
6872

6973
function constraints(t, x, u, w)
70-
return (x[1]<=VOLUME_MAX)&(x[1]>=VOLUME_MIN)&(x[2]<=VOLUME_MAX)&(x[2]>=VOLUME_MIN)
74+
return true
7175
end
7276

7377
function finalCostFunction(x)

0 commit comments

Comments
 (0)