ROL
ROL_VectorController_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_SIMCONTROLLER_DEF_H
11 #define ROL_SIMCONTROLLER_DEF_H
12 
13 namespace ROL {
14 
15 template <class Real, class Key>
17  : maxIndex_(0), maxIndex_trial_(0), maxIndex_temp_(0),
18  trial_(false), temp_(false), objUpdated_(false), conUpdated_(false) {
19  indices_.clear(); indices_trial_.clear(); indices_temp_.clear();
20  flags_.clear(); flags_trial_.clear(); flags_temp_.clear();
21  vectors_.clear(); vectors_trial_.clear(); vectors_temp_.clear();
22 }
23 
24 template <class Real, class Key>
26  if ( flag ) {
27  for (auto it = indices_.begin(); it != indices_.end(); ++it) {
28  flags_[it->second] = false;
29  }
30  }
31 }
32 
33 template <class Real, class Key>
35  temp_ = false;
36  trial_ = false;
37  if (!conUpdated_) {
38  reset(flag);
39  }
40  objUpdated_ = true;
41  if (conUpdated_ && objUpdated_) {
42  objUpdated_ = false;
43  conUpdated_ = false;
44  }
45 }
46 
47 template <class Real, class Key>
49  temp_ = false;
50  trial_ = false;
51  if (!objUpdated_) {
52  reset(flag);
53  }
54  conUpdated_ = true;
55  if (conUpdated_ && objUpdated_) {
56  objUpdated_ = false;
57  conUpdated_ = false;
58  }
59 }
60 
61 template <class Real, class Key>
63  if (type == UpdateType::Temp) {
64  temp_ = true;
65  trial_ = false;
66  objUpdated_ = false;
67  conUpdated_ = false;
68  resetTemp();
69  }
70  else {
71  if (!conUpdated_) {
72  switch(type) {
73  case UpdateType::Initial: temp_ = false; trial_ = false; reset(true); break;
74  case UpdateType::Trial: temp_ = false; trial_ = true; resetTrial(); break;
75  case UpdateType::Accept: temp_ = false; trial_ = false; accept(); break;
76  case UpdateType::Revert: temp_ = false; trial_ = false; break;
77  case UpdateType::Temp: temp_ = true; trial_ = false; resetTemp(); break;
78  }
79  }
80  objUpdated_ = true;
81  if (conUpdated_ && objUpdated_) {
82  objUpdated_ = false;
83  conUpdated_ = false;
84  }
85  }
86 }
87 
88 template <class Real, class Key>
90  if (type == UpdateType::Temp) {
91  temp_ = true;
92  trial_ = false;
93  objUpdated_ = false;
94  conUpdated_ = false;
95  resetTemp();
96  }
97  else {
98  if (!objUpdated_) {
99  switch(type) {
100  case UpdateType::Initial: temp_ = false; trial_ = false; reset(true); break;
101  case UpdateType::Trial: temp_ = false; trial_ = true; resetTrial(); break;
102  case UpdateType::Accept: temp_ = false; trial_ = false; accept(); break;
103  case UpdateType::Revert: temp_ = false; trial_ = false; break;
104  case UpdateType::Temp: temp_ = true; trial_ = false; resetTemp(); break;
105  }
106  }
107  conUpdated_ = true;
108  if (conUpdated_ && objUpdated_) {
109  objUpdated_ = false;
110  conUpdated_ = false;
111  }
112  }
113 }
114 
115 template <class Real, class Key>
116 bool VectorController<Real,Key>::isNull(const Key &param) const {
117  if (!temp_) {
118  if (trial_) {
119  return isNull(param,indices_trial_);
120  }
121  else {
122  return isNull(param,indices_);
123  }
124  }
125  else {
126  return isNull(param,indices_temp_);
127  }
128  return false;
129 }
130 
131 template <class Real, class Key>
132 bool VectorController<Real,Key>::isComputed(const Key &param) const {
133  if (!temp_) {
134  if (trial_) {
135  return isComputed(param,indices_trial_,flags_trial_);
136  }
137  else {
138  return isComputed(param,indices_,flags_);
139  }
140  }
141  else {
142  return isComputed(param,indices_temp_,flags_temp_);
143  }
144  return false;
145 }
146 
147 template <class Real, class Key>
148 void VectorController<Real,Key>::allocate(const Vector<Real> &x, const Key &param) {
149  if (!temp_) {
150  if (trial_) {
151  allocate(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
152  }
153  else {
154  allocate(x,param,indices_,flags_,vectors_,maxIndex_);
155  }
156  }
157  else {
158  allocate(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
159  }
160 }
161 
162 template <class Real, class Key>
163 const Ptr<const Vector<Real>> VectorController<Real,Key>::get(const Key &param) const {
164  if (!temp_) {
165  if (trial_) {
166  return get(param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
167  }
168  else {
169  return get(param,indices_,flags_,vectors_,maxIndex_);
170  }
171  }
172  else {
173  return get(param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
174  }
175  return nullPtr;
176 }
177 
178 template <class Real, class Key>
179 const Ptr<Vector<Real>> VectorController<Real,Key>::set(const Key &param) {
180  if (!temp_) {
181  if (trial_) {
182  return set(param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
183  }
184  else {
185  return set(param,indices_,flags_,vectors_,maxIndex_);
186  }
187  }
188  else {
189  return set(param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
190  }
191  return nullPtr;
192 }
193 
194 template <class Real, class Key>
195 bool VectorController<Real,Key>::get(Vector<Real> &x, const Key &param) {
196  bool flag = false;
197  if (!temp_) {
198  if (trial_) {
199  flag = get(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
200  }
201  else {
202  flag = get(x,param,indices_,flags_,vectors_,maxIndex_);
203  }
204  }
205  else {
206  flag = get(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
207  }
208  return flag;
209 }
210 
211 template <class Real, class Key>
212 void VectorController<Real,Key>::set(const Vector<Real> &x, const Key &param) {
213  if (!temp_) {
214  if (trial_) {
215  set(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
216  }
217  else {
218  set(x,param,indices_,flags_,vectors_,maxIndex_);
219  }
220  }
221  else {
222  set(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
223  }
224 }
225 
226 template <class Real, class Key>
228  for (auto it = indices_.begin(); it != indices_.end(); ++it) {
229  to.set(*vectors_[it->second],it->first);
230  }
231 }
232 
233 template <class Real, class Key>
235  for (auto it = indices_trial_.begin(); it != indices_trial_.end(); ++it) {
236  flags_trial_[it->second] = false;
237  }
238 }
239 
240 template <class Real, class Key>
242  for (auto it = indices_temp_.begin(); it != indices_temp_.end(); ++it) {
243  flags_temp_[it->second] = false;
244  }
245 }
246 
247 template <class Real, class Key>
249  const std::map<Key,int> &indices) const {
250  return (indices.count(param)==0);
251 }
252 
253 template <class Real, class Key>
255  const std::map<Key,int> &indices, const std::vector<bool> &flags) const {
256  if (indices.count(param)>0) {
257  auto it = indices.find(param);
258  return flags[it->second];
259  }
260  return false;
261 }
262 
263 template <class Real, class Key>
264 void VectorController<Real,Key>::allocate(const Vector<Real> &x, const Key &param,
265  std::map<Key,int> &indices, std::vector<bool> &flags,
266  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
267  if (isNull(param,indices)) {
268  int index = maxIndex;
269  indices.insert(std::pair<Key,int>(param, index));
270  flags.push_back(false);
271  vectors.push_back(x.clone());
272  maxIndex++;
273  }
274 }
275 
276 template <class Real, class Key>
277 const Ptr<const Vector<Real>> VectorController<Real,Key>::get(const Key &param,
278  const std::map<Key,int> &indices, const std::vector<bool> &flags,
279  const std::vector<Ptr<Vector<Real>>> &vectors, const int &maxIndex) const {
280  int count = indices.count(param);
281  int index = maxIndex;
282  if (count) {
283  auto it = indices.find(param);
284  index = it->second;
285  return vectors[index];
286  }
287  return nullPtr;
288 }
289 
290 template <class Real, class Key>
291 const Ptr<Vector<Real>> VectorController<Real,Key>::set(const Key &param,
292  std::map<Key,int> &indices, std::vector<bool> &flags,
293  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
294  ROL_TEST_FOR_EXCEPTION(isNull(param,indices),std::logic_error,
295  ">>> ROL::VectorController::set : Vector has not been allocated!");
296  ROL_TEST_FOR_EXCEPTION(isComputed(param,indices,flags),std::logic_error,
297  ">>> ROL::VectorController::set : Vector is already computed!");
298  int count = indices.count(param);
299  int index = maxIndex;
300  if (count) {
301  auto it = indices.find(param);
302  index = it->second;
303  flags[index] = true;
304  return vectors[index];
305  }
306  return nullPtr;
307 }
308 
309 template <class Real, class Key>
311  std::map<Key,int> &indices, std::vector<bool> &flags,
312  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
313  int count = indices.count(param);
314  bool flag = false;
315  int index = maxIndex;
316  if (count) {
317  auto it = indices.find(param);
318  index = it->second;
319  flag = flags[index];
320  if (flag) {
321  x.set(*vectors[index]);
322  }
323  }
324  else {
325  indices.insert(std::pair<Key,int>(param, index));
326  flags.push_back(false);
327  vectors.push_back(x.clone());
328  maxIndex++;
329  }
330  return flag;
331 }
332 
333 template <class Real, class Key>
334 void VectorController<Real,Key>::set(const Vector<Real> &x,const Key &param,
335  std::map<Key,int> &indices, std::vector<bool> &flags,
336  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
337  int count = indices.count(param);
338  int index = maxIndex;
339  if (count) {
340  auto it = indices.find(param);
341  index = it->second;
342  flags[index] = true;
343  vectors[index]->set(x);
344  }
345  else {
346  indices.insert(std::pair<Key,int>(param, index));
347  flags.push_back(true);
348  vectors.push_back(x.clone());
349  vectors[index]->set(x);
350  maxIndex++;
351  }
352 }
353 
354 template <class Real, class Key>
356  reset(true);
357  for (auto it = indices_trial_.begin(); it != indices_trial_.end(); ++it) {
358  set(*vectors_trial_[it->second],it->first,indices_,flags_,vectors_,maxIndex_);
359  }
360 }
361 
362 } // namespace ROL
363 
364 #endif
std::map< Key, int > indices_temp_
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
std::vector< bool > flags_
bool isNull(const Key &param) const
Check if vector associated with provided key is allocated.
void constraintUpdate(bool flag=true)
Equality constraint update for VectorController storage.
const Ptr< Vector< Real > > set(const Key &param)
Set the vector associated with provided key. This assumes the vector data will be changed...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
std::map< Key, int > indices_
VectorController(void)
Constructor.
void push(VectorController< Real, Key > &to) const
Push the contents of *this into another VectorController.
bool isComputed(const Key &param) const
Check if vector has been computed.
void allocate(const Vector< Real > &x, const Key &param)
Allocate the vector associated with provided key.
std::map< Key, int > indices_trial_
void objectiveUpdate(bool flag=true)
Objective function update for VectorController storage.
std::vector< Ptr< Vector< Real > > > vectors_trial_
std::vector< Ptr< Vector< Real > > > vectors_temp_
std::vector< bool > flags_temp_
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:175
std::vector< bool > flags_trial_
const Ptr< const Vector< Real > > get(const Key &param) const
Return the vector associated with provided key.
std::vector< Ptr< Vector< Real > > > vectors_