Skip to content

Commit adc6681

Browse files
authored
Merge pull request #121 from JuliaOpt/julia-0.5
Fix breaking changes in julia 0.5
2 parents 0cc03ba + cd2b22a commit adc6681

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

examples/damsvalley.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function init_problem()
110110
model = LinearSPModel(N_STAGES, u_bounds,
111111
X0, cost_t,
112112
dynamic, aleas,
113-
final_cost_dams)
113+
Vfinal=final_cost_dams)
114114

115115
# Add bounds for stocks:
116116
set_state_bounds(model, x_bounds)

src/SDPoptimize.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,19 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
527527
V,
528528
display=true::Bool)
529529

530+
if VERSION.minor < 5
531+
scenario = reshape(scenario, size(scenario, 1), size(scenario, 3))
532+
end
530533
TF = model.stageNumber
531534
law = model.noises
532535
u_bounds = model.ulim
533536
x_bounds = model.xlim
534537
x_steps = param.stateSteps
535538

536539
#Compute cartesian product spaces
537-
product_states, product_controls = generate_grid(model, param)
540+
p_states, p_controls = generate_grid(model, param)
541+
product_states = collect(p_states)
542+
product_controls = collect(p_controls)
538543

539544
controls = Inf*ones(TF-1, 1, model.dimControls)
540545
states = Inf*ones(TF, 1, model.dimStates)
@@ -591,7 +596,7 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
591596
current_V = current_V/count_admissible_w
592597
if (current_V < best_V)&(count_admissible_w>0)
593598
best_control = u
594-
best_state = model.dynamics(t, x, u, scenario[t,1,:])
599+
best_state = model.dynamics(t, x, u, scenario[t,:])
595600
best_V = current_V
596601
end
597602
end
@@ -607,7 +612,7 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
607612
index_state = index_state +1
608613
states[t+1,1,index_state] = xj
609614
end
610-
J += model.costFunctions(t, x, best_control, scenario[t,1,:])
615+
J += model.costFunctions(t, x, best_control, scenario[t,:])
611616
end
612617

613618
else
@@ -622,15 +627,15 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
622627

623628
for u = product_controls
624629

625-
next_state = model.dynamics(t, x, u, scenario[t,1,:])
630+
next_state = model.dynamics(t, x, u, scenario[t,:])
626631

627632
if model.constraints(t, x, u, scenario[t])&&SDPutils.is_next_state_feasible(next_state, model.dimStates, model.xlim)
628633
ind_next_state = SDPutils.real_index_from_variable(next_state, x_bounds, x_steps)
629634
next_V = Vitp[ind_next_state...]
630-
current_V = model.costFunctions(t, x, u, scenario[t,1,:]) + next_V
635+
current_V = model.costFunctions(t, x, u, scenario[t,:]) + next_V
631636
if (current_V < best_V)
632637
best_control = u
633-
best_state = model.dynamics(t, x, u, scenario[t,1,:])
638+
best_state = model.dynamics(t, x, u, scenario[t,:])
634639
best_V = current_V
635640
end
636641
end
@@ -647,7 +652,7 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
647652
index_state = index_state +1
648653
states[t+1,1,index_state] = xj
649654
end
650-
J += model.costFunctions(t, x, best_control, scenario[t,1,:])
655+
J += model.costFunctions(t, x, best_control, scenario[t,:])
651656
end
652657
end
653658

src/objects.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type LinearSPModel <: SPModel
3535

3636
initialState::Array{Float64, 1}
3737

38-
costFunctions::Union{Function, Vector{Function}}
38+
#FIXME: add a correct typage for costFunctions that dont break in 0.5
39+
costFunctions
3940
dynamics::Function
4041
noises::Vector{NoiseLaw}
4142

@@ -115,7 +116,8 @@ type StochDynProgModel <: SPModel
115116
function StochDynProgModel(model::LinearSPModel, final, cons)
116117
if isa(model.costFunctions, Function)
117118
cost = model.costFunctions
118-
elseif isa(model.costFunctions, Vector{Function})
119+
#FIXME: broken test since 0.5 release
120+
else
119121
function cost(t,x,u,w)
120122
current_cost = -Inf
121123
for aff_func in model.costFunctions

test/sdp.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,9 @@ facts("SDP algorithm") do
6666
end
6767

6868
# Define cost corresponding to each timestep:
69-
function cost_t(t, x, u, w)
70-
return COST[t] * (u[1])
71-
end
72-
73-
function constraints(t, x, u, w)
74-
return true
75-
end
76-
77-
function finalCostFunction(x)
78-
return 0.
79-
end
69+
cost_t(t, x, u, w) = COST[t] * (u[1])
70+
constraints(t, x, u, w) = true
71+
finalCostFunction(x) = 0.
8072

8173
"""Build admissible scenarios for water inflow over the time horizon."""
8274
function build_scenarios(n_scenarios::Int64, N_STAGES)
@@ -145,9 +137,9 @@ facts("SDP algorithm") do
145137
set_state_bounds(modelSDPPiecewise, x_bounds)
146138

147139
modelSDPLinear = StochDynamicProgramming.LinearSPModel(TF,
148-
u_bounds, x0,
149-
cost_t,
150-
dynamic, aleas)
140+
u_bounds, x0,
141+
cost_t,
142+
dynamic, aleas)
151143

152144
set_state_bounds(modelSDPLinear, x_bounds)
153145

0 commit comments

Comments
 (0)