ROL
json/example_01.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
33#include "json/json.h" // For JSON definitions
34#include "Teuchos_ParameterList.hpp" // For Parameter List
35#include <fstream>
36#include <string>
37
38
39namespace ROL {
40
41void addJSONBlockToPL(const Json::Value& block,ROL::ParameterList& parlist);
42void addJSONPairToPL(const Json::Value& block, const std::string &key,ROL::ParameterList& parlist);
43
44
52void addJSONPairToPL(const Json::Value& block,
53 const std::string &key,
54 ROL::ParameterList& parlist) {
55
56 Json::Value val = block[key];
57
58 if(val.isString()) {
59 parlist.set(key,val.asString());
60 } else if(val.isBool()) {
61 parlist.set(key,val.asBool());
62 } else if(val.isInt()) {
63 parlist.set(key,val.asInt());
64 } else if(val.isUInt()) {
65 parlist.set(key,val.asUInt());
66 } else if(val.isDouble()) {
67 parlist.set(key,val.asDouble());
68 } else if(val.isObject()) { // This is a block. Iterate over its pairs
69 addJSONBlockToPL(val,parlist);
70 }
71 else {
72 ROL_TEST_FOR_EXCEPTION( true, std::invalid_argument, ">>> ERROR (addJSONPairToPL, "
73 "json value has unsupported type.");
74 }
75}
76
77
83void addJSONBlockToPL(const Json::Value& block,
84 ROL::ParameterList& parlist) {
85 if(block.size()>0) {
86 for(Json::ValueIterator itr = block.begin(); itr != block.end(); ++itr) {
87 addJSONPairToPL(block,itr.key().asString(),parlist);
88 }
89 }
90}
91
92
93
102void JSON_Parameters(const std::string& jsonFileName,
103 ROL::ParameterList& parlist) {
104
105Json::Value json;
106std::ifstream stream(jsonFileName, std::ifstream::binary);
107stream >> json;
108
109if(json.isMember("ROL")) {
110 Json::Value rolBlock = json["ROL"];
111
112 // Make a flat parameter list from the ROL JSON block
113 addJSONBlockToPL(rolBlock,parlist);
114
115 // Check for an algorithm
116 if(rolBlock.isMember("Algorithm")) {
117 std::string rolAlgorithm = rolBlock["Algorithm"].asString();
118
119 if(rolAlgorithm.find("Trust-Region") != std::string::npos) {
120
121 parlist.set("Step Type","Trust-Region");
122
123 // Set subproblem solver
124 if(rolAlgorithm.find("Cauchy Point") != std::string::npos) {
125 parlist.set("Trust-Region Subproblem Solver Type","Cauchy Point");
126 }
127 else if(rolAlgorithm.find("Double Dogleg") != std::string::npos) {
128 parlist.set("Trust-Region Subproblem Solver Type","Double Dogleg");
129 }
130 else if(rolAlgorithm.find("Dogleg") != std::string::npos) {
131 parlist.set("Trust-Region Subproblem Solver Type","Dogleg");
132 }
133 else if(rolAlgorithm.find("Truncated CG") != std::string::npos) {
134 parlist.set("Trust-Region Subproblem Solver Type","Truncated CG");
135 }
136
137 }
138 else { // Use Linesearch
139 parlist.set("Step Type","Linesearch");
140
141 // Set descent type
142 if(rolAlgorithm.find("Steepest Descent") != std::string::npos) {
143 parlist.set("Descent Type","Steepest Descent");
144 }
145 else if(rolAlgorithm.find("Quasi-Newton") != std::string::npos) {
146 parlist.set("Descent Type","Quasi-Newton Method");
147 }
148 else if(rolAlgorithm.find("Newton-Krylov") != std::string::npos) {
149 parlist.set("Descent Type","Newton-Krylov");
150 }
151 else if(rolAlgorithm.find("Nonlinear CG") != std::string::npos) {
152 parlist.set("Descent Type","Nonlinear CG");
153 }
154
155 }
156
157 }
158 else { // No algorithm block found - use defaults
159 parlist.set("Step Type","Linesearch");
160 parlist.set("Descent Type","Nonlinear CG");
161 }
162
163 }
164}
165
166
172template <class Real>
173void stepFactory(ROL::ParameterList &parlist,ROL::Ptr<ROL::Step<Real> > &step) {
174
175 if(parlist.get("Step Type","Linesearch")=="Trust-Region") {
176 step = ROL::makePtr<ROL::TrustRegionStep<Real>>(parlist);
177 }
178 else {
179 step = ROL::makePtr<ROL::LineSearchStep<Real>>(parlist);
180
181 }
182}
183
184}
Provides the interface to compute optimization steps.
Definition ROL_Step.hpp:34
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...
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....
void addJSONBlockToPL(const Json::Value &block, ROL::ParameterList &parlist)
Iterate over a block and insert key-value pairs into the ROL::ParameterList.
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 "Algorithm...