ROL
ROL_LinearRegression.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_LINEARREGRESSION_H
11#define ROL_LINEARREGRESSION_H
12
18#include "ROL_Problem.hpp"
21
30namespace ROL {
31
32template <class Real>
34private:
35 const Ptr<RegressionError<Real>> error_;
36 const Ptr<SampleGenerator<Real>> data_;
37
38 Ptr<RandVarFunctional<Real>> em_;
39 Ptr<StochasticObjective<Real>> obj_;
40 Ptr<std::vector<Real>> cdata_;
41 Ptr<RiskVector<Real>> c_;
42
43 Ptr<std::vector<Real>> lower_;
44 Ptr<std::vector<Real>> upper_;
45 Ptr<BoundConstraint<Real>> bnd_;
46 Ptr<RiskBoundConstraint<Real>> rbnd_;
47
49
50public:
52 : error_(makePtr<RegressionError<Real>>()), data_(data),
53 lower_(nullPtr), upper_(nullPtr), bnd_(nullPtr), rbnd_(nullPtr),
54 initialized_(false) {
55 int dim = data_->getMyPoint(0).size();
56 cdata_ = makePtr<std::vector<Real>>(dim,0);
57 c_ = makePtr<RiskVector<Real>>(makePtr<StdVector<Real>>(cdata_));
58 }
59
60 void setErrorMeasure(ROL::ParameterList &parlist, bool reset = false) {
61 if (!initialized_ || reset) {
62 em_ = ErrorMeasureFactory<Real>(parlist);
63 obj_ = makePtr<StochasticObjective<Real>>(error_,em_,data_);
64 initialized_ = true;
65 }
66 }
67
68 void setLowerBound(const std::vector<Real> &lower) {
69 lower_ = makePtr<std::vector<Real>>(lower);
70 }
71
72 void setUpperBound(const std::vector<Real> &upper) {
73 upper_ = makePtr<std::vector<Real>>(upper);
74 }
75
76 void reset(void) {
77 c_->zero();
78 initialized_ = false;
79 }
80
81 const Ptr<OptimizationProblem<Real>> getOptimizationProblem(void) {
82 if (!initialized_) {
83 throw Exception::NotImplemented("ROL::LinearRegression::getOptimizationProblem : setErrorMeasure was not called!");
84 }
85 if (lower_ != nullPtr && upper_ == nullPtr) {
86 bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,true);
87 }
88 if (lower_ == nullPtr && upper_ != nullPtr) {
89 bnd_ = makePtr<StdBoundConstraint<Real>>(*upper_,false);
90 }
91 if (lower_ != nullPtr && upper_ != nullPtr) {
92 bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,*upper_);
93 }
94 if (bnd_ != nullPtr) {
95 rbnd_ = makePtr<RiskBoundConstraint<Real>>(bnd_);
96 return makePtr<OptimizationProblem<Real>>(obj_,c_,rbnd_);
97 }
98 return makePtr<OptimizationProblem<Real>>(obj_,c_);
99 }
100
101 const Ptr<Problem<Real>> getProblem(void) {
102 if (!initialized_) {
103 throw Exception::NotImplemented("ROL::LinearRegression::getProblem : setErrorMeasure was not called!");
104 }
105 Ptr<Problem<Real>> prob
106 = makePtr<Problem<Real>>(obj_,c_);
107 if (lower_ != nullPtr && upper_ == nullPtr) {
108 bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,true);
109 }
110 if (lower_ == nullPtr && upper_ != nullPtr) {
111 bnd_ = makePtr<StdBoundConstraint<Real>>(*upper_,false);
112 }
113 if (lower_ != nullPtr && upper_ != nullPtr) {
114 bnd_ = makePtr<StdBoundConstraint<Real>>(*lower_,*upper_);
115 }
116 if (bnd_ != nullPtr) {
117 rbnd_ = makePtr<RiskBoundConstraint<Real>>(bnd_);
118 prob->addBoundConstraint(rbnd_);
119 }
120 return prob;
121 }
122
123 const Ptr<std::vector<Real>> getCoefficients(void) const {
124 return cdata_;
125 }
126
127 void print(std::ostream &out = std::cout, const std::string delim = " ") const {
128 int dim = cdata_->size();
129 out << std::endl;
130 for (int i = 0; i < dim; ++i) {
131 out << delim << (*cdata_)[i];
132 }
133 out << std::endl << std::endl;
134 }
135}; // class LinearRegression
136
137} // namespace ROL
138
139#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