QMCPACK
QMCFixedSampleLinearOptimizeBatched Class Reference
+ Inheritance diagram for QMCFixedSampleLinearOptimizeBatched:
+ Collaboration diagram for QMCFixedSampleLinearOptimizeBatched:

Classes

struct  LMYOptions
 

Public Member Functions

 QMCFixedSampleLinearOptimizeBatched (const ProjectData &project_data, QMCDriverInput &&qmcdriver_input, const std::optional< EstimatorManagerInput > &global_emi, VMCDriverInput &&vmcdriver_input, WalkerConfigurations &wc, MCPopulation &&population, SampleStack &samples, Communicate *comm)
 Constructor. More...
 
 ~QMCFixedSampleLinearOptimizeBatched () override
 Destructor. More...
 
QMCRunType getRunType () override
 
void setWaveFunctionNode (xmlNodePtr cur)
 
bool run () override
 Run the Optimization algorithm. More...
 
void process (xmlNodePtr cur) override
 preprocess xml node More...
 
bool processOptXML (xmlNodePtr cur, const std::string &vmcMove, bool reportH5, bool useGPU)
 process xml node value (parameters for both VMC and OPT) for the actual optimization More...
 
RealType costFunc (RealType dl)
 
void start ()
 common operation to start optimization More...
 
void finish ()
 common operation to finish optimization, used by the derived classes More...
 
void generateSamples ()
 
- Public Member Functions inherited from QMCDriverNew
 QMCDriverNew (const ProjectData &project_data, QMCDriverInput &&input, const std::optional< EstimatorManagerInput > &global_emi, WalkerConfigurations &wc, MCPopulation &&population, const std::string timer_prefix, Communicate *comm, const std::string &QMC_driver_type)
 Constructor. More...
 
 QMCDriverNew (QMCDriverNew &&)=default
 Move Constructor. More...
 
 QMCDriverNew (const QMCDriverNew &)=delete
 Copy Constructor (disabled). More...
 
QMCDriverNewoperator= (const QMCDriverNew &)=delete
 Copy operator (disabled). More...
 
 ~QMCDriverNew () override
 
bool putQMCInfo (xmlNodePtr cur)
 
void makeLocalWalkers (int nwalkers, RealType reserve)
 Adjust populations local walkers to this number. More...
 
DriftModifierBaseget_drift_modifier () const
 
void recordBlock (int block) override
 record the state of the block More...
 
bool finalize (int block, bool dumpwalkers=true)
 finalize a qmc section More...
 
IndexType current () const
 return current step More...
 
void setStatus (const std::string &aname, const std::string &h5name, bool append) override
 Set the status of the QMCDriver. More...
 
void add_H_and_Psi (QMCHamiltonian *h, TrialWaveFunction *psi) override
 
void createRngsStepContexts (int num_crowds)
 Creates Random Number generators for crowds and step contexts. More...
 
void putWalkers (std::vector< xmlNodePtr > &wset) override
 Read walker configurations from *.config.h5 files. More...
 
RefVector< RandomBase< FullPrecRealType > > getRngRefs () const
 
RandomBase< FullPrecRealType > & getRng (int i) override
 return the i-th random generator More...
 
std::string getEngineName () override
 intended for logging output and debugging you should base behavior on type preferably at compile time or if necessary at runtime using and protected by dynamic cast. More...
 
unsigned long getDriverMode () override
 
IndexType get_num_living_walkers () const
 
IndexType get_num_dead_walkers () const
 
const QMCDriverInputgetQMCDriverInput () const
 
bool put (xmlNodePtr cur) override
 
More...
 
void setUpdateMode (bool pbyp) override
 should be set in input don't see a reason to set individually More...
 
void putTraces (xmlNodePtr txml) override
 
void requestTraces (bool allow_traces) override
 
void putWalkerLogs (xmlNodePtr wlxml) override
 
void requestWalkerLogs (bool allow_walker_logs_) override
 
- Public Member Functions inherited from QMCDriverInterface
virtual ~QMCDriverInterface ()
 
virtual void setBranchEngine (std::unique_ptr< BranchEngineType > &&be)
 
virtual std::unique_ptr< BranchEngineTypegetBranchEngine ()
 
- Public Member Functions inherited from MPIObjectBase
 MPIObjectBase (Communicate *c)
 constructor with communicator More...
 
int rank () const
 return the rank of the communicator More...
 
int getGroupID () const
 return the group id of the communicator More...
 
CommunicategetCommunicator () const
 return myComm More...
 
CommunicategetCommRef () const
 return a TEMPORARY reference to Communicate More...
 
mpi_comm_type getMPI () const
 return MPI communicator if one wants to use MPI directly More...
 
bool is_manager () const
 return true if the rank == 0 More...
 
const std::string & getName () const
 return the name More...
 
void setName (const std::string &aname)
 

Private Member Functions

bool ValidCostFunction (bool valid)
 
bool is_best_cost (const int ii, const std::vector< RealType > &cv, const std::vector< double > &sh, const RealType ic) const
 Returns whether the proposed new cost is the best compared to the others. More...
 
bool adaptive_three_shift_run ()
 
bool one_shift_run ()
 Performs one iteration of the linear method using an adaptive scheme that tries three different shift magnitudes and picks the best one. More...
 
bool descent_run ()
 
bool previous_linear_methods_run ()
 
bool test_run ()
 
void solveShiftsWithoutLMYEngine (const std::vector< double > &shifts_i, const std::vector< double > &shiffts_s, std::vector< std::vector< RealType >> &parameterDirections)
 For each set of shifts, solves the linear method eigenproblem by building and diagonalizing the matrices. More...
 
std::vector< double > prepare_shifts (const double central_shift) const
 returns a vector of three shift values centered around the provided shift. More...
 
void print_cost_summary_header ()
 prints a header for the summary of each shift's result More...
 
void print_cost_summary (const double si, const double ss, const RealType mc, const RealType cv, const int ind, const int bi, const bool gu)
 prints a summary of the computed cost for the given shift More...
 
- Private Member Functions inherited from LinearMethod
Real getLowestEigenvector (Matrix< Real > &A, std::vector< Real > &ev) const
 
Real getNonLinearRescale (std::vector< Real > &dP, Matrix< Real > &S, const QMCCostFunctionBase &optTarget) const
 

Private Attributes

NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatchedobjFuncWrapper_
 
std::unique_ptr< GradientTesttestEngineObj
 
std::unique_ptr< DescentEnginedescentEngineObj
 
std::unique_ptr< HybridEnginehybridEngineObj
 
std::vector< RealTypeoptdir
 
std::vector< RealTypeoptparam
 
int Max_iterations
 Number of iterations maximum before generating new configurations. More...
 
RealType param_tol
 
std::string eigensolver_
 Choice of eigenvalue solver. More...
 
int nstabilizers
 Number of iterations maximum before generating new configurations. More...
 
RealType stabilizerScale
 
RealType bigChange
 
RealType exp0
 
RealType bestShift_i
 the previous best identity shift More...
 
RealType bestShift_s
 the previous best overlap shift More...
 
RealType shift_i_input
 current shift_i, shift_s input values More...
 
RealType shift_s_input
 
std::bitset< 2 > accept_history
 accept history, remember the last 2 iterations, value 00, 01, 10, 11 More...
 
RealType shift_s_base
 Shift_s adjustment base. More...
 
std::string MinMethod
 name of the current optimization method, updated by processOptXML before run More...
 
LMYOptions options_LMY_
 LMY engine related options. More...
 
bool doGradientTest
 
bool do_output_matrices_csv_
 
bool do_output_matrices_hdf_
 
bool output_matrices_initialized_
 
OutputMatrix output_hamiltonian_
 
OutputMatrix output_overlap_
 
bool freeze_parameters_
 
NewTimerinitialize_timer_
 
NewTimergenerate_samples_timer_
 
NewTimerbuild_olv_ham_timer_
 
NewTimerinvert_olvmat_timer_
 
NewTimereigenvalue_timer_
 
NewTimerline_min_timer_
 
NewTimercost_function_timer_
 
xmlNodePtr wfNode
 xml node to be dumped More...
 
ParameterSet m_param
 
std::unique_ptr< QMCCostFunctionBaseoptTarget
 target cost function to optimize More...
 
std::unique_ptr< VMCBatchedvmcEngine
 vmc engine More...
 
VMCDriverInput vmcdriver_input_
 
SampleStacksamples_
 
const std::optional< EstimatorManagerInputglobal_emi_
 This is retained in order to construct and reconstruct the vmcEngine. More...
 

Additional Inherited Members

- Public Types inherited from QMCDriverNew
enum  { QMC_UPDATE_MODE, QMC_MULTIPLE, QMC_OPTIMIZE, QMC_WARMUP }
 separate but similar to QMCModeEnum More...
 
using RealType = QMCTraits::RealType
 
using IndexType = QMCTraits::IndexType
 
using FullPrecRealType = QMCTraits::FullPrecRealType
 
using MCPWalker = MCPopulation::MCPWalker
 
using WFBuffer = MCPopulation::WFBuffer
 
using SetNonLocalMoveHandler = std::function< void(QMCHamiltonian &)>
 
- Public Types inherited from QMCDriverInterface
using BranchEngineType = SimpleFixedNodeBranch
 
using FullPrecRealType = QMCTraits::FullPrecRealType
 
- Public Types inherited from MPIObjectBase
using mpi_comm_type = Communicate::mpi_comm_type
 
- Static Public Member Functions inherited from QMCDriverNew
static void initialLogEvaluation (int crowd_id, UPtrVector< Crowd > &crowds, UPtrVector< ContextForSteps > &step_context)
 
template<typename RT , CoordsType CT>
static void scaleBySqrtTau (const TauParams< RT, CT > &taus, MCCoords< CT > &coords)
 
template<typename RT , CoordsType CT>
static void computeLogGreensFunction (const MCCoords< CT > &coords, const TauParams< RT, CT > &taus, std::vector< QMCTraits::RealType > &log_gb)
 calculates Green Function from displacements stored in MCCoords [param, out] log_g More...
 
- Public Attributes inherited from QMCDriverNew
std::bitset< QMC_MODE_MAXqmc_driver_mode_
 bits to classify QMCDriver More...
 
bool allow_walker_logs
 whether to allow walker logs More...
 
WalkerLogInput walker_logs_input
 walker logs input More...
 
- Protected Member Functions inherited from QMCDriverNew
void initializeQMC (const AdjustedWalkerCounts &awc)
 Do common section starting tasks for VMC and DMC. More...
 
void measureImbalance (const std::string &tag) const
 inject additional barrier and measure load imbalance. More...
 
void endBlock ()
 end of a block operations. Aggregates statistics across all MPI ranks and write to disk. More...
 
const std::string & get_root_name () const override
 
- Static Protected Member Functions inherited from QMCDriverNew
static QMCDriverNew::AdjustedWalkerCounts adjustGlobalWalkerCount (Communicate &comm, const IndexType current_configs, const IndexType requested_total_walkers, const IndexType requested_walkers_per_rank, const RealType reserve_walkers, int num_crowds)
 }@ More...
 
static size_t determineStepsPerBlock (IndexType global_walkers, IndexType requested_samples, IndexType requested_steps, IndexType blocks)
 pure function calculating the actual number of steps per block More...
 
static void checkNumCrowdsLTNumThreads (const int num_crowds)
 
static void checkLogAndGL (Crowd &crowd, const std::string_view location)
 check logpsi and grad and lap against values computed from scratch More...
 
static void setWalkerOffsets (WalkerConfigurations &, Communicate *comm)
 update the global offsets of walker configurations after active walkers being touched. More...
 
- Protected Attributes inherited from QMCDriverNew
QMCDriverInput qmcdriver_input_
 
RealType max_disp_sq_
 
they should be limited to values that can be changed from input or are live state. More...
 
IndexType target_samples_
 the number of saved samples More...
 
IndexType nBlocksBetweenRecompute
 the number of blocks between recomptePsi More...
 
UPtrVector< Crowdcrowds_
 }@ More...
 
std::string h5_file_root_
 
std::unique_ptr< DriftModifierBasedrift_modifier_
 drift modifer More...
 
int k_delay
 the number to delay updates by More...
 
int walker_dump_period
 period of recording walker configurations More...
 
IndexType current_step_
 
size_t steps_per_block_ = 0
 actual number of steps per block More...
 
IndexType nAccept
 counter for number of moves accepted More...
 
IndexType nReject
 counter for number of moves /rejected More...
 
RealType m_oneover2tau
 Time-step factor $ 1/(2\tau)$. More...
 
RealType m_sqrttau
 Time-step factor $ \sqrt{\tau}$. More...
 
const std::string QMCType
 type of qmc: assigned by subclasses More...
 
MCPopulation population_
 the entire (on node) walker population it serves VMCBatch and DMCBatch right now but will be polymorphic More...
 
struct DriverWalkerResourceCollection golden_resource_
 the golden multi walker shared resource serves ParticleSet TrialWaveFunction right now but actually should be based on MCPopulation. More...
 
const MultiWalkerDispatchers dispatchers_
 multi walker dispatchers More...
 
std::unique_ptr< EstimatorManagerNewestimator_manager_
 Observables manager Has very problematic owner ship and life cycle. More...
 
std::unique_ptr< HDFWalkerOutputwOut
 record engine for walkers More...
 
UPtrVector< ContextForStepsstep_contexts_
 Per crowd move contexts, this is where the DistanceTables etc. More...
 
UPtrVector< RandomBase< FullPrecRealType > > Rng
 Random number generators. More...
 
std::vector< xmlNodePtr > mcwalkerNodePtr
 a list of mcwalkerset element More...
 
DriverTimers timers_
 period of dumping walker configurations and everything else for restart More...
 
ScopedProfiler driver_scope_profiler_
 profile the driver lifetime More...
 
const ProjectDataproject_data_
 project info for accessing global fileroot and series id More...
 
WalkerConfigurationswalker_configs_ref_
 
- Protected Attributes inherited from MPIObjectBase
CommunicatemyComm
 pointer to Communicate More...
 
std::string ClassName
 class Name More...
 
std::string myName
 name of the object More...
 
- Static Private Member Functions inherited from LinearMethod
static Real selectEigenvalue (std::vector< Real > &eigenvals, Matrix< Real > &eigenvectors, Real zerozero, std::vector< Real > &ev)
 Select eigenvalue and return corresponding scaled eigenvector. More...
 
static Real getLowestEigenvector_Inv (Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
 Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selected eigenvalue. More...
 
static Real getLowestEigenvector_Gen (Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
 Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selected eigenvalue. More...
 

Detailed Description

Definition at line 52 of file QMCFixedSampleLinearOptimizeBatched.h.


Class Documentation

◆ qmcplusplus::QMCFixedSampleLinearOptimizeBatched::LMYOptions

struct qmcplusplus::QMCFixedSampleLinearOptimizeBatched::LMYOptions

Definition at line 208 of file QMCFixedSampleLinearOptimizeBatched.h.

+ Collaboration diagram for QMCFixedSampleLinearOptimizeBatched::LMYOptions:
Class Members
bool block_first whether to do the first part of block lm
bool block_lm whether we are doing block algorithm
bool block_second whether to do the second part of block lm
bool block_third whether to do the third part of block lm
RealType cost_increase_tol the tolerance to cost function increases when choosing the best shift in the adaptive shift method
OptimizerType current_optimizer_type type of the current optimization method, updated by processOptXML before run
bool doHybrid whether to use hybrid method
bool filter_info whether to filter parameters for the lm
bool filter_param whether to filter parameters for the lm
RealType max_param_change max amount a parameter may change relative to current wave function weight
RealType max_relative_cost_change the maximum relative change in the cost function for the adaptive three-shift scheme
int nblocks number of blocks used in block algorithm
int nkept number of directions kept
int nolds number of old updates kept
int nsamp_comp number of samples to do in correlated sampling part
int num_shifts number of shifts we will try
RealType omega_shift the shift to use when targeting an excited state
OptimizerType previous_optimizer_type type of the previous optimization method, updated by processOptXML before run
double ratio_threshold threshold for filtering parameters for the lm
bool store_samples whether to store samples for the lm
RealType target_shift_i the shift_i value that the adaptive shift method should aim for
bool targetExcited whether we are targeting an excited state

Constructor & Destructor Documentation

◆ QMCFixedSampleLinearOptimizeBatched()

QMCFixedSampleLinearOptimizeBatched ( const ProjectData project_data,
QMCDriverInput &&  qmcdriver_input,
const std::optional< EstimatorManagerInput > &  global_emi,
VMCDriverInput &&  vmcdriver_input,
WalkerConfigurations wc,
MCPopulation &&  population,
SampleStack samples,
Communicate comm 
)

Constructor.

Definition at line 49 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References ParameterSet::add(), qmcplusplus::app_log(), QMCFixedSampleLinearOptimizeBatched::bigChange, QMCFixedSampleLinearOptimizeBatched::LMYOptions::block_lm, QMCFixedSampleLinearOptimizeBatched::LMYOptions::cost_increase_tol, QMCFixedSampleLinearOptimizeBatched::exp0, QMCFixedSampleLinearOptimizeBatched::LMYOptions::filter_info, QMCFixedSampleLinearOptimizeBatched::LMYOptions::filter_param, QMCFixedSampleLinearOptimizeBatched::m_param, QMCFixedSampleLinearOptimizeBatched::Max_iterations, QMCFixedSampleLinearOptimizeBatched::LMYOptions::max_param_change, QMCFixedSampleLinearOptimizeBatched::LMYOptions::max_relative_cost_change, QMCFixedSampleLinearOptimizeBatched::MinMethod, QMCFixedSampleLinearOptimizeBatched::LMYOptions::nblocks, QMCFixedSampleLinearOptimizeBatched::LMYOptions::nkept, QMCFixedSampleLinearOptimizeBatched::LMYOptions::nolds, QMCFixedSampleLinearOptimizeBatched::LMYOptions::nsamp_comp, QMCFixedSampleLinearOptimizeBatched::nstabilizers, QMCFixedSampleLinearOptimizeBatched::LMYOptions::num_shifts, QMCFixedSampleLinearOptimizeBatched::LMYOptions::omega_shift, QMCFixedSampleLinearOptimizeBatched::options_LMY_, QMCFixedSampleLinearOptimizeBatched::param_tol, QMCDriverNew::qmc_driver_mode_, QMCDriverNew::QMC_OPTIMIZE, QMCFixedSampleLinearOptimizeBatched::LMYOptions::ratio_threshold, QMCFixedSampleLinearOptimizeBatched::shift_i_input, QMCFixedSampleLinearOptimizeBatched::shift_s_input, QMCFixedSampleLinearOptimizeBatched::stabilizerScale, QMCFixedSampleLinearOptimizeBatched::LMYOptions::store_samples, QMCFixedSampleLinearOptimizeBatched::LMYOptions::target_shift_i, and QMCFixedSampleLinearOptimizeBatched::LMYOptions::targetExcited.

58  : QMCDriverNew(
59  project_data,
60  std::move(qmcdriver_input),
61  std::
62  nullopt, // this class is not a real QMCDriverNew as far as I can tell so we don't give it the actual global_emi_
63  wc,
64  std::move(population),
65  "QMCLinearOptimizeBatched::",
66  comm,
67  "QMCLinearOptimizeBatched"),
68  objFuncWrapper_(*this),
69 #ifdef HAVE_LMY_ENGINE
70  vdeps(1, std::vector<double>()),
71 #endif
72  Max_iterations(1),
73  param_tol(1e-4),
74  nstabilizers(3),
75  stabilizerScale(2.0),
76  bigChange(50),
77  exp0(-16),
78  bestShift_i(-1.0),
79  bestShift_s(-1.0),
80  shift_i_input(0.01),
81  shift_s_input(1.00),
82  accept_history(3),
83  shift_s_base(4.0),
84  MinMethod("OneShiftOnly"),
88  freeze_parameters_(false),
89  initialize_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Initialize", timer_level_medium)),
90  generate_samples_timer_(createGlobalTimer("QMCLinearOptimizeBatched::generateSamples", timer_level_medium)),
91  build_olv_ham_timer_(createGlobalTimer("QMCLinearOptimizedBatched::build_ovl_ham_matrix", timer_level_medium)),
92  invert_olvmat_timer_(createGlobalTimer("QMCLinearOptimizedBatched::invert_ovlMat", timer_level_medium)),
93  eigenvalue_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Eigenvalue", timer_level_medium)),
94  line_min_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Line_Minimization", timer_level_medium)),
95  cost_function_timer_(createGlobalTimer("QMCLinearOptimizeBatched::CostFunction", timer_level_medium)),
96  wfNode(NULL),
97  vmcdriver_input_(vmcdriver_input),
98  samples_(samples),
99  global_emi_(global_emi)
100 {
101  //set the optimization flag
103  //read to use vmc output (just in case)
104  m_param.add(MinMethod, "MinMethod");
105  m_param.add(Max_iterations, "max_its");
106  m_param.add(nstabilizers, "nstabilizers");
107  m_param.add(stabilizerScale, "stabilizerscale");
108  m_param.add(bigChange, "bigchange");
109  m_param.add(exp0, "exp0");
110  m_param.add(param_tol, "alloweddifference");
111  m_param.add(shift_i_input, "shift_i");
112  m_param.add(shift_s_input, "shift_s");
113  // options_LMY_
114  m_param.add(options_LMY_.targetExcited, "options_LMY_.targetExcited");
115  m_param.add(options_LMY_.block_lm, "options_LMY_.block_lm");
116  m_param.add(options_LMY_.nblocks, "options_LMY_.nblocks");
117  m_param.add(options_LMY_.nolds, "options_LMY_.nolds");
118  m_param.add(options_LMY_.nkept, "options_LMY_.nkept");
119  m_param.add(options_LMY_.nsamp_comp, "options_LMY_.nsamp_comp");
121  m_param.add(options_LMY_.max_relative_cost_change, "options_LMY_.max_relative_cost_change");
122  m_param.add(options_LMY_.max_param_change, "options_LMY_.max_param_change");
123  m_param.add(options_LMY_.num_shifts, "options_LMY_.num_shifts");
124  m_param.add(options_LMY_.cost_increase_tol, "options_LMY_.cost_increase_tol");
125  m_param.add(options_LMY_.target_shift_i, "options_LMY_.target_shift_i");
126  m_param.add(options_LMY_.filter_param, "filter_param");
127  m_param.add(options_LMY_.ratio_threshold, "deriv_threshold");
128  m_param.add(options_LMY_.store_samples, "store_samples");
129  m_param.add(options_LMY_.filter_info, "filter_info");
130 
131 
132 #ifdef HAVE_LMY_ENGINE
133  //app_log() << "construct QMCFixedSampleLinearOptimizeBatched" << endl;
134  std::vector<double> shift_scales(3, 1.0);
135  EngineObj = new cqmc::engine::LMYEngine<ValueType>(&vdeps,
136  false, // exact sampling
137  true, // ground state?
138  false, // variance correct,
139  true,
140  true, // print matrices,
141  true, // build matrices
142  false, // spam
143  false, // use var deps?
144  true, // chase lowest
145  false, // chase closest
146  false, // eom
147  false,
148  false, // eom related
149  false, // eom related
150  false, // use block?
151  120000, // number of samples
152  0, // number of parameters
153  60, // max krylov iter
154  0, // max spam inner iter
155  1, // spam appro degree
156  0, // eom related
157  0, // eom related
158  0, // eom related
159  0.0, // omega
160  0.0, // var weight
161  1.0e-6, // convergence threshold
162  0.99, // minimum S singular val
163  0.0, 0.0,
164  10.0, // max change allowed
165  1.00, // identity shift
166  1.00, // overlap shift
167  0.3, // max parameter change
168  shift_scales, app_log());
169 #endif
170 }
QMCDriverNew(const ProjectData &project_data, QMCDriverInput &&input, const std::optional< EstimatorManagerInput > &global_emi, WalkerConfigurations &wc, MCPopulation &&population, const std::string timer_prefix, Communicate *comm, const std::string &QMC_driver_type)
Constructor.
std::ostream & app_log()
Definition: OutputManager.h:65
std::bitset< QMC_MODE_MAX > qmc_driver_mode_
bits to classify QMCDriver
Definition: QMCDriverNew.h:103
RealType shift_i_input
current shift_i, shift_s input values
RealType max_param_change
max amount a parameter may change relative to current wave function weight
RealType omega_shift
the shift to use when targeting an excited state
RealType cost_increase_tol
the tolerance to cost function increases when choosing the best shift in the adaptive shift method ...
NewTimer & createGlobalTimer(const std::string &myname, timer_levels mylevel)
int nsamp_comp
number of samples to do in correlated sampling part
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
RealType target_shift_i
the shift_i value that the adaptive shift method should aim for
std::string MinMethod
name of the current optimization method, updated by processOptXML before run
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
RealType max_relative_cost_change
the maximum relative change in the cost function for the adaptive three-shift scheme ...
int nstabilizers
Number of iterations maximum before generating new configurations.
std::bitset< 2 > accept_history
accept history, remember the last 2 iterations, value 00, 01, 10, 11
int Max_iterations
Number of iterations maximum before generating new configurations.
double ratio_threshold
threshold for filtering parameters for the lm
const std::optional< EstimatorManagerInput > global_emi_
This is retained in order to construct and reconstruct the vmcEngine.

◆ ~QMCFixedSampleLinearOptimizeBatched()

Destructor.

Clean up the vector.

Definition at line 173 of file QMCFixedSampleLinearOptimizeBatched.cpp.

174 {
175 #ifdef HAVE_LMY_ENGINE
176  delete EngineObj;
177 #endif
178 }

Member Function Documentation

◆ adaptive_three_shift_run()

bool adaptive_three_shift_run ( )
private

◆ costFunc()

Definition at line 180 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References QMCFixedSampleLinearOptimizeBatched::objFuncWrapper_, QMCFixedSampleLinearOptimizeBatched::optdir, QMCFixedSampleLinearOptimizeBatched::optparam, and QMCFixedSampleLinearOptimizeBatched::optTarget.

181 {
182  for (int i = 0; i < optparam.size(); i++)
183  optTarget->Params(i) = optparam[i] + dl * optdir[i];
185  //only allow this to go false if it was true. If false, stay false
186  // if (validFuncVal)
187  objFuncWrapper_.validFuncVal = optTarget->IsValid;
188  return c;
189 }
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
QMCTraits::RealType RealType
Definition: QMCDriverNew.h:78

◆ descent_run()

bool descent_run ( )
private

◆ finish()

void finish ( )

common operation to finish optimization, used by the derived classes

Definition at line 258 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References QMCFixedSampleLinearOptimizeBatched::optTarget.

Referenced by QMCFixedSampleLinearOptimizeBatched::one_shift_run(), QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run(), and QMCFixedSampleLinearOptimizeBatched::test_run().

259 {
260  if (optTarget->reportH5)
261  optTarget->reportParametersH5();
262  optTarget->reportParameters();
263 }
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize

◆ generateSamples()

void generateSamples ( )

Definition at line 265 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::app_log(), Timer::elapsed(), QMCFixedSampleLinearOptimizeBatched::generate_samples_timer_, QMCDriverNew::get_root_name(), QMCDriverNew::h5_file_root_, QMCFixedSampleLinearOptimizeBatched::optTarget, SampleStack::resetSampleCount(), QMCFixedSampleLinearOptimizeBatched::samples_, and QMCFixedSampleLinearOptimizeBatched::vmcEngine.

Referenced by QMCFixedSampleLinearOptimizeBatched::start().

266 {
268  app_log() << std::endl
269  << "******************" << std::endl
270  << "Generating samples" << std::endl
271  << "******************" << std::endl
272  << std::endl;
274 
275  Timer t_gen;
276  vmcEngine->run();
277  app_log() << " Execution time (sampling) = " << std::setprecision(4) << t_gen.elapsed() << std::endl;
278 
279  //reset the rootname
281  optTarget->setRootName(get_root_name());
282 }
void resetSampleCount()
Set the sample count to zero but preserve the storage.
Definition: SampleStack.cpp:57
std::ostream & app_log()
Definition: OutputManager.h:65
ScopeGuard< NewTimer > ScopedTimer
Definition: NewTimer.h:257
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
const std::string & get_root_name() const override
Definition: QMCDriverNew.h:331

◆ getRunType()

QMCRunType getRunType ( )
inlineoverridevirtual

◆ is_best_cost()

bool is_best_cost ( const int  ii,
const std::vector< RealType > &  cv,
const std::vector< double > &  sh,
const RealType  ic 
) const
private

Returns whether the proposed new cost is the best compared to the others.

Parameters
[in]iiindex of the proposed best cost
[in]cvvector of new costs
[in]shvector of identity shifts (shift_i values)
[in]icthe initial cost

Definition at line 885 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::abs(), QMCFixedSampleLinearOptimizeBatched::LMYOptions::cost_increase_tol, QMCFixedSampleLinearOptimizeBatched::LMYOptions::max_relative_cost_change, QMCFixedSampleLinearOptimizeBatched::options_LMY_, and QMCFixedSampleLinearOptimizeBatched::LMYOptions::target_shift_i.

889 {
890  //app_log() << "determining best cost with options_LMY_.cost_increase_tol = " << options_LMY_.cost_increase_tol << " and options_LMY_.target_shift_i = " << options_LMY_.target_shift_i << std::endl;
891 
892  // initialize return value
893  bool retval = true;
894 
895  //app_log() << "retval = " << retval << std::endl;
896 
897  // compare to other costs
898  for (int i = 0; i < cv.size(); i++)
899  {
900  // don't compare to yourself
901  if (i == ii)
902  continue;
903 
904  // we only worry about the other value if it is within the maximum relative change threshold and not too high
905  const bool other_is_valid =
906  ((ic == 0.0 ? 0.0 : std::abs((cv.at(i) - ic) / ic)) < options_LMY_.max_relative_cost_change &&
907  cv.at(i) < ic + options_LMY_.cost_increase_tol);
908  if (other_is_valid)
909  {
910  // if we are using a target shift and the cost is not too much higher, then prefer this cost if its shift is closer to the target shift
911  if (options_LMY_.target_shift_i > 0.0)
912  {
913  const bool closer_to_target =
915  const bool cost_is_similar = (std::abs(cv.at(ii) - cv.at(i)) < options_LMY_.cost_increase_tol);
916  const bool cost_is_much_lower = (!cost_is_similar && cv.at(ii) < cv.at(i) - options_LMY_.cost_increase_tol);
917  if (cost_is_much_lower || (closer_to_target && cost_is_similar))
918  retval = (retval && true);
919  else
920  retval = false;
921 
922  // if we are not using a target shift, then prefer this cost if it is lower
923  }
924  else
925  {
926  retval = (retval && cv.at(ii) <= cv.at(i));
927  }
928  }
929 
930  //app_log() << "cv.at(ii) = " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv.at(ii) << " <= "
931  // << "cv.at(i) = " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv.at(i) << " ?" << std::endl;
932  //app_log() << "retval = " << retval << std::endl;
933  }
934 
935  // new cost can only be the best cost if it is less than (or not too much higher than) the initial cost
936  retval = (retval && cv.at(ii) < ic + options_LMY_.cost_increase_tol);
937  //app_log() << "cv.at(ii) = " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv.at(ii) << " <= "
938  // << "ic = " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << ic << " ?" << std::endl;
939  //app_log() << "retval = " << retval << std::endl;
940 
941  // new cost is only best if it's relative change from the initial cost is not too large ( or if the initial cost is exactly zero )
942  retval = (retval && (ic == 0.0 ? 0.0 : std::abs((cv.at(ii) - ic) / ic)) < options_LMY_.max_relative_cost_change);
943  //app_log() << "std::abs( ( cv.at(ii) - ic ) / ic ) = " << std::fixed << std::right << std::setw(20) << std::setprecision(12)
944  // << std::abs( ( cv.at(ii) - ic ) / ic ) << " <= " << this->options_LMY_.max_relative_cost_change << " ? " << std::endl;
945  //app_log() << "retval = " << retval << std::endl;
946  //app_log() << std::endl;
947 
948  // return whether the proposed cost is actually the best
949  return retval;
950 }
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
RealType cost_increase_tol
the tolerance to cost function increases when choosing the best shift in the adaptive shift method ...
RealType target_shift_i
the shift_i value that the adaptive shift method should aim for
RealType max_relative_cost_change
the maximum relative change in the cost function for the adaptive three-shift scheme ...

◆ one_shift_run()

bool one_shift_run ( )
private

Performs one iteration of the linear method using an adaptive scheme that tries three different shift magnitudes and picks the best one.

The scheme is adaptive in that it saves the best shift to use as a starting point in the next iteration. Note that the best shift is chosen based on a different sample than that used to construct the linear method matrices in order to avoid over-optimizing on a particular sample.

Returns
???

Definition at line 1569 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::abs(), QMCFixedSampleLinearOptimizeBatched::accept_history, qmcplusplus::app_log(), Communicate::bcast(), QMCFixedSampleLinearOptimizeBatched::bestShift_i, QMCFixedSampleLinearOptimizeBatched::bestShift_s, QMCFixedSampleLinearOptimizeBatched::build_olv_ham_timer_, hdf_archive::close(), Matrix< T, Alloc >::copy(), hdf_archive::create(), QMCFixedSampleLinearOptimizeBatched::do_output_matrices_csv_, QMCFixedSampleLinearOptimizeBatched::do_output_matrices_hdf_, qmcplusplus::Units::charge::e, QMCFixedSampleLinearOptimizeBatched::eigensolver_, QMCFixedSampleLinearOptimizeBatched::eigenvalue_timer_, Timer::elapsed(), QMCFixedSampleLinearOptimizeBatched::finish(), QMCFixedSampleLinearOptimizeBatched::freeze_parameters_, QMCDriverNew::get_root_name(), LinearMethod::getLowestEigenvector_Gen(), LinearMethod::getLowestEigenvector_Inv(), LinearMethod::getNonLinearRescale(), MPIObjectBase::is_manager(), qmcplusplus::isnan(), MPIObjectBase::myComm, qmcplusplus::Units::force::N, QMCFixedSampleLinearOptimizeBatched::objFuncWrapper_, QMCFixedSampleLinearOptimizeBatched::optTarget, OutputMatrix::output(), QMCFixedSampleLinearOptimizeBatched::output_hamiltonian_, QMCFixedSampleLinearOptimizeBatched::output_overlap_, QMCFixedSampleLinearOptimizeBatched::shift_s_base, qmcplusplus::sqrt(), QMCFixedSampleLinearOptimizeBatched::start(), and hdf_archive::write().

Referenced by QMCFixedSampleLinearOptimizeBatched::run().

1570 {
1571  // ensure the cost function is set to compute derivative vectors
1572  optTarget->setneedGrads(true);
1573 
1574  // generate samples and compute weights, local energies, and derivative vectors
1575  start();
1576 
1577  // get number of optimizable parameters
1578  const int numParams = optTarget->getNumParams();
1579 
1580  // get dimension of the linear method matrices
1581  const int N = numParams + 1;
1582 
1583  // prepare vectors to hold the initial and current parameters
1584  std::vector<RealType> currentParameters(numParams, 0.0);
1585 
1586  // initialize the initial and current parameter vectors
1587  for (int i = 0; i < numParams; i++)
1588  currentParameters.at(i) = std::real(optTarget->Params(i));
1589 
1590  // prepare vectors to hold the parameter update directions for each shift
1591  std::vector<RealType> parameterDirections;
1592  parameterDirections.assign(N, 0.0);
1593 
1594  // compute the initial cost
1595  const RealType initCost = optTarget->computedCost();
1596 
1597  // allocate the matrices we will need
1598  Matrix<RealType> ovlMat(N, N);
1599  ovlMat = 0.0;
1600  Matrix<RealType> hamMat(N, N);
1601  hamMat = 0.0;
1602  Matrix<RealType> invMat(N, N);
1603  invMat = 0.0;
1604  Matrix<RealType> prdMat(N, N);
1605  prdMat = 0.0;
1606 
1607  // for outputing matrices and eigenvalue/vectors to disk
1608  hdf_archive hout;
1609 
1610  {
1612  Timer t_build_matrices;
1613  // say what we are doing
1614  app_log() << std::endl
1615  << "*****************************************" << std::endl
1616  << "Building overlap and Hamiltonian matrices" << std::endl
1617  << "*****************************************" << std::endl;
1618 
1619  // build the overlap and hamiltonian matrices
1620  optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);
1621 
1623  {
1624  output_overlap_.output(ovlMat);
1625  output_hamiltonian_.output(hamMat);
1626  }
1627 
1629  {
1630  std::string newh5 = get_root_name() + ".linear_matrices.h5";
1631  hout.create(newh5, H5F_ACC_TRUNC);
1632  hout.write(ovlMat, "overlap");
1633  hout.write(hamMat, "Hamiltonian");
1634  hout.write(bestShift_i, "bestShift_i");
1635  hout.write(bestShift_s, "bestShift_s");
1636  }
1637  app_log() << " Execution time (building matrices) = " << std::setprecision(4) << t_build_matrices.elapsed()
1638  << std::endl;
1639  }
1640 
1641  // Solve eigenvalue problem on one rank.
1642  if (is_manager())
1643  {
1645  Timer t_eigen;
1646  app_log() << std::endl
1647  << "**************************" << std::endl
1648  << "Solving eigenvalue problem" << std::endl
1649  << "**************************" << std::endl;
1650 
1651  invMat.copy(ovlMat);
1652  // apply the identity shift
1653  for (int i = 1; i < N; i++)
1654  {
1655  hamMat(i, i) += bestShift_i;
1656  if (invMat(i, i) == 0)
1657  invMat(i, i) = bestShift_i * bestShift_s;
1658  }
1659 
1660  // apply the overlap shift
1661  for (int i = 1; i < N; i++)
1662  for (int j = 1; j < N; j++)
1663  hamMat(i, j) += bestShift_s * ovlMat(i, j);
1664 
1665  RealType lowestEV;
1666  // compute the lowest eigenvalue and the corresponding eigenvector
1667  if (eigensolver_ == "general")
1668  {
1669  app_log() << " Using generalized eigenvalue solver (ggev)" << std::endl;
1670  lowestEV = getLowestEigenvector_Gen(hamMat, invMat, parameterDirections);
1671  }
1672  else if (eigensolver_ == "inverse")
1673  {
1674  app_log() << " Using inverse + regular eigenvalue solver (geev)" << std::endl;
1675  lowestEV = getLowestEigenvector_Inv(hamMat, invMat, parameterDirections);
1676  }
1677  else if (eigensolver_ == "arpack")
1678  {
1679  app_log() << "ARPACK not compiled into this QMCPACK executable" << std::endl;
1680  throw std::runtime_error("ARPACK not present (QMC_USE_ARPACK not set)");
1681  }
1682  else
1683  {
1684  throw std::runtime_error("Unknown eigenvalue solver: " + eigensolver_);
1685  }
1686 
1687  app_log() << " Execution time (eigenvalue) = " << std::setprecision(4) << t_eigen.elapsed() << std::endl;
1688 
1689  // compute the scaling constant to apply to the update
1690  objFuncWrapper_.Lambda = getNonLinearRescale(parameterDirections, ovlMat, *optTarget);
1691 
1693  {
1694  hout.write(lowestEV, "lowest_eigenvalue");
1695  hout.write(parameterDirections, "scaled_eigenvector");
1696  hout.write(objFuncWrapper_.Lambda, "non_linear_rescale");
1697  hout.close();
1698  }
1699 
1700  // scale the update by the scaling constant
1701  for (int i = 0; i < numParams; i++)
1702  parameterDirections.at(i + 1) *= objFuncWrapper_.Lambda;
1703  }
1704  myComm->bcast(parameterDirections);
1705 
1706  // now that we are done building the matrices, prevent further computation of derivative vectors
1707  optTarget->setneedGrads(false);
1708 
1709  // prepare to use the middle shift's update as the guiding function for a new sample
1710  if (!freeze_parameters_)
1711  {
1712  for (int i = 0; i < numParams; i++)
1713  optTarget->Params(i) = currentParameters.at(i) + parameterDirections.at(i + 1);
1714  }
1715 
1716  RealType largestChange(0);
1717  int max_element = 0;
1718  for (int i = 0; i < numParams; i++)
1719  if (std::abs(parameterDirections.at(i + 1)) > largestChange)
1720  {
1721  largestChange = std::abs(parameterDirections.at(i + 1));
1722  max_element = i;
1723  }
1724  app_log() << std::endl
1725  << "Among totally " << numParams << " optimized parameters, "
1726  << "largest LM parameter change : " << largestChange << " at parameter " << max_element << std::endl;
1727 
1728  // compute the new cost
1729  optTarget->IsValid = true;
1730  const RealType newCost = optTarget->Cost(false);
1731 
1732  app_log() << std::endl
1733  << "******************************************************************************" << std::endl
1734  << "Init Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << initCost
1735  << " New Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << newCost
1736  << " Delta Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4)
1737  << newCost - initCost << std::endl
1738  << "******************************************************************************" << std::endl;
1739 
1740  if (!optTarget->IsValid || qmcplusplus::isnan(newCost))
1741  {
1742  app_log() << std::endl << "The new set of parameters is not valid. Revert to the old set!" << std::endl;
1743  for (int i = 0; i < numParams; i++)
1744  optTarget->Params(i) = currentParameters.at(i);
1746  if (accept_history[0] == true && accept_history[1] == false) // rejected the one before last and accepted the last
1747  {
1749  app_log() << "Update shift_s_base to " << shift_s_base << std::endl;
1750  }
1751  accept_history <<= 1;
1752  }
1753  else
1754  {
1755  if (bestShift_s > 1.0e-2)
1757  // say what we are doing
1758  app_log() << std::endl << "The new set of parameters is valid. Updating the trial wave function!" << std::endl;
1759  accept_history <<= 1;
1760  accept_history.set(0, true);
1761  }
1762 
1763  app_log() << std::endl
1764  << "*****************************************************************************" << std::endl
1765  << "Applying the update for shift_i = " << std::scientific << std::right << std::setw(12)
1766  << std::setprecision(4) << bestShift_i << " and shift_s = " << std::scientific << std::right
1767  << std::setw(12) << std::setprecision(4) << bestShift_s << std::endl
1768  << "*****************************************************************************" << std::endl;
1769 
1770  // perform some finishing touches for this linear method iteration
1771  finish();
1772 
1773  // return whether the cost function's report counter is positive
1774  return (optTarget->getReportCounter() > 0);
1775 }
QMCTraits::RealType real
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
std::ostream & app_log()
Definition: OutputManager.h:65
ScopeGuard< NewTimer > ScopedTimer
Definition: NewTimer.h:257
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
static Real getLowestEigenvector_Inv(Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selecte...
static Real getLowestEigenvector_Gen(Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selecte...
const std::string & get_root_name() const override
Definition: QMCDriverNew.h:331
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
void output(Matrix< RealType > &mat)
Print matrix to text-formatted scalar.dat file.
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
QMCTraits::RealType RealType
bool is_manager() const
return true if the rank == 0
Definition: MPIObjectBase.h:51
void bcast(T &)
void finish()
common operation to finish optimization, used by the derived classes
std::bitset< 2 > accept_history
accept history, remember the last 2 iterations, value 00, 01, 10, 11
Real getNonLinearRescale(std::vector< Real > &dP, Matrix< Real > &S, const QMCCostFunctionBase &optTarget) const
bool isnan(float a)
return true if the value is NaN.
Definition: math.cpp:18

◆ prepare_shifts()

std::vector< double > prepare_shifts ( const double  central_shift) const
private

returns a vector of three shift values centered around the provided shift.

Parameters
[in]central_shiftthe central shift

Definition at line 779 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References QMCFixedSampleLinearOptimizeBatched::LMYOptions::num_shifts, and QMCFixedSampleLinearOptimizeBatched::options_LMY_.

780 {
781  std::vector<double> retval(options_LMY_.num_shifts);
782 
783  // check to see whether the number of shifts is odd
784  if (options_LMY_.num_shifts % 2 == 0)
785  throw std::runtime_error("number of shifts must be odd in QMCFixedSampleLinearOptimizeBatched::prepare_shifts");
786 
787  // decide the central shift index
788  int central_index = options_LMY_.num_shifts / 2;
789 
790  for (int i = 0; i < options_LMY_.num_shifts; i++)
791  {
792  if (i < central_index)
793  retval.at(i) = central_shift / (4.0 * (central_index - i));
794  else if (i > central_index)
795  retval.at(i) = central_shift * (4.0 * (i - central_index));
796  else if (i == central_index)
797  retval.at(i) = central_shift;
798  //retval.at(i) = central_shift
799  //retval.at(0) = central_shift * 4.0;
800  //retval.at(1) = central_shift;
801  //retval.at(2) = central_shift / 4.0;
802  }
803  return retval;
804 }

◆ previous_linear_methods_run()

bool previous_linear_methods_run ( )
private

Definition at line 334 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::abs(), qmcplusplus::app_log(), QMCFixedSampleLinearOptimizeBatched::bigChange, Matrix< T, Alloc >::copy(), QMCFixedSampleLinearOptimizeBatched::cost_function_timer_, qmcplusplus::Units::charge::e, QMCFixedSampleLinearOptimizeBatched::eigenvalue_timer_, qmcplusplus::exp(), QMCFixedSampleLinearOptimizeBatched::exp0, QMCFixedSampleLinearOptimizeBatched::finish(), LinearMethod::getLowestEigenvector(), LinearMethod::getNonLinearRescale(), qmcplusplus::invert_matrix(), QMCFixedSampleLinearOptimizeBatched::line_min_timer_, QMCFixedSampleLinearOptimizeBatched::Max_iterations, QMCFixedSampleLinearOptimizeBatched::MinMethod, qmcplusplus::Units::force::N, QMCFixedSampleLinearOptimizeBatched::nstabilizers, QMCFixedSampleLinearOptimizeBatched::objFuncWrapper_, QMCFixedSampleLinearOptimizeBatched::optdir, QMCFixedSampleLinearOptimizeBatched::optparam, QMCFixedSampleLinearOptimizeBatched::optTarget, QMCFixedSampleLinearOptimizeBatched::param_tol, qmcplusplus::MatrixOperators::product(), QMCFixedSampleLinearOptimizeBatched::stabilizerScale, QMCFixedSampleLinearOptimizeBatched::start(), TimerType< CLOCK >::start(), TimerType< CLOCK >::stop(), and QMCFixedSampleLinearOptimizeBatched::ValidCostFunction().

Referenced by QMCFixedSampleLinearOptimizeBatched::run().

335 {
336  start();
337  bool Valid(true);
338  int Total_iterations(0);
339  //size of matrix
340  const int numParams = optTarget->getNumParams();
341  const int N = numParams + 1;
342  // where we are and where we are pointing
343  std::vector<RealType> currentParameterDirections(N, 0);
344  std::vector<RealType> currentParameters(numParams, 0);
345  std::vector<RealType> bestParameters(numParams, 0);
346  for (int i = 0; i < numParams; i++)
347  bestParameters[i] = currentParameters[i] = std::real(optTarget->Params(i));
348  // proposed direction and new parameters
349  optdir.resize(numParams, 0);
350  optparam.resize(numParams, 0);
351 
352  while (Total_iterations < Max_iterations)
353  {
354  Total_iterations += 1;
355  app_log() << "Iteration: " << Total_iterations << "/" << Max_iterations << std::endl;
356  if (!ValidCostFunction(Valid))
357  continue;
358  //this is the small amount added to the diagonal to stabilize the eigenvalue equation. 10^stabilityBase
359  RealType stabilityBase(exp0);
360  // reset params if necessary
361  for (int i = 0; i < numParams; i++)
362  optTarget->Params(i) = currentParameters[i];
364  RealType lastCost(optTarget->Cost(true));
366  // if cost function is currently invalid continue
367  Valid = optTarget->IsValid;
368  if (!ValidCostFunction(Valid))
369  continue;
370  RealType newCost(lastCost);
371  RealType startCost(lastCost);
372  Matrix<RealType> Left(N, N);
373  Matrix<RealType> Right(N, N);
374  Matrix<RealType> S(N, N);
375  // stick in wrong matrix to reduce the number of matrices we need by 1.( Left is actually stored in Right, & vice-versa)
376  optTarget->fillOverlapHamiltonianMatrices(Right, Left);
377  S.copy(Left);
378  bool apply_inverse(true);
379  if (apply_inverse)
380  {
381  Matrix<RealType> RightT(Left);
382  invert_matrix(RightT, false);
383  Left = 0;
384  product(RightT, Right, Left);
385  // Now the left matrix is the Hamiltonian with the inverse of the overlap applied ot it.
386  }
387  //Find largest off-diagonal element compared to diagonal element.
388  //This gives us an idea how well conditioned it is, used to stabilize.
389  RealType od_largest(0);
390  for (int i = 0; i < N; i++)
391  for (int j = 0; j < N; j++)
392  od_largest = std::max(std::max(od_largest, std::abs(Left(i, j)) - std::abs(Left(i, i))),
393  std::abs(Left(i, j)) - std::abs(Left(j, j)));
394  app_log() << "od_largest " << od_largest << std::endl;
395  //if(od_largest>0)
396  // od_largest = std::log(od_largest);
397  //else
398  // od_largest = -1e16;
399  //if (od_largest<stabilityBase)
400  // stabilityBase=od_largest;
401  //else
402  // stabilizerScale = std::max( 0.2*(od_largest-stabilityBase)/nstabilizers, stabilizerScale);
403  app_log() << " stabilityBase " << stabilityBase << std::endl;
404  app_log() << " stabilizerScale " << stabilizerScale << std::endl;
405  int failedTries(0);
406  bool acceptedOneMove(false);
407  for (int stability = 0; stability < nstabilizers; stability++)
408  {
409  bool goodStep(true);
410  // store the Hamiltonian matrix in Right
411  for (int i = 0; i < N; i++)
412  for (int j = 0; j < N; j++)
413  Right(i, j) = Left(j, i);
414  RealType XS(stabilityBase + stabilizerScale * (failedTries + stability));
415  for (int i = 1; i < N; i++)
416  Right(i, i) += std::exp(XS);
417  app_log() << " Using XS:" << XS << " " << failedTries << " " << stability << std::endl;
418  {
420  getLowestEigenvector(Right, currentParameterDirections);
421  objFuncWrapper_.Lambda = getNonLinearRescale(currentParameterDirections, S, *optTarget);
422  }
423  // biggest gradient in the parameter direction vector
424  RealType bigVec(0);
425  for (int i = 0; i < numParams; i++)
426  bigVec = std::max(bigVec, std::abs(currentParameterDirections[i + 1]));
427  // this can be overwritten during the line minimization
428  RealType evaluated_cost(startCost);
429  if (MinMethod == "rescale")
430  {
431  if (std::abs(objFuncWrapper_.Lambda * bigVec) > bigChange)
432  {
433  goodStep = false;
434  app_log() << " Failed Step. Magnitude of largest parameter change: "
435  << std::abs(objFuncWrapper_.Lambda * bigVec) << std::endl;
436  if (stability == 0)
437  {
438  failedTries++;
439  stability--;
440  }
441  else
442  stability = nstabilizers;
443  }
444  for (int i = 0; i < numParams; i++)
445  optTarget->Params(i) = currentParameters[i] + objFuncWrapper_.Lambda * currentParameterDirections[i + 1];
446  optTarget->IsValid = true;
447  }
448  else
449  {
450  for (int i = 0; i < numParams; i++)
451  optparam[i] = currentParameters[i];
452  for (int i = 0; i < numParams; i++)
453  optdir[i] = currentParameterDirections[i + 1];
454  objFuncWrapper_.TOL = param_tol / bigVec;
455  objFuncWrapper_.AbsFuncTol = true;
456  objFuncWrapper_.largeQuarticStep = bigChange / bigVec;
457  objFuncWrapper_.LambdaMax = 0.5 * objFuncWrapper_.Lambda;
459  if (MinMethod == "quartic")
460  {
461  int npts(7);
462  objFuncWrapper_.quadstep = objFuncWrapper_.stepsize * objFuncWrapper_.Lambda;
463  objFuncWrapper_.largeQuarticStep = bigChange / bigVec;
464  Valid = objFuncWrapper_.lineoptimization3(npts, evaluated_cost);
465  }
466  else
467  Valid = objFuncWrapper_.lineoptimization2();
469  RealType biggestParameterChange = bigVec * std::abs(objFuncWrapper_.Lambda);
470  if (biggestParameterChange > bigChange)
471  {
472  goodStep = false;
473  failedTries++;
474  app_log() << " Failed Step. Largest LM parameter change:" << biggestParameterChange << std::endl;
475  if (stability == 0)
476  stability--;
477  else
478  stability = nstabilizers;
479  }
480  else
481  {
482  for (int i = 0; i < numParams; i++)
483  optTarget->Params(i) = optparam[i] + objFuncWrapper_.Lambda * optdir[i];
484  app_log() << " Good Step. Largest LM parameter change:" << biggestParameterChange << std::endl;
485  }
486  }
487 
488  if (goodStep)
489  {
490  // this may have been evaluated already
491  // newCost=evaluated_cost;
492  //get cost at new minimum
493  newCost = optTarget->Cost(false);
494  app_log() << " OldCost: " << lastCost << " NewCost: " << newCost << " Delta Cost:" << (newCost - lastCost)
495  << std::endl;
496  optTarget->printEstimates();
497  // quit if newcost is greater than lastcost. E(Xs) looks quadratic (between steepest descent and parabolic)
498  // mmorales
499  Valid = optTarget->IsValid;
500  //if (MinMethod!="rescale" && !ValidCostFunction(Valid))
501  if (!ValidCostFunction(Valid))
502  {
503  goodStep = false;
504  app_log() << " Good Step, but cost function invalid" << std::endl;
505  failedTries++;
506  if (stability > 0)
507  stability = nstabilizers;
508  else
509  stability--;
510  }
511  if (newCost < lastCost && goodStep)
512  {
513  //Move was acceptable
514  for (int i = 0; i < numParams; i++)
515  bestParameters[i] = std::real(optTarget->Params(i));
516  lastCost = newCost;
517  acceptedOneMove = true;
518  if (std::abs(newCost - lastCost) < 1e-4)
519  {
520  failedTries++;
521  stability = nstabilizers;
522  continue;
523  }
524  }
525  else if (stability > 0)
526  {
527  failedTries++;
528  stability = nstabilizers;
529  continue;
530  }
531  }
532  app_log().flush();
533  if (failedTries > 20)
534  break;
535  //APP_ABORT("QMCFixedSampleLinearOptimizeBatched::run TOO MANY FAILURES");
536  }
537 
538  if (acceptedOneMove)
539  {
540  app_log() << "Setting new Parameters" << std::endl;
541  for (int i = 0; i < numParams; i++)
542  optTarget->Params(i) = bestParameters[i];
543  }
544  else
545  {
546  app_log() << "Reverting to old Parameters" << std::endl;
547  for (int i = 0; i < numParams; i++)
548  optTarget->Params(i) = currentParameters[i];
549  }
550  app_log().flush();
551  }
552 
553  finish();
554  return (optTarget->getReportCounter() > 0);
555 }
MatrixA::value_type invert_matrix(MatrixA &M, bool getdet=true)
invert a matrix
QMCTraits::RealType real
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
std::ostream & app_log()
Definition: OutputManager.h:65
ScopeGuard< NewTimer > ScopedTimer
Definition: NewTimer.h:257
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
void product(const Matrix< T > &A, const Matrix< T > &B, Matrix< T > &C)
static function to perform C=AB for real matrices
Real getLowestEigenvector(Matrix< Real > &A, std::vector< Real > &ev) const
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
std::string MinMethod
name of the current optimization method, updated by processOptXML before run
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
QMCTraits::RealType RealType
void finish()
common operation to finish optimization, used by the derived classes
int nstabilizers
Number of iterations maximum before generating new configurations.
int Max_iterations
Number of iterations maximum before generating new configurations.
Real getNonLinearRescale(std::vector< Real > &dP, Matrix< Real > &S, const QMCCostFunctionBase &optTarget) const

◆ print_cost_summary()

void print_cost_summary ( const double  si,
const double  ss,
const RealType  mc,
const RealType  cv,
const int  ind,
const int  bi,
const bool  gu 
)
private

prints a summary of the computed cost for the given shift

Parameters
[in]sithe identity shift
[in]ssthe overlap shift
[in]mcthe maximum parameter change
[in]cvthe cost function value
[in]indthe shift index: -1 (for initial state), 0, 1, or 2
[in]biindex of the best shift
[in]guflag telling whether it was a good update

Definition at line 836 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::app_log().

843 {
844  if (ind >= 0)
845  {
846  if (gu)
847  {
848  app_log() << " " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << si;
849  app_log() << " " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << ss;
850  app_log() << " " << std::scientific << std::right << std::setw(20) << std::setprecision(4) << mc;
851  app_log() << " " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv;
852  //app_log() << " " << std::right << std::setw(12) << ( ind == 0 ? "big shift" : ( ind == 1 ? "medium shift" : "small shift" ) );
853  }
854  else
855  {
856  app_log() << " " << std::right << std::setw(12) << "N/A";
857  app_log() << " " << std::right << std::setw(12) << "N/A";
858  app_log() << " " << std::right << std::setw(20) << "N/A";
859  app_log() << " " << std::right << std::setw(20) << "N/A";
860  app_log() << " " << std::right << std::setw(12) << "bad update";
861  }
862  }
863  else
864  {
865  app_log() << " " << std::right << std::setw(12) << "N/A";
866  app_log() << " " << std::right << std::setw(12) << "N/A";
867  app_log() << " " << std::right << std::setw(20) << "N/A";
868  app_log() << " " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv;
869  app_log() << " " << std::right << std::setw(12) << "initial";
870  }
871  if (ind == bi)
872  app_log() << " <--";
873  app_log() << std::endl;
874 }
std::ostream & app_log()
Definition: OutputManager.h:65

◆ print_cost_summary_header()

void print_cost_summary_header ( )
private

prints a header for the summary of each shift's result

Definition at line 810 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::app_log().

811 {
812  app_log() << " " << std::right << std::setw(12) << "shift_i";
813  app_log() << " " << std::right << std::setw(12) << "shift_s";
814  app_log() << " " << std::right << std::setw(20) << "max param change";
815  app_log() << " " << std::right << std::setw(20) << "cost function value";
816  app_log() << std::endl;
817  app_log() << " " << std::right << std::setw(12) << "------------";
818  app_log() << " " << std::right << std::setw(12) << "------------";
819  app_log() << " " << std::right << std::setw(20) << "--------------------";
820  app_log() << " " << std::right << std::setw(20) << "--------------------";
821  app_log() << std::endl;
822 }
std::ostream & app_log()
Definition: OutputManager.h:65

◆ process()

void process ( xmlNodePtr  q)
overridevirtual

preprocess xml node

Parses the xml input file for parameter definitions for the wavefunction optimization.

Parameters
qcurrent xmlNode
Returns
true if successful

Implements QMCDriverNew.

Definition at line 562 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References OhmmsAttributeSet::add(), ParameterSet::add(), qmcplusplus::app_log(), qmcplusplus::app_warning(), QMCFixedSampleLinearOptimizeBatched::do_output_matrices_csv_, QMCFixedSampleLinearOptimizeBatched::do_output_matrices_hdf_, QMCFixedSampleLinearOptimizeBatched::doGradientTest, QMCFixedSampleLinearOptimizeBatched::LMYOptions::doHybrid, QMCFixedSampleLinearOptimizeBatched::eigensolver_, QMCFixedSampleLinearOptimizeBatched::freeze_parameters_, getXMLAttributeValue(), QMCFixedSampleLinearOptimizeBatched::hybridEngineObj, QMCFixedSampleLinearOptimizeBatched::m_param, QMCFixedSampleLinearOptimizeBatched::MinMethod, MPIObjectBase::myComm, QMCFixedSampleLinearOptimizeBatched::options_LMY_, processChildren(), QMCFixedSampleLinearOptimizeBatched::processOptXML(), ParameterSet::put(), OhmmsAttributeSet::put(), GradientTestInput::readXML(), and QMCFixedSampleLinearOptimizeBatched::testEngineObj.

563 {
564  std::string useGPU("yes");
565  std::string vmcMove("pbyp");
566  std::string ReportToH5("no");
567  std::string OutputMatrices("no");
568  std::string OutputMatricesHDF("no");
569  std::string FreezeParameters("no");
570  OhmmsAttributeSet oAttrib;
571  oAttrib.add(useGPU, "gpu");
572  oAttrib.add(vmcMove, "move");
573  oAttrib.add(ReportToH5, "hdf5");
574 
575  m_param.add(OutputMatrices, "output_matrices_csv", {"no", "yes"});
576  m_param.add(OutputMatricesHDF, "output_matrices_hdf", {"no", "yes"});
577  m_param.add(FreezeParameters, "freeze_parameters", {"no", "yes"});
578 
579  m_param.add(eigensolver_, "eigensolver",
580  {
581  "inverse", // Inverse + nonsymmetric eigenvalue solver
582  "general" // General eigenvalue problem solver
583  });
584 
585  oAttrib.put(q);
586  m_param.put(q);
587 
588  do_output_matrices_csv_ = (OutputMatrices == "yes");
589  do_output_matrices_hdf_ = (OutputMatricesHDF == "yes");
590  freeze_parameters_ = (FreezeParameters == "yes");
591 
592  // Use freeze_parameters with output_matrices to generate multiple lines in the output with
593  // the same parameters so statistics can be computed in post-processing.
594 
595  if (freeze_parameters_)
596  {
597  app_log() << std::endl;
598  app_warning() << " The option 'freeze_parameters' is enabled. Variational parameters will not be updated. This "
599  "run will not perform variational parameter optimization!"
600  << std::endl;
601  app_log() << std::endl;
602  }
603 
604 
605  doGradientTest = false;
606  processChildren(q, [&](const std::string& cname, const xmlNodePtr element) {
607  if (cname == "optimize")
608  {
609  const std::string att(getXMLAttributeValue(element, "method"));
610  if (!att.empty() && att == "gradient_test")
611  {
612  GradientTestInput test_grad_input;
613  test_grad_input.readXML(element);
614  if (!testEngineObj)
615  testEngineObj = std::make_unique<GradientTest>(std::move(test_grad_input));
616  doGradientTest = true;
617  MinMethod = "gradient_test";
618  }
619  else
620  {
621  std::stringstream error_msg;
622  app_log() << "Unknown or missing 'method' attribute in optimize tag: " << att << "\n";
623  throw UniformCommunicateError(error_msg.str());
624  }
625  }
626  });
627 
628 
629  options_LMY_.doHybrid = false;
630  if (MinMethod == "hybrid")
631  {
632  options_LMY_.doHybrid = true;
633  if (!hybridEngineObj)
634  hybridEngineObj = std::make_unique<HybridEngine>(myComm, q);
635 
636  hybridEngineObj->incrementStepCounter();
637 
638  processOptXML(hybridEngineObj->getSelectedXML(), vmcMove, ReportToH5 == "yes", useGPU == "yes");
639  }
640  else
641  {
642  processOptXML(q, vmcMove, ReportToH5 == "yes", useGPU == "yes");
643  }
644 }
std::ostream & app_warning()
Definition: OutputManager.h:69
std::ostream & app_log()
Definition: OutputManager.h:65
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
std::string MinMethod
name of the current optimization method, updated by processOptXML before run
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
void processChildren(const xmlNodePtr cur, const F &functor)
process through all the children of an XML element F is a lambda or functor void F/[](const std::stri...
Definition: libxmldefs.h:175
bool processOptXML(xmlNodePtr cur, const std::string &vmcMove, bool reportH5, bool useGPU)
process xml node value (parameters for both VMC and OPT) for the actual optimization ...
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
Definition: AttributeSet.h:42

◆ processOptXML()

bool processOptXML ( xmlNodePtr  cur,
const std::string &  vmcMove,
bool  reportH5,
bool  useGPU 
)

process xml node value (parameters for both VMC and OPT) for the actual optimization

Definition at line 646 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::ADAPTIVE, QMCDriverNew::adjustGlobalWalkerCount(), APP_ABORT, qmcplusplus::app_log(), QMCFixedSampleLinearOptimizeBatched::bestShift_i, QMCFixedSampleLinearOptimizeBatched::bestShift_s, OHMMS::Controller, QMCFixedSampleLinearOptimizeBatched::LMYOptions::cost_increase_tol, QMCFixedSampleLinearOptimizeBatched::LMYOptions::current_optimizer_type, qmcplusplus::DESCENT, QMCFixedSampleLinearOptimizeBatched::descentEngineObj, QMCFixedSampleLinearOptimizeBatched::LMYOptions::doHybrid, MCPopulation::get_golden_electrons(), MCPopulation::get_golden_hamiltonian(), MCPopulation::get_golden_twf(), QMCDriverInput::get_num_crowds(), QMCDriverNew::get_root_name(), QMCDriverInput::get_total_walkers(), QMCDriverInput::get_walkers_per_rank(), WalkerConfigurations::getActiveWalkers(), QMCFixedSampleLinearOptimizeBatched::global_emi_, QMCDriverNew::h5_file_root_, QMCFixedSampleLinearOptimizeBatched::hybridEngineObj, QMCFixedSampleLinearOptimizeBatched::m_param, QMCFixedSampleLinearOptimizeBatched::LMYOptions::max_param_change, QMCFixedSampleLinearOptimizeBatched::LMYOptions::max_relative_cost_change, QMCDriverNew::mcwalkerNodePtr, QMCFixedSampleLinearOptimizeBatched::MinMethod, MPIObjectBase::myComm, omp_get_max_threads(), qmcplusplus::ONESHIFTONLY, qmcplusplus::OptimizerNames, QMCFixedSampleLinearOptimizeBatched::options_LMY_, QMCFixedSampleLinearOptimizeBatched::optTarget, QMCDriverNew::population_, QMCFixedSampleLinearOptimizeBatched::LMYOptions::previous_optimizer_type, QMCDriverNew::project_data_, ParameterSet::put(), QMCDriverNew::qmcdriver_input_, Communicate::rank(), VMCDriverInput::readXML(), QMCDriverInput::readXML(), QMCFixedSampleLinearOptimizeBatched::samples_, QMCFixedSampleLinearOptimizeBatched::shift_i_input, QMCFixedSampleLinearOptimizeBatched::shift_s_input, Communicate::size(), QMCFixedSampleLinearOptimizeBatched::LMYOptions::targetExcited, QMCFixedSampleLinearOptimizeBatched::vmcdriver_input_, QMCFixedSampleLinearOptimizeBatched::vmcEngine, QMCDriverNew::walker_configs_ref_, and QMCDriverNew::AdjustedWalkerCounts::walkers_per_crowd.

Referenced by QMCFixedSampleLinearOptimizeBatched::process().

650 {
651  m_param.put(opt_xml);
652 
653  auto iter = OptimizerNames.find(MinMethod);
654  if (iter == OptimizerNames.end())
655  throw std::runtime_error("Unknown MinMethod!\n");
658 
660  descentEngineObj = std::make_unique<DescentEngine>(myComm, opt_xml);
661 
662  // sanity check
665  APP_ABORT("options_LMY_.targetExcited = \"yes\" requires that MinMethod = \"adaptive or descent");
666 
667 #ifdef _OPENMP
669  {
670  // throw std::runtime_error("OpenMP threading not enabled with AdaptiveThreeShift optimizer. Use MPI for parallelism instead, and set OMP_NUM_THREADS to 1.");
671  app_log() << "test version of OpenMP threading with AdaptiveThreeShift optimizer" << std::endl;
672  }
673 #endif
674 
675  // check parameter change sanity
676  if (options_LMY_.max_param_change <= 0.0)
677  throw std::runtime_error(
678  "options_LMY_.max_param_change must be positive in QMCFixedSampleLinearOptimizeBatched::put");
679 
680  // check cost change sanity
682  throw std::runtime_error(
683  "options_LMY_.max_relative_cost_change must be positive in QMCFixedSampleLinearOptimizeBatched::put");
684 
685  // check shift sanity
686  if (shift_i_input <= 0.0)
687  throw std::runtime_error("shift_i must be positive in QMCFixedSampleLinearOptimizeBatched::put");
688  if (shift_s_input <= 0.0)
689  throw std::runtime_error("shift_s must be positive in QMCFixedSampleLinearOptimizeBatched::put");
690 
691  // check cost increase tolerance sanity
693  throw std::runtime_error(
694  "options_LMY_.cost_increase_tol must be non-negative in QMCFixedSampleLinearOptimizeBatched::put");
695 
696  // if this is the first time this function has been called, set the initial shifts
701  if (bestShift_s < 0.0)
703 
704  xmlNodePtr qsave = opt_xml;
705  xmlNodePtr cur = qsave->children;
706  int pid = OHMMS::Controller->rank();
707  while (cur != NULL)
708  {
709  std::string cname((const char*)(cur->name));
710  if (cname == "mcwalkerset")
711  {
712  mcwalkerNodePtr.push_back(cur);
713  }
714  cur = cur->next;
715  }
716 
717  // Destroy old object to stop timer to correctly order timer with object lifetime scope
718  vmcEngine.reset(nullptr);
719 
720  // Explicitly copy the driver input objects since they will be used to instantiate the VMCEngine repeatedly.
721  //Overwriting input information is also done here to account for the hybrid method
722  QMCDriverInput qmcdriver_input_copy = qmcdriver_input_;
723  VMCDriverInput vmcdriver_input_copy = vmcdriver_input_;
724 
725  if (MinMethod == "hybrid")
726  {
727  qmcdriver_input_copy.readXML(hybridEngineObj->getSelectedXML());
728  vmcdriver_input_copy.readXML(hybridEngineObj->getSelectedXML());
729  }
730  else
731  {
732  qmcdriver_input_copy.readXML(opt_xml);
733  vmcdriver_input_copy.readXML(opt_xml);
734  }
735 
736 
737  // create VMC engine
738  vmcEngine =
739  std::make_unique<VMCBatched>(project_data_, std::move(qmcdriver_input_copy), global_emi_,
740  std::move(vmcdriver_input_copy), walker_configs_ref_,
741  MCPopulation(myComm->size(), myComm->rank(), &population_.get_golden_electrons(),
743  samples_, myComm);
744 
745  vmcEngine->setUpdateMode(vmcMove[0] == 'p');
746 
747 
748  bool AppendRun = false;
749  vmcEngine->setStatus(get_root_name(), h5_file_root_, AppendRun);
750  vmcEngine->process(qsave);
751 
752  vmcEngine->enable_sample_collection();
753 
754  auto& qmcdriver_input = vmcEngine->getQMCDriverInput();
755  QMCDriverNew::AdjustedWalkerCounts awc =
758 
759 
760  bool success = true;
761  //allways reset optTarget
762  optTarget = std::make_unique<QMCCostFunctionBatched>(population_.get_golden_electrons(), population_.get_golden_twf(),
764  awc.walkers_per_crowd, myComm);
765  optTarget->setStream(&app_log());
766  if (reportH5)
767  optTarget->reportH5 = true;
768  success = optTarget->put(qsave);
769 
770  return success;
771 }
OptimizerType previous_optimizer_type
type of the previous optimization method, updated by processOptXML before run
const ParticleSet & get_golden_electrons() const
Definition: MCPopulation.h:176
MCPopulation population_
the entire (on node) walker population it serves VMCBatch and DMCBatch right now but will be polymorp...
Definition: QMCDriverNew.h:426
QMCHamiltonian & get_golden_hamiltonian()
Definition: MCPopulation.h:181
int rank() const
return the rank
Definition: Communicate.h:116
const std::map< std::string, OptimizerType > OptimizerNames
size_t getActiveWalkers() const
return the number of active walkers
OptimizerType current_optimizer_type
type of the current optimization method, updated by processOptXML before run
std::ostream & app_log()
Definition: OutputManager.h:65
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
int size() const
return the number of tasks
Definition: Communicate.h:118
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
RealType shift_i_input
current shift_i, shift_s input values
IndexType get_num_crowds() const
RealType max_param_change
max amount a parameter may change relative to current wave function weight
IndexType get_total_walkers() const
const std::string & get_root_name() const override
Definition: QMCDriverNew.h:331
void readXML(xmlNodePtr xml_input)
omp_int_t omp_get_max_threads()
Definition: OpenMP.h:26
IndexType get_walkers_per_rank() const
WalkerConfigurations & walker_configs_ref_
Definition: QMCDriverNew.h:481
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
RealType cost_increase_tol
the tolerance to cost function increases when choosing the best shift in the adaptive shift method ...
std::string MinMethod
name of the current optimization method, updated by processOptXML before run
static QMCDriverNew::AdjustedWalkerCounts adjustGlobalWalkerCount(Communicate &comm, const IndexType current_configs, const IndexType requested_total_walkers, const IndexType requested_walkers_per_rank, const RealType reserve_walkers, int num_crowds)
}@
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
const TrialWaveFunction & get_golden_twf() const
Definition: MCPopulation.h:178
std::vector< xmlNodePtr > mcwalkerNodePtr
a list of mcwalkerset element
Definition: QMCDriverNew.h:456
RealType max_relative_cost_change
the maximum relative change in the cost function for the adaptive three-shift scheme ...
const ProjectData & project_data_
project info for accessing global fileroot and series id
Definition: QMCDriverNew.h:478
QMCDriverInput qmcdriver_input_
Definition: QMCDriverNew.h:372
const std::optional< EstimatorManagerInput > global_emi_
This is retained in order to construct and reconstruct the vmcEngine.

◆ run()

bool run ( )
overridevirtual

Run the Optimization algorithm.

Implements QMCDriverInterface.

Definition at line 284 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::ADAPTIVE, QMCFixedSampleLinearOptimizeBatched::adaptive_three_shift_run(), qmcplusplus::app_log(), QMCFixedSampleLinearOptimizeBatched::LMYOptions::current_optimizer_type, qmcplusplus::DESCENT, QMCFixedSampleLinearOptimizeBatched::descent_run(), QMCFixedSampleLinearOptimizeBatched::do_output_matrices_csv_, QMCFixedSampleLinearOptimizeBatched::doGradientTest, QMCFixedSampleLinearOptimizeBatched::LMYOptions::doHybrid, QMCDriverNew::get_root_name(), OutputMatrix::init_file(), qmcplusplus::Units::force::N, QMCFixedSampleLinearOptimizeBatched::one_shift_run(), qmcplusplus::ONESHIFTONLY, QMCFixedSampleLinearOptimizeBatched::options_LMY_, QMCFixedSampleLinearOptimizeBatched::optTarget, QMCFixedSampleLinearOptimizeBatched::output_hamiltonian_, QMCFixedSampleLinearOptimizeBatched::output_matrices_initialized_, QMCFixedSampleLinearOptimizeBatched::output_overlap_, QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run(), and QMCFixedSampleLinearOptimizeBatched::test_run().

285 {
287  {
288  const int numParams = optTarget->getNumParams();
289  const int N = numParams + 1;
293  }
294 
295  if (doGradientTest)
296  {
297  app_log() << "Doing gradient test run" << std::endl;
298  return test_run();
299  }
300 #ifdef HAVE_LMY_ENGINE
302  {
303  app_log() << "Doing hybrid run" << std::endl;
304  return hybrid_run();
305  }
306 
307  // if requested, perform the update via the adaptive three-shift or single-shift method
309  return adaptive_three_shift_run();
310 
312  return descent_run();
313 
314 #endif
315 
317  return one_shift_run();
318 
320 }
OptimizerType current_optimizer_type
type of the current optimization method, updated by processOptXML before run
std::ostream & app_log()
Definition: OutputManager.h:65
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
bool one_shift_run()
Performs one iteration of the linear method using an adaptive scheme that tries three different shift...
const std::string & get_root_name() const override
Definition: QMCDriverNew.h:331
void init_file(const std::string &root_name, const std::string &name, int N)
Open a text-formatted scalar.dat file and print the header line.

◆ setWaveFunctionNode()

void setWaveFunctionNode ( xmlNodePtr  cur)
inline

◆ solveShiftsWithoutLMYEngine()

void solveShiftsWithoutLMYEngine ( const std::vector< double > &  shifts_i,
const std::vector< double > &  shifts_s,
std::vector< std::vector< RealType >> &  parameterDirections 
)
private

For each set of shifts, solves the linear method eigenproblem by building and diagonalizing the matrices.

Parameters
[in]shfits_ivector of identity shifts
[in]shfits_svector of overlap shifts
[out]parameterDirectionson exit, the update directions for the different shifts

Definition at line 961 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References Matrix< T, Alloc >::copy(), LinearMethod::getLowestEigenvector(), LinearMethod::getNonLinearRescale(), qmcplusplus::invert_matrix(), qmcplusplus::Units::force::N, QMCFixedSampleLinearOptimizeBatched::objFuncWrapper_, QMCFixedSampleLinearOptimizeBatched::optTarget, and qmcplusplus::MatrixOperators::product().

965 {
966  // get number of shifts to solve
967  const int nshifts = shifts_i.size();
968 
969  // get number of optimizable parameters
970  const int numParams = optTarget->getNumParams();
971 
972  // get dimension of the linear method matrices
973  const int N = numParams + 1;
974 
975  // prepare vectors to hold the parameter updates
976  parameterDirections.resize(nshifts);
977  for (int i = 0; i < parameterDirections.size(); i++)
978  parameterDirections.at(i).assign(N, 0.0);
979 
980  // allocate the matrices we will need
981  Matrix<RealType> ovlMat(N, N);
982  ovlMat = 0.0;
983  Matrix<RealType> hamMat(N, N);
984  hamMat = 0.0;
985  Matrix<RealType> invMat(N, N);
986  invMat = 0.0;
987  Matrix<RealType> sftMat(N, N);
988  sftMat = 0.0;
989  Matrix<RealType> prdMat(N, N);
990  prdMat = 0.0;
991 
992  // build the overlap and hamiltonian matrices
993  optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);
994 
995  //// print the hamiltonian matrix
996  //app_log() << std::endl;
997  //app_log() << "printing H matrix:" << std::endl;
998  //for (int i = 0; i < hamMat.rows(); i++) {
999  // for (int j = 0; j < hamMat.cols(); j++)
1000  // app_log() << " " << std::scientific << std::right << std::setw(14) << std::setprecision(5) << hamMat(i,j);
1001  // app_log() << std::endl;
1002  //}
1003  //app_log() << std::endl;
1004 
1005  //// print the overlap matrix
1006  //app_log() << std::endl;
1007  //app_log() << "printing S matrix:" << std::endl;
1008  //for (int i = 0; i < ovlMat.rows(); i++) {
1009  // for (int j = 0; j < ovlMat.cols(); j++)
1010  // app_log() << " " << std::scientific << std::right << std::setw(14) << std::setprecision(5) << ovlMat(i,j);
1011  // app_log() << std::endl;
1012  //}
1013  //app_log() << std::endl;
1014 
1015  // compute the inverse of the overlap matrix
1016  invMat.copy(ovlMat);
1017  invert_matrix(invMat, false);
1018 
1019  // compute the update for each shift
1020  for (int shift_index = 0; shift_index < nshifts; shift_index++)
1021  {
1022  // prepare to shift the hamiltonain matrix
1023  sftMat.copy(hamMat);
1024 
1025  // apply the identity shift
1026  for (int i = 1; i < N; i++)
1027  sftMat(i, i) += shifts_i.at(shift_index);
1028 
1029  // apply the overlap shift
1030  for (int i = 1; i < N; i++)
1031  for (int j = 1; j < N; j++)
1032  sftMat(i, j) += shifts_s.at(shift_index) * ovlMat(i, j);
1033 
1034  // multiply the shifted hamiltonian matrix by the inverse of the overlap matrix
1035  qmcplusplus::MatrixOperators::product(invMat, sftMat, prdMat);
1036 
1037  // transpose the result (why?)
1038  for (int i = 0; i < N; i++)
1039  for (int j = i + 1; j < N; j++)
1040  std::swap(prdMat(i, j), prdMat(j, i));
1041 
1042  // compute the lowest eigenvalue of the product matrix and the corresponding eigenvector
1043  getLowestEigenvector(prdMat, parameterDirections.at(shift_index));
1044 
1045  // compute the scaling constant to apply to the update
1046  objFuncWrapper_.Lambda = getNonLinearRescale(parameterDirections.at(shift_index), ovlMat, *optTarget);
1047 
1048  // scale the update by the scaling constant
1049  for (int i = 0; i < numParams; i++)
1050  parameterDirections.at(shift_index).at(i + 1) *= objFuncWrapper_.Lambda;
1051  }
1052 }
MatrixA::value_type invert_matrix(MatrixA &M, bool getdet=true)
invert a matrix
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
void product(const Matrix< T > &A, const Matrix< T > &B, Matrix< T > &C)
static function to perform C=AB for real matrices
Real getLowestEigenvector(Matrix< Real > &A, std::vector< Real > &ev) const
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
Real getNonLinearRescale(std::vector< Real > &dP, Matrix< Real > &S, const QMCCostFunctionBase &optTarget) const

◆ start()

void start ( )

common operation to start optimization

Definition at line 191 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References qmcplusplus::app_log(), Timer::elapsed(), QMCFixedSampleLinearOptimizeBatched::generateSamples(), QMCFixedSampleLinearOptimizeBatched::initialize_timer_, QMCFixedSampleLinearOptimizeBatched::optTarget, QMCFixedSampleLinearOptimizeBatched::vmcEngine, and QMCFixedSampleLinearOptimizeBatched::wfNode.

Referenced by QMCFixedSampleLinearOptimizeBatched::one_shift_run(), QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run(), and QMCFixedSampleLinearOptimizeBatched::test_run().

192 {
193  generateSamples();
194 
195  optTarget->setWaveFunctionNode(wfNode);
196 
197  {
198  app_log() << std::endl
199  << "*****************************" << std::endl
200  << "Compute parameter derivatives" << std::endl
201  << "*****************************" << std::endl
202  << std::endl;
204  Timer t_deriv;
205  optTarget->getConfigurations("");
206  optTarget->setRng(vmcEngine->getRngRefs());
207  NullEngineHandle handle;
208  optTarget->checkConfigurations(handle);
209  app_log() << " Execution time (derivatives) = " << std::setprecision(4) << t_deriv.elapsed() << std::endl;
210  }
211 }
std::ostream & app_log()
Definition: OutputManager.h:65
ScopeGuard< NewTimer > ScopedTimer
Definition: NewTimer.h:257
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize

◆ test_run()

bool test_run ( )
private

Definition at line 322 of file QMCFixedSampleLinearOptimizeBatched.cpp.

References QMCFixedSampleLinearOptimizeBatched::finish(), QMCDriverNew::get_root_name(), QMCFixedSampleLinearOptimizeBatched::optTarget, QMCFixedSampleLinearOptimizeBatched::start(), and QMCFixedSampleLinearOptimizeBatched::testEngineObj.

Referenced by QMCFixedSampleLinearOptimizeBatched::run().

323 {
324  // generate samples and compute weights, local energies, and derivative vectors
325  start();
326 
328 
329  finish();
330 
331  return true;
332 }
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
const std::string & get_root_name() const override
Definition: QMCDriverNew.h:331
void finish()
common operation to finish optimization, used by the derived classes

◆ ValidCostFunction()

bool ValidCostFunction ( bool  valid)
inlineprivate

Definition at line 101 of file QMCFixedSampleLinearOptimizeBatched.h.

References qmcplusplus::app_log().

Referenced by QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run().

102  {
103  if (!valid)
104  app_log() << " Cost Function is Invalid. If this frequently, try reducing the step size of the line minimization "
105  "or reduce the number of cycles. "
106  << std::endl;
107  return valid;
108  }
std::ostream & app_log()
Definition: OutputManager.h:65

Member Data Documentation

◆ accept_history

std::bitset<2> accept_history
private

accept history, remember the last 2 iterations, value 00, 01, 10, 11

Definition at line 196 of file QMCFixedSampleLinearOptimizeBatched.h.

Referenced by QMCFixedSampleLinearOptimizeBatched::one_shift_run().

◆ bestShift_i

RealType bestShift_i
private

◆ bestShift_s

RealType bestShift_s
private

◆ bigChange

◆ build_olv_ham_timer_

NewTimer& build_olv_ham_timer_
private

◆ cost_function_timer_

NewTimer& cost_function_timer_
private

◆ descentEngineObj

std::unique_ptr<DescentEngine> descentEngineObj
private

◆ do_output_matrices_csv_

◆ do_output_matrices_hdf_

◆ doGradientTest

◆ eigensolver_

std::string eigensolver_
private

◆ eigenvalue_timer_

◆ exp0

◆ freeze_parameters_

◆ generate_samples_timer_

NewTimer& generate_samples_timer_
private

◆ global_emi_

const std::optional<EstimatorManagerInput> global_emi_
private

This is retained in order to construct and reconstruct the vmcEngine.

Definition at line 302 of file QMCFixedSampleLinearOptimizeBatched.h.

Referenced by QMCFixedSampleLinearOptimizeBatched::processOptXML().

◆ hybridEngineObj

◆ initialize_timer_

NewTimer& initialize_timer_
private

◆ invert_olvmat_timer_

NewTimer& invert_olvmat_timer_
private

Definition at line 282 of file QMCFixedSampleLinearOptimizeBatched.h.

◆ line_min_timer_

◆ m_param

◆ Max_iterations

int Max_iterations
private

Number of iterations maximum before generating new configurations.

Definition at line 178 of file QMCFixedSampleLinearOptimizeBatched.h.

Referenced by QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run(), and QMCFixedSampleLinearOptimizeBatched::QMCFixedSampleLinearOptimizeBatched().

◆ MinMethod

◆ nstabilizers

int nstabilizers
private

Number of iterations maximum before generating new configurations.

Definition at line 187 of file QMCFixedSampleLinearOptimizeBatched.h.

Referenced by QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run(), and QMCFixedSampleLinearOptimizeBatched::QMCFixedSampleLinearOptimizeBatched().

◆ objFuncWrapper_

◆ optdir

◆ options_LMY_

◆ optparam

◆ optTarget

◆ output_hamiltonian_

◆ output_matrices_initialized_

bool output_matrices_initialized_
private

◆ output_overlap_

◆ param_tol

◆ samples_

◆ shift_i_input

◆ shift_s_base

RealType shift_s_base
private

Shift_s adjustment base.

Definition at line 198 of file QMCFixedSampleLinearOptimizeBatched.h.

Referenced by QMCFixedSampleLinearOptimizeBatched::one_shift_run().

◆ shift_s_input

◆ stabilizerScale

◆ testEngineObj

◆ vmcdriver_input_

VMCDriverInput vmcdriver_input_
private

◆ vmcEngine

◆ wfNode

xmlNodePtr wfNode
private

The documentation for this class was generated from the following files: