ROL
json/example_01.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 
67 #include "json/json.h" // For JSON definitions
68 #include "Teuchos_ParameterList.hpp" // For Parameter List
69 #include <fstream>
70 #include <string>
71 
72 
73 namespace ROL {
74 
75 void addJSONBlockToPL(const Json::Value& block,ROL::ParameterList& parlist);
76 void addJSONPairToPL(const Json::Value& block, const std::string &key,ROL::ParameterList& parlist);
77 
78 
86 void addJSONPairToPL(const Json::Value& block,
87  const std::string &key,
88  ROL::ParameterList& parlist) {
89 
90  Json::Value val = block[key];
91 
92  if(val.isString()) {
93  parlist.set(key,val.asString());
94  } else if(val.isBool()) {
95  parlist.set(key,val.asBool());
96  } else if(val.isInt()) {
97  parlist.set(key,val.asInt());
98  } else if(val.isUInt()) {
99  parlist.set(key,val.asUInt());
100  } else if(val.isDouble()) {
101  parlist.set(key,val.asDouble());
102  } else if(val.isObject()) { // This is a block. Iterate over its pairs
103  addJSONBlockToPL(val,parlist);
104  }
105  else {
106  ROL_TEST_FOR_EXCEPTION( true, std::invalid_argument, ">>> ERROR (addJSONPairToPL, "
107  "json value has unsupported type.");
108  }
109 }
110 
111 
117 void addJSONBlockToPL(const Json::Value& block,
118  ROL::ParameterList& parlist) {
119  if(block.size()>0) {
120  for(Json::ValueIterator itr = block.begin(); itr != block.end(); ++itr) {
121  addJSONPairToPL(block,itr.key().asString(),parlist);
122  }
123  }
124 }
125 
126 
127 
136 void JSON_Parameters(const std::string& jsonFileName,
137  ROL::ParameterList& parlist) {
138 
139 Json::Value json;
140 std::ifstream stream(jsonFileName, std::ifstream::binary);
141 stream >> json;
142 
143 if(json.isMember("ROL")) {
144  Json::Value rolBlock = json["ROL"];
145 
146  // Make a flat parameter list from the ROL JSON block
147  addJSONBlockToPL(rolBlock,parlist);
148 
149  // Check for an algorithm
150  if(rolBlock.isMember("Algorithm")) {
151  std::string rolAlgorithm = rolBlock["Algorithm"].asString();
152 
153  if(rolAlgorithm.find("Trust-Region") != std::string::npos) {
154 
155  parlist.set("Step Type","Trust-Region");
156 
157  // Set subproblem solver
158  if(rolAlgorithm.find("Cauchy Point") != std::string::npos) {
159  parlist.set("Trust-Region Subproblem Solver Type","Cauchy Point");
160  }
161  else if(rolAlgorithm.find("Double Dogleg") != std::string::npos) {
162  parlist.set("Trust-Region Subproblem Solver Type","Double Dogleg");
163  }
164  else if(rolAlgorithm.find("Dogleg") != std::string::npos) {
165  parlist.set("Trust-Region Subproblem Solver Type","Dogleg");
166  }
167  else if(rolAlgorithm.find("Truncated CG") != std::string::npos) {
168  parlist.set("Trust-Region Subproblem Solver Type","Truncated CG");
169  }
170 
171  }
172  else { // Use Linesearch
173  parlist.set("Step Type","Linesearch");
174 
175  // Set descent type
176  if(rolAlgorithm.find("Steepest Descent") != std::string::npos) {
177  parlist.set("Descent Type","Steepest Descent");
178  }
179  else if(rolAlgorithm.find("Quasi-Newton") != std::string::npos) {
180  parlist.set("Descent Type","Quasi-Newton Method");
181  }
182  else if(rolAlgorithm.find("Newton-Krylov") != std::string::npos) {
183  parlist.set("Descent Type","Newton-Krylov");
184  }
185  else if(rolAlgorithm.find("Nonlinear CG") != std::string::npos) {
186  parlist.set("Descent Type","Nonlinear CG");
187  }
188 
189  }
190 
191  }
192  else { // No algorithm block found - use defaults
193  parlist.set("Step Type","Linesearch");
194  parlist.set("Descent Type","Nonlinear CG");
195  }
196 
197  }
198 }
199 
200 
206 template <class Real>
207 void stepFactory(ROL::ParameterList &parlist,ROL::Ptr<ROL::Step<Real> > &step) {
208 
209  if(parlist.get("Step Type","Linesearch")=="Trust-Region") {
210  step = ROL::makePtr<ROL::TrustRegionStep<Real>>(parlist);
211  }
212  else {
213  step = ROL::makePtr<ROL::LineSearchStep<Real>>(parlist);
214 
215  }
216 }
217 
218 }
void stepFactory(ROL::ParameterList &parlist, ROL::Ptr< ROL::Step< Real > > &step)
A minimalist step factory which specializes the Step Type depending on whether a Trust-Region or Line...
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
void JSON_Parameters(const std::string &jsonFileName, ROL::ParameterList &parlist)
Read a JSON file and store all parameters in a ROL::ParameterList. Checks for a key called &quot;Algorithm...
void addJSONBlockToPL(const Json::Value &block, ROL::ParameterList &parlist)
Iterate over a block and insert key-value pairs into the ROL::ParameterList.
void addJSONPairToPL(const Json::Value &block, const std::string &key, ROL::ParameterList &parlist)
Given a JSON block and a key, get the value and insert the key-value pair into a ROL::ParameterList. If the value is itself a block, recursively iterate.