ROL
poisson-control/example_01.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
15#define USE_HESSVEC 1
16
17#include "ROL_Bounds.hpp"
19#include "ROL_Algorithm.hpp"
22#include "ROL_StatusTest.hpp"
23#include "ROL_Types.hpp"
24
25#include "ROL_Stream.hpp"
26#include "ROL_GlobalMPISession.hpp"
27
28#include <iostream>
29#include <algorithm>
30
31
32
33template <class Real>
34class StatusTest_PDAS : public ROL::StatusTest<Real> {
35private:
36
37 Real gtol_;
38 Real stol_;
40
41public:
42
43 virtual ~StatusTest_PDAS() {}
44
45 StatusTest_PDAS( Real gtol = 1.e-6, Real stol = 1.e-12, int max_iter = 100 ) :
46 gtol_(gtol), stol_(stol), max_iter_(max_iter) {}
47
50 virtual bool check( ROL::AlgorithmState<Real> &state ) {
51 if ( (state.gnorm > this->gtol_) &&
52 (state.snorm > this->stol_) &&
53 (state.iter < this->max_iter_) ) {
54 return true;
55 }
56 else {
57 if ( state.iter < 2 ) {
58 return true;
59 }
60 return false;
61 }
62 }
63
64};
65
66typedef double RealT;
67
68int main(int argc, char *argv[]) {
69
70 typedef std::vector<RealT> vector;
71 typedef ROL::Vector<RealT> V;
72 typedef ROL::StdVector<RealT> SV;
73
74 typedef typename vector::size_type luint;
75
76
77
78 ROL::GlobalMPISession mpiSession(&argc, &argv);
79
80 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
81 int iprint = argc - 1;
82 ROL::Ptr<std::ostream> outStream;
83 ROL::nullstream bhs; // outputs nothing
84 if (iprint > 0)
85 outStream = ROL::makePtrFromRef(std::cout);
86 else
87 outStream = ROL::makePtrFromRef(bhs);
88
89 int errorFlag = 0;
90
91 // *** Example body.
92
93 try {
94 luint dim = 256; // Set problem dimension.
95 RealT alpha = 1.e-4;
97
98 ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(dim);
99 ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim);
100
101 ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
102 ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
103
104 for ( luint i = 0; i < dim; i++ ) {
105 if ( i < dim/3.0 || i > 2*dim/3.0 ) {
106 (*l_ptr)[i] = 0.0;
107 (*u_ptr)[i] = 0.25;
108 }
109 else {
110 (*l_ptr)[i] = 0.75;
111 (*u_ptr)[i] = 1.0;
112 }
113 }
114 ROL::Bounds<RealT> icon(lo,up);
115
116 // Primal dual active set.
117 std::string filename = "input.xml";
118 auto parlist = ROL::getParametersFromXmlFile( filename );
119
120 // Krylov parameters.
121 parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
122 parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
123 parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
124
125 // PDAS parameters.
126 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-8);
127 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-6);
128 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 1);
129 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
130
131 // Status test parameters.
132 parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
133 parlist->sublist("Status Test").set("Step Tolerance",1.e-14);
134 parlist->sublist("Status Test").set("Iteration Limit",100);
135
136 // Define algorithm.
137 ROL::Ptr<ROL::Step<RealT>> step = ROL::makePtr<ROL::PrimalDualActiveSetStep<RealT>>(*parlist);
138 ROL::Ptr<ROL::StatusTest<RealT>> status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
139 ROL::Ptr<ROL::Algorithm<RealT>> algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
140
141 // Iteration vector.
142 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim, 0.0);
143 SV x(x_ptr);
144
145 // Run algorithm.
146 x.zero();
147 algo->run(x, obj, icon, true, *outStream);
148 std::ofstream file;
149 file.open("control_PDAS.txt");
150
151 for ( luint i = 0; i < dim; i++ ) {
152 file << (*x_ptr)[i] << "\n";
153 }
154 file.close();
155
156 // Projected Newton.
157 // re-load parameters
158 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
159 // Set algorithm.
160 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
161 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
162 algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
163 // Iteration vector.
164 ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(dim, 0.0);
165 SV y(y_ptr);
166
167 // Run Algorithm
168 y.zero();
169 algo->run(y, obj, icon, true, *outStream);
170
171 std::ofstream file_tr;
172 file_tr.open("control_TR.txt");
173 for ( luint i = 0; i < dim; i++ ) {
174 file_tr << (*y_ptr)[i] << "\n";
175 }
176 file_tr.close();
177
178 ROL::Ptr<V> error = x.clone();
179 error->set(x);
180 error->axpy(-1.0,y);
181 *outStream << "\nError between PDAS solution and TR solution is " << error->norm() << "\n";
182 errorFlag = ((error->norm() > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
183 }
184 catch (std::logic_error& err) {
185 *outStream << err.what() << "\n";
186 errorFlag = -1000;
187 }; // end try
188
189 if (errorFlag != 0)
190 std::cout << "End Result: TEST FAILED\n";
191 else
192 std::cout << "End Result: TEST PASSED\n";
193
194 return 0;
195
196}
197
Vector< Real > V
Contains definitions for Poisson optimal control.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions of custom data types in ROL.
Provides the elementwise interface to apply upper and lower bound constraints.
Provides an interface to check status of optimization algorithms.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
virtual bool check(ROL::AlgorithmState< Real > &state)
Check algorithm status.
StatusTest_PDAS(Real gtol=1.e-6, Real stol=1.e-12, int max_iter=100)
int main(int argc, char *argv[])
State for algorithm class. Will be used for restarts.
constexpr auto dim