ROL
ROL_TypeB_MoreauYosidaAlgorithm_Def.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_TYPEB_MOREAUYOSIDAALGORITHM_DEF_HPP
45 #define ROL_TYPEB_MOREAUYOSIDAALGORITHM_DEF_HPP
46 
48 
49 namespace ROL {
50 namespace TypeB {
51 
52 template<typename Real>
54  : TypeB::Algorithm<Real>::Algorithm(),
55  tau_(10), print_(false), list_(list), subproblemIter_(0) {
56  // Set status test
57  status_->reset();
58  status_->add(makePtr<StatusTest<Real>>(list));
59 
60  // Parse parameters
61  Real ten(10), oem6(1.e-6), oem8(1.e-8), oe8(1e8);
62  ParameterList& steplist = list.sublist("Step").sublist("Moreau-Yosida Penalty");
63  state_->searchSize = steplist.get("Initial Penalty Parameter", ten);
64  maxPenalty_ = steplist.get("Maximum Penalty Parameter", oe8);
65  tau_ = steplist.get("Penalty Parameter Growth Factor", ten);
66  updatePenalty_ = steplist.get("Update Penalty", true);
67  updateMultiplier_ = steplist.get("Update Multiplier", true);
68  print_ = steplist.sublist("Subproblem").get("Print History", false);
69  // Set parameters for step subproblem
70  Real gtol = steplist.sublist("Subproblem").get("Optimality Tolerance", oem8);
71  Real ctol = steplist.sublist("Subproblem").get("Feasibility Tolerance", oem8);
72  int maxit = steplist.sublist("Subproblem").get("Iteration Limit", 1000);
73  Real stol = oem6*std::min(gtol,ctol);
74  list_.sublist("Status Test").set("Gradient Tolerance", gtol);
75  list_.sublist("Status Test").set("Constraint Tolerance", ctol);
76  list_.sublist("Status Test").set("Step Tolerance", stol);
77  list_.sublist("Status Test").set("Iteration Limit", maxit);
78  // Get step name from parameterlist
79  stepname_ = steplist.sublist("Subproblem").get("Step Type","Trust Region");
80 
81  // Output settings
82  verbosity_ = list.sublist("General").get("Output Level", 0);
84  print_ = (verbosity_ > 2 ? true : print_);
85  list_.sublist("General").set("Output Level",(print_ ? verbosity_ : 0));
86 }
87 
88 template<typename Real>
90  const Vector<Real> &g,
93  Vector<Real> &pwa,
94  std::ostream &outStream) {
95  hasEcon_ = true;
96  if (proj_ == nullPtr) {
97  proj_ = makePtr<PolyhedralProjection<Real>>(makePtrFromRef(bnd));
98  hasEcon_ = false;
99  }
100  // Initialize data
102  // Initialize the algorithm state
103  state_->nfval = 0;
104  state_->ngrad = 0;
105  updateState(x,myobj,bnd,pwa,outStream);
106 }
107 
108 
109 template<typename Real>
113  Vector<Real> &pwa,
114  std::ostream &outStream) {
115  const Real one(1);
116  Real zerotol = std::sqrt(ROL_EPSILON<Real>());
117  // Update objective and constraint.
118  if (state_->iter == 0) {
119  myobj.update(x,UpdateType::Initial,state_->iter);
120  }
121  //else {
122  // myobj.update(x,UpdateType::Accept,state_->iter);
123  //}
124  // Compute norm of the gradient of the Lagrangian
125  state_->value = myobj.getObjectiveValue(x, zerotol);
126  myobj.getObjectiveGradient(*state_->gradientVec, x, zerotol);
127  //myobj.gradient(*state_->gradientVec, x, zerotol);
128  //gnorm_ = state_->gradientVec->norm();
129  pwa.set(x);
130  pwa.axpy(-one,state_->gradientVec->dual());
131  proj_->project(pwa,outStream);
132  pwa.axpy(-one,x);
133  gnorm_ = pwa.norm();
134  // Compute constraint violation
135  compViolation_ = myobj.testComplementarity(x);
136  state_->gnorm = std::max(gnorm_,compViolation_);
137  // Update state
138  state_->nfval++;
139  state_->ngrad++;
140 }
141 
142 template<typename Real>
144  const Vector<Real> &g,
145  Objective<Real> &obj,
147  std::ostream &outStream ) {
148  const Real one(1);
149  Ptr<Vector<Real>> pwa = x.clone();
150  // Initialize Moreau-Yosida data
151  MoreauYosidaObjective<Real> myobj(makePtrFromRef(obj),makePtrFromRef(bnd),
152  x,g,state_->searchSize,updateMultiplier_,
153  updatePenalty_);
154  initialize(x,g,myobj,bnd,*pwa,outStream);
155  Ptr<TypeU::Algorithm<Real>> algo;
156 
157  // Output
158  if (verbosity_ > 0) writeOutput(outStream,true);
159 
160  while (status_->check(*state_)) {
161  // Solve augmented Lagrangian subproblem
162  algo = TypeU::AlgorithmFactory<Real>(list_);
163  if (hasEcon_) algo->run(x,g,myobj,*proj_->getLinearConstraint(),
164  *proj_->getMultiplier(),*proj_->getResidual(),
165  outStream);
166  else algo->run(x,g,myobj,outStream);
167  subproblemIter_ = algo->getState()->iter;
168 
169  // Compute step
170  state_->stepVec->set(x);
171  state_->stepVec->axpy(-one,*state_->iterateVec);
172  state_->snorm = state_->stepVec->norm();
173 
174  // Update iterate and Lagrange multiplier
175  state_->iterateVec->set(x);
176 
177  // Update objective and constraint
178  state_->iter++;
179 
180  // Update state
181  updateState(x,myobj,bnd,*pwa,outStream);
182 
183  // Update multipliers
184  if (updatePenalty_) {
185  state_->searchSize *= tau_;
186  state_->searchSize = std::min(state_->searchSize,maxPenalty_);
187  }
188  myobj.updateMultipliers(state_->searchSize,x);
189 
190  state_->nfval += myobj.getNumberFunctionEvaluations() + algo->getState()->nfval;
191  state_->ngrad += myobj.getNumberGradientEvaluations() + algo->getState()->ngrad;
192 
193  // Update Output
194  if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
195  }
196  if (verbosity_ > 0) TypeB::Algorithm<Real>::writeExitStatus(outStream);
197 }
198 
199 template<typename Real>
200 void MoreauYosidaAlgorithm<Real>::writeHeader( std::ostream& os ) const {
201  std::stringstream hist;
202  if (verbosity_ > 1) {
203  hist << std::string(109,'-') << std::endl;
204  hist << "Moreau-Yosida Penalty Solver";
205  hist << " status output definitions" << std::endl << std::endl;
206  hist << " iter - Number of iterates (steps taken)" << std::endl;
207  hist << " fval - Objective function value" << std::endl;
208  hist << " gnorm - Norm of the gradient" << std::endl;
209  hist << " ifeas - Infeasibility metric" << std::endl;
210  hist << " snorm - Norm of the step (update to optimization vector)" << std::endl;
211  hist << " penalty - Penalty parameter for bound constraints" << std::endl;
212  hist << " #fval - Cumulative number of times the objective function was evaluated" << std::endl;
213  hist << " #grad - Cumulative number of times the gradient was computed" << std::endl;
214  hist << " subiter - Number of subproblem iterations" << std::endl;
215  hist << std::string(109,'-') << std::endl;
216  }
217 
218  hist << " ";
219  hist << std::setw(6) << std::left << "iter";
220  hist << std::setw(15) << std::left << "fval";
221  hist << std::setw(15) << std::left << "gnorm";
222  hist << std::setw(15) << std::left << "ifeas";
223  hist << std::setw(15) << std::left << "snorm";
224  hist << std::setw(10) << std::left << "penalty";
225  hist << std::setw(8) << std::left << "#fval";
226  hist << std::setw(8) << std::left << "#grad";
227  hist << std::setw(8) << std::left << "subIter";
228  hist << std::endl;
229  os << hist.str();
230 }
231 
232 template<typename Real>
233 void MoreauYosidaAlgorithm<Real>::writeName( std::ostream& os ) const {
234  std::stringstream hist;
235  hist << std::endl << " Moreau-Yosida Penalty Solver";
236  hist << std::endl;
237  os << hist.str();
238 }
239 
240 template<typename Real>
241 void MoreauYosidaAlgorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
242  std::stringstream hist;
243  hist << std::scientific << std::setprecision(6);
244  if ( state_->iter == 0 ) writeName(os);
245  if ( write_header ) writeHeader(os);
246  if ( state_->iter == 0 ) {
247  hist << " ";
248  hist << std::setw(6) << std::left << state_->iter;
249  hist << std::setw(15) << std::left << state_->value;
250  hist << std::setw(15) << std::left << gnorm_;
251  hist << std::setw(15) << std::left << compViolation_;
252  hist << std::setw(15) << std::left << "---";
253  hist << std::scientific << std::setprecision(2);
254  hist << std::setw(10) << std::left << state_->searchSize;
255  hist << std::scientific << std::setprecision(6);
256  hist << std::setw(8) << std::left << state_->nfval;
257  hist << std::setw(8) << std::left << state_->ngrad;
258  hist << std::setw(8) << std::left << "---";
259  hist << std::endl;
260  }
261  else {
262  hist << " ";
263  hist << std::setw(6) << std::left << state_->iter;
264  hist << std::setw(15) << std::left << state_->value;
265  hist << std::setw(15) << std::left << gnorm_;
266  hist << std::setw(15) << std::left << compViolation_;
267  hist << std::setw(15) << std::left << state_->snorm;
268  hist << std::scientific << std::setprecision(2);
269  hist << std::setw(10) << std::left << state_->searchSize;
270  hist << std::scientific << std::setprecision(6);
271  hist << std::setw(8) << std::left << state_->nfval;
272  hist << std::setw(8) << std::left << state_->ngrad;
273  hist << std::setw(8) << std::left << subproblemIter_;
274  hist << std::endl;
275  }
276  os << hist.str();
277 }
278 
279 } // namespace TypeB
280 } // namespace ROL
281 
282 #endif
Provides the interface to evaluate objective functions.
void writeHeader(std::ostream &os) const override
Print iterate header.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void initialize(Vector< Real > &x, const Vector< Real > &g, MoreauYosidaObjective< Real > &myobj, BoundConstraint< Real > &bnd, Vector< Real > &pwa, std::ostream &outStream=std::cout)
void writeOutput(std::ostream &os, bool write_header=false) const override
Print iterate status.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Real getObjectiveValue(const Vector< Real > &x, Real &tol)
void writeName(std::ostream &os) const override
Print step name.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void updateMultipliers(Real mu, const Vector< Real > &x)
void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update Moreau-Yosida penalty function.
Provides an interface to run bound constrained optimization algorithms.
Provides the interface to evaluate the Moreau-Yosida penalty function.
const Ptr< AlgorithmState< Real > > state_
Provides an interface to check status of optimization algorithms.
virtual void writeExitStatus(std::ostream &os) const
Provides the interface to apply upper and lower bound constraints.
void updateState(const Vector< Real > &x, MoreauYosidaObjective< Real > &myobj, BoundConstraint< Real > &bnd, Vector< Real > &pwa, std::ostream &outStream=std::cout)
void initialize(const Vector< Real > &x, const Vector< Real > &g)
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
void getObjectiveGradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
virtual Real norm() const =0
Returns where .
Real testComplementarity(const Vector< Real > &x)
const Ptr< CombinedStatusTest< Real > > status_