QMCPACK
DescentEngine.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source
3 // License.
4 // See LICENSE file in top directory for details.
5 //
6 // Copyright (c) 2019 QMCPACK developers.
7 //
8 // File developed by: Leon Otis, leon_otis@berkeley.edu University, University
9 // of California Berkeley
10 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
11 //
12 // File created by: Leon Otis, leon_otis@berkeley.edu University, University of
13 // California Berkeley
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 #ifndef QMCPLUSPLUS_DESCENT_ENGINE_HEADER
17 #define QMCPLUSPLUS_DESCENT_ENGINE_HEADER
18 
19 #include "Configuration.h"
20 #include "Message/Communicate.h"
21 #include "VariableSet.h"
22 #include <libxml/tree.h>
23 #include <vector>
24 
25 namespace qmcplusplus
26 {
28 {
33 
34 private:
35  ///Vector for local energy parameter derivatives
36  std::vector<FullPrecValueType> avg_le_der_samp_;
37  ///Vector for local energy parameter derivatives on one thread
38  std::vector<std::vector<FullPrecValueType>> replica_le_der_samp_;
39 
40  ///Vector for WF parameter derivatives
41  std::vector<FullPrecValueType> avg_der_rat_samp_;
42  ///Vector for WF parameter derivatives on one thread
43  std::vector<std::vector<FullPrecValueType>> replica_der_rat_samp_;
44 
45  ///Vector for target function numerator parameter derivatives
46  std::vector<FullPrecValueType> avg_numer_der_samp_;
47  ///Vector for target function numerator parameter derivatives on one thread
48  std::vector<std::vector<FullPrecValueType>> replica_numer_der_samp_;
49 
50  ///Vector for target function denominator parameter derivatives
51  std::vector<FullPrecValueType> avg_denom_der_samp_;
52  ///Vector for target function denominator parameter derivatives on one thread
53  std::vector<std::vector<FullPrecValueType>> replica_denom_der_samp_;
54 
55  ///Total sum of weights
57 
58  ///Average energy on a descent step
60  ///Variance of the energy
62  ///Standard deviation of the energy
64  ///Standard error of the energy
66 
67  ///Average target function numerator on a descent step
69  ///Variance of the target function numerator
71  ///Standard error of the target function numerator
73 
74  ///Average target function denominator on a descent step
76  ///Variance of the target function denominator
78  ///Standard error of the target function denominator
80 
81  ///Average target function value on a descent step
83  ///Variance of the target function
85  ///Standard error of the target function
87 
88  /// history of sampled |value/guiding|^2 ratios for one iteration
89  std::vector<ValueType> vg_history_;
90  std::vector<std::vector<ValueType>> replica_vg_history_;
91 
92  /// history of sampled |value/guiding|^2 ratios during the descent
93  /// finalization phase
94  std::vector<ValueType> final_vg_history_;
95  std::vector<std::vector<ValueType>> replica_final_vg_history_;
96 
97 
98  /// history of sampled configuration weights for one iteration
99  std::vector<ValueType> w_history_;
100  std::vector<std::vector<ValueType>> replica_w_history_;
101  /// history of sampled configuration weights during descent finalization phase
102  std::vector<ValueType> final_w_history_;
103  std::vector<std::vector<ValueType>> replica_final_w_history_;
104 
105  /// a history of sampled local energies times the |value/guiding|^2 raitos for
106  /// one iteration
107  std::vector<ValueType> lev_history_;
108  std::vector<std::vector<ValueType>> replica_lev_history_;
109 
110  /// history of sampled local energies times the |value/guiding|^2 raitos during
111  /// the descent finalization phase
112  std::vector<ValueType> final_lev_history_;
113  std::vector<std::vector<ValueType>> replica_final_lev_history_;
114 
115  /// a vector to store the averages of the energy during the descent
116  /// finalization phase
117  std::vector<ValueType> final_le_avg_history_;
118 
119  /// a vector to store the variances of the energy during the descent
120  /// finalization phase
121  std::vector<ValueType> final_var_avg_history_;
122 
123  /// a history of target function numerator times the |value/guiding|^2 ratios
124  /// for one iteration
125  std::vector<ValueType> tnv_history_;
126  std::vector<std::vector<ValueType>> replica_tnv_history_;
127 
128  /// a history of target function numerator times the |value/guiding|^2 ratios
129  /// during the descent finalization phase
130  std::vector<ValueType> final_tnv_history_;
131  std::vector<std::vector<ValueType>> replica_final_tnv_history_;
132 
133  /// a history of target function denominator times the |value/guiding|^2 ratios
134  /// for one iteration
135  std::vector<ValueType> tdv_history_;
136  std::vector<std::vector<ValueType>> replica_tdv_history_;
137  /// a history of target function denomerator times the |value/guiding|^2 ratios
138  /// during the descent finalization phase
139  std::vector<ValueType> final_tdv_history_;
140  std::vector<std::vector<ValueType>> replica_final_tdv_history_;
141 
142  /// a vector to store the averages of the target function during the descent
143  /// finalization phase
144  std::vector<ValueType> final_tar_avg_history_;
145  /// a vector to store the variances of the target function during the descent
146  /// finalization phase
147  std::vector<ValueType> final_tar_var_history_;
148 
149  /// Vector that stores the final averaged derivatives of the cost function
150  std::vector<ValueType> lderivs_;
151 
152  /// Communicator handles MPI reduction
154 
155  /// Whether to target excited state
157 
158  /// Number of optimizable parameters
160 
161  /// Vector for storing parameter values from previous optimization step
162  std::vector<ValueType> params_copy_;
163 
164  /// Vector for storing parameter values for current optimization step
165  std::vector<ValueType> current_params_;
166 
167  /// Vector for storing Lagrangian derivatives from previous optimization steps
168  std::vector<std::vector<ValueType>> deriv_records_;
169 
170  /// Vector for storing step size denominator values from previous optimization
171  /// step
172  std::vector<ValueType> denom_records_;
173 
174  /// Vector for storing step size numerator values from previous optimization
175  /// step
176  std::vector<ValueType> numer_records_;
177 
178  /// Parameter for accelerated descent recursion relation
180  /// Vector for storing step sizes from previous optimization step.
181  std::vector<ValueType> taus_;
182  /// Vector for storing running average of squares of the derivatives
183  std::vector<ValueType> derivs_squared_;
184 
185  /// Integer for keeping track of only number of descent steps taken
187 
188  /// What variety of gradient descent will be used
189  std::string flavor_;
190 
191  /// Step sizes for different types of parameters
198 
199  /// Whether to gradually ramp up step sizes in descent
200  bool ramp_eta_;
201 
202  /// Number of steps over which to ramp up step size
204 
205  /// Number of parameter difference vectors stored when descent is used in a
206  /// hybrid optimization
208 
209  /// Counter of how many vectors have been stored so far
211 
212  /// Vectors of parameter names and types, used in the assignment of step sizes
213  std::vector<std::string> engine_param_names_;
214  std::vector<int> engine_param_types_;
215 
216  /// Vector for storing parameter values for calculating differences to be given
217  /// to hybrid method
218  std::vector<ValueType> params_for_diff_;
219 
220  /// Vector for storing the input vectors to the BLM steps of hybrid method
221  std::vector<std::vector<ValueType>> hybrid_blm_input_;
222 
223  /// Value of omega in excited state functional
225 
226  /// Iteration to start collecting samples for final average and error blocking
227  /// analysis
229 
230  /// Iteration to start computing averages and errors from the stored values
231  /// during the finalization phase
233 
234  /// Whether to start collecting samples for the histories in the finalization
235  /// phase
236  bool collect_count_ = false;
237 
238  /// Counter for the number of descent steps taken in the finalization phase
240 
241  /// Whether to print out derivative terms for each parameter
242  std::string print_deriv_;
243 
244 public:
245  /// Constructor for engine
246  DescentEngine(Communicate* comm, const xmlNodePtr cur);
247 
248  /// process xml node
249  bool processXML(const xmlNodePtr cur);
250 
251  /// Prepare for taking samples
252  void prepareStorage(const int num_replicas, const int num_optimizables);
253 
254  ////////////////////////////////////////////////////////////////////////////////////////////////////////
255  /// \brief Function that Take Sample Data from the Host Code
256  ///
257  /// \param[in] der_rat_samp <n|Psi_i>/<n|Psi> (i = 0 (|Psi>), 1, ... N_var
258  /// )
259  /// \param[in] le_der_samp <n|H|Psi_i>/<n|Psi> (i = 0 (|Psi>), 1, ...
260  /// N_var )
261  /// \param[in] ls_der_samp <|S^2|Psi_i>/<n|Psi> (i = 0 (|Psi>), 1, ...
262  /// N_var )
263  /// \param[in] vgs_samp |<n|value_fn>/<n|guiding_fn>|^2
264  /// \param[in] weight_samp weight for this sample
265  ///
266  ////////////////////////////////////////////////////////////////////////////////////////////////////////
267  void takeSample(const int replica_id,
268  const std::vector<FullPrecValueType>& der_rat_samp,
269  const std::vector<FullPrecValueType>& le_der_samp,
270  const std::vector<FullPrecValueType>& ls_der_samp,
271  ValueType vgs_samp,
272  ValueType weight_samp);
273 
274  ////////////////////////////////////////////////////////////////////////////////////////////////////////
275  /// \brief Function that reduces all vector information from all processors
276  /// to the root
277  /// processor
278  ///
279  ////////////////////////////////////////////////////////////////////////////////////////////////////////
280  void sample_finish();
281 
282  /// Function for computing ratios of the form <f>/<g> as well as the associated
283  /// variance and standard error
284  void mpi_unbiased_ratio_of_means(int numSamples,
285  std::vector<ValueType>& weights,
286  std::vector<ValueType>& numerSamples,
287  std::vector<ValueType>& denomSamples,
288  ValueType& mean,
289  ValueType& variance,
290  ValueType& stdErr);
291 
292  /// Returns the derivatives of the cost function we are minimizing
293  const std::vector<ValueType>& getAveragedDerivatives() const { return lderivs_; }
294 
295  /// helper method for updating parameter values with descent
296  void updateParameters();
297 
298  /// helper method for seting step sizes for different parameter types in
299  /// descent optimization
300  ValueType setStepSize(int i);
301 
302  /// stores derivatives so they can be used in accelerated descent algorithm on
303  /// later iterations
304  void storeDerivRecord() { deriv_records_.push_back(lderivs_); }
305 
306  /// helper method for transferring information on parameter names and types to
307  /// the engine
308  void setupUpdate(const optimize::VariableSet& my_vars);
309 
310  /// Store a vector of parameter differences to be used by the BLM in a hybrid
311  /// optimization
312  void storeVectors(std::vector<ValueType>& current_params);
313 
314  /// Compute uncertainties for energy/target function and variance over a
315  /// history of samples from a set of iterations
316  void computeFinalizationUncertainties(std::vector<ValueType>& weights,
317  std::vector<ValueType>& numerSamples,
318  std::vector<ValueType>& denomSamples);
319 
320  /// Returns number of times a parameter difference vector will be stored in the
321  /// optimization
322  int retrieveStoreFrequency() const { return store_num_; }
323 
324  /// Returns the set of stored parameter difference vectors that will be given
325  /// to the BLM
326  const std::vector<std::vector<ValueType>>& retrieveHybridBLM_Input() const { return hybrid_blm_input_; }
327 
328  /// Returns the current set of parameter values
329  const std::vector<ValueType>& retrieveNewParams() const { return current_params_; }
330 
331  /// Returns number of optimization steps that have been taken with descent
332  int getDescentNum() const { return descent_num_; }
333 
334  /// Returns current value of omega
335  ValueType getOmega() const { return omega_; }
336 
337  /// Returns value of average energy
338  ValueType getEnergy() const { return e_avg_; }
339 
340  /// Returns variance of the energy
341  ValueType getVariance() const { return e_var_; }
342 
343  /// Returns standard deviation of energy
344  ValueType getSD() const { return e_sd_; }
345 
346  /// Returns whether an excited state is being targeted
347  bool targetingExcited() const { return engine_target_excited_; }
348 
349  /// Returns the descent iteration number when on a finalizing descent section
350  int getFinalDescentNum() const { return final_descent_num_; }
351 
352  /// Resets the number of vectors stored to 0 for next hybrid method
353  /// macro-iteration
355 
356  /// Function for setting averaged derivatives, currently only used as part of a
357  /// unit test of the engine's parameter update
358  void setDerivs(std::vector<ValueType>& test_derivs) { lderivs_ = test_derivs; }
359 
360  /// Function for setting parameter value, used to keep descent parameter values
361  /// up to date with changes that occur on BLM steps of hybrid method
362  void setParamVal(int index, ValueType value) { current_params_[index] = value; }
363 };
364 
365 } // namespace qmcplusplus
366 #endif
int num_params_
Number of optimizable parameters.
const std::vector< ValueType > & getAveragedDerivatives() const
Returns the derivatives of the cost function we are minimizing.
std::vector< std::vector< ValueType > > deriv_records_
Vector for storing Lagrangian derivatives from previous optimization steps.
DescentEngine(Communicate *comm, const xmlNodePtr cur)
Constructor for engine.
int getDescentNum() const
Returns number of optimization steps that have been taken with descent.
Communicate * my_comm_
Communicator handles MPI reduction.
std::vector< std::vector< ValueType > > replica_final_tnv_history_
std::vector< ValueType > final_tar_var_history_
a vector to store the variances of the target function during the descent finalization phase ...
std::vector< ValueType > final_tdv_history_
a history of target function denomerator times the |value/guiding|^2 ratios during the descent finali...
int store_count_
Counter of how many vectors have been stored so far.
ValueType getOmega() const
Returns current value of omega.
std::vector< ValueType > final_vg_history_
history of sampled |value/guiding|^2 ratios during the descent finalization phase ...
Definition: DescentEngine.h:94
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
ValueType numer_err_
Standard error of the target function numerator.
Definition: DescentEngine.h:72
int final_descent_num_
Counter for the number of descent steps taken in the finalization phase.
std::vector< ValueType > tnv_history_
a history of target function numerator times the |value/guiding|^2 ratios for one iteration ...
int ramp_num_
Number of steps over which to ramp up step size.
std::vector< ValueType > denom_records_
Vector for storing step size denominator values from previous optimization step.
QTBase::RealType RealType
Definition: Configuration.h:58
void sample_finish()
Function that reduces all vector information from all processors to the root processor.
qmcplusplus::QMCTraits::RealType RealType
Definition: DescentEngine.h:31
std::vector< ValueType > taus_
Vector for storing step sizes from previous optimization step.
ValueType tjf_2body_eta_
Step sizes for different types of parameters.
std::vector< std::vector< FullPrecValueType > > replica_denom_der_samp_
Vector for target function denominator parameter derivatives on one thread.
Definition: DescentEngine.h:53
std::vector< ValueType > vg_history_
history of sampled |value/guiding|^2 ratios for one iteration
Definition: DescentEngine.h:89
int store_num_
Number of parameter difference vectors stored when descent is used in a hybrid optimization.
ValueType target_avg_
Average target function value on a descent step.
Definition: DescentEngine.h:82
std::vector< std::vector< ValueType > > replica_final_w_history_
ValueType denom_avg_
Average target function denominator on a descent step.
Definition: DescentEngine.h:75
void setupUpdate(const optimize::VariableSet &my_vars)
helper method for transferring information on parameter names and types to the engine ...
std::vector< ValueType > final_le_avg_history_
a vector to store the averages of the energy during the descent finalization phase ...
std::vector< std::vector< FullPrecValueType > > replica_numer_der_samp_
Vector for target function numerator parameter derivatives on one thread.
Definition: DescentEngine.h:48
int collection_step_
Iteration to start collecting samples for final average and error blocking analysis.
ValueType denom_err_
Standard error of the target function denominator.
Definition: DescentEngine.h:79
std::vector< ValueType > tdv_history_
a history of target function denominator times the |value/guiding|^2 ratios for one iteration ...
std::vector< int > engine_param_types_
void updateParameters()
helper method for updating parameter values with descent
void resetStorageCount()
Resets the number of vectors stored to 0 for next hybrid method macro-iteration.
std::vector< FullPrecValueType > avg_le_der_samp_
Vector for local energy parameter derivatives.
Definition: DescentEngine.h:36
std::vector< std::vector< ValueType > > hybrid_blm_input_
Vector for storing the input vectors to the BLM steps of hybrid method.
ValueType getEnergy() const
Returns value of average energy.
void prepareStorage(const int num_replicas, const int num_optimizables)
Prepare for taking samples.
ValueType e_err_
Standard error of the energy.
Definition: DescentEngine.h:65
std::vector< FullPrecValueType > avg_denom_der_samp_
Vector for target function denominator parameter derivatives.
Definition: DescentEngine.h:51
std::vector< FullPrecValueType > avg_numer_der_samp_
Vector for target function numerator parameter derivatives.
Definition: DescentEngine.h:46
std::vector< FullPrecValueType > avg_der_rat_samp_
Vector for WF parameter derivatives.
Definition: DescentEngine.h:41
bool engine_target_excited_
Whether to target excited state.
std::vector< std::vector< ValueType > > replica_final_lev_history_
Wrapping information on parallelism.
Definition: Communicate.h:68
ValueType denom_var_
Variance of the target function denominator.
Definition: DescentEngine.h:77
std::vector< std::vector< ValueType > > replica_tnv_history_
std::vector< ValueType > lderivs_
Vector that stores the final averaged derivatives of the cost function.
int compute_step_
Iteration to start computing averages and errors from the stored values during the finalization phase...
std::vector< ValueType > w_history_
history of sampled configuration weights for one iteration
Definition: DescentEngine.h:99
std::vector< std::vector< ValueType > > replica_lev_history_
QTFull::ValueType FullPrecValueType
Definition: Configuration.h:67
std::vector< std::string > engine_param_names_
Vectors of parameter names and types, used in the assignment of step sizes.
std::vector< ValueType > final_w_history_
history of sampled configuration weights during descent finalization phase
QTBase::ValueType ValueType
Definition: Configuration.h:60
std::vector< std::vector< ValueType > > replica_tdv_history_
ValueType setStepSize(int i)
helper method for seting step sizes for different parameter types in descent optimization ...
ValueType e_sd_
Standard deviation of the energy.
Definition: DescentEngine.h:63
std::vector< ValueType > params_copy_
Vector for storing parameter values from previous optimization step.
std::vector< ValueType > current_params_
Vector for storing parameter values for current optimization step.
const std::vector< std::vector< ValueType > > & retrieveHybridBLM_Input() const
Returns the set of stored parameter difference vectors that will be given to the BLM.
const std::vector< ValueType > & retrieveNewParams() const
Returns the current set of parameter values.
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
ValueType target_var_
Variance of the target function.
Definition: DescentEngine.h:84
void computeFinalizationUncertainties(std::vector< ValueType > &weights, std::vector< ValueType > &numerSamples, std::vector< ValueType > &denomSamples)
Compute uncertainties for energy/target function and variance over a history of samples from a set of...
std::vector< ValueType > final_var_avg_history_
a vector to store the variances of the energy during the descent finalization phase ...
ValueType getVariance() const
Returns variance of the energy.
std::vector< ValueType > final_tar_avg_history_
a vector to store the averages of the target function during the descent finalization phase ...
qmcplusplus::QMCTraits::FullPrecRealType FullPrecRealType
Definition: DescentEngine.h:32
ValueType w_sum_
Total sum of weights.
Definition: DescentEngine.h:56
void storeDerivRecord()
stores derivatives so they can be used in accelerated descent algorithm on later iterations ...
bool processXML(const xmlNodePtr cur)
process xml node
ValueType numer_avg_
Average target function numerator on a descent step.
Definition: DescentEngine.h:68
bool targetingExcited() const
Returns whether an excited state is being targeted.
ValueType e_avg_
Average energy on a descent step.
Definition: DescentEngine.h:59
qmcplusplus::QMCTraits::ValueType ValueType
Definition: DescentEngine.h:30
std::vector< ValueType > final_lev_history_
history of sampled local energies times the |value/guiding|^2 raitos during the descent finalization ...
ValueType omega_
Value of omega in excited state functional.
bool ramp_eta_
Whether to gradually ramp up step sizes in descent.
std::vector< std::vector< FullPrecValueType > > replica_le_der_samp_
Vector for local energy parameter derivatives on one thread.
Definition: DescentEngine.h:38
std::vector< ValueType > derivs_squared_
Vector for storing running average of squares of the derivatives.
std::vector< ValueType > params_for_diff_
Vector for storing parameter values for calculating differences to be given to hybrid method...
int descent_num_
Integer for keeping track of only number of descent steps taken.
LatticeGaussianProduct::ValueType ValueType
int getFinalDescentNum() const
Returns the descent iteration number when on a finalizing descent section.
ValueType getSD() const
Returns standard deviation of energy.
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
bool collect_count_
Whether to start collecting samples for the histories in the finalization phase.
void mpi_unbiased_ratio_of_means(int numSamples, std::vector< ValueType > &weights, std::vector< ValueType > &numerSamples, std::vector< ValueType > &denomSamples, ValueType &mean, ValueType &variance, ValueType &stdErr)
Function for computing ratios of the form <f>/<g> as well as the associated variance and standard err...
ValueType lambda_
Parameter for accelerated descent recursion relation.
std::vector< std::vector< FullPrecValueType > > replica_der_rat_samp_
Vector for WF parameter derivatives on one thread.
Definition: DescentEngine.h:43
void storeVectors(std::vector< ValueType > &current_params)
Store a vector of parameter differences to be used by the BLM in a hybrid optimization.
std::vector< std::vector< ValueType > > replica_final_vg_history_
Definition: DescentEngine.h:95
std::vector< std::vector< ValueType > > replica_final_tdv_history_
qmcplusplus::QMCTraits::FullPrecValueType FullPrecValueType
Definition: DescentEngine.h:29
std::string flavor_
What variety of gradient descent will be used.
std::vector< ValueType > lev_history_
a history of sampled local energies times the |value/guiding|^2 raitos for one iteration ...
ValueType e_var_
Variance of the energy.
Definition: DescentEngine.h:61
void takeSample(const int replica_id, const std::vector< FullPrecValueType > &der_rat_samp, const std::vector< FullPrecValueType > &le_der_samp, const std::vector< FullPrecValueType > &ls_der_samp, ValueType vgs_samp, ValueType weight_samp)
Function that Take Sample Data from the Host Code.
void setDerivs(std::vector< ValueType > &test_derivs)
Function for setting averaged derivatives, currently only used as part of a unit test of the engine&#39;s...
std::string print_deriv_
Whether to print out derivative terms for each parameter.
ACC::value_type mean(const ACC &ac)
Definition: accumulators.h:147
std::vector< ValueType > numer_records_
Vector for storing step size numerator values from previous optimization step.
std::vector< std::vector< ValueType > > replica_w_history_
int retrieveStoreFrequency() const
Returns number of times a parameter difference vector will be stored in the optimization.
ValueType target_err_
Standard error of the target function.
Definition: DescentEngine.h:86
std::vector< std::vector< ValueType > > replica_vg_history_
Definition: DescentEngine.h:90
ValueType numer_var_
Variance of the target function numerator.
Definition: DescentEngine.h:70
std::vector< ValueType > final_tnv_history_
a history of target function numerator times the |value/guiding|^2 ratios during the descent finaliza...
void setParamVal(int index, ValueType value)
Function for setting parameter value, used to keep descent parameter values up to date with changes t...