ROL
algorithm/ROL_Problem.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_PROBLEM_HPP
45 #define ROL_PROBLEM_HPP
46 
47 #include <utility>
48 #include <unordered_map>
49 
50 #include "ROL_Ptr.hpp"
51 #include "ROL_Types.hpp"
56 
57 namespace ROL {
58 
59 template<typename Real>
60 class Problem {
61 private:
62  bool isFinalized_;
63  bool hasBounds_;
64  bool hasEquality_;
65  bool hasInequality_;
66  bool hasLinearEquality_;
68  unsigned cnt_econ_;
69  unsigned cnt_icon_;
70  unsigned cnt_linear_econ_;
71  unsigned cnt_linear_icon_;
72 
73  ParameterList ppa_list_;
74 
75  Ptr<Objective<Real>> obj_;
76  Ptr<Vector<Real>> xprim_;
77  Ptr<Vector<Real>> xdual_;
78  Ptr<BoundConstraint<Real>> bnd_;
79  Ptr<Constraint<Real>> con_;
80  Ptr<Vector<Real>> mul_;
81  Ptr<Vector<Real>> res_;
82  Ptr<PolyhedralProjection<Real>> proj_;
83 
84  Ptr<Vector<Real>> xfeas_;
85  Ptr<ReduceLinearConstraint<Real>> rlc_;
86 
88 
89 protected:
90 
91  Ptr<Objective<Real>> INPUT_obj_;
92  Ptr<Vector<Real>> INPUT_xprim_;
93  Ptr<Vector<Real>> INPUT_xdual_;
94  Ptr<BoundConstraint<Real>> INPUT_bnd_;
95  std::unordered_map<std::string,ConstraintData<Real>> INPUT_con_;
96  std::unordered_map<std::string,ConstraintData<Real>> INPUT_linear_con_;
97 
98 public:
99  virtual ~Problem() {}
100 
107  Problem( const Ptr<Objective<Real>> &obj,
108  const Ptr<Vector<Real>> &x,
109  const Ptr<Vector<Real>> &g = nullPtr);
110 
115  Problem(const Problem &problem)
116  : isFinalized_(false),
117  hasBounds_(problem.hasBounds_),
118  hasEquality_(problem.hasEquality_),
122  cnt_econ_(problem.cnt_econ_),
123  cnt_icon_(problem.cnt_icon_),
126  ppa_list_(problem.ppa_list_),
127  INPUT_obj_(problem.INPUT_obj_),
128  INPUT_xprim_(problem.INPUT_xprim_),
129  INPUT_xdual_(problem.INPUT_xdual_),
130  INPUT_bnd_(problem.INPUT_bnd_),
131  INPUT_con_(problem.INPUT_con_),
133 
134  /***************************************************************************/
135  /*** Set and remove methods for constraints ********************************/
136  /***************************************************************************/
137 
142  void addBoundConstraint(const Ptr<BoundConstraint<Real>> &bnd);
143 
146  void removeBoundConstraint();
147 
156  void addConstraint(std::string name,
157  const Ptr<Constraint<Real>> &econ,
158  const Ptr<Vector<Real>> &emul,
159  const Ptr<Vector<Real>> &eres = nullPtr,
160  bool reset = false);
161 
171  void addConstraint(std::string name,
172  const Ptr<Constraint<Real>> &icon,
173  const Ptr<Vector<Real>> &imul,
174  const Ptr<BoundConstraint<Real>> &ibnd,
175  const Ptr<Vector<Real>> &ires = nullPtr,
176  bool reset = false);
177 
182  void removeConstraint(std::string name);
183 
192  void addLinearConstraint(std::string name,
193  const Ptr<Constraint<Real>> &linear_econ,
194  const Ptr<Vector<Real>> &linear_emul,
195  const Ptr<Vector<Real>> &linear_eres = nullPtr,
196  bool reset = false);
197 
207  void addLinearConstraint(std::string name,
208  const Ptr<Constraint<Real>> &linear_icon,
209  const Ptr<Vector<Real>> &linear_imul,
210  const Ptr<BoundConstraint<Real>> &linear_ibnd,
211  const Ptr<Vector<Real>> &linear_ires = nullPtr,
212  bool reset = false);
213 
218  void removeLinearConstraint(std::string name);
219 
224  void setProjectionAlgorithm(ParameterList &parlist);
225 
226  /***************************************************************************/
227  /*** Accessor methods ******************************************************/
228  /***************************************************************************/
229 
232  const Ptr<Objective<Real>>& getObjective();
233 
236  const Ptr<Vector<Real>>& getPrimalOptimizationVector();
237 
240  const Ptr<Vector<Real>>& getDualOptimizationVector();
241 
244  const Ptr<BoundConstraint<Real>>& getBoundConstraint();
245 
248  const Ptr<Constraint<Real>>& getConstraint();
249 
252  const Ptr<Vector<Real>>& getMultiplierVector();
253 
256  const Ptr<Vector<Real>>& getResidualVector();
257 
261  const Ptr<PolyhedralProjection<Real>>& getPolyhedralProjection();
262 
266 
267  /***************************************************************************/
268  /*** Consistency checks ****************************************************/
269  /***************************************************************************/
270 
281  Real checkLinearity(bool printToStream = false, std::ostream &outStream = std::cout) const;
282 
288  void checkVectors(bool printToStream = false, std::ostream &outStream = std::cout) const;
289 
295  void checkDerivatives(bool printToStream = false, std::ostream &outStream = std::cout, const Ptr<Vector<Real>> &x0 = nullPtr, Real scale = Real(1)) const;
296 
303  void check(bool printToStream = false, std::ostream &outStream = std::cout, const Ptr<Vector<Real>> &x0 = nullPtr, Real scale = Real(1)) const;
304 
305  /***************************************************************************/
306  /*** Finalize and edit methods *********************************************/
307  /***************************************************************************/
308 
317  virtual void finalize(bool lumpConstraints = false, bool printToStream = false,
318  std::ostream &outStream = std::cout);
319 
322  bool isFinalized() const;
323 
326  virtual void edit();
327 
331  void finalizeIteration();
332 
333 }; // class Problem
334 
335 } // namespace ROL
336 
337 #include "ROL_Problem_Def.hpp"
338 
339 #endif // ROL_NEWOPTIMIZATIONPROBLEM_HPP
void check(bool printToStream=false, std::ostream &outStream=std::cout) const
Run vector, linearity and derivative checks for user-supplied vectors, objective function and constra...
Provides the interface to evaluate objective functions.
const Ptr< Constraint< Real > > & getConstraint()
Get the equality constraint.
Ptr< BoundConstraint< Real > > bnd_
void checkDerivatives(bool printToStream=false, std::ostream &outStream=std::cout) const
Run derivative checks for user-supplied objective function and constraints.
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
void addBoundConstraint(const Ptr< BoundConstraint< Real >> &bnd)
Add a bound constraint.
void addLinearConstraint(std::string name, const Ptr< Constraint< Real >> &linear_econ, const Ptr< Vector< Real >> &linear_emul, const Ptr< Vector< Real >> &linear_eres=nullPtr, bool reset=false)
Add a linear equality constraint.
void removeBoundConstraint()
Remove an existing bound constraint.
Contains definitions of custom data types in ROL.
void removeLinearConstraint(std::string name)
Remove an existing linear constraint.
const Ptr< BoundConstraint< Real > > & getBoundConstraint()
Get the bound constraint.
Ptr< ReduceLinearConstraint< Real > > rlc_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void finalize(bool lumpConstraints=false, bool printToStream=false, std::ostream &outStream=std::cout)
Tranform user-supplied constraints to consist of only bounds and equalities. Optimization problem can...
Ptr< Objective< Real > > INPUT_obj_
Ptr< Objective< Real > > obj_
bool isFinalized() const
Indicate whether or no finalize has been called.
Ptr< Vector< Real > > res_
Ptr< Vector< Real > > INPUT_xprim_
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
Problem(const Ptr< Objective< Real >> &obj, const Ptr< Vector< Real >> &x, const Ptr< Vector< Real >> &g=nullPtr)
Default constructor for OptimizationProblem.
std::unordered_map< std::string, ConstraintData< Real > > INPUT_linear_con_
std::unordered_map< std::string, ConstraintData< Real > > INPUT_con_
Ptr< Vector< Real > > INPUT_xdual_
const Ptr< Vector< Real > > & getResidualVector()
Get the primal constraint space vector.
const Ptr< Vector< Real > > & getMultiplierVector()
Get the dual constraint space vector.
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
Ptr< Vector< Real > > xdual_
Problem(const Problem &problem)
Copy constructor for OptimizationProblem.
Ptr< Constraint< Real > > con_
Provides the interface to apply upper and lower bound constraints.
Real checkLinearity(bool printToStream=false, std::ostream &outStream=std::cout) const
Check if user-supplied linear constraints are affine.
void addConstraint(std::string name, const Ptr< Constraint< Real >> &econ, const Ptr< Vector< Real >> &emul, const Ptr< Vector< Real >> &eres=nullPtr, bool reset=false)
Add an equality constraint.
const Ptr< PolyhedralProjection< Real > > & getPolyhedralProjection()
Get the polyhedral projection object. This is a null pointer if no linear constraints and/or bounds a...
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
void removeConstraint(std::string name)
Remove an existing constraint.
Ptr< Vector< Real > > xfeas_
Ptr< BoundConstraint< Real > > INPUT_bnd_
virtual void edit()
Resume editting optimization problem after finalize has been called.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
void checkVectors(bool printToStream=false, std::ostream &outStream=std::cout) const
Run vector checks for user-supplied vectors.
EProblem
Definition: ROL_Types.hpp:257
Ptr< Vector< Real > > xprim_
Ptr< Vector< Real > > mul_
Defines the general constraint operator interface.
void setProjectionAlgorithm(ParameterList &parlist)
Set polyhedral projection algorithm.
Ptr< PolyhedralProjection< Real > > proj_