ROL
ROL_LinearRegression.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_LINEARREGRESSION_H
45 #define ROL_LINEARREGRESSION_H
46 
47 #include "ROL_RegressionError.hpp"
48 #include "ROL_SampleGenerator.hpp"
52 #include "ROL_Problem.hpp"
55 
64 namespace ROL {
65 
66 template <class Real>
68 private:
69  const Ptr<RegressionError<Real>> error_;
70  const Ptr<SampleGenerator<Real>> data_;
71 
72  Ptr<RandVarFunctional<Real>> em_;
73  Ptr<StochasticObjective<Real>> obj_;
74  Ptr<std::vector<Real>> cdata_;
75  Ptr<RiskVector<Real>> c_;
76 
77  Ptr<std::vector<Real>> lower_;
78  Ptr<std::vector<Real>> upper_;
79  Ptr<BoundConstraint<Real>> bnd_;
80  Ptr<RiskBoundConstraint<Real>> rbnd_;
81 
83 
84 public:
86  : error_(makePtr<RegressionError<Real>>()), data_(data),
87  lower_(nullPtr), upper_(nullPtr), bnd_(nullPtr), rbnd_(nullPtr),
88  initialized_(false) {
89  int dim = data_->getMyPoint(0).size();
90  cdata_ = makePtr<std::vector<Real>>(dim,0);
91  c_ = makePtr<RiskVector<Real>>(makePtr<StdVector<Real>>(cdata_));
92  }
93 
94  void setErrorMeasure(ROL::ParameterList &parlist, bool reset = false) {
95  if (!initialized_ || reset) {
96  em_ = ErrorMeasureFactory<Real>(parlist);
97  obj_ = makePtr<StochasticObjective<Real>>(error_,em_,data_);
98  initialized_ = true;
99  }
100  }
101 
102  void setLowerBound(const std::vector<Real> &lower) {
103  lower_ = makePtr<std::vector<Real>>(lower);
104  }
105 
106  void setUpperBound(const std::vector<Real> &upper) {
107  upper_ = makePtr<std::vector<Real>>(upper);
108  }
109 
110  void reset(void) {
111  c_->zero();
112  initialized_ = false;
113  }
114 
115  const Ptr<OptimizationProblem<Real>> getOptimizationProblem(void) {
116  if (!initialized_) {
117  throw Exception::NotImplemented("ROL::LinearRegression::getOptimizationProblem : setErrorMeasure was not called!");
118  }
119  if (lower_ != nullPtr && upper_ == nullPtr) {
120  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,true);
121  }
122  if (lower_ == nullPtr && upper_ != nullPtr) {
123  bnd_ = makePtr<StdBoundConstraint<Real>>(*upper_,false);
124  }
125  if (lower_ != nullPtr && upper_ != nullPtr) {
126  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,*upper_);
127  }
128  if (bnd_ != nullPtr) {
129  rbnd_ = makePtr<RiskBoundConstraint<Real>>(bnd_);
130  return makePtr<OptimizationProblem<Real>>(obj_,c_,rbnd_);
131  }
132  return makePtr<OptimizationProblem<Real>>(obj_,c_);
133  }
134 
135  const Ptr<Problem<Real>> getProblem(void) {
136  if (!initialized_) {
137  throw Exception::NotImplemented("ROL::LinearRegression::getProblem : setErrorMeasure was not called!");
138  }
139  Ptr<Problem<Real>> prob
140  = makePtr<Problem<Real>>(obj_,c_);
141  if (lower_ != nullPtr && upper_ == nullPtr) {
142  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,true);
143  }
144  if (lower_ == nullPtr && upper_ != nullPtr) {
145  bnd_ = makePtr<StdBoundConstraint<Real>>(*upper_,false);
146  }
147  if (lower_ != nullPtr && upper_ != nullPtr) {
148  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,*upper_);
149  }
150  if (bnd_ != nullPtr) {
151  rbnd_ = makePtr<RiskBoundConstraint<Real>>(bnd_);
152  prob->addBoundConstraint(rbnd_);
153  }
154  return prob;
155  }
156 
157  const Ptr<std::vector<Real>> getCoefficients(void) const {
158  return cdata_;
159  }
160 
161  void print(std::ostream &out = std::cout, const std::string delim = " ") const {
162  int dim = cdata_->size();
163  out << std::endl;
164  for (int i = 0; i < dim; ++i) {
165  out << delim << (*cdata_)[i];
166  }
167  out << std::endl << std::endl;
168  }
169 }; // class LinearRegression
170 
171 } // namespace ROL
172 
173 #endif
Contains definitions for std::vector bound constraints.
void setUpperBound(const std::vector< Real > &upper)
Provides the interface to evaluate linear regression error.
Ptr< RiskVector< Real > > c_
Ptr< BoundConstraint< Real > > bnd_
Ptr< RiskBoundConstraint< Real > > rbnd_
void print(std::ostream &out=std::cout, const std::string delim=" ") const
Provides the interface to construct linear regression problem.
Ptr< std::vector< Real > > upper_
void setLowerBound(const std::vector< Real > &lower)
Ptr< std::vector< Real > > cdata_
void setErrorMeasure(ROL::ParameterList &parlist, bool reset=false)
Ptr< std::vector< Real > > lower_
const Ptr< OptimizationProblem< Real > > getOptimizationProblem(void)
const Ptr< RegressionError< Real > > error_
LinearRegression(const Ptr< SampleGenerator< Real >> &data)
const Ptr< SampleGenerator< Real > > data_
const Ptr< Problem< Real > > getProblem(void)
const Ptr< std::vector< Real > > getCoefficients(void) const
constexpr auto dim
Ptr< StochasticObjective< Real > > obj_
Ptr< RandVarFunctional< Real > > em_