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 "Teuchos_GlobalMPISession.hpp"
27#include "Teuchos_XMLParameterListHelpers.hpp"
28
29#include <iostream>
30#include <algorithm>
31
32
33
34template <class Real>
35class StatusTest_PDAS : public ROL::StatusTest<Real> {
36private:
37
38 Real gtol_;
39 Real stol_;
41
42public:
43
44 virtual ~StatusTest_PDAS() {}
45
46 StatusTest_PDAS( Real gtol = 1.e-6, Real stol = 1.e-12, int max_iter = 100 ) :
47 gtol_(gtol), stol_(stol), max_iter_(max_iter) {}
48
51 virtual bool check( ROL::AlgorithmState<Real> &state ) {
52 if ( (state.gnorm > this->gtol_) &&
53 (state.snorm > this->stol_) &&
54 (state.iter < this->max_iter_) ) {
55 return true;
56 }
57 else {
58 if ( state.iter < 2 ) {
59 return true;
60 }
61 return false;
62 }
63 }
64
65};
66
67typedef double RealT;
68
69int main(int argc, char *argv[]) {
70
71 typedef std::vector<RealT> vector;
72 typedef ROL::Vector<RealT> V;
73 typedef ROL::StdVector<RealT> SV;
74
75 typedef typename vector::size_type luint;
76
77
78
79 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
80
81 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
82 int iprint = argc - 1;
83 ROL::Ptr<std::ostream> outStream;
84 ROL::nullstream bhs; // outputs nothing
85 if (iprint > 0)
86 outStream = ROL::makePtrFromRef(std::cout);
87 else
88 outStream = ROL::makePtrFromRef(bhs);
89
90 int errorFlag = 0;
91
92 // *** Example body.
93
94 try {
95 luint dim = 256; // Set problem dimension.
96 RealT alpha = 1.e-4;
98
99 ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(dim);
100 ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim);
101
102 ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
103 ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
104
105 for ( luint i = 0; i < dim; i++ ) {
106 if ( i < dim/3.0 || i > 2*dim/3.0 ) {
107 (*l_ptr)[i] = 0.0;
108 (*u_ptr)[i] = 0.25;
109 }
110 else {
111 (*l_ptr)[i] = 0.75;
112 (*u_ptr)[i] = 1.0;
113 }
114 }
115 ROL::Bounds<RealT> icon(lo,up);
116
117 // Primal dual active set.
118 std::string filename = "input.xml";
119 auto parlist = ROL::getParametersFromXmlFile( filename );
120
121 // Krylov parameters.
122 parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
123 parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
124 parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
125
126 // PDAS parameters.
127 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-8);
128 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-6);
129 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 1);
130 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
131
132 // Status test parameters.
133 parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
134 parlist->sublist("Status Test").set("Step Tolerance",1.e-14);
135 parlist->sublist("Status Test").set("Iteration Limit",100);
136
137 // Define algorithm.
138 ROL::Ptr<ROL::Step<RealT>> step = ROL::makePtr<ROL::PrimalDualActiveSetStep<RealT>>(*parlist);
139 ROL::Ptr<ROL::StatusTest<RealT>> status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
140 ROL::Ptr<ROL::Algorithm<RealT>> algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
141
142 // Iteration vector.
143 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim, 0.0);
144 SV x(x_ptr);
145
146 // Run algorithm.
147 x.zero();
148 algo->run(x, obj, icon, true, *outStream);
149 std::ofstream file;
150 file.open("control_PDAS.txt");
151
152 for ( luint i = 0; i < dim; i++ ) {
153 file << (*x_ptr)[i] << "\n";
154 }
155 file.close();
156
157 // Projected Newton.
158 // re-load parameters
159 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
160 // Set algorithm.
161 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
162 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
163 algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
164 // Iteration vector.
165 ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(dim, 0.0);
166 SV y(y_ptr);
167
168 // Run Algorithm
169 y.zero();
170 algo->run(y, obj, icon, true, *outStream);
171
172 std::ofstream file_tr;
173 file_tr.open("control_TR.txt");
174 for ( luint i = 0; i < dim; i++ ) {
175 file_tr << (*y_ptr)[i] << "\n";
176 }
177 file_tr.close();
178
179 ROL::Ptr<V> error = x.clone();
180 error->set(x);
181 error->axpy(-1.0,y);
182 *outStream << "\nError between PDAS solution and TR solution is " << error->norm() << "\n";
183 errorFlag = ((error->norm() > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
184 }
185 catch (std::logic_error& err) {
186 *outStream << err.what() << "\n";
187 errorFlag = -1000;
188 }; // end try
189
190 if (errorFlag != 0)
191 std::cout << "End Result: TEST FAILED\n";
192 else
193 std::cout << "End Result: TEST PASSED\n";
194
195 return 0;
196
197}
198
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