ROL
ROL_ParameterListConverters.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_PARAMETER_LIST_CONVERTERS_H
46 #define ROL_PARAMETER_LIST_CONVERTERS_H
47 
48 #include <map>
49 #include "ROL_Types.hpp"
50 #include "Teuchos_ParameterList.hpp"
51 
52 
53 namespace ROL {
54 
55 namespace StringList {
56 
58 inline std::vector<std::string> join( const std::string &s1,
59  const std::string &s2 ) {
60  std::vector<std::string> v;
61  v.push_back(s1);
62  v.push_back(s2);
63 
64  return v;
65 }
66 
68 inline std::vector<std::string> join( const std::string &s1,
69  const std::string &s2,
70  const std::string &s3 ) {
71  std::vector<std::string> v;
72  v.push_back(s1);
73  v.push_back(s2);
74  v.push_back(s3);
75 
76  return v;
77 }
78 
80 inline std::vector<std::string> join( const std::string &s1,
81  const std::string &s2,
82  const std::string &s3,
83  const std::string &s4 ) {
84  std::vector<std::string> v;
85  v.push_back(s1);
86  v.push_back(s2);
87  v.push_back(s3);
88  v.push_back(s4);
89 
90  return v;
91 }
92 
94 inline std::vector<std::string> join( const std::string &s1,
95  const std::string &s2,
96  const std::string &s3,
97  const std::string &s4,
98  const std::string &s5 ) {
99  std::vector<std::string> v;
100  v.push_back(s1);
101  v.push_back(s2);
102  v.push_back(s3);
103  v.push_back(s4);
104  v.push_back(s5);
105 
106  return v;
107 }
108 
109 } // namespace StringList
110 
111 template<class ParameterType>
112 void setParameter( Teuchos::ParameterList &parlist,
113  const std::vector<std::string> &location,
114  const std::vector<std::string>::iterator iter,
115  ParameterType value ) {
116 
117  if( iter == location.end()-1 ) { // Key name
118  parlist.set(*iter,value);
119  }
120  else { // sublist
121  Teuchos::ParameterList &sublist = parlist.sublist(*iter);
122  setParameter(sublist,location,iter+1,value);
123  }
124 
125 
126 }
127 
128 
129 
131 inline void tierParameterList( Teuchos::ParameterList &outList,
132  const Teuchos::ParameterList &inList ) {
133 
134  using namespace Teuchos;
135  using StringList::join;
136 
137  typedef std::string Str;
138  typedef std::vector<Str> Vec;
139  typedef std::map<Str,Vec> Map;
140  typedef ParameterList::ConstIterator IterPL;
141  typedef typename Vec::iterator IterVec;
142  typedef typename Map::iterator IterMap;
143 
144  Map dict;
145 
146  // Original flat list name heirarchical list name
147  dict["Use Inexact Gradient"] = join("General","Inexact Gradient");
148  dict["Use Inexact Objective Function"] = join("General","Inexact Objective Function");
149  dict["Use Inexact Hessian-Times-A-Vector"] = join("General","Inexact Hessian-Times-A-Vector");
150  dict["Use Projected Gradient Criticality Measure"] = join("General","Projected Gradient Criticality Measure");
151  dict["Scale for Epsilon Active Sets"] = join("General","Scale for Epsilon Active Sets");
152 
153  dict["Absolute Krylov Tolerance"] = join("General","Krylov","Absolute Tolerance");
154  dict["Relative Krylov Tolerance"] = join("General","Krylov","Relative Tolerance");
155  dict["Maximum Number of Krylov Iterations"] = join("General","Krylov","Iteration Limit");
156  dict["Krylov Type"] = join("General","Krylov","Type");
157 
158  dict["Barzilai-Borwein"] = join("General","Secant","Barzilai-Borwein");
159  dict["Maximum Secant Storage"] = join("General","Secant","Maximum Storage");
160  dict["Secant Type"] = join("General","Secant","Type");
161  dict["Use Secant Hessian-Times-A-Vector"] = join("General","Secant","Use as Hessian");
162  dict["Use Secant Preconditioning"] = join("General","Secant","Use as Preconditioner");
163 
164  dict["Gradient Tolerance"] = join("Status Test","Gradient Tolerance");
165  dict["Maximum Number of Iterations"] = join("Status Test","Iteration Limit");
166  dict["Step Tolerance"] = join("Status Test","Step Tolerance");
167 
168  dict["Accept Last Alpha"] = join("Step","Line Search","Accept Last Alpha");
169  dict["Accept Linesearch Minimizer"] = join("Step","Line Search","Accept Linesearch Minimizer");
170  dict["Maximum Number of Function Evaluations"] = join("Step","Line Search","Function Evaluation Limit");
171  dict["Initial Linesearch Parameter"] = join("Step","Line Search","Initial Step Size");
172  dict["Sufficient Decrease Parameter"] = join("Step","Line Search","Sufficient Decrease Tolerance");
173  dict["User Defined Linesearch Parameter"] = join("Step","Line Search","User Defined Initial Step Size");
174 
175  dict["Curvature Conditions Parameter"] = join("Step","Line Search","Curvature Condition","General Parameter");
176  dict["Curvature Conditions Parameter: Generalized Wolfe"] = join("Step","Line Search","Curvature Condition","Generalized Wolfe Parameter");
177  dict["Linesearch Curvature Condition"] = join("Step","Line Search","Curvature Condition","Type");
178 
179  dict["Nonlinear CG Type"] = join("Step","Line Search","Descent Method","Nonlinear CG Type");
180  dict["Descent Type"] = join("Step","Line Search","Descent Method","Type");
181 
182  dict["Backtracking Rate"] = join("Step","Line Search","Line-Search Method","Backtracking Rate");
183  dict["Bracketing Tolerance"] = join("Step","Line Search","Line-Search Method","Bracketing Tolerance");
184  dict["Linesearch Type"] = join("Step","Line Search","Line-Search Method","Type");
185 
186  dict["Initial Trust-Region Radius"] = join("Step","Trust Region","Initial Radius");
187  dict["Maximum Trust-Region Radius"] = join("Step","Trust Region","Maximum Radius");
188  dict["Radius Growing Threshold"] = join("Step","Trust Region","Radius Growing Threshold");
189  dict["Radius Growing Rate"] = join("Step","Trust Region","Radius Growing Rate");
190  dict["Radius Shrinking Threshold"] = join("Step","Trust Region","Radius Shrinking Threshold");
191  dict["Trust-Region Safeguard"] = join("Step","Trust Region","Safeguard Size");
192  dict["Trust-Region Subproblem Solver Type"] = join("Step","Trust Region","Subproblem Solver");
193  dict["Step Acceptance Parameter"] = join("Step","Trust Region","Step Acceptance Threshold");
194 
195  dict["Gradient Update Relative Tolerance"] = join("Step","Trust Region","Gradient","Relative Tolerance");
196  dict["Gradient Update Tolerance Scaling"] = join("Step","Trust Region","Gradient","Tolerance Scaling");
197  dict["Value Update Exponent"] = join("Step","Trust Region","Inexact","Value","Exponent");
198  dict["Value Update Forcing Sequence Initial Value"] = join("Step","Trust Region","Inexact","Value","Forcing Sequence Initial Value");
199  dict["Value Update Forcing Sequence Reduction Factor"] = join("Step","Trust Region","Inexact","Value","Forcing Sequence Reduction Factor");
200  dict["Value Update Forcing Sequence Update Frequency"] = join("Step","Trust Region","Inexact","Value","Forcing Sequence Update Frequency");
201  dict["Value Update Tolerance Scaling"] = join("Step","Trust Region","Inexact","Value","Tolerance Scaling");
202 
203 
204  // Add duplicate entries with unformatted keys
205  for(IterMap itmap = dict.begin(); itmap != dict.end(); ++itmap) {
206  Str key = itmap->first;
207  Vec value = itmap->second;
208  dict[removeStringFormat(key)] = value;
209  }
210 
211 
212  // Iterate over parameter list
213  for(IterPL itpl = inList.begin(); itpl != inList.end(); ++itpl) {
214 
215  // Get name of old key
216  Str key( inList.name(itpl) );
217 
218  // Look up location/name of new key
219  Vec location = dict[removeStringFormat(key)];
220 
221  // Skip if not found in map
222  if(location.size() != 0) {
223 
224  IterVec itvec = location.begin();
225 
226  if( inList.isType<bool>(key) ) {
227  bool value = inList.get<bool>( key );
228  setParameter( outList, location, itvec, value );
229  }
230  else if( inList.isType<int>(key) ) {
231  int value = inList.get<int>( key );
232  setParameter( outList, location, itvec, value );
233  }
234  else if( inList.isType<double>(key) ) {
235  double value = inList.get<double>( key );
236  setParameter( outList, location, itvec, value );
237  }
238  else if( inList.isType<std::string>(key) ) {
239  std::string value = inList.get<std::string>( key );
240  setParameter( outList, location, itvec, value );
241  }
242  else {
243  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
244  ">>>Error: Unhandled parameter type." );
245  }
246  }
247 
248  }
249 }
250 
251 } // namespace ROL
252 
253 #endif
Contains definitions of custom data types in ROL.
std::string removeStringFormat(std::string s)
Definition: ROL_Types.hpp:174
void tierParameterList(Teuchos::ParameterList &outList, const Teuchos::ParameterList &inList)
Produce a heirarchical parameter list using the new names from a flat list of the old names...
void setParameter(Teuchos::ParameterList &parlist, const std::vector< std::string > &location, const std::vector< std::string >::iterator iter, ParameterType value)
std::vector< std::string > join(const std::string &s1, const std::string &s2)
Helper function for making vectors of strings.
void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &sol, const Real &mu)