File: DynamicProgrammingByTreeHighLevel.py

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (37 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (C) 2019 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import numpy as np
import StOptGrids 
import StOptTree
import StOptGlobal
import StOptGeners


def DynamicProgrammingByTreeHighLevel(p_grid, p_optimize,  p_funcFinalValue, p_pointStock, p_initialRegime, p_fileToDump) :
    
    # from the optimizer get back the simulation
    simulator = p_optimize.getSimulator()
    # final values
    fin = StOptGlobal.FinalStepDP(p_grid, p_optimize.getNbRegime())
    valuesNext = fin.set(p_funcFinalValue, simulator.getNodes())
    ar = StOptGeners.BinaryFileArchive(p_fileToDump, "w")
    nameAr = "Continuation"
    nsteps =simulator.getNbStep()
    # iterate on time steps
    for iStep in range(nsteps) :
        simulator.stepBackward()
        # get back probability
        proba = simulator.getProba()
        # and connection matrix
        connected = simulator.getConnected()
        # creta tree for conditional expectation
        tree=StOptTree.Tree(proba,connected)      
        # transition object
        transStep = StOptGlobal.TransitionStepTreeDP(p_grid, p_grid, p_optimize)
        valuesAndControl = transStep.oneStep(valuesNext, tree)
        transStep.dumpContinuationValues(ar, nameAr, iStep, valuesNext, valuesAndControl[1], tree)
        valuesNext = valuesAndControl[0]
        
    # interpolate at the initial stock point and initial regime
    return (p_grid.createInterpolator(p_pointStock).applyVec(valuesNext[p_initialRegime])).mean()