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