Skip to content

Commit fe55414

Browse files
committed
[UPD] Clean sdp_optimize
1 parent 5885b12 commit fe55414

File tree

1 file changed

+37
-60
lines changed

1 file changed

+37
-60
lines changed

src/SDPoptimize.jl

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,21 @@ function generate_grid(model::SPModel, param::SDPparameters)
117117
end
118118

119119
"""
120-
Value iteration algorithm to compute optimal value functions in
121-
the Decision Hazard (DH) as well as the Hazard Decision (HD) case
120+
Transform a general SPmondel into a StochDynProgModel
122121
123122
Parameters:
124123
- model (SPmodel)
125-
the DPSPmodel of our problem
124+
the model of the problem
126125
127126
- param (SDPparameters)
128-
the parameters for the SDP algorithm
129-
130-
- display (Bool)
131-
the output display or verbosity parameter
127+
the parameters of the problem
132128
133129
134130
Returns :
135-
- value_functions (Array)
136-
the vector representing the value functions as functions of the state
137-
of the system at each time step
138-
131+
- sdpmodel : (StochDynProgModel)
132+
the corresponding StochDynProgModel
139133
"""
140-
function sdp_optimize(model::SPModel,
141-
param::SDPparameters,
142-
display=true::Bool)
134+
function SPmodel_to_SDPmodel(model::SPModel, param::SDPparameters)
143135

144136
function zero_fun(x)
145137
return 0
@@ -164,6 +156,35 @@ function sdp_optimize(model::SPModel,
164156
error("cannot build StochDynProgModel from current SPmodel. You need to implement
165157
a new StochDynProgModel constructor.")
166158
end
159+
return(SDPmodel)
160+
end
161+
162+
"""
163+
Value iteration algorithm to compute optimal value functions in
164+
the Decision Hazard (DH) as well as the Hazard Decision (HD) case
165+
166+
Parameters:
167+
- model (SPmodel)
168+
the DPSPmodel of our problem
169+
170+
- param (SDPparameters)
171+
the parameters for the SDP algorithm
172+
173+
- display (Bool)
174+
the output display or verbosity parameter
175+
176+
177+
Returns :
178+
- value_functions (Array)
179+
the vector representing the value functions as functions of the state
180+
of the system at each time step
181+
182+
"""
183+
function sdp_optimize(model::SPModel,
184+
param::SDPparameters,
185+
display=true::Bool)
186+
187+
SDPmodel = SPmodel_to_SDPmodel(model, param)
167188

168189
#Display start of the algorithm in DH and HD cases
169190
if (param.infoStructure == "DH")
@@ -469,29 +490,7 @@ Returns :
469490
"""
470491
function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::Int64, x::Array)
471492

472-
function zero_fun(x)
473-
return 0
474-
end
475-
476-
if isa(model,PiecewiseLinearCostSPmodel)||isa(model,LinearDynamicLinearCostSPmodel)
477-
function cons_fun(t,x,u,w)
478-
test = true
479-
for i in 1:model.dimStates
480-
test &= (x[i]>=model.xlim[i][1])&(x[i]<=model.xlim[i][2])
481-
end
482-
return test
483-
end
484-
if in(:finalCostFunction,fieldnames(model))
485-
SDPmodel = StochDynProgModel(model, model.finalCostFunction, cons_fun)
486-
else
487-
SDPmodel = StochDynProgModel(model, zero_fun, cons_fun)
488-
end
489-
elseif isa(model,StochDynProgModel)
490-
SDPmodel = model
491-
else
492-
error("cannot build StochDynProgModel from current SPmodel. You need to implement
493-
a new StochDynProgModel constructor.")
494-
end
493+
SDPmodel = SPmodel_to_SDPmodel(model, param)
495494

496495
product_controls = product([SDPmodel.ulim[i][1]:param.controlSteps[i]:SDPmodel.ulim[i][2] for i in 1:SDPmodel.dimControls]...)
497496

@@ -571,29 +570,7 @@ Returns :
571570
"""
572571
function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::Int64, x::Array, w::Array)
573572

574-
function zero_fun(x)
575-
return 0
576-
end
577-
578-
if isa(model,PiecewiseLinearCostSPmodel)||isa(model,LinearDynamicLinearCostSPmodel)
579-
function cons_fun(t,x,u,w)
580-
test = true
581-
for i in 1:model.dimStates
582-
test &= (x[i]>=model.xlim[i][1])&(x[i]<=model.xlim[i][2])
583-
end
584-
return test
585-
end
586-
if in(:finalCostFunction,fieldnames(model))
587-
SDPmodel = StochDynProgModel(model, model.finalCostFunction, cons_fun)
588-
else
589-
SDPmodel = StochDynProgModel(model, zero_fun, cons_fun)
590-
end
591-
elseif isa(model,StochDynProgModel)
592-
SDPmodel = model
593-
else
594-
error("cannot build StochDynProgModel from current SPmodel. You need to implement
595-
a new StochDynProgModel constructor.")
596-
end
573+
SDPmodel = SPmodel_to_SDPmodel(model, param)
597574

598575
product_controls = product([SDPmodel.ulim[i][1]:param.controlSteps[i]:SDPmodel.ulim[i][2] for i in 1:SDPmodel.dimControls]...)
599576

0 commit comments

Comments
 (0)