Debian Codegraph @

packages/rol/example/PDE-OPT/published/TRSPG_Kouri2021/simpreg/example_01.cpp

raw
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 /*! \file example_01.cpp
11 \brief Shows how to minimize a function with binary (0/1) constraints.
12 */
13
14 #include "ROL_Solver.hpp"
15 #include "ROL_Bounds.hpp"
16 #include "ROL_StdObjective.hpp"
17 #include "ROL_StdConstraint.hpp"
18 #include "ROL_Stream.hpp"
19
20 #include "Teuchos_Time.hpp"
21 #include "Teuchos_GlobalMPISession.hpp"
22 #include "Teuchos_XMLParameterListHelpers.hpp"
23
24 #include <iostream>
25 #include <random>
26
27 typedef double RealT;
28
29 template<class Real>
30 class SimpRegObjective : public ROL::StdObjective<Real> {
31 private:
32 const std::vector<std::vector<Real>> X_;
33 const std::vector<Real> y_;
34 const int nrows_, ncols_;
35 int fval_, grad_, hess_;
36
37 public:
38 SimpRegObjective(const std::vector<std::vector<Real>> &X, const std::vector<Real> &y)
39 : X_(X), y_(y), nrows_(X_.size()), ncols_(X_[0].size()),
40 fval_(0), grad_(0), hess_(0) {}
41
42 void summarize(std::ostream &stream) const {
43 stream << std::endl;
44 stream << std::string(114,'=') << std::endl;
45 stream << " LassoObjective::summarize" << std::endl;
46 stream << " Number of calls to value: " << fval_ << std::endl;
47 stream << " Number of calls to gradient: " << grad_ << std::endl;
48 stream << " Number of calls to hessVec: " << hess_ << std::endl;
49 stream << std::string(114,'=') << std::endl;
50 stream << std::endl;
51 }
52
53 void reset(void) {
54 fval_ = 0; grad_ = 0; hess_ = 0;
55 }
56
57 Real value(const std::vector<Real> &x, Real &tol) {
58 fval_++;
59 Real val(0), resid(0);
60 for (int i=0; i<nrows_; ++i) {
61 resid = y_[i];
62 for (int j=0; j<ncols_; ++j) {
63 resid -= X_[i][j]*x[j];
64 }
65 val += resid*resid;
66 }
67 return static_cast<Real>(0.5)*val;
68 }
69
70 void gradient(std::vector<Real> &g, const std::vector<Real> &x, Real &tol) {
71 grad_++;
72 g.assign(x.size(),static_cast<Real>(0));
73 std::vector<Real> resid(nrows_,0);
74 for (int i=0; i<nrows_; ++i) {
75 for (int j=0; j<ncols_; ++j) {
76 resid[i] += X_[i][j]*x[j];
77 }
78 resid[i] -= y_[i];
79 }
80 Real val(0);
81 for (int j=0; j<ncols_; ++j) {
82 val = static_cast<Real>(0);
83 for (int i=0; i<nrows_; ++i) {
84 val += X_[i][j]*resid[i];
85 }
86 g[j] += val;
87 }
88 }
89
90 void hessVec(std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
91 hess_++;
92 hv.assign(x.size(),static_cast<Real>(0));
93 std::vector<Real> resid(nrows_,0);
94 for (int i=0; i<nrows_; ++i) {
95 for (int j=0; j<ncols_; ++j) {
96 resid[i] += X_[i][j]*v[j];
97 }
98 }
99 Real val(0);
100 for (int j=0; j<ncols_; ++j) {
101 val = static_cast<Real>(0);
102 for (int i=0; i<nrows_; ++i) {
103 val += X_[i][j]*resid[i];
104 }
105 hv[j] += val;
106 }
107 }
108
109 };
110
111 template<typename Real>
112 class SimpRegConstraint : public ROL::StdConstraint<Real> {
113 public:
114 SimpRegConstraint() {}
115
116 void value(std::vector<Real> &c, const std::vector<Real> &x, Real &tol) {
117 c[0] = static_cast<Real>(-1);
118 for (const auto xi : x) c[0] += xi;
119 }
120
121 void applyJacobian(std::vector<Real> &jv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
122 jv[0] = static_cast<Real>(0);
123 for (const auto vi : v) jv[0] += vi;
124 }
125
126 void applyAdjointJacobian(std::vector<Real> &ajv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
127 ajv.assign(x.size(),v[0]);
128 }
129
130 void applyAdjointHessian(std::vector<Real> &ahuv, const std::vector<Real> &u, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
131 ahuv.assign(x.size(),static_cast<Real>(0));
132 }
133 };
134
135 int main(int argc, char *argv[]) {
136 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
137
138 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
139 int iprint = argc - 1;
140 ROL::Ptr<std::ostream> outStream;
141 ROL::nullstream bhs; // outputs nothing
142 if (iprint > 0)
143 outStream = ROL::makePtrFromRef(std::cout);
144 else
145 outStream = ROL::makePtrFromRef(bhs);
146
147 int errorFlag = 0;
148
149 // *** Example body.
150
151 try {
152
153 /*** Read in XML input ***/
154 std::string filename = "input.xml";
155 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
156 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
157
158 // Set up problem data
159 int nInstances = 50;
160 int nVars = 10;
161 std::default_random_engine generator;
162 std::normal_distribution<RealT> dist(0.0,1.0);
163 std::vector<std::vector<RealT>> X(nInstances);
164 for (int i = 0; i < nInstances; ++i) {
165 for (int j = 0; j < nVars; ++j) {
166 X[i].push_back(dist(generator));
167 }
168 }
169 std::vector<RealT> w(nVars);
170 for (int j = 0; j < nVars; ++j) {
171 w[j] = dist(generator);
172 }
173 //RealT yval(0);
174 std::vector<RealT> y(nInstances,0);
175 for (int i = 0; i < nInstances; ++i) {
176 y[i] = dist(generator);
177 for (int j = 0; j < nVars; ++j) {
178 y[i] += X[i][j]*w[j];
179 }
180 }
181 ROL::Ptr<SimpRegObjective<RealT>> obj = ROL::makePtr<SimpRegObjective<RealT>>(X,y);
182 ROL::Ptr<SimpRegConstraint<RealT>> con = ROL::makePtr<SimpRegConstraint<RealT>>();
183 ROL::Ptr<ROL::StdVector<RealT>> mul = ROL::makePtr<ROL::StdVector<RealT>>(1,0);
184 ROL::Ptr<ROL::StdVector<RealT>> x = ROL::makePtr<ROL::StdVector<RealT>>(nInstances,0);
185 ROL::Ptr<ROL::StdVector<RealT>> l = ROL::makePtr<ROL::StdVector<RealT>>(nInstances,0);
186 ROL::Ptr<ROL::Bounds<RealT>> bnd = ROL::makePtr<ROL::Bounds<RealT>>(*l,true);
187 ROL::Ptr<ROL::Problem<RealT>> prob
188 = ROL::makePtr<ROL::Problem<RealT>>(obj,x);
189 prob->addBoundConstraint(bnd);
190 prob->addLinearConstraint("Simplex",con,mul);
191 prob->setProjectionAlgorithm(*parlist);
192 prob->finalize(false,true,*outStream);
193 prob->check(true,*outStream);
194
195 ROL::Ptr<ROL::Solver<RealT>> solver;
196 std::vector<RealT> vals(6);
197
198 // SPG
199 obj->reset();
200 x->zero();
201 parlist->sublist("Step").set("Type","Spectral Gradient");
202 Teuchos::Time spgTimer("SPG Time", true);
203 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
204 solver->solve(*outStream);
205 spgTimer.stop();
206 *outStream << "Total optimization time = " << spgTimer.totalElapsedTime() << " seconds.\n";
207 obj->summarize(*outStream);
208 vals[0] = solver->getAlgorithmState()->value;
209
210 // PQN
211 obj->reset();
212 x->zero();
213 parlist->sublist("Step").set("Type","Line Search");
214 parlist->sublist("Step").sublist("Line Search").sublist("Descent Method").set("Type","Quasi-Newton Method");
215 Teuchos::Time pqnTimer("PQN Time", true);
216 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
217 solver->solve(*outStream);
218 pqnTimer.stop();
219 *outStream << "Total optimization time = " << pqnTimer.totalElapsedTime() << " seconds.\n";
220 obj->summarize(*outStream);
221 vals[1] = solver->getAlgorithmState()->value;
222
223 // LMTR
224 obj->reset();
225 x->zero();
226 parlist->sublist("Step").set("Type","Trust Region");
227 parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","Lin-More");
228 Teuchos::Time lmtrTimer("LMTR Time", true);
229 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
230 solver->solve(*outStream);
231 lmtrTimer.stop();
232 *outStream << "Total optimization time = " << lmtrTimer.totalElapsedTime() << " seconds.\n";
233 obj->summarize(*outStream);
234 vals[2] = solver->getAlgorithmState()->value;
235
236 // TRSPG
237 obj->reset();
238 x->zero();
239 parlist->sublist("Step").set("Type","Trust Region");
240 parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","SPG");
241 Teuchos::Time trspgTimer("TRSPG Time", true);
242 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
243 solver->solve(*outStream);
244 trspgTimer.stop();
245 *outStream << "Total optimization time = " << trspgTimer.totalElapsedTime() << " seconds.\n";
246 obj->summarize(*outStream);
247 vals[3] = solver->getAlgorithmState()->value;
248
249 // AL-LMTR
250 obj->reset();
251 x->zero();
252 mul->zero();
253 prob->edit();
254 prob->finalize(true,true,*outStream);
255 parlist->sublist("Step").set("Type","Augmented Lagrangian");
256 parlist->sublist("Step").sublist("Augmented Lagrangian").set("Subproblem Step Type","Trust Region");
257 parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","Lin-More");
258 Teuchos::Time allmtrTimer("AL-LMTR Time", true);
259 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
260 solver->solve(*outStream);
261 allmtrTimer.stop();
262 *outStream << "Total optimization time = " << allmtrTimer.totalElapsedTime() << " seconds.\n";
263 obj->summarize(*outStream);
264 vals[4] = solver->getAlgorithmState()->value;
265
266 // AL-TRSPG
267 obj->reset();
268 x->zero();
269 mul->zero();
270 parlist->sublist("Step").set("Type","Augmented Lagrangian");
271 parlist->sublist("Step").sublist("Augmented Lagrangian").set("Subproblem Step Type","Trust Region");
272 parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","SPG");
273 Teuchos::Time altrspgTimer("AL-TRSPG Time", true);
274 solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist);
275 solver->solve(*outStream);
276 altrspgTimer.stop();
277 *outStream << "Total optimization time = " << altrspgTimer.totalElapsedTime() << " seconds.\n";
278 obj->summarize(*outStream);
279 vals[5] = solver->getAlgorithmState()->value;
280
281 RealT minval = *std::min_element(vals.begin(),vals.end());
282 *outStream << std::scientific << std::setprecision(16);
283 *outStream << "SPG " << (vals[0]-minval)/minval << std::endl;
284 *outStream << "PQN " << (vals[1]-minval)/minval << std::endl;
285 *outStream << "LMTR " << (vals[2]-minval)/minval << std::endl;
286 *outStream << "TRSPG " << (vals[3]-minval)/minval << std::endl;
287 *outStream << "AL-LMTR " << (vals[4]-minval)/minval << std::endl;
288 *outStream << "AL-TRSPG " << (vals[5]-minval)/minval << std::endl;
289 *outStream << std::endl;
290 }
291 catch (std::logic_error& err) {
292 *outStream << err.what() << "\n";
293 errorFlag = -1000;
294 }; // end try
295
296 if (errorFlag != 0)
297 std::cout << "End Result: TEST FAILED\n";
298 else
299 std::cout << "End Result: TEST PASSED\n";
300
301 return 0;
302
303 }
304
305