@@ -140,34 +140,41 @@ Returns :
140140function sdp_optimize (model:: SPModel ,
141141 param:: SDPparameters ,
142142 display= true :: Bool )
143-
144- function true_fun (x ... )
143+
144+ function true_fun (t,x,u,w )
145145 return true
146146 end
147- function zero_fun (x... )
147+ function zero_fun (x)
148148 return 0
149149 end
150-
150+
151151 if isa (model,PiecewiseLinearCostSPmodel)|| isa (model,LinearDynamicLinearCostSPmodel)
152+ function cons_fun (t,x,u,w)
153+ test = true
154+ for i in 1 : model. dimStates
155+ test &= (x[i]>= model. xlim[i][1 ])& (x[i]<= model. xlim[i][2 ])
156+ end
157+ return test
158+ end
152159 if in (:finalCostFunction ,fieldnames (model))
153- SDPmodel = StochDynProgModel (model, model. finalCostFunction, true_fun )
160+ SDPmodel = StochDynProgModel (model, model. finalCostFunction, cons_fun )
154161 else
155- SDPmodel = StochDynProgModel (model, zero_fun, true_fun )
162+ SDPmodel = StochDynProgModel (model, zero_fun, cons_fun )
156163 end
157164 elseif isa (model,StochDynProgModel)
158165 SDPmodel = model
159166 else
160- error (" cannot build StochDynProgModel from current SPmodel. You need to implement
167+ error (" cannot build StochDynProgModel from current SPmodel. You need to implement
161168 a new StochDynProgModel constructor." )
162169 end
163-
170+
164171 # Display start of the algorithm in DH and HD cases
165172 if (param. infoStructure == " DH" )
166173 V = sdp_solve_DH (SDPmodel, param, display)
167174 elseif (param. infoStructure == " HD" )
168175 V = sdp_solve_HD (SDPmodel, param, display)
169176 else
170- error (" param.infoStructure is neither 'DH' nor 'HD'" )
177+ error (" param.infoStructure is neither 'DH' nor 'HD'" )
171178 end
172179
173180 return V
@@ -198,7 +205,7 @@ Returns :
198205function sdp_solve_DH (model:: StochDynProgModel ,
199206 param:: SDPparameters ,
200207 display= true :: Bool )
201-
208+
202209 TF = model. stageNumber
203210 next_state = zeros (Float64, model. dimStates)
204211 law = model. noises
@@ -264,10 +271,10 @@ function sdp_solve_DH(model::StochDynProgModel,
264271 w_sample = samples[:, w]
265272 proba = probas[w]
266273 next_state = model. dynamics (t, x, u, w_sample)
267-
268-
274+
275+
269276 if model. constraints (t, next_state, u, w_sample)
270-
277+
271278 count_admissible_w = count_admissible_w + proba
272279 ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
273280 next_V = Vitp[ind_next_state... ]
@@ -383,7 +390,7 @@ function sdp_solve_HD(model::StochDynProgModel,
383390 # Compute expectation
384391 for w in 1 : sampling_size
385392 admissible_u_w_count = 0
386- best_V_x_w = 0.
393+ best_V_x_w = Inf
387394 next_V_x_w = Inf
388395 w_sample = samples[:, w]
389396 proba = probas[w]
@@ -410,7 +417,6 @@ function sdp_solve_HD(model::StochDynProgModel,
410417 expected_V += proba* best_V_x_w
411418 count_admissible_w += (admissible_u_w_count> 0 )* proba
412419 end
413-
414420 if (count_admissible_w> 0. )
415421 expected_V = expected_V / count_admissible_w
416422 end
@@ -438,8 +444,9 @@ Returns :
438444- V(x0) (Float64)
439445"""
440446function get_value (model:: SPModel ,param:: SDPparameters ,V:: Array{Float64} )
441- ind_x0 = index_from_variable (model. initialState, model. xlim, param. stateSteps)
442- return V[ind_x0... ,1 ]
447+ ind_x0 = real_index_from_variable (model. initialState, model. xlim, param. stateSteps)
448+ Vi = value_function_interpolation (model, V, 1 )
449+ return Vi[ind_x0... ,1 ]
443450end
444451
445452
0 commit comments