File: DynamicProgrammingByRegressionDist.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 (60 lines) | stat: -rw-r--r-- 2,282 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright (C) 2016 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import StOptGrids 
import StOptReg
import StOptGlobal
import StOptGeners
import imp
import numpy as np


def DynamicProgrammingByRegressionDist(p_grid, p_optimize, p_regressor, p_funcFinalValue, p_pointStock, p_initialRegime, p_fileToDump, p_bOneFile) :
    
    try:
        imp.find_module('mpi4py')
        found = True
    except:
        print("Not parallel module found ")
        found = False
        
    if found :
        # from the optimizer get back the simulation
        simulator = p_optimize.getSimulator()
        fin = StOptGlobal.FinalStepDPDist(p_grid, p_optimize.getNbRegime(),p_optimize.getDimensionToSplit())
        # final values
        valuesNext = fin.set(p_funcFinalValue, np.asarray(simulator.getParticles()))
        
        from mpi4py import MPI
        world = MPI.COMM_WORLD
        # dump
        toDump = p_fileToDump
        
        # test if one file generated
        if p_bOneFile is False:
            toDump += "_" + str(world.rank)
            
        ar = StOptGeners.BinaryFileArchive(toDump, "w")
        # name for object in archive
        nameAr = "Continuation"
        
        nsteps =simulator.getNbStep()
        # iterate on time steps
        for iStep in range(nsteps) :
            asset = simulator.stepBackwardAndGetParticles()
            
            # conditional espectation operator
            if iStep == (simulator.getNbStep() - 1):
                p_regressor.updateSimulations(True, asset)
            
            else:
                p_regressor.updateSimulations(False, asset)
            
            # transition object
            transStep = StOptGlobal.TransitionStepRegressionDPDist(p_grid, p_grid, p_optimize)
            valuesAndControl = transStep.oneStep(valuesNext, p_regressor)
            transStep.dumpContinuationValues(ar, nameAr,  nsteps - 1 - iStep, valuesNext, valuesAndControl[1], p_regressor, p_bOneFile)
            valuesNext = valuesAndControl[0]
            
        # reconstruct a small grid for interpolation
        return StOptGlobal.reconstructProc0Mpi(p_pointStock, p_grid, valuesNext[p_initialRegime],p_optimize.getDimensionToSplit()).mean()