ROL
ROL_TypeU_Algorithm_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_TYPEU_ALGORITHM_DEF_H
45#define ROL_TYPEU_ALGORITHM_DEF_H
46
47#include "ROL_Types.hpp"
50
51namespace ROL {
52namespace TypeU {
53
54template<typename Real>
56 : status_(makePtr<CombinedStatusTest<Real>>()),
57 state_(makePtr<AlgorithmState<Real>>()) {
58 status_->reset();
59 status_->add(makePtr<StatusTest<Real>>());
60}
61
62template<typename Real>
63void Algorithm<Real>::initialize(const Vector<Real> &x, const Vector<Real> &g) {
64 if (state_->iterateVec == nullPtr) {
65 state_->iterateVec = x.clone();
66 }
67 state_->iterateVec->set(x);
68 if (state_->stepVec == nullPtr) {
69 state_->stepVec = x.clone();
70 }
71 state_->stepVec->zero();
72 if (state_->gradientVec == nullPtr) {
73 state_->gradientVec = g.clone();
74 }
75 state_->gradientVec->set(g);
76 if (state_->minIterVec == nullPtr) {
77 state_->minIterVec = x.clone();
78 }
79 state_->minIterVec->set(x);
80 state_->minIter = state_->iter;
81 state_->minValue = state_->value;
82}
83
84template<typename Real>
85void Algorithm<Real>::setStatusTest(const Ptr<StatusTest<Real>> &status,
86 bool combineStatus) {
87 if (!combineStatus) { // Do not combine status tests
88 status_->reset();
89 }
90 status_->add(status); // Add user-defined StatusTest
91}
92
93template<typename Real>
94void Algorithm<Real>::run( Problem<Real> &problem,
95 std::ostream &outStream ) {
96 if (problem.getProblemType() == TYPE_U) {
97 run(*problem.getPrimalOptimizationVector(),
98 *problem.getDualOptimizationVector(),
99 *problem.getObjective(),
100 outStream);
101 problem.finalizeIteration();
102 }
103 else {
104 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::run : Optimization problem is not Type U!");
105 }
106}
107
108template<typename Real>
109void Algorithm<Real>::run( Vector<Real> &x,
110 Objective<Real> &obj,
111 std::ostream &outStream ) {
112 run(x,x.dual(),obj,outStream);
113}
114
115template<typename Real>
116void Algorithm<Real>::run( Vector<Real> &x,
117 Objective<Real> &obj,
118 Constraint<Real> &linear_con,
119 Vector<Real> &linear_mul,
120 std::ostream &outStream ) {
121 run(x,x.dual(),obj,linear_con,linear_mul,linear_mul.dual(),outStream);
122}
123
124template<typename Real>
125void Algorithm<Real>::run( Vector<Real> &x,
126 const Vector<Real> &g,
127 Objective<Real> &obj,
128 Constraint<Real> &linear_con,
129 Vector<Real> &linear_mul,
130 const Vector<Real> &linear_c,
131 std::ostream &outStream ) {
132 Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
133 ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_con),xfeas,makePtrFromRef(linear_c));
134 Ptr<Vector<Real>> s = x.clone(); s->zero();
135
136 run(*s,g,*rlc.transform(makePtrFromRef(obj)),outStream);
137 rlc.project(x,*s);
138 x.plus(*rlc.getFeasibleVector());
139}
140
141template<typename Real>
142void Algorithm<Real>::writeHeader( std::ostream& os ) const {
143 std::stringstream hist;
144 hist << " ";
145 hist << std::setw(6) << std::left << "iter";
146 hist << std::setw(15) << std::left << "value";
147 hist << std::setw(15) << std::left << "gnorm";
148 hist << std::setw(15) << std::left << "snorm";
149 hist << std::setw(10) << std::left << "#fval";
150 hist << std::setw(10) << std::left << "#grad";
151 hist << std::endl;
152 os << hist.str();
153}
154
155template<typename Real>
156void Algorithm<Real>::writeName( std::ostream& os ) const {
157 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
158}
159
160template<typename Real>
161void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
162 std::stringstream hist;
163 hist << std::scientific << std::setprecision(6);
164 if ( write_header ) writeHeader(os);
165 if ( state_->iter == 0 ) {
166 hist << " ";
167 hist << std::setw(6) << std::left << state_->iter;
168 hist << std::setw(15) << std::left << state_->value;
169 hist << std::setw(15) << std::left << state_->gnorm;
170 hist << std::endl;
171 }
172 else {
173 hist << " ";
174 hist << std::setw(6) << std::left << state_->iter;
175 hist << std::setw(15) << std::left << state_->value;
176 hist << std::setw(15) << std::left << state_->gnorm;
177 hist << std::setw(15) << std::left << state_->snorm;
178 hist << std::setw(10) << std::left << state_->nfval;
179 hist << std::setw(10) << std::left << state_->ngrad;
180 hist << std::endl;
181 }
182 os << hist.str();
183}
184
185template<typename Real>
186void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
187 std::stringstream hist;
188 hist << "Optimization Terminated with Status: ";
189 hist << EExitStatusToString(state_->statusFlag);
190 hist << std::endl;
191 os << hist.str();
192}
193
194template<typename Real>
195//Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
196Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
197 return state_;
198}
199
200template<typename Real>
201void Algorithm<Real>::reset() {
202 state_->reset();
203}
204
205} // namespace TypeU
206} // namespace ROL
207
208#endif
Contains definitions of custom data types in ROL.
Algorithm()
Constructor, given a step and a status test.
std::string EExitStatusToString(EExitStatus tr)