44#ifndef ROL_TYPEU_ALGORITHM_DEF_H
45#define ROL_TYPEU_ALGORITHM_DEF_H
54template<
typename Real>
56 : status_(makePtr<CombinedStatusTest<Real>>()),
59 status_->add(makePtr<StatusTest<Real>>());
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();
67 state_->iterateVec->set(x);
68 if (state_->stepVec == nullPtr) {
69 state_->stepVec = x.clone();
71 state_->stepVec->zero();
72 if (state_->gradientVec == nullPtr) {
73 state_->gradientVec = g.clone();
75 state_->gradientVec->set(g);
76 if (state_->minIterVec == nullPtr) {
77 state_->minIterVec = x.clone();
79 state_->minIterVec->set(x);
80 state_->minIter = state_->iter;
81 state_->minValue = state_->value;
84template<
typename Real>
85void Algorithm<Real>::setStatusTest(
const Ptr<StatusTest<Real>> &status,
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(),
101 problem.finalizeIteration();
104 throw Exception::NotImplemented(
">>> ROL::TypeU::Algorithm::run : Optimization problem is not Type U!");
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);
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);
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();
136 run(*s,g,*rlc.transform(makePtrFromRef(obj)),outStream);
138 x.plus(*rlc.getFeasibleVector());
141template<
typename Real>
142void Algorithm<Real>::writeHeader( std::ostream& os )
const {
143 std::stringstream 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";
155template<
typename Real>
156void Algorithm<Real>::writeName( std::ostream& os )
const {
157 throw Exception::NotImplemented(
">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
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 ) {
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;
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;
185template<
typename Real>
186void Algorithm<Real>::writeExitStatus( std::ostream& os )
const {
187 std::stringstream hist;
188 hist <<
"Optimization Terminated with Status: ";
194template<
typename Real>
196Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState()
const {
200template<
typename Real>
201void 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)