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