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
52#include "ROL_Problem.hpp"
55
64namespace ROL {
65
66template <class Real>
68private:
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
84public:
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.
Provides the interface to construct linear regression problem.
void print(std::ostream &out=std::cout, const std::string delim=" ") const
void setUpperBound(const std::vector< Real > &upper)
void setErrorMeasure(ROL::ParameterList &parlist, bool reset=false)
Ptr< std::vector< Real > > upper_
Ptr< BoundConstraint< Real > > bnd_
const Ptr< SampleGenerator< Real > > data_
Ptr< RiskBoundConstraint< Real > > rbnd_
Ptr< RiskVector< Real > > c_
Ptr< std::vector< Real > > cdata_
const Ptr< OptimizationProblem< Real > > getOptimizationProblem(void)
Ptr< RandVarFunctional< Real > > em_
void setLowerBound(const std::vector< Real > &lower)
const Ptr< RegressionError< Real > > error_
const Ptr< Problem< Real > > getProblem(void)
Ptr< std::vector< Real > > lower_
LinearRegression(const Ptr< SampleGenerator< Real > > &data)
Ptr< StochasticObjective< Real > > obj_
const Ptr< std::vector< Real > > getCoefficients(void) const
Provides the interface to evaluate linear regression error.
constexpr auto dim