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"
54 
63 namespace ROL {
64 
65 template <class Real>
67 private:
68  const Ptr<RegressionError<Real>> error_;
69  const Ptr<SampleGenerator<Real>> data_;
70 
71  Ptr<RandVarFunctional<Real>> em_;
72  Ptr<StochasticObjective<Real>> obj_;
73  Ptr<std::vector<Real>> cdata_;
74  Ptr<RiskVector<Real>> c_;
75 
76  Ptr<std::vector<Real>> lower_;
77  Ptr<std::vector<Real>> upper_;
78  Ptr<BoundConstraint<Real>> bnd_;
79  Ptr<RiskBoundConstraint<Real>> rbnd_;
80 
82 
83 public:
85  : error_(makePtr<RegressionError<Real>>()), data_(data),
86  lower_(nullPtr), upper_(nullPtr), bnd_(nullPtr), rbnd_(nullPtr),
87  initialized_(false) {
88  int dim = data_->getMyPoint(0).size();
89  cdata_ = makePtr<std::vector<Real>>(dim,0);
90  c_ = makePtr<RiskVector<Real>>(makePtr<StdVector<Real>>(cdata_));
91  }
92 
93  void setErrorMeasure(ROL::ParameterList &parlist, bool reset = false) {
94  if (!initialized_ || reset) {
95  em_ = ErrorMeasureFactory<Real>(parlist);
96  obj_ = makePtr<StochasticObjective<Real>>(error_,em_,data_);
97  initialized_ = true;
98  }
99  }
100 
101  void setLowerBound(const std::vector<Real> &lower) {
102  lower_ = makePtr<std::vector<Real>>(lower);
103  }
104 
105  void setUpperBound(const std::vector<Real> &upper) {
106  upper_ = makePtr<std::vector<Real>>(upper);
107  }
108 
109  void reset(void) {
110  c_->zero();
111  initialized_ = false;
112  }
113 
114  const Ptr<OptimizationProblem<Real>> getOptimizationProblem(void) {
115  if (!initialized_) {
116  throw Exception::NotImplemented("ROL::LinearRegression::getOptimizationProblem : setErrorMeasure was not called!");
117  }
118  if (lower_ != nullPtr && upper_ == nullPtr) {
119  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,true);
120  }
121  if (lower_ == nullPtr && upper_ != nullPtr) {
122  bnd_ = makePtr<StdBoundConstraint<Real>>(*upper_,false);
123  }
124  if (lower_ != nullPtr && upper_ != nullPtr) {
125  bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,*upper_);
126  }
127  if (bnd_ != nullPtr) {
128  rbnd_ = makePtr<RiskBoundConstraint<Real>>(bnd_);
129  return makePtr<OptimizationProblem<Real>>(obj_,c_,rbnd_);
130  }
131  return makePtr<OptimizationProblem<Real>>(obj_,c_);
132  }
133 
134  const Ptr<std::vector<Real>> getCoefficients(void) const {
135  return cdata_;
136  }
137 
138  void print(std::ostream &out = std::cout, const std::string delim = " ") const {
139  int dim = cdata_->size();
140  out << std::endl;
141  for (int i = 0; i < dim; ++i) {
142  out << delim << (*cdata_)[i];
143  }
144  out << std::endl << std::endl;
145  }
146 }; // class LinearRegression
147 
148 } // namespace ROL
149 
150 #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< std::vector< Real > > getCoefficients(void) const
constexpr auto dim
Ptr< StochasticObjective< Real > > obj_
Ptr< RandVarFunctional< Real > > em_