File: DynamicProgrammingSwitchingByRegressionHighLevel.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 (39 lines) | stat: -rw-r--r-- 1,616 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
38
39
# Copyright (C) 2021 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import numpy as np
import StOptGrids 
import StOptReg
import StOptGlobal
import StOptGeners

def DynamicProgrammingSwitchingByRegressionHighLevel(p_grid, p_optimize, p_regressor, p_pointState, p_initialRegime, p_fileToDump) :
    
    # from the optimizer get back the simulation
    simulator = p_optimize.getSimulator()
    print("simulator", simulator)
    # final values
    valuesNext= []
    for iReg in range(len(p_grid)):
        valuesNext.append( np.zeros([simulator.getNbSimul(),p_grid[iReg].getNbPoints()]))
        
    ar = StOptGeners.BinaryFileArchive(p_fileToDump, "w")
    nameAr = "ContinuationSwitching"
    nsteps =simulator.getNbStep()
    # iterate on time steps
    for iStep in range(nsteps) :
        asset = simulator.stepBackwardAndGetParticles()
        # conditional expectation operator
        if iStep == (simulator.getNbStep() - 1):
            p_regressor.updateSimulations(True, asset)
        else:
            p_regressor.updateSimulations(False, asset)
        
        # transition object
        transStep = StOptGlobal.TransitionStepRegressionSwitch(p_grid, p_grid, p_optimize)
        values = transStep.oneStep(valuesNext, p_regressor)
        transStep.dumpContinuationValues(ar, nameAr, nsteps - 1 -iStep, valuesNext, p_regressor)
        valuesNext = values
        
    # interpolate at the initial stock point and initial regime
    return valuesNext[p_initialRegime][:,p_grid[p_initialRegime].globCoordPerDimToLocal( p_pointState)].mean()