@@ -117,12 +117,23 @@ function generate_grid(model::SPModel, param::SDPparameters)
117117end
118118
119119"""
120- Try to construct a StochDynProgModel from an SPModel
120+ <<<<<<< HEAD
121+ Transform a general SPmodel into a StochDynProgModel
122+
123+ Parameters:
124+ - model (SPmodel)
125+ the model of the problem
126+
127+ - param (SDPparameters)
128+ the parameters of the problem
129+
130+
131+ Returns :
132+ - sdpmodel : (StochDynProgModel)
133+ the corresponding StochDynProgModel
121134"""
122135function build_sdpmodel_from_spmodel (model:: SPModel )
123- function true_fun (t,x,u,w)
124- return true
125- end
136+
126137 function zero_fun (x)
127138 return 0
128139 end
@@ -147,6 +158,7 @@ function build_sdpmodel_from_spmodel(model::SPModel)
147158 error (" cannot build StochDynProgModel from current SPmodel. You need to implement
148159 a new StochDynProgModel constructor." )
149160 end
161+
150162 return SDPmodel
151163end
152164
@@ -276,7 +288,6 @@ function sdp_solve_DH(model::StochDynProgModel,
276288 end
277289
278290 for w = 1 : sampling_size
279-
280291 w_sample = samples[:, w]
281292 proba = probas[w]
282293 next_state = model. dynamics (t, x, u, w_sample)
@@ -495,16 +506,16 @@ function sdp_forward_simulation(model::SPModel,
495506 scenarios:: Array{Float64,3} ,
496507 value:: Array ,
497508 display= true :: Bool )
498-
499- SDPmodel = build_sdpmodel_from_spmodel (model)
500- TF = SDPmodel. stageNumber
501- nb_scenarios = size (scenarios)[2 ]
502-
509+
510+ SDPmodel = build_sdpmodel_from_spmodel (model)
511+ TF = SDPmodel. stageNumber
512+ nb_scenarios = size (scenarios)[2 ]
513+
503514 costs = zeros (nb_scenarios)
504515 states = zeros (TF,nb_scenarios)
505516 controls = zeros (TF- 1 ,nb_scenarios)
506-
507-
517+
518+
508519 for k = 1 : nb_scenarios
509520 # println(k)
510521 costs[k],states[:,k], controls[:,k] = sdp_forward_single_simulation (SDPmodel,
@@ -514,6 +525,148 @@ function sdp_forward_simulation(model::SPModel,
514525 return costs, controls, states
515526end
516527
528+ """
529+ Get the optimal control at time t knowing the state of the system in the decision hazard case
530+
531+ Parameters:
532+ - model (SPmodel)
533+ the DPSPmodel of our problem
534+
535+ - param (SDPparameters)
536+ the parameters for the SDP algorithm
537+
538+ - V (Array{Float64})
539+ the Bellman Functions
540+
541+ - t (int)
542+ the time step
543+
544+ - x (Array)
545+ the state variable
546+
547+ - w (Array)
548+ the alea realization
549+
550+ Returns :
551+ - V(x0) (Float64)
552+ """
553+ function get_control (model:: SPModel ,param:: SDPparameters ,V:: Array{Float64} , t:: Int64 , x:: Array )
554+
555+ SDPmodel = build_sdpmodel_from_spmodel (model)
556+
557+ product_controls = product ([SDPmodel. ulim[i][1 ]: param. controlSteps[i]: SDPmodel. ulim[i][2 ] for i in 1 : SDPmodel. dimControls]. .. )
558+
559+ law = SDPmodel. noises
560+ best_control = tuple ()
561+ Vitp = value_function_interpolation (SDPmodel, V, t+ 1 )
562+
563+ u_bounds = SDPmodel. ulim
564+ x_bounds = SDPmodel. xlim
565+ x_steps = param. stateSteps
566+
567+ best_V = Inf
568+
569+ for u in product_controls
570+
571+ count_admissible_w = 0.
572+ current_V = 0.
573+
574+ if (param. expectation_computation== " MonteCarlo" )
575+ sampling_size = param. monteCarloSize
576+ samples = [sampling (law,t) for i in 1 : sampling_size]
577+ probas = (1 / sampling_size)
578+ else
579+ sampling_size = law[t]. supportSize
580+ samples = law[t]. support[:]
581+ probas = law[t]. proba
582+ end
583+
584+ for w = 1 : sampling_size
585+
586+ w_sample = samples[w]
587+ proba = probas[w]
588+
589+ next_state = SDPmodel. dynamics (t, x, u, w_sample)
590+
591+ if SDPmodel. constraints (t, next_state, u, w_sample)
592+ ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
593+ next_V = Vitp[ind_next_state... ]
594+ current_V += proba * (SDPmodel. costFunctions (t, x, u, w_sample) + next_V)
595+ count_admissible_w = count_admissible_w + proba
596+ end
597+ end
598+ current_V = current_V/ count_admissible_w
599+ if (current_V < best_V)& (count_admissible_w> 0 )
600+ best_control = u
601+ best_V = current_V
602+ end
603+ end
604+
605+ return best_control
606+ end
607+
608+ """
609+ Get the optimal control at time t knowing the state of the system and the alea in the hazard decision case
610+
611+ Parameters:
612+ - model (SPmodel)
613+ the DPSPmodel of our problem
614+
615+ - param (SDPparameters)
616+ the parameters for the SDP algorithm
617+
618+ - V (Array{Float64})
619+ the Bellman Functions
620+
621+ - t (int)
622+ the time step
623+
624+ - x (Array)
625+ the state variable
626+
627+ - w (Array)
628+ the alea realization
629+
630+ Returns :
631+ - V(x0) (Float64)
632+ """
633+ function get_control (model:: SPModel ,param:: SDPparameters ,V:: Array{Float64} , t:: Int64 , x:: Array , w:: Array )
634+
635+ SDPmodel = build_sdpmodel_from_spmodel (model)
636+
637+ product_controls = product ([SDPmodel. ulim[i][1 ]: param. controlSteps[i]: SDPmodel. ulim[i][2 ] for i in 1 : SDPmodel. dimControls]. .. )
638+
639+ law = SDPmodel. noises
640+ best_control = tuple ()
641+ Vitp = value_function_interpolation (SDPmodel, V, t+ 1 )
642+
643+ u_bounds = SDPmodel. ulim
644+ x_bounds = SDPmodel. xlim
645+ x_steps = param. stateSteps
646+
647+ best_V = Inf
648+
649+ for u = product_controls
650+
651+ next_state = SDPmodel. dynamics (t, x, u, w)
652+
653+ if SDPmodel. constraints (t, next_state, u, w)
654+ ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
655+ next_V = Vitp[ind_next_state... ]
656+ current_V = SDPmodel. costFunctions (t, x, u, w) + next_V
657+ if (current_V < best_V)
658+ best_control = u
659+ best_state = SDPmodel. dynamics (t, x, u, w)
660+ best_V = current_V
661+ end
662+ end
663+
664+ end
665+
666+ return best_control
667+
668+ end
669+
517670"""
518671Simulation of optimal control given an initial state and an alea scenario
519672
@@ -610,10 +763,10 @@ function sdp_forward_single_simulation(model::StochDynProgModel,
610763
611764 next_state = model. dynamics (t, x, u, w_sample)
612765
613- if model. constraints (t, next_state, u, scenario[t] )
766+ if model. constraints (t, next_state, u, w_sample )
614767 ind_next_state = real_index_from_variable (next_state, x_bounds, x_steps)
615768 next_V = Vitp[ind_next_state... ]
616- current_V += proba * (model. costFunctions (t, x, u, scenario[t] ) + next_V)
769+ current_V += proba * (model. costFunctions (t, x, u, w_sample ) + next_V)
617770 count_admissible_w = count_admissible_w + proba
618771 end
619772 end
0 commit comments