ROL
ROL_RiskMeasureInfo.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 
10 #ifndef ROL_RISKMEASUREINFO_HPP
11 #define ROL_RISKMEASUREINFO_HPP
12 
13 #include "ROL_ParameterList.hpp"
14 #include "ROL_Types.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 inline void RiskMeasureInfo(ROL::ParameterList &parlist, std::string &name,
20  int &nStatistic, std::vector<Real> &lower,
21  std::vector<Real> &upper, bool &isBoundActivated,
22  const bool printToStream = false,
23  std::ostream &outStream = std::cout) {
24  name = parlist.sublist("SOL").sublist("Risk Measure").get<std::string>("Name");
25  Real zero(0);
26  lower.clear(); upper.clear();
27  nStatistic = 0; isBoundActivated = false;
28  if ( name == "CVaR" ||
29  name == "HMCR" ||
30  name == "Moreau-Yosida CVaR" ||
31  name == "Generalized Moreau-Yosida CVaR" ||
32  name == "Log Quantile" ||
33  name == "Smoothed Worst Case" ||
34  name == "Safety Margin" ||
35  name == "Log Exponential" ||
36  name == "Truncated Mean" ) {
37  nStatistic = 1;
38  lower.resize(nStatistic,ROL_NINF<Real>());
39  upper.resize(nStatistic,ROL_INF<Real>());
40  }
41  else if ( name == "Quantile Radius" ) {
42  nStatistic = 2;
43  lower.resize(nStatistic,ROL_NINF<Real>());
44  upper.resize(nStatistic,ROL_INF<Real>());
45  }
46  else if ( name == "Coherent Entropic Risk" ||
47  name == "KL Divergence" ) {
48  nStatistic = 1;
49  isBoundActivated = true;
50  lower.resize(nStatistic,zero);
51  upper.resize(nStatistic,ROL_INF<Real>());
52  }
53  else if ( name == "Chi-Squared Divergence" ) {
54  nStatistic = 2;
55  isBoundActivated = true;
56  lower.resize(nStatistic,ROL_NINF<Real>()); lower[0] = zero;
57  upper.resize(nStatistic,ROL_INF<Real>());
58  }
59  else if ( name == "Mixed CVaR" ) {
60  ROL::ParameterList &list
61  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed CVaR");
62  std::vector<Real> prob
63  = ROL::getArrayFromStringParameter<Real>(list,"Probability Array");
64  nStatistic = prob.size();
65  lower.resize(nStatistic,ROL_NINF<Real>());
66  upper.resize(nStatistic,ROL_INF<Real>());
67  }
68  else if ( name == "Second Order CVaR" ||
69  name == "Chebyshev Spectral Risk" ||
70  name == "Spectral Risk" ) {
71  ROL::ParameterList &list
72  = parlist.sublist("SOL").sublist("Risk Measure").sublist(name);
73  nStatistic = list.get("Number of Quadrature Points",5);
74  lower.resize(nStatistic,ROL_NINF<Real>());
75  upper.resize(nStatistic,ROL_INF<Real>());
76  }
77  else if ( name == "Entropic Risk" ||
78  name == "Mean Plus Semi-Deviation From Target" ||
79  name == "Mean Plus Semi-Deviation" ||
80  name == "Mean Plus Deviation From Target" ||
81  name == "Mean Plus Deviation" ||
82  name == "Mean Plus Variance From Target" ||
83  name == "Mean Plus Variance" ) {
84  nStatistic = 0;
85  }
86  else if ( name == "Convex Combination Risk Measure" ) {
87  ROL::ParameterList &list
88  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Convex Combination Risk Measure");
89  // Get convex combination parameters
90  std::vector<Real> lambda
91  = ROL::getArrayFromStringParameter<Real>(list,"Convex Combination Parameters");
92  // Build risk measures
93  std::vector<std::string> riskString;
94  for (typename std::vector<Real>::size_type i = 0; i < lambda.size(); ++i) {
95  std::ostringstream convert;
96  convert << i;
97  std::string si = convert.str();
98  ROL::ParameterList &ilist = list.sublist(si);
99  std::string namei = ilist.get<std::string>("Name");
100  riskString.push_back(namei);
101  }
102  for (typename std::vector<Real>::size_type i = 0; i < riskString.size(); ++i) {
103  if ( riskString[i] == "CVaR" ||
104  riskString[i] == "HMCR" ||
105  riskString[i] == "Moreau-Yosida CVaR" ||
106  riskString[i] == "Generalized Moreau-Yosida CVaR" ||
107  riskString[i] == "Log Quantile" ||
108  riskString[i] == "Smoothed Worst Case" ||
109  riskString[i] == "Safety Margin" ||
110  riskString[i] == "Log Exponential" ||
111  riskString[i] == "Truncated Mean" ) {
112  nStatistic += 1;
113  lower.push_back(ROL_NINF<Real>());
114  upper.push_back(ROL_INF<Real>());
115  }
116  else if ( riskString[i] == "Quantile Radius" ) {
117  nStatistic += 2;
118  lower.push_back(ROL_NINF<Real>()); lower.push_back(ROL_NINF<Real>());
119  upper.push_back(ROL_INF<Real>()); upper.push_back(ROL_INF<Real>());
120  }
121  else if ( riskString[i] == "Coherent Entropic Risk" ||
122  riskString[i] == "KL Divergence" ) {
123  nStatistic += 1;
124  isBoundActivated = true;
125  lower.push_back(zero);
126  upper.push_back(ROL_INF<Real>());
127  }
128  else if ( riskString[i] == "Chi-Squared Divergence" ) {
129  nStatistic += 2;
130  isBoundActivated = true;
131  lower.push_back(zero); lower.push_back(ROL_NINF<Real>());
132  upper.push_back(ROL_INF<Real>()); upper.push_back(ROL_INF<Real>());
133  }
134  else if ( riskString[i] == "Mixed CVaR" ) {
135  ROL::ParameterList &MQlist = list.sublist("Mixed CVaR");
136  std::vector<Real> prob
137  = ROL::getArrayFromStringParameter<Real>(MQlist,"Probability Array");
138  nStatistic += prob.size();
139  for (typename std::vector<Real>::size_type j = 0; j < prob.size(); ++j) {
140  lower.push_back(ROL_NINF<Real>());
141  upper.push_back(ROL_INF<Real>());
142  }
143  }
144  else if ( riskString[i] == "Second Order CVaR" ||
145  riskString[i] == "Chebyshev Spectral Risk" ||
146  riskString[i] == "Spectral Risk" ) {
147  ROL::ParameterList &SQlist = list.sublist(riskString[i]);
148  int nSQQstat = SQlist.get("Number of Quadrature Points",5);
149  nStatistic += nSQQstat;
150  for (int j = 0; j < nSQQstat; ++j) {
151  lower.push_back(ROL_NINF<Real>());
152  upper.push_back(ROL_INF<Real>());
153  }
154  }
155  else if ( riskString[i] == "Entropic Risk" ||
156  riskString[i] == "Mean Plus Semi-Deviation From Target" ||
157  riskString[i] == "Mean Plus Semi-Deviation" ||
158  riskString[i] == "Mean Plus Deviation From Target" ||
159  riskString[i] == "Mean Plus Deviation" ||
160  riskString[i] == "Mean Plus Variance From Target" ||
161  riskString[i] == "Mean Plus Variance" ) {
162  nStatistic += 0;
163  }
164  else {
165  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
166  ">>> (ROL::RiskMeasureInfo): Invalid risk measure " << riskString[i] << "!");
167  }
168  }
169  }
170  else {
171  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
172  ">>> (ROL::RiskMeasureInfo): Invalid risk measure " << name << "!");
173  }
174 
175  // Print Information
176  if ( printToStream ) {
177  ROL::nullstream oldFormatState;
178  oldFormatState.copyfmt(outStream);
179 
180  outStream << std::endl;
181  outStream << std::scientific << std::setprecision(6);
182  outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
183  outStream << " RISK MEASURE INFORMATION" << std::endl;
184  outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
185  outStream << " NAME" << std::endl;
186  outStream << " " << name << std::endl;
187  outStream << " NUMBER OF STATISTICS" << std::endl;
188  outStream << " " << nStatistic << std::endl;
189  outStream << " ARE BOUNDS ACTIVATED" << std::endl;
190  outStream << " " << (isBoundActivated ? "TRUE" : "FALSE") << std::endl;
191  if ( isBoundActivated ) {
192  outStream << " STATISTIC LOWER BOUNDS" << std::endl;
193  for (int i = 0; i < nStatistic-1; ++i) {
194  outStream << " " << lower[i] << std::endl;
195  }
196  outStream << " " << lower[nStatistic-1] << std::endl;
197  outStream << " STATISTIC UPPER BOUNDS" << std::endl;
198  for (int i = 0; i < nStatistic-1; ++i) {
199  outStream << " " << upper[i] << std::endl;
200  }
201  outStream << " " << upper[nStatistic-1] << std::endl;
202  }
203  outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
204  outStream << std::endl;
205 
206  outStream.copyfmt(oldFormatState);
207  }
208 }
209 
210 }
211 #endif
typename PV< Real >::size_type size_type
void RiskMeasureInfo(ROL::ParameterList &parlist, std::string &name, int &nStatistic, std::vector< Real > &lower, std::vector< Real > &upper, bool &isBoundActivated, const bool printToStream=false, std::ostream &outStream=std::cout)
Contains definitions of custom data types in ROL.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36