ROL
ROL_VectorController_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 
45 #ifndef ROL_SIMCONTROLLER_DEF_H
46 #define ROL_SIMCONTROLLER_DEF_H
47 
48 namespace ROL {
49 
50 template <class Real, class Key>
52  : maxIndex_(0), maxIndex_trial_(0), maxIndex_temp_(0),
53  trial_(false), temp_(false), objUpdated_(false), conUpdated_(false) {
54  indices_.clear(); indices_trial_.clear(); indices_temp_.clear();
55  flags_.clear(); flags_trial_.clear(); flags_temp_.clear();
56  vectors_.clear(); vectors_trial_.clear(); vectors_temp_.clear();
57 }
58 
59 template <class Real, class Key>
61  if ( flag ) {
62  for (auto it = indices_.begin(); it != indices_.end(); ++it) {
63  flags_[it->second] = false;
64  }
65  }
66 }
67 
68 template <class Real, class Key>
70  temp_ = false;
71  trial_ = false;
72  if (!conUpdated_) {
73  reset(flag);
74  }
75  objUpdated_ = true;
76  if (conUpdated_ && objUpdated_) {
77  objUpdated_ = false;
78  conUpdated_ = false;
79  }
80 }
81 
82 template <class Real, class Key>
84  temp_ = false;
85  trial_ = false;
86  if (!objUpdated_) {
87  reset(flag);
88  }
89  conUpdated_ = true;
90  if (conUpdated_ && objUpdated_) {
91  objUpdated_ = false;
92  conUpdated_ = false;
93  }
94 }
95 
96 template <class Real, class Key>
98  if (type == UpdateType::Temp) {
99  temp_ = true;
100  trial_ = false;
101  objUpdated_ = false;
102  conUpdated_ = false;
103  resetTemp();
104  }
105  else {
106  if (!conUpdated_) {
107  switch(type) {
108  case UpdateType::Initial: temp_ = false; trial_ = false; reset(true); break;
109  case UpdateType::Trial: temp_ = false; trial_ = true; resetTrial(); break;
110  case UpdateType::Accept: temp_ = false; trial_ = false; accept(); break;
111  case UpdateType::Revert: temp_ = false; trial_ = false; break;
112  case UpdateType::Temp: temp_ = true; trial_ = false; resetTemp(); break;
113  }
114  }
115  objUpdated_ = true;
116  if (conUpdated_ && objUpdated_) {
117  objUpdated_ = false;
118  conUpdated_ = false;
119  }
120  }
121 }
122 
123 template <class Real, class Key>
125  if (type == UpdateType::Temp) {
126  temp_ = true;
127  trial_ = false;
128  objUpdated_ = false;
129  conUpdated_ = false;
130  resetTemp();
131  }
132  else {
133  if (!objUpdated_) {
134  switch(type) {
135  case UpdateType::Initial: temp_ = false; trial_ = false; reset(true); break;
136  case UpdateType::Trial: temp_ = false; trial_ = true; resetTrial(); break;
137  case UpdateType::Accept: temp_ = false; trial_ = false; accept(); break;
138  case UpdateType::Revert: temp_ = false; trial_ = false; break;
139  case UpdateType::Temp: temp_ = true; trial_ = false; resetTemp(); break;
140  }
141  }
142  conUpdated_ = true;
143  if (conUpdated_ && objUpdated_) {
144  objUpdated_ = false;
145  conUpdated_ = false;
146  }
147  }
148 }
149 
150 template <class Real, class Key>
151 bool VectorController<Real,Key>::isNull(const Key &param) const {
152  if (!temp_) {
153  if (trial_) {
154  return isNull(param,indices_trial_);
155  }
156  else {
157  return isNull(param,indices_);
158  }
159  }
160  else {
161  return isNull(param,indices_temp_);
162  }
163  return false;
164 }
165 
166 template <class Real, class Key>
167 bool VectorController<Real,Key>::isComputed(const Key &param) const {
168  if (!temp_) {
169  if (trial_) {
170  return isComputed(param,indices_trial_,flags_trial_);
171  }
172  else {
173  return isComputed(param,indices_,flags_);
174  }
175  }
176  else {
177  return isComputed(param,indices_temp_,flags_temp_);
178  }
179  return false;
180 }
181 
182 template <class Real, class Key>
183 void VectorController<Real,Key>::allocate(const Vector<Real> &x, const Key &param) {
184  if (!temp_) {
185  if (trial_) {
186  allocate(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
187  }
188  else {
189  allocate(x,param,indices_,flags_,vectors_,maxIndex_);
190  }
191  }
192  else {
193  allocate(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
194  }
195 }
196 
197 template <class Real, class Key>
198 const Ptr<const Vector<Real>> VectorController<Real,Key>::get(const Key &param) const {
199  if (!temp_) {
200  if (trial_) {
201  return get(param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
202  }
203  else {
204  return get(param,indices_,flags_,vectors_,maxIndex_);
205  }
206  }
207  else {
208  return get(param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
209  }
210  return nullPtr;
211 }
212 
213 template <class Real, class Key>
214 const Ptr<Vector<Real>> VectorController<Real,Key>::set(const Key &param) {
215  if (!temp_) {
216  if (trial_) {
217  return set(param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
218  }
219  else {
220  return set(param,indices_,flags_,vectors_,maxIndex_);
221  }
222  }
223  else {
224  return set(param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
225  }
226  return nullPtr;
227 }
228 
229 template <class Real, class Key>
230 bool VectorController<Real,Key>::get(Vector<Real> &x, const Key &param) {
231  bool flag = false;
232  if (!temp_) {
233  if (trial_) {
234  flag = get(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
235  }
236  else {
237  flag = get(x,param,indices_,flags_,vectors_,maxIndex_);
238  }
239  }
240  else {
241  flag = get(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
242  }
243  return flag;
244 }
245 
246 template <class Real, class Key>
247 void VectorController<Real,Key>::set(const Vector<Real> &x, const Key &param) {
248  if (!temp_) {
249  if (trial_) {
250  set(x,param,indices_trial_,flags_trial_,vectors_trial_,maxIndex_trial_);
251  }
252  else {
253  set(x,param,indices_,flags_,vectors_,maxIndex_);
254  }
255  }
256  else {
257  set(x,param,indices_temp_,flags_temp_,vectors_temp_,maxIndex_temp_);
258  }
259 }
260 
261 template <class Real, class Key>
263  for (auto it = indices_.begin(); it != indices_.end(); ++it) {
264  to.set(*vectors_[it->second],it->first);
265  }
266 }
267 
268 template <class Real, class Key>
270  for (auto it = indices_trial_.begin(); it != indices_trial_.end(); ++it) {
271  flags_trial_[it->second] = false;
272  }
273 }
274 
275 template <class Real, class Key>
277  for (auto it = indices_temp_.begin(); it != indices_temp_.end(); ++it) {
278  flags_temp_[it->second] = false;
279  }
280 }
281 
282 template <class Real, class Key>
284  const std::map<Key,int> &indices) const {
285  return (indices.count(param)==0);
286 }
287 
288 template <class Real, class Key>
290  const std::map<Key,int> &indices, const std::vector<bool> &flags) const {
291  if (indices.count(param)>0) {
292  auto it = indices.find(param);
293  return flags[it->second];
294  }
295  return false;
296 }
297 
298 template <class Real, class Key>
299 void VectorController<Real,Key>::allocate(const Vector<Real> &x, const Key &param,
300  std::map<Key,int> &indices, std::vector<bool> &flags,
301  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
302  if (isNull(param,indices)) {
303  int index = maxIndex;
304  indices.insert(std::pair<Key,int>(param, index));
305  flags.push_back(false);
306  vectors.push_back(x.clone());
307  maxIndex++;
308  }
309 }
310 
311 template <class Real, class Key>
312 const Ptr<const Vector<Real>> VectorController<Real,Key>::get(const Key &param,
313  const std::map<Key,int> &indices, const std::vector<bool> &flags,
314  const std::vector<Ptr<Vector<Real>>> &vectors, const int &maxIndex) const {
315  int count = indices.count(param);
316  int index = maxIndex;
317  if (count) {
318  auto it = indices.find(param);
319  index = it->second;
320  return vectors[index];
321  }
322  return nullPtr;
323 }
324 
325 template <class Real, class Key>
326 const Ptr<Vector<Real>> VectorController<Real,Key>::set(const Key &param,
327  std::map<Key,int> &indices, std::vector<bool> &flags,
328  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
329  ROL_TEST_FOR_EXCEPTION(isNull(param,indices),std::logic_error,
330  ">>> ROL::VectorController::set : Vector has not been allocated!");
331  ROL_TEST_FOR_EXCEPTION(isComputed(param,indices,flags),std::logic_error,
332  ">>> ROL::VectorController::set : Vector is already computed!");
333  int count = indices.count(param);
334  int index = maxIndex;
335  if (count) {
336  auto it = indices.find(param);
337  index = it->second;
338  flags[index] = true;
339  return vectors[index];
340  }
341  return nullPtr;
342 }
343 
344 template <class Real, class Key>
346  std::map<Key,int> &indices, std::vector<bool> &flags,
347  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
348  int count = indices.count(param);
349  bool flag = false;
350  int index = maxIndex;
351  if (count) {
352  auto it = indices.find(param);
353  index = it->second;
354  flag = flags[index];
355  if (flag) {
356  x.set(*vectors[index]);
357  }
358  }
359  else {
360  indices.insert(std::pair<Key,int>(param, index));
361  flags.push_back(false);
362  vectors.push_back(x.clone());
363  maxIndex++;
364  }
365  return flag;
366 }
367 
368 template <class Real, class Key>
369 void VectorController<Real,Key>::set(const Vector<Real> &x,const Key &param,
370  std::map<Key,int> &indices, std::vector<bool> &flags,
371  std::vector<Ptr<Vector<Real>>> &vectors, int &maxIndex) const {
372  int count = indices.count(param);
373  int index = maxIndex;
374  if (count) {
375  auto it = indices.find(param);
376  index = it->second;
377  flags[index] = true;
378  vectors[index]->set(x);
379  }
380  else {
381  indices.insert(std::pair<Key,int>(param, index));
382  flags.push_back(true);
383  vectors.push_back(x.clone());
384  vectors[index]->set(x);
385  maxIndex++;
386  }
387 }
388 
389 template <class Real, class Key>
391  reset(true);
392  for (auto it = indices_trial_.begin(); it != indices_trial_.end(); ++it) {
393  set(*vectors_trial_[it->second],it->first,indices_,flags_,vectors_,maxIndex_);
394  }
395 }
396 
397 } // namespace ROL
398 
399 #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:80
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:209
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_