@@ -117,12 +117,22 @@ function generate_grid(model::SPModel, param::SDPparameters)
117117end
118118
119119"""
120- Try to construct a StochDynProgModel from an SPModel
120+ Transform a general SPmodel into a StochDynProgModel
121+
122+ Parameters:
123+ - model (SPmodel)
124+ the model of the problem
125+
126+ - param (SDPparameters)
127+ the parameters of the problem
128+
129+
130+ Returns :
131+ - sdpmodel : (StochDynProgModel)
132+ the corresponding StochDynProgModel
121133"""
122134function build_sdpmodel_from_spmodel (model:: SPModel )
123- function true_fun (t,x,u,w)
124- return true
125- end
135+
126136 function zero_fun (x)
127137 return 0
128138 end
@@ -147,6 +157,7 @@ function build_sdpmodel_from_spmodel(model::SPModel)
147157 error (" cannot build StochDynProgModel from current SPmodel. You need to implement
148158 a new StochDynProgModel constructor." )
149159 end
160+
150161 return SDPmodel
151162end
152163
@@ -276,7 +287,6 @@ function sdp_solve_DH(model::StochDynProgModel,
276287 end
277288
278289 for w = 1 : sampling_size
279-
280290 w_sample = samples[:, w]
281291 proba = probas[w]
282292 next_state = model. dynamics (t, x, u, w_sample)
@@ -495,16 +505,16 @@ function sdp_forward_simulation(model::SPModel,
495505 scenarios:: Array{Float64,3} ,
496506 value:: Array ,
497507 display= true :: Bool )
498-
499- SDPmodel = build_sdpmodel_from_spmodel (model)
500- TF = SDPmodel. stageNumber
501- nb_scenarios = size (scenarios)[2 ]
502-
508+
509+ SDPmodel = build_sdpmodel_from_spmodel (model)
510+ TF = SDPmodel. stageNumber
511+ nb_scenarios = size (scenarios)[2 ]
512+
503513 costs = zeros (nb_scenarios)
504514 states = zeros (TF,nb_scenarios)
505515 controls = zeros (TF- 1 ,nb_scenarios)
506-
507-
516+
517+
508518 for k = 1 : nb_scenarios
509519 # println(k)
510520 costs[k],states[:,k], controls[:,k] = sdp_forward_single_simulation (SDPmodel,
@@ -514,6 +524,148 @@ function sdp_forward_simulation(model::SPModel,
514524 return costs, controls, states
515525end
516526
527+ """
528+ Get the optimal control at time t knowing the state of the system in the decision hazard case
529+
530+ Parameters:
531+ - model (SPmodel)
532+ the DPSPmodel of our problem
533+
534+ - param (SDPparameters)
535+ the parameters for the SDP algorithm
536+
537+ - V (Array{Float64})
538+ the Bellman Functions
539+
540+ - t (int)
541+ the time step
542+
543+ - x (Array)
544+ the state variable
545+
546+ - w (Array)
547+ the alea realization
548+
549+ Returns :
550+ - V(x0) (Float64)
551+ """
552+ function get_control (model:: SPModel ,param:: SDPparameters ,V:: Array{Float64} , t:: Int64 , x:: Array )
553+
554+ SDPmodel = build_sdpmodel_from_spmodel (model)
555+
556+ product_controls = product ([SDPmodel. ulim[i][1 ]: param. controlSteps[i]: SDPmodel. ulim[i][2 ] for i in 1 : SDPmodel. dimControls]. .. )
557+
558+ law = SDPmodel. noises
559+ best_control = tuple ()
560+ Vitp = value_function_interpolation (SDPmodel, V, t+ 1 )
561+
562+ u_bounds = SDPmodel. ulim
563+ x_bounds = SDPmodel. xlim
564+ x_steps = param. stateSteps
565+
566+ best_V = Inf
567+
568+ for u in product_controls
569+
570+ count_admissible_w = 0.
571+ current_V = 0.
572+
573+ if (param. expectation_computation== " MonteCarlo" )
574+ sampling_size = param. monteCarloSize
575+ samples = [sampling (law,t) for i in 1 : sampling_size]
576+ probas = (1 / sampling_size)
577+ else
578+ sampling_size = law[t]. supportSize
579+ samples = law[t]. support
580+ probas = law[t]. proba
581+ end
582+
583+ for w = 1 : sampling_size
584+
585+ w_sample = samples[:, w]
586+ proba = probas[w]
587+
588+ next_state = SDPmodel. dynamics (t, x, u, w_sample)
589+
590+ if SDPmodel. constraints (t, next_state, u, w_sample)
591+ ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
592+ next_V = Vitp[ind_next_state... ]
593+ current_V += proba * (SDPmodel. costFunctions (t, x, u, w_sample) + next_V)
594+ count_admissible_w = count_admissible_w + proba
595+ end
596+ end
597+ current_V = current_V/ count_admissible_w
598+ if (current_V < best_V)& (count_admissible_w> 0 )
599+ best_control = u
600+ best_V = current_V
601+ end
602+ end
603+
604+ return best_control
605+ end
606+
607+ """
608+ Get the optimal control at time t knowing the state of the system and the alea in the hazard decision case
609+
610+ Parameters:
611+ - model (SPmodel)
612+ the DPSPmodel of our problem
613+
614+ - param (SDPparameters)
615+ the parameters for the SDP algorithm
616+
617+ - V (Array{Float64})
618+ the Bellman Functions
619+
620+ - t (int)
621+ the time step
622+
623+ - x (Array)
624+ the state variable
625+
626+ - w (Array)
627+ the alea realization
628+
629+ Returns :
630+ - V(x0) (Float64)
631+ """
632+ function get_control (model:: SPModel ,param:: SDPparameters ,V:: Array{Float64} , t:: Int64 , x:: Array , w:: Array )
633+
634+ SDPmodel = build_sdpmodel_from_spmodel (model)
635+
636+ product_controls = product ([SDPmodel. ulim[i][1 ]: param. controlSteps[i]: SDPmodel. ulim[i][2 ] for i in 1 : SDPmodel. dimControls]. .. )
637+
638+ law = SDPmodel. noises
639+ best_control = tuple ()
640+ Vitp = value_function_interpolation (SDPmodel, V, t+ 1 )
641+
642+ u_bounds = SDPmodel. ulim
643+ x_bounds = SDPmodel. xlim
644+ x_steps = param. stateSteps
645+
646+ best_V = Inf
647+
648+ for u = product_controls
649+
650+ next_state = SDPmodel. dynamics (t, x, u, w)
651+
652+ if SDPmodel. constraints (t, next_state, u, w)
653+ ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
654+ next_V = Vitp[ind_next_state... ]
655+ current_V = SDPmodel. costFunctions (t, x, u, w) + next_V
656+ if (current_V < best_V)
657+ best_control = u
658+ best_state = SDPmodel. dynamics (t, x, u, w)
659+ best_V = current_V
660+ end
661+ end
662+
663+ end
664+
665+ return best_control
666+
667+ end
668+
517669"""
518670Simulation of optimal control given an initial state and an alea scenario
519671
@@ -599,21 +751,21 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
599751 probas = (1 / sampling_size)
600752 else
601753 sampling_size = law[t]. supportSize
602- samples = law[t]. support[:]
754+ samples = law[t]. support
603755 probas = law[t]. proba
604756 end
605757
606758 for w = 1 : sampling_size
607759
608- w_sample = samples[w]
760+ w_sample = samples[:, w]
609761 proba = probas[w]
610762
611763 next_state = model. dynamics (t, x, u, w_sample)
612764
613- if model. constraints (t, next_state, u, scenario[t] )
765+ if model. constraints (t, next_state, u, w_sample )
614766 ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
615767 next_V = Vitp[ind_next_state... ]
616- current_V += proba * (model. costFunctions (t, x, u, scenario[t] ) + next_V)
768+ current_V += proba * (model. costFunctions (t, x, u, w_sample ) + next_V)
617769 count_admissible_w = count_admissible_w + proba
618770 end
619771 end
0 commit comments