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