QMCPACK
QMCFixedSampleLinearOptimize.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 /** @file QMCFixedSampleLinearOptimize.h
15  * @brief Definition of QMCDriver which performs VMC and optimization.
16  */
17 #ifndef QMCPLUSPLUS_QMCFSLINEAROPTIMIZATION_VMCSINGLE_H
18 #define QMCPLUSPLUS_QMCFSLINEAROPTIMIZATION_VMCSINGLE_H
19 
20 #include "Optimize/NRCOptimization.h"
21 #ifdef HAVE_LMY_ENGINE
22 #include "formic/utils/matrix.h"
23 #include "formic/utils/lmyengine/engine.h"
24 #endif
25 #include "QMCDrivers/QMCDriver.h"
28 #include "OutputMatrix.h"
29 #include "LinearMethod.h"
30 
31 namespace qmcplusplus
32 {
33 
34 class GradientTest;
35 
36 /** @ingroup QMCDrivers
37  * @brief Implements wave-function optimization
38  *
39  * Optimization by correlated sampling method with configurations
40  * generated from VMC.
41  */
42 
43 class QMCFixedSampleLinearOptimize : public QMCDriver, public LinearMethod, private NRCOptimization<QMCTraits::RealType>
44 {
45 public:
46  ///Constructor.
47  QMCFixedSampleLinearOptimize(const ProjectData& project_data,
49  TrialWaveFunction& psi,
50  QMCHamiltonian& h,
51  Communicate*);
52 
53  ///Destructor
55 
56  ///Run the Optimization algorithm.
57  bool run() override;
58  ///preprocess xml node
59  bool put(xmlNodePtr cur) override;
60  ///process xml node value (parameters for both VMC and OPT) for the actual optimization
61  bool processOptXML(xmlNodePtr cur, const std::string& vmcMove, bool reportH5);
62 
63  RealType Func(RealType dl) override;
64 
65  void setWaveFunctionNode(xmlNodePtr cur) { wfNode = cur; }
66 
68 
69 private:
70  inline bool ValidCostFunction(bool valid)
71  {
72  if (!valid)
73  app_log() << " Cost Function is Invalid. If this frequently, try reducing the step size of the line minimization "
74  "or reduce the number of cycles. "
75  << std::endl;
76  return valid;
77  }
78 
79  // check if the proposed new cost function value is the best available
80  bool is_best_cost(const int ii,
81  const std::vector<RealType>& cv,
82  const std::vector<double>& sh,
83  const RealType ic) const;
84 
85  // perform the adaptive three-shift update
87 
88  // perform the single-shift update, no sample regeneration
89  bool one_shift_run();
90 
91  // perform optimization using a gradient descent algorithm
92  bool descent_run();
93 
94 #ifdef HAVE_LMY_ENGINE
95  // use hybrid approach of descent and blocked linear method for optimization
96  bool hybrid_run();
97 #endif
98 
99  // Perform test of parameter gradients
100  bool test_run();
101 
102  std::unique_ptr<GradientTest> testEngineObj;
103 
104 
105  void solveShiftsWithoutLMYEngine(const std::vector<double>& shifts_i,
106  const std::vector<double>& shiffts_s,
107  std::vector<std::vector<RealType>>& parameterDirections);
108 
109 #ifdef HAVE_LMY_ENGINE
110  formic::VarDeps vdeps;
111  cqmc::engine::LMYEngine<ValueType>* EngineObj;
112 #endif
113 
114  //engine for running various gradient descent based algorithms for optimization
115  std::unique_ptr<DescentEngine> descentEngineObj;
116 
117  //engine for controlling a optimization using a hybrid combination of linear method and descent
118  std::unique_ptr<HybridEngine> hybridEngineObj;
119 
120  // prepare a vector of shifts to try
121  std::vector<double> prepare_shifts(const double central_shift) const;
122 
123  // previous update
124 #ifdef HAVE_LMY_ENGINE
125  std::vector<formic::ColVec<double>> previous_update;
126 #endif
127 
129  void print_cost_summary(const double si,
130  const double ss,
131  const RealType mc,
132  const RealType cv,
133  const int ind,
134  const int bi,
135  const bool gu);
136 
139  std::string StabilizerMethod;
141  /// number of previous steps to orthogonalize to.
142  int eigCG;
143  /// total number of cg steps per iterations
145  /// the previous best identity shift
147  /// the previous best overlap shift
149  /// current shift_i, shift_s input values
151  /// accept history, remember the last 2 iterations, value 00, 01, 10, 11
152  std::bitset<2> accept_history;
153  /// Shift_s adjustment base
155  /// number of shifts we will try
157  /// the maximum relative change in the cost function for the adaptive three-shift scheme
159  ///max amount a parameter may change relative to current wave function weight
161  /// the tolerance to cost function increases when choosing the best shift in the adaptive shift method
163  /// the shift_i value that the adaptive shift method should aim for
165  ///whether we are targeting an excited state
166  std::string targetExcitedStr;
167  ///whether we are targeting an excited state
169  ///whether we are doing block algorithm
170  std::string block_lmStr;
171  ///whether we are doing block algorithm
172  bool block_lm;
173  ///number of blocks used in block algorithm
174  int nblocks;
175  ///number of old updates kept
176  int nolds;
177  ///number of directions kept
178  int nkept;
179  ///number of samples to do in correlated sampling part
181  ///the shift to use when targeting an excited state
183  ///whether to do the first part of block lm
185  ///whether to do the second part of block lm
187  ///whether to do the third part of block lm
189 
190  //Variables for alternatives to linear method
191 
192  //name of the current optimization method, updated by processOptXML before run
193  std::string MinMethod;
194 
195  //type of the previous optimization method, updated by processOptXML before run
197 
198  //type of the current optimization method, updated by processOptXML before run
200 
201  //whether to use hybrid method
202  bool doHybrid;
203 
205 
206  // Output Hamiltonian and overlap matrices
208 
209  // Flag to open the files on first pass and print header line
211 
214 
215  // Freeze variational parameters. Do not update them during each step.
217 
218  std::vector<RealType> optdir, optparam;
219  ///total number of VMC walkers
221  ///Number of iterations maximum before generating new configurations.
223  ///target cost function to optimize
224  std::unique_ptr<QMCCostFunctionBase> optTarget;
225  ///vmc engine
226  std::unique_ptr<QMCDriver> vmcEngine;
227  ///xml node to be dumped
228  xmlNodePtr wfNode;
229 
231 
232  ///common operation to start optimization, used by the derived classes
233  void start();
234 #ifdef HAVE_LMY_ENGINE
235  void engine_start(cqmc::engine::LMYEngine<ValueType>* EngineObj,
237  std::string MinMethod);
238 #endif
239  ///common operation to finish optimization, used by the derived classes
240  void finish();
241  void generateSamples();
242 
250 };
251 } // namespace qmcplusplus
252 #endif
bool targetExcited
whether we are targeting an excited state
std::unique_ptr< QMCDriver > vmcEngine
vmc engine
bool block_first
whether to do the first part of block lm
bool run() override
Run the Optimization algorithm.
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
RealType max_param_change
max amount a parameter may change relative to current wave function weight
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool block_lm
whether we are doing block algorithm
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
std::ostream & app_log()
Definition: OutputManager.h:65
LinearMethod related functions.
class ProjectData
Definition: ProjectData.h:36
RealType max_relative_cost_change
the maximum relative change in the cost function for the adaptive three-shift scheme ...
bool block_second
whether to do the second part of block lm
Collection of Local Energy Operators.
int eigCG
number of previous steps to orthogonalize to.
int nsamp_comp
number of samples to do in correlated sampling part
Timer accumulates time and call counts.
Definition: NewTimer.h:135
std::string block_lmStr
whether we are doing block algorithm
RealType bestShift_i
the previous best identity shift
abstract base class for QMC engines
Definition: QMCDriver.h:71
bool block_third
whether to do the third part of block lm
int TotalCGSteps
total number of cg steps per iterations
RealType shift_i_input
current shift_i, shift_s input values
Wrapping information on parallelism.
Definition: Communicate.h:68
Declaration of QMCDriver.
std::vector< double > prepare_shifts(const double central_shift) const
void start()
common operation to start optimization, used by the derived classes
void solveShiftsWithoutLMYEngine(const std::vector< double > &shifts_i, const std::vector< double > &shiffts_s, std::vector< std::vector< RealType >> &parameterDirections)
RealType omega_shift
the shift to use when targeting an excited state
bool processOptXML(xmlNodePtr cur, const std::string &vmcMove, bool reportH5)
process xml node value (parameters for both VMC and OPT) for the actual optimization ...
Class to represent a many-body trial wave function.
RealType cost_increase_tol
the tolerance to cost function increases when choosing the best shift in the adaptive shift method ...
std::string targetExcitedStr
whether we are targeting an excited state
int nblocks
number of blocks used in block algorithm
RealType target_shift_i
the shift_i value that the adaptive shift method should aim for
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)
bool put(xmlNodePtr cur) override
preprocess xml node
int Max_iterations
Number of iterations maximum before generating new configurations.
Implements wave-function optimization.
Output the Hamiltonian and overlap matrices from linear method.
RealType bestShift_s
the previous best overlap shift
QMCFixedSampleLinearOptimize(const ProjectData &project_data, MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, Communicate *)
Constructor.
bool is_best_cost(const int ii, const std::vector< RealType > &cv, const std::vector< double > &sh, const RealType ic) const