| 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 NONLINEAR_PROGRAM_HPP |
| 11 |
#define NONLINEAR_PROGRAM_HPP |
| 12 |
|
| 13 |
#include "Sacado.hpp" |
| 14 |
|
| 15 |
#include "ROL_StdVector.hpp" |
| 16 |
#include "ROL_Bounds.hpp" |
| 17 |
#include "ROL_PartitionedVector.hpp" |
| 18 |
#include "ROL_OptimizationProblem.hpp" |
| 19 |
#include "ROL_Sacado_StdObjective.hpp" |
| 20 |
#include "ROL_Sacado_StdConstraint.hpp" |
| 21 |
#include <cmath> |
| 22 |
|
| 23 |
/* \class ROL::NonlinearProgram |
| 24 |
* \brief Provides a simple interface to formulating and solving |
| 25 |
* modest sized nonlinear programs consisting of objective functions with |
| 26 |
* optional equality, inequality, and bound constraints. Creates |
| 27 |
* OptimizationProblem objects which encapsulate the quantities needed |
| 28 |
* to represent a problem as Type-U, Type-E, Type-B, or Type-EB. |
| 29 |
* |
| 30 |
* Automatic differentiation to obtain gradients, Hessians, and Jacobians |
| 31 |
* is provided by Sacado, so the user only needs to implement value functions |
| 32 |
* for the objective and constraints and provide upper and lower bounds |
| 33 |
* when they exist. |
| 34 |
* |
| 35 |
* NonlinearProgram assumes the user's Objective and Constraints are |
| 36 |
* implemented using ROL::StdVector |
| 37 |
*/ |
| 38 |
|
| 39 |
namespace ROL { |
| 40 |
|
| 41 |
template<class Real> |
| 42 |
class NonlinearProgram { |
| 43 |
|
| 44 |
template <typename T> using vector = std::vector<T>; |
| 45 |
|
| 46 |
protected: |
| 47 |
|
| 48 |
typedef Vector<Real> V; |
| 49 |
typedef StdVector<Real> SV; |
| 50 |
typedef PartitionedVector<Real> PV; |
| 51 |
|
| 52 |
typedef Objective<Real> OBJ; |
| 53 |
typedef Constraint<Real> CON; |
| 54 |
typedef Bounds<Real> BND; |
| 55 |
typedef OptimizationProblem<Real> OPT; |
| 56 |
|
| 57 |
typedef typename vector<Real>::size_type size_type; |
| 58 |
|
| 59 |
private: |
| 60 |
|
| 61 |
ROL::Ptr<vector<Real> > lp_; |
| 62 |
ROL::Ptr<vector<Real> > up_; |
| 63 |
ROL::Ptr<BND> bnd_; |
| 64 |
|
| 65 |
protected: |
| 66 |
|
| 67 |
virtual ~NonlinearProgram(){} |
| 68 |
|
| 69 |
/* Set the ith element of the of lower bound equal to val \f$\ell_i=\text{val}\f$ |
| 70 |
@param[in] i is the element to set |
| 71 |
@param[in] val is the value to set it to |
| 72 |
*/ |
| 73 |
void setLower(int i, Real val) { |
| 74 |
(*lp_)[i] = val; |
| 75 |
} |
| 76 |
|
| 77 |
/* Set the ith element of the of upper bound equal to val \f$u_=\text{val}\f$ |
| 78 |
@param[in] i is the element to set |
| 79 |
@param[in] val is the value to set it to |
| 80 |
*/ |
| 81 |
void setUpper(int i, Real val) { |
| 82 |
(*up_)[i] = val; |
| 83 |
} |
| 84 |
|
| 85 |
/* Set the lower bound to l */ |
| 86 |
void setLower(Real l[]) { |
| 87 |
lp_ = ROL::makePtr<vector<Real>>(l, l+dimension_x() ); |
| 88 |
} |
| 89 |
|
| 90 |
/* Set the upper bound to u */ |
| 91 |
void setUpper(Real u[]) { |
| 92 |
up_ = ROL::makePtr<vector<Real>>(u, u+dimension_x() ); |
| 93 |
} |
| 94 |
|
| 95 |
/* \brief Call this function in your problem's constructor to turn |
| 96 |
off bound constraints */ |
| 97 |
void noBound() { |
| 98 |
bnd_->deactivate(); |
| 99 |
} |
| 100 |
|
| 101 |
/* \brief Return the objective function. */ |
| 102 |
virtual const ROL::Ptr<OBJ> getObjective() = 0; |
| 103 |
|
| 104 |
/* \brief Return the equality constraint. Do not overload if you |
| 105 |
have no equality constraint. */ |
| 106 |
virtual const ROL::Ptr<CON> getEqualityConstraint() { |
| 107 |
return ROL::nullPtr; |
| 108 |
} |
| 109 |
|
| 110 |
/* \brief Return the equality constraint vector (used for cloning). |
| 111 |
Returns a ROL::nullPtr if there is no equality constraint */ |
| 112 |
const ROL::Ptr<V> getEqualityMultiplier() { |
| 113 |
int n = dimension_ce(); |
| 114 |
if( n > 0 ) { |
| 115 |
ROL::Ptr<vector<Real> > l = ROL::makePtr<vector<Real>>(n,1.0); |
| 116 |
return ROL::makePtr<SV>(l); |
| 117 |
} |
| 118 |
else { |
| 119 |
return ROL::nullPtr; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* \brief Return the inequality constraint. Do not overload if you |
| 124 |
have no inequality constraint. */ |
| 125 |
virtual const ROL::Ptr<CON> getInequalityConstraint() { |
| 126 |
return ROL::nullPtr; |
| 127 |
} |
| 128 |
|
| 129 |
/* \brief Return the inequality constraint vector (used for cloning). |
| 130 |
Returns a ROL::nullPtr if there is no inequality constraint */ |
| 131 |
const ROL::Ptr<V> getInequalityMultiplier() { |
| 132 |
int n = dimension_ci(); |
| 133 |
if( n > 0 ) { |
| 134 |
ROL::Ptr<vector<Real> > l = ROL::makePtr<vector<Real>>(n,1.0); |
| 135 |
return ROL::makePtr<SV>(l); |
| 136 |
} |
| 137 |
else { |
| 138 |
return ROL::nullPtr; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/* \brief Return the bounds on the inequality constraint. |
| 143 |
Returns a ROL::nullPtr if there is no inequality constraint */ |
| 144 |
const ROL::Ptr<BND> getInequalityBoundConstraint() { |
| 145 |
int n = dimension_ci(); |
| 146 |
if( n > 0 ) { |
| 147 |
const Real lval(0), uval(ROL_INF<Real>()); |
| 148 |
ROL::Ptr<V> l = ROL::makePtr<SV>( |
| 149 |
ROL::makePtr<vector<Real>>(n,lval) ); |
| 150 |
ROL::Ptr<V> u = ROL::makePtr<SV>( |
| 151 |
ROL::makePtr<vector<Real>>(n,uval) ); |
| 152 |
return ROL::makePtr<BND>(l,u); |
| 153 |
} |
| 154 |
else { |
| 155 |
return ROL::nullPtr; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/* \brief Create vector */ |
| 160 |
ROL::Ptr<V> createOptVector( const Real * const array ) { |
| 161 |
ROL::Ptr<vector<Real> > x = |
| 162 |
ROL::makePtr<vector<Real>>(array,array+dimension_x()); |
| 163 |
return ROL::makePtr<SV>( x ); |
| 164 |
} |
| 165 |
|
| 166 |
// Create an ROL::Ptr to a std::vector with dimensionality of the optimization space |
| 167 |
// ROL::Ptr<vector<Real> > createOptVector() { |
| 168 |
// int n = dimension_x(); |
| 169 |
// ROL::Ptr<V> x = ROL::makePtr<vector<Real>>(n); |
| 170 |
// return x; |
| 171 |
// } |
| 172 |
|
| 173 |
// Default deactivated bound |
| 174 |
ROL::Ptr<BND> getBoundConstraint() { |
| 175 |
return bnd_; |
| 176 |
} |
| 177 |
|
| 178 |
public: |
| 179 |
/** \brief Return the dimension of the optimization (and bound) vectors. */ |
| 180 |
virtual int dimension_x() { return 1; } |
| 181 |
|
| 182 |
/** \brief Return the dimension of the equality constraint vector */ |
| 183 |
virtual int dimension_ce() { return 0; } |
| 184 |
|
| 185 |
/** \brief Return the dimension of the inequality constrant vector */ |
| 186 |
virtual int dimension_ci() { return 0; } |
| 187 |
|
| 188 |
|
| 189 |
NonlinearProgram(int n) { |
| 190 |
// Create lower and upper bounds of negative and positive infinity respectively |
| 191 |
lp_ = ROL::makePtr<vector<Real>>(n,ROL::ROL_NINF<Real>()); |
| 192 |
up_ = ROL::makePtr<vector<Real>>(n,ROL::ROL_INF<Real>()); |
| 193 |
ROL::Ptr<V> l = ROL::makePtr<SV>( lp_ ); |
| 194 |
ROL::Ptr<V> u = ROL::makePtr<SV>( up_ ); |
| 195 |
bnd_ = ROL::makePtr<BND>( l, u ); |
| 196 |
} |
| 197 |
|
| 198 |
/* \brief Create the OptimizationProblem from the supplied components and |
| 199 |
return it */ |
| 200 |
ROL::Ptr<OPT> getOptimizationProblem() { |
| 201 |
ROL::Ptr<V> x = getInitialGuess()->clone(); |
| 202 |
x->set(*getInitialGuess()); |
| 203 |
return ROL::makePtr<OPT>( getObjective(), |
| 204 |
x, |
| 205 |
getBoundConstraint(), |
| 206 |
getEqualityConstraint(), |
| 207 |
getEqualityMultiplier(), |
| 208 |
getInequalityConstraint(), |
| 209 |
getInequalityMultiplier(), |
| 210 |
getInequalityBoundConstraint() ); |
| 211 |
} |
| 212 |
|
| 213 |
/* \brief Return the initial guess for the optimization vector */ |
| 214 |
virtual const ROL::Ptr<const V> getInitialGuess() = 0; |
| 215 |
|
| 216 |
/* \brief Return whether or not the initial guess is feasible */ |
| 217 |
virtual bool initialGuessIsFeasible() = 0; |
| 218 |
|
| 219 |
/* \brief Return the value of the Objective function for the initial guess */ |
| 220 |
virtual Real getInitialObjectiveValue() = 0; |
| 221 |
|
| 222 |
/* \brief Return the set of vectors that solve the nonlinear program if |
| 223 |
they are known */ |
| 224 |
virtual ROL::Ptr<const V> getSolutionSet() { return ROL::nullPtr; } |
| 225 |
|
| 226 |
/* \brief Return the value of the objective function for a solution vector. |
| 227 |
If not known, return infinity */ |
| 228 |
virtual Real getSolutionObjectiveValue() { |
| 229 |
return ROL_INF<Real>(); |
| 230 |
}; |
| 231 |
|
| 232 |
/* \brief If the problem has known solutions, return whether ROL |
| 233 |
has acceptibly solved problem */ |
| 234 |
bool foundAcceptableSolution( const V &x, const Real &tolerance=std::sqrt(ROL_EPSILON<Real>()) ) { |
| 235 |
ROL::Ptr<const PV> sol = ROL::dynamicPtrCast<const PV>( getSolutionSet() ); |
| 236 |
ROL::Ptr<V> error; |
| 237 |
ROL::Ptr<const V> xv; |
| 238 |
if ( dimension_ci() > 0 ) { |
| 239 |
xv = dynamic_cast<const PV&>(x).get(0); |
| 240 |
error = xv->clone(); |
| 241 |
} |
| 242 |
else { |
| 243 |
xv = ROL::makePtrFromRef(x); |
| 244 |
error = x.clone(); |
| 245 |
} |
| 246 |
|
| 247 |
Real minerror = ROL::ROL_INF<Real>(); |
| 248 |
for( size_type k=0; k<sol->numVectors(); ++k) { |
| 249 |
error->set(*xv); |
| 250 |
error->axpy(-1.0,*(sol->get(k))); |
| 251 |
minerror = std::min(minerror,error->norm()); |
| 252 |
} |
| 253 |
return (minerror < tolerance); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
}; // class NonlinearProgram |
| 259 |
|
| 260 |
} // namespace ROL |
| 261 |
|
| 262 |
#endif // NONLINEAR_PROGRAM_HPP |