File: BinaryFileArchiveStOpt.h

package info (click to toggle)
stopt 5.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,772 kB
  • sloc: cpp: 70,373; python: 5,942; makefile: 67; sh: 57
file content (243 lines) | stat: -rw-r--r-- 10,956 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef _BINARYFILEARCHIVESTOPT_H
#define _BINARYFILEARCHIVESTOPT_H
#include "geners/BinaryFileArchive.hh"
#include "geners/Record.hh"
#include "geners/Reference.hh"
#include "StOpt/regression/BaseRegression.h"
#include "StOpt/regression/BaseRegressionGeners.h"
#include "StOpt/regression/LocalLinearRegressionGeners.h"
#include "StOpt/regression/LocalConstRegressionGeners.h"
#include "StOpt/regression/LocalSameSizeLinearRegressionGeners.h"
#include "StOpt/regression/LocalSameSizeConstRegressionGeners.h"
#include "StOpt/regression/SparseRegressionGeners.h"
#include "StOpt/regression/GridAndRegressedValue.h"
#include "StOpt/regression/GridAndRegressedValueGeners.h"
#include "StOpt/regression/ContinuationCuts.h"
#include "StOpt/regression/ContinuationCutsGeners.h"
#include "StOpt/tree/ContinuationValueTree.h"
#include "StOpt/tree/ContinuationValueTreeGeners.h"
#include "StOpt/tree/GridTreeValue.h"
#include "StOpt/tree/GridTreeValueGeners.h"
#include "StOpt/core/utils/eigenGeners.h"
#include "StOpt/core/grids/SpaceGrid.h"
#include "StOpt/core/grids/FullGrid.h"
#ifdef SDDPPYTHON
#include "StOpt/sddp/LocalLinearRegressionForSDDP.h"
#include "StOpt/sddp/LocalLinearRegressionForSDDPGeners.h"
#include "StOpt/sddp/SDDPVisitedStatesGeners.h"
#endif
#include  "StOpt/python/Pybind11VectorAndList.h"


/// \file BinaryFileArchiveStOpt.h
/// \brief defines a wrapping for geners binary file
///        permits to extend the binary archive for StOpt

// create a class to encapsulate a Binary Archive
class BinaryFileArchiveStOpt: public gs::BinaryFileArchive
{


public:

    BinaryFileArchiveStOpt(const char *p_fileName, const char *p_type): gs::BinaryFileArchive(p_fileName, p_type)
    {
    }


    // to get distinct Names in archive used for Continuation values
    std::vector< std::string> getNamesForGridAndRegressedValue(const std::string &p_nameRegex)
    {
        std::set<std::string> set;
        auto reference = gs::Reference<std::vector<StOpt::GridAndRegressedValue>>(*this, std::regex(p_nameRegex), std::regex(".*"));
        for (int i = 0; i < reference.size(); ++i)
        {
            set.emplace(reference.indexedCatalogEntry(i)->name());
        }
        std::vector<std::string> ret(set.begin(), set.end());
        return ret;
    }

    // to get distinct Names in archive used for Continuation values (cuts)
    std::vector< std::string> getNamesForContinuationCut(const std::string &p_nameRegex)
    {
        std::set<std::string> set;
        auto reference = gs::Reference<std::vector<StOpt::ContinuationCuts>>(*this, std::regex(p_nameRegex), std::regex(".*"));
        for (int i = 0; i < reference.size(); ++i)
        {
            set.emplace(reference.indexedCatalogEntry(i)->name());
        }
        std::vector<std::string> ret(set.begin(), set.end());
        return ret;
    }

    void dumpGridAndRegressedValue(const std::string &p_name, const int &p_iStep, const  pybind11::list  &p_phiIn,
                                   const   std::shared_ptr<StOpt::BaseRegression>    &p_condExp,
                                   const std::shared_ptr< StOpt::SpaceGrid >   &p_grid)
    {
        // convert python to c++ object
        std::vector< std::shared_ptr< Eigen::ArrayXXd > >  phiIn(convertFromListShPtr<Eigen::ArrayXXd>(p_phiIn));

        // values to regress
        std::vector< StOpt::GridAndRegressedValue  > contVal(phiIn.size());
        for (size_t iReg = 0; iReg < phiIn.size(); ++iReg)
            contVal[iReg] =  StOpt::GridAndRegressedValue(p_grid, p_condExp, *phiIn[iReg]);
        std::string stepString = boost::lexical_cast<std::string>(p_iStep) ;
        *this << gs::Record(contVal, p_name.c_str(), stepString.c_str()) ;
        this->flush() ; // necessary for python mapping
    }

    // read continuation values
    std::vector< std::shared_ptr<StOpt::ContinuationCuts > >  readGridAndRegressedCuts(const int &p_iStep, const std::string &p_nameCont)
    {
        std::vector< StOpt::ContinuationCuts >  continuationObj;
        gs::Reference< std::vector<StOpt::ContinuationCuts  > >(*this, p_nameCont.c_str(), boost::lexical_cast<std::string>(p_iStep).c_str()).restore(0, &continuationObj);
        std::vector< std::shared_ptr<StOpt::ContinuationCuts >>  continuationObjWrap(continuationObj.size());
        for (size_t i = 0; i < continuationObjWrap.size(); ++i)
            continuationObjWrap[i] = std::make_shared<StOpt::ContinuationCuts>(continuationObj[i]);
        return continuationObjWrap;
    }

    // read continuation values
    std::vector< std::shared_ptr<StOpt::GridAndRegressedValue > >  readGridAndRegressedValue(const int &p_iStep,  const std::string &p_nameCont)
    {
        std::vector< StOpt::GridAndRegressedValue >  continuationObj;
        gs::Reference< std::vector<StOpt::GridAndRegressedValue  > >(*this, p_nameCont.c_str(), boost::lexical_cast<std::string>(p_iStep).c_str()).restore(0, &continuationObj);
        std::vector< std::shared_ptr<StOpt::GridAndRegressedValue >>  continuationObjWrap(continuationObj.size());
        for (size_t i = 0; i < continuationObjWrap.size(); ++i)
            continuationObjWrap[i] = std::make_shared<StOpt::GridAndRegressedValue>(continuationObj[i]);
        return continuationObjWrap;
    }
    // to get distinct Names in archive used for Continuation values for Tree
    std::vector< std::string> getNamesForGridTreeValue(const std::string &p_nameRegex)
    {
        std::set<std::string> set;
        auto reference = gs::Reference<std::vector<StOpt::GridTreeValue>>(*this, std::regex(p_nameRegex), std::regex(".*"));
        for (int i = 0; i < reference.size(); ++i)
        {
            set.emplace(reference.indexedCatalogEntry(i)->name());
        }
        std::vector<std::string> ret(set.begin(), set.end());
        return ret;
    }

    // to get distinct Names in archive used for Continuation values (cuts) for tree
    std::vector< std::string> getNamesForContinuationCutTree(const std::string &p_nameRegex)
    {
        std::set<std::string> set;
        auto reference = gs::Reference<std::vector<StOpt::GridTreeValue>>(*this, std::regex(p_nameRegex), std::regex(".*"));
        for (int i = 0; i < reference.size(); ++i)
        {
            set.emplace(reference.indexedCatalogEntry(i)->name());
        }
        std::vector<std::string> ret(set.begin(), set.end());
        return ret;
    }

    void dumpGridTreeValue(const std::string &p_name, const int &p_iStep, const  pybind11::list  &p_phiIn,
                           const   std::shared_ptr<StOpt::Tree>    &p_condExp,
                           const std::shared_ptr< StOpt::SpaceGrid >   &p_grid)
    {
        // convert python to c++ object
        std::vector< std::shared_ptr< Eigen::ArrayXXd > >  phiIn(convertFromListShPtr<Eigen::ArrayXXd>(p_phiIn));

        // values to regress
        std::vector< StOpt::GridTreeValue  > contVal(phiIn.size());
        for (size_t iReg = 0; iReg < phiIn.size(); ++iReg)
        {
            Eigen::ArrayXXd phiExp =  p_condExp->expCondMultiple(phiIn[iReg]->transpose()).transpose();
            contVal[iReg] =  StOpt::GridTreeValue(p_grid, phiExp);
        }
        std::string stepString = boost::lexical_cast<std::string>(p_iStep) ;
        *this << gs::Record(contVal, p_name.c_str(), stepString.c_str()) ;
        this->flush() ; // necessary for python mapping
    }

    // read continuation values
    std::vector< std::shared_ptr<StOpt::GridTreeValue > >  readGridTreeValue(const int &p_iStep,  const std::string &p_nameCont)
    {
        std::vector< StOpt::GridTreeValue >  continuationObj;
        gs::Reference< std::vector<StOpt::GridTreeValue  > >(*this, p_nameCont.c_str(), boost::lexical_cast<std::string>(p_iStep).c_str()).restore(0, &continuationObj);
        std::vector< std::shared_ptr<StOpt::GridTreeValue >>  continuationObjWrap(continuationObj.size());
        for (size_t i = 0; i < continuationObjWrap.size(); ++i)
            continuationObjWrap[i] = std::make_shared<StOpt::GridTreeValue>(continuationObj[i]);
        return continuationObjWrap;
    }



    // dump a 2D  numpy array
    void dumpSome2DArray(const std::string &p_name, const int &p_iStep,  const Eigen::ArrayXXd &p_functionValues)
    {
        std::string stepString = boost::lexical_cast<std::string>(p_iStep) ;
        *this << gs::Record(p_functionValues, p_name.c_str(), stepString.c_str()) ;
        this->flush() ; // necessary for python mapping
    }

    // dump a regressor and a numpy array storing a list of coefficients basis
    // p_name
    void dumpSomeRegressor(const std::string &p_name, const int &p_iStep, const std::shared_ptr<StOpt::BaseRegression>    &p_condExp)
    {
        std::string stepString = boost::lexical_cast<std::string>(p_iStep) ;
        *this << gs::Record(*p_condExp, p_name.c_str(), stepString.c_str()) ;
        this->flush() ; // necessary for python mapping
    }

    // dump a vector of strings
    // p_name
    // p_category
    void dumpSomeStringVector(const std::string &p_name, const std::string &p_category, const std::vector<std::string> &p_stringVector)
    {
        *this << gs::Record(p_stringVector, p_name.c_str(), p_category.c_str());
        this->flush(); // necessary for python mapping
    }

    // read  some basis coefficients
    Eigen::ArrayXXd readSome2DArray(const std::string &p_name, const int &p_iStep)
    {
        Eigen::ArrayXXd basisFunctions;
        gs::Reference<  Eigen::ArrayXXd >(*this, p_name.c_str(), boost::lexical_cast<std::string>(p_iStep).c_str()).restore(0, &basisFunctions);
        return basisFunctions;
    }

    // read a regressor
    std::shared_ptr<StOpt::BaseRegression > readSomeRegressor(const std::string &p_name, const int &p_iStep)
    {
        std::shared_ptr<StOpt::BaseRegression > condExp;
        gs::Reference< StOpt::BaseRegression  > reg(*this, p_name.c_str(), boost::lexical_cast<std::string>(p_iStep).c_str());
        condExp = std::move(reg.get(0));
        return condExp;
    }

    // read a vector of strings
    // p_name
    // p_category
    const std::vector<std::string>  readSomeStringVector(const std::string &p_name, const std::string &p_category)
    {
        std::vector<std::string> ret;
        auto item = gs::Reference<std::vector<std::string>>(*this, p_name.c_str(), p_category.c_str());
        item.restore(0, &ret);
        return ret;
    }


#ifdef SDDPPYTHON
    void dumpLocalLinearRegressionForSDDP(const StOpt::LocalLinearRegressionForSDDP &p_sddpReg)
    {
        *this << gs::Record(p_sddpReg, "Regressor", "Top");
        this->flush(); // necessary in python
    }

    void dumpLocalVisitedStateForSDDP(const StOpt::SDDPVisitedStates &p_state)
    {
        *this << gs::Record(p_state, "States", "Top");
        this->flush(); // necessary in python
    }
#endif

};

#endif