QMCPACK
VariableSet.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: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef QMCPLUSPLUS_OPTIMIZE_VARIABLESET_H
17 #define QMCPLUSPLUS_OPTIMIZE_VARIABLESET_H
18 #include "config.h"
19 #include <map>
20 #include <vector>
21 #include <iostream>
22 #include <complex>
23 #include "Configuration.h"
24 
25 namespace qmcplusplus
26 {
27 class hdf_archive;
28 }
29 
30 namespace optimize
31 {
32 /** An enum useful for determining the type of parameter is being optimized.
33 * knowing this in the opt routine can reduce the computational load.
34 */
35 enum
36 {
37  OTHER_P = 0,
38  LOGLINEAR_P, //B-spline Jastrows
39  LOGLINEAR_K, //K space Jastrows
40  LINEAR_P, //Multi-determinant coefficients
41  SPO_P, //SPO set Parameters
42  BACKFLOW_P //Backflow parameters
43 };
44 
45 /** class to handle a set of variables that can be modified during optimizations
46  *
47  * A serialized container of named variables.
48  */
50 {
52 
53  using pair_type = std::pair<std::string, real_type>;
54  using index_pair_type = std::pair<std::string, int>;
55  using iterator = std::vector<pair_type>::iterator;
56  using const_iterator = std::vector<pair_type>::const_iterator;
57  using size_type = std::vector<pair_type>::size_type;
58 
59  ///number of active variables
61  /** store locator of the named variable
62  *
63  * if(Index[i] == -1), the named variable is not active
64  */
65  std::vector<int> Index;
66  std::vector<pair_type> NameAndValue;
67  std::vector<index_pair_type> ParameterType;
68  std::vector<index_pair_type> Recompute;
69 
70  ///default constructor
71  inline VariableSet() : num_active_vars(0) {}
72  ///viturval destructor for safety
73  virtual ~VariableSet() = default;
74  /** if any of Index value is not zero, return true
75  */
76  inline bool is_optimizable() const { return num_active_vars > 0; }
77  ///return the number of active variables
78  inline int size_of_active() const { return num_active_vars; }
79  ///return the first const_iterator
80  inline const_iterator begin() const { return NameAndValue.begin(); }
81  ///return the last const_iterator
82  inline const_iterator end() const { return NameAndValue.end(); }
83  ///return the first iterator
84  inline iterator begin() { return NameAndValue.begin(); }
85  ///return the last iterator
86  inline iterator end() { return NameAndValue.end(); }
87  ///return the size
88  inline size_type size() const { return NameAndValue.size(); }
89  ///return the locator of the i-th Index
90  inline int where(int i) const { return Index[i]; }
91  /** return the iterator of a named parameter
92  * @param vname name of a parameter
93  * @return the locator of vname
94  *
95  * If vname is not found among the Names, return NameAndValue.end()
96  * so that ::end() member function can be used to validate the iterator.
97  */
98  inline iterator find(const std::string& vname)
99  {
100  return std::find_if(NameAndValue.begin(), NameAndValue.end(),
101  [&vname](const auto& value) { return value.first == vname; });
102  }
103 
104  /** return the Index vaule for the named parameter
105  * @param vname name of the variable
106  *
107  * If vname is not found in this variables, return -1;
108  */
109  int getIndex(const std::string& vname) const;
110 
111  /* return the NameAndValue index for the named parameter
112  * @ param vname name of the variable
113  *
114  * Differs from getIndex by not relying on the indices cached in Index
115  * myVars[i] will always return the value of the parameter if it is stored
116  * regardless of whether or not the Index array has been correctly reset
117  *
118  * if vname is not found, return -1
119  *
120  */
121  inline int getLoc(const std::string& vname) const
122  {
123  int loc = 0;
124  while (loc != NameAndValue.size())
125  {
126  if (NameAndValue[loc].first == vname)
127  return loc;
128  ++loc;
129  }
130  return -1;
131  }
132 
133  inline void insert(const std::string& vname, real_type v, bool enable = true, int type = OTHER_P)
134  {
135  iterator loc = find(vname);
136  int ind_loc = loc - NameAndValue.begin();
137  if (loc == NameAndValue.end()) // && enable==true)
138  {
139  Index.push_back(ind_loc);
140  NameAndValue.push_back(pair_type(vname, v));
141  ParameterType.push_back(index_pair_type(vname, type));
142  Recompute.push_back(index_pair_type(vname, 1));
143  }
144  //disable it if enable == false
145  if (!enable)
146  Index[ind_loc] = -1;
147  }
148 
149  inline void setParameterType(int type)
150  {
151  std::vector<index_pair_type>::iterator PTit(ParameterType.begin()), PTend(ParameterType.end());
152  while (PTit != PTend)
153  {
154  (*PTit).second = type;
155  PTit++;
156  }
157  }
158 
159  inline void getParameterTypeList(std::vector<int>& types) const
160  {
161  auto ptit(ParameterType.begin()), ptend(ParameterType.end());
162  types.resize(ptend - ptit);
163  auto tit(types.begin());
164  while (ptit != ptend)
165  (*tit++) = (*ptit++).second;
166  }
167 
168 
169  /** equivalent to std::map<std::string,T>[string] operator
170  */
171  inline real_type& operator[](const std::string& vname)
172  {
173  iterator loc = find(vname);
174  if (loc == NameAndValue.end())
175  {
176  Index.push_back(-1);
177  NameAndValue.push_back(pair_type(vname, 0));
178  ParameterType.push_back(index_pair_type(vname, 0));
179  Recompute.push_back(index_pair_type(vname, 1));
180  return NameAndValue.back().second;
181  }
182  return (*loc).second;
183  }
184 
185 
186  /** return the name of i-th variable
187  * @param i index
188  */
189  const std::string& name(int i) const { return NameAndValue[i].first; }
190 
191  /** return the i-th value
192  * @param i index
193  */
194  inline real_type operator[](int i) const { return NameAndValue[i].second; }
195 
196  /** assign the i-th value
197  * @param i index
198  */
199  inline real_type& operator[](int i) { return NameAndValue[i].second; }
200 
201  /** get the i-th parameter's type
202  * @param i index
203  */
204  inline int getType(int i) const { return ParameterType[i].second; }
205 
206  inline bool recompute(int i) const { return (Recompute[i].second == 1); }
207 
208  inline int& recompute(int i) { return Recompute[i].second; }
209 
210  inline void setComputed()
211  {
212  for (int i = 0; i < Recompute.size(); i++)
213  {
214  if (ParameterType[i].second == LOGLINEAR_P)
215  Recompute[i].second = 0;
216  else if (ParameterType[i].second == LOGLINEAR_K)
217  Recompute[i].second = 0;
218  else
219  Recompute[i].second = 1;
220  }
221  }
222 
223  inline void setRecompute()
224  {
225  for (int i = 0; i < Recompute.size(); i++)
226  Recompute[i].second = 1;
227  }
228 
229  /** clear the variable set
230  *
231  * Remove all the data.
232  */
233  void clear();
234 
235  /** insert a VariableSet to the list
236  * @param input variables
237  */
238  void insertFrom(const VariableSet& input);
239 
240  /** sum together the values of the optimizable parameter values in
241  * two VariableSet objects, and set this object's values to equal them.
242  * @param first set of input variables
243  * @param second set of input variables
244  */
245  void insertFromSum(const VariableSet& input_1, const VariableSet& input_2);
246 
247  /** take the difference (input_1-input_2) of values of the optimizable
248  * parameter values in two VariableSet objects, and set this object's
249  * values to equal them.
250  * @param first set of input variables
251  * @param second set of input variables
252  */
253  void insertFromDiff(const VariableSet& input_1, const VariableSet& input_2);
254 
255  /** activate variables for optimization
256  * @param first iterator of the first name
257  * @param last iterator of the last name
258  * @param reindex if true, Index is updated
259  *
260  * The status of a variable that is not included in the [first,last)
261  * remains the same.
262  */
263  template<typename ForwardIterator>
264  void activate(ForwardIterator first, ForwardIterator last, bool reindex)
265  {
266  while (first != last)
267  {
268  iterator loc = find(*first++);
269  if (loc != NameAndValue.end())
270  {
271  int i = loc - NameAndValue.begin();
272  if (Index[i] < 0)
273  Index[i] = num_active_vars++;
274  }
275  }
276  if (reindex)
277  {
278  removeInactive();
279  resetIndex();
280  }
281  }
282 
283  /** deactivate variables for optimization
284  * @param first iterator of the first name
285  * @param last iterator of the last name
286  * @param reindex if true, the variales are removed and Index is updated
287  */
288  template<typename ForwardIterator>
289  void disable(ForwardIterator first, ForwardIterator last, bool reindex)
290  {
291  while (first != last)
292  {
293  int loc = find(*first++) - NameAndValue.begin();
294  if (loc < NameAndValue.size())
295  Index[loc] = -1;
296  }
297  if (reindex)
298  {
299  removeInactive();
300  resetIndex();
301  }
302  }
303 
304  /** reset Index
305  */
306  void resetIndex();
307  /** remove inactive variables and trim the internal data
308  */
309  void removeInactive();
310 
311  /** set the index table of this VariableSet
312  * @param selected input variables
313  *
314  * This VariableSet is a subset of selected.
315  */
316  void getIndex(const VariableSet& selected);
317 
318  /** set default Indices, namely all the variables are active
319  */
320  void setIndexDefault();
321 
322  void print(std::ostream& os, int leftPadSpaces = 0, bool printHeader = false) const;
323 
324  // Save variational parameters to an HDF file
325  void writeToHDF(const std::string& filename, qmcplusplus::hdf_archive& hout) const;
326 
327  /// Read variational parameters from an HDF file.
328  /// This assumes VariableSet is already set up.
329  void readFromHDF(const std::string& filename, qmcplusplus::hdf_archive& hin);
330 };
331 } // namespace optimize
332 
333 #endif
iterator find(const std::string &vname)
return the iterator of a named parameter
Definition: VariableSet.h:98
bool recompute(int i) const
Definition: VariableSet.h:206
void writeToHDF(const std::string &filename, qmcplusplus::hdf_archive &hout) const
real_type & operator[](int i)
assign the i-th value
Definition: VariableSet.h:199
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void setIndexDefault()
set default Indices, namely all the variables are active
bool is_optimizable() const
if any of Index value is not zero, return true
Definition: VariableSet.h:76
QTBase::RealType RealType
Definition: Configuration.h:58
const_iterator end() const
return the last const_iterator
Definition: VariableSet.h:82
std::vector< pair_type >::iterator iterator
Definition: VariableSet.h:55
void readFromHDF(const std::string &filename, qmcplusplus::hdf_archive &hin)
Read variational parameters from an HDF file.
std::vector< int > Index
store locator of the named variable
Definition: VariableSet.h:65
virtual ~VariableSet()=default
viturval destructor for safety
class to handle hdf file
Definition: hdf_archive.h:51
void disable(ForwardIterator first, ForwardIterator last, bool reindex)
deactivate variables for optimization
Definition: VariableSet.h:289
int getLoc(const std::string &vname) const
Definition: VariableSet.h:121
void resetIndex()
reset Index
void activate(ForwardIterator first, ForwardIterator last, bool reindex)
activate variables for optimization
Definition: VariableSet.h:264
void setParameterType(int type)
Definition: VariableSet.h:149
real_type operator[](int i) const
return the i-th value
Definition: VariableSet.h:194
std::pair< std::string, int > index_pair_type
Definition: VariableSet.h:54
int & recompute(int i)
Definition: VariableSet.h:208
void insertFromDiff(const VariableSet &input_1, const VariableSet &input_2)
take the difference (input_1-input_2) of values of the optimizable parameter values in two VariableSe...
Definition: VariableSet.cpp:95
std::vector< pair_type >::const_iterator const_iterator
Definition: VariableSet.h:56
iterator begin()
return the first iterator
Definition: VariableSet.h:84
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Definition: VariableSet.h:133
std::pair< std::string, real_type > pair_type
Definition: VariableSet.h:53
VariableSet()
default constructor
Definition: VariableSet.h:71
const_iterator begin() const
return the first const_iterator
Definition: VariableSet.h:80
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
int where(int i) const
return the locator of the i-th Index
Definition: VariableSet.h:90
int getType(int i) const
get the i-th parameter&#39;s type
Definition: VariableSet.h:204
int size_of_active() const
return the number of active variables
Definition: VariableSet.h:78
void clear()
clear the variable set
Definition: VariableSet.cpp:28
size_type size() const
return the size
Definition: VariableSet.h:88
void insertFromSum(const VariableSet &input_1, const VariableSet &input_2)
sum together the values of the optimizable parameter values in two VariableSet objects, and set this object&#39;s values to equal them.
Definition: VariableSet.cpp:55
void getParameterTypeList(std::vector< int > &types) const
Definition: VariableSet.h:159
std::vector< pair_type > NameAndValue
Definition: VariableSet.h:66
qmcplusplus::QMCTraits::RealType real_type
Definition: VariableSet.h:51
const std::string & name(int i) const
return the name of i-th variable
Definition: VariableSet.h:189
std::vector< index_pair_type > ParameterType
Definition: VariableSet.h:67
void insertFrom(const VariableSet &input)
insert a VariableSet to the list
Definition: VariableSet.cpp:37
testing::ValidSpinDensityInput input
real_type & operator[](const std::string &vname)
equivalent to std::map<std::string,T>[string] operator
Definition: VariableSet.h:171
void removeInactive()
remove inactive variables and trim the internal data
void print(std::ostream &os, int leftPadSpaces=0, bool printHeader=false) const
int getIndex(const std::string &vname) const
return the Index vaule for the named parameter
std::vector< index_pair_type > Recompute
Definition: VariableSet.h:68
std::vector< pair_type >::size_type size_type
Definition: VariableSet.h:57
iterator end()
return the last iterator
Definition: VariableSet.h:86
int num_active_vars
number of active variables
Definition: VariableSet.h:60