10#ifndef ROL_TYPEU_ALGORITHM_DEF_H
11#define ROL_TYPEU_ALGORITHM_DEF_H
20template<
typename Real>
22 : status_(makePtr<CombinedStatusTest<Real>>()),
25 status_->add(makePtr<StatusTest<Real>>());
28template<
typename Real>
29void Algorithm<Real>::initialize(
const Vector<Real> &x,
const Vector<Real> &g) {
30 if (state_->iterateVec == nullPtr) {
31 state_->iterateVec = x.clone();
33 state_->iterateVec->set(x);
34 if (state_->stepVec == nullPtr) {
35 state_->stepVec = x.clone();
37 state_->stepVec->zero();
38 if (state_->gradientVec == nullPtr) {
39 state_->gradientVec = g.clone();
41 state_->gradientVec->set(g);
42 if (state_->minIterVec == nullPtr) {
43 state_->minIterVec = x.clone();
45 state_->minIterVec->set(x);
46 state_->minIter = state_->iter;
47 state_->minValue = state_->value;
50template<
typename Real>
51void Algorithm<Real>::setStatusTest(
const Ptr<StatusTest<Real>> &status,
59template<
typename Real>
60void Algorithm<Real>::run( Problem<Real> &problem,
61 std::ostream &outStream ) {
62 if (problem.getProblemType() == TYPE_U) {
63 run(*problem.getPrimalOptimizationVector(),
64 *problem.getDualOptimizationVector(),
65 *problem.getObjective(),
67 problem.finalizeIteration();
70 throw Exception::NotImplemented(
">>> ROL::TypeU::Algorithm::run : Optimization problem is not Type U!");
74template<
typename Real>
75void Algorithm<Real>::run( Vector<Real> &x,
77 std::ostream &outStream ) {
78 run(x,x.dual(),obj,outStream);
81template<
typename Real>
82void Algorithm<Real>::run( Vector<Real> &x,
84 Constraint<Real> &linear_con,
85 Vector<Real> &linear_mul,
86 std::ostream &outStream ) {
87 run(x,x.dual(),obj,linear_con,linear_mul,linear_mul.dual(),outStream);
90template<
typename Real>
91void Algorithm<Real>::run( Vector<Real> &x,
92 const Vector<Real> &g,
94 Constraint<Real> &linear_con,
95 Vector<Real> &linear_mul,
96 const Vector<Real> &linear_c,
97 std::ostream &outStream ) {
98 Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
99 ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_con),xfeas,makePtrFromRef(linear_c));
100 Ptr<Vector<Real>> s = x.clone(); s->zero();
102 run(*s,g,*rlc.transform(makePtrFromRef(obj)),outStream);
104 x.plus(*rlc.getFeasibleVector());
107template<
typename Real>
108void Algorithm<Real>::writeHeader( std::ostream& os )
const {
109 std::ios_base::fmtflags osFlags(os.flags());
111 os << std::setw(6) << std::left <<
"iter";
112 os << std::setw(15) << std::left <<
"value";
113 os << std::setw(15) << std::left <<
"gnorm";
114 os << std::setw(15) << std::left <<
"snorm";
115 os << std::setw(10) << std::left <<
"#fval";
116 os << std::setw(10) << std::left <<
"#grad";
121template<
typename Real>
122void Algorithm<Real>::writeName( std::ostream& os )
const {
123 throw Exception::NotImplemented(
">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
126template<
typename Real>
127void Algorithm<Real>::writeOutput( std::ostream& os,
bool write_header )
const {
128 std::ios_base::fmtflags osFlags(os.flags());
129 os << std::scientific << std::setprecision(6);
130 if ( write_header ) writeHeader(os);
131 if ( state_->iter == 0 ) {
133 os << std::setw(6) << std::left << state_->iter;
134 os << std::setw(15) << std::left << state_->value;
135 os << std::setw(15) << std::left << state_->gnorm;
140 os << std::setw(6) << std::left << state_->iter;
141 os << std::setw(15) << std::left << state_->value;
142 os << std::setw(15) << std::left << state_->gnorm;
143 os << std::setw(15) << std::left << state_->snorm;
144 os << std::setw(10) << std::left << state_->nfval;
145 os << std::setw(10) << std::left << state_->ngrad;
151template<
typename Real>
152void Algorithm<Real>::writeExitStatus( std::ostream& os )
const {
153 std::ios_base::fmtflags osFlags(os.flags());
154 os <<
"Optimization Terminated with Status: ";
160template<
typename Real>
162Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState()
const {
166template<
typename Real>
167void Algorithm<Real>::reset() {
Contains definitions of custom data types in ROL.
Algorithm()
Constructor, given a step and a status test.
std::string EExitStatusToString(EExitStatus tr)