ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
example
json
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
31
32
33
#include "json/json.h"
// For JSON definitions
34
#include "Teuchos_ParameterList.hpp"
// For Parameter List
35
#include <fstream>
36
#include <string>
37
38
39
namespace
ROL
{
40
41
void
addJSONBlockToPL
(
const
Json::Value& block,ROL::ParameterList& parlist);
42
void
addJSONPairToPL
(
const
Json::Value& block,
const
std::string &key,ROL::ParameterList& parlist);
43
44
52
void
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
83
void
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
102
void
JSON_Parameters
(
const
std::string& jsonFileName,
103
ROL::ParameterList& parlist) {
104
105
Json::Value json;
106
std::ifstream stream(jsonFileName, std::ifstream::binary);
107
stream >> json;
108
109
if
(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
172
template
<
class
Real>
173
void
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
}
ROL::Step
Provides the interface to compute optimization steps.
Definition
ROL_Step.hpp:34
ROL
Definition
ROL_ElementwiseVector.hpp:27
ROL::stepFactory
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...
Definition
json/example_01.hpp:173
ROL::addJSONPairToPL
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....
Definition
json/example_01.hpp:52
ROL::addJSONBlockToPL
void addJSONBlockToPL(const Json::Value &block, ROL::ParameterList &parlist)
Iterate over a block and insert key-value pairs into the ROL::ParameterList.
Definition
json/example_01.hpp:83
ROL::JSON_Parameters
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...
Definition
json/example_01.hpp:102
Generated by
1.17.0