// Copyright (C) 2025 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef SIMULATESTEPREGRESSIONCUTGRIDADAPT_H
#define SIMULATESTEPREGRESSIONCUTGRIDADAPT_H
#ifdef OMP
#include <omp.h>
#endif
#ifdef USE_MPI
#include <boost/mpi.hpp>
#endif
#include <boost/lexical_cast.hpp>
#include "geners/BinaryFileArchive.hh"
#include "geners/Reference.hh"
#include "StOpt/dp/SimulateStepBase.h"
#include "StOpt/core/utils/StateWithStocks.h"
#include "StOpt/core/grids/GridAdaptBase.h"
#include "StOpt/dp/OptimizerDPCutGridAdaptBase.h"
#include "StOpt/regression/ContinuationCutsGridAdaptNonConcaveGeners.h"

/** \file SimulateStepRegressionCutGridAdapt.h
 *  \brief  In simulation part, permits to  use  continuation cuts values stored in in optimization step and
 *           calculate optimal control. Locally, controls are calculated by a LP so that conditional cuts are used as terminal condition.
 *           This version is for grid adaption. Resolution is for potential non convave VB.
 *  \author Xavier Warin
 */
namespace StOpt
{

/// \class SimulateStepRegressionCutGridAdapt SimulateStepRegressionCutGridAdapt.h
/// One step in forward simulation
class SimulateStepRegressionCutGridAdapt : public SimulateStepBase
{
private :

    std::shared_ptr<StOpt::OptimizerDPCutGridAdaptBase >          m_pOptimize ; ///< optimizer solving the problem for one point and one step
    ContinuationCutsGridAdaptNonConcave  m_continuationObj ; ///< to store continuation cut values per regime  on the grid at following step
#ifdef USE_MPI
    boost::mpi::communicator  m_world; ///< Mpi communicator
#endif

public :

    /// \brief Constructor
    /// \param p_ar               Archive where continuation values are stored
    /// \param p_iStep            Step number indentifier
    /// \param p_nameCont         Name use to store conuation valuation
    /// \param p_pOptimize        Optimize object defining the transition step
    /// \param p_world            MPI communicator
    SimulateStepRegressionCutGridAdapt(gs::BinaryFileArchive &p_ar,  const int &p_iStep,  const std::string &p_nameCont,
					 const  std::shared_ptr<StOpt::OptimizerDPCutGridAdaptBase > &p_pOptimize
#ifdef USE_MPI
					 , const boost::mpi::communicator &p_world
#endif
                             );

    /// \brief Define one step arbitraging between possible commands
    /// \param p_statevector    Vector of states (regime, stock descritor, uncertainty)
    /// \param p_phiInOut       actual contract values modified at current time step by applying an optimal command (number of function by number of simulations)
    void oneStep(std::vector<StateWithStocks > &p_statevector, Eigen::ArrayXXd  &p_phiInOut) const ;

};
}
#endif /* SIMULATESTEPREGRESSIONCUTGRIDADAPT_H */
