ROL
function/test_13.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
20
24#include "ROL_RandomVector.hpp"
25#include "ROL_StdVector.hpp"
26#include "ROL_Bounds.hpp"
27
28#include "ROL_Stream.hpp"
29#include "Teuchos_GlobalMPISession.hpp"
30
31
32
33
34// Creates f(x) = <x,Dx>/2 - <x,b> where D is a diagonal matrix
35// If D_{ii}>0 for all i, then the minimizer is the solution to
36// the linear system x_i=b_i/d_i
37template<class Real>
38ROL::Ptr<ROL::Objective<Real>>
40 const ROL::Vector<Real> &b ) {
41
42 auto op = ROL::makePtr<ROL::DiagonalOperator<Real>>(a);
43 auto vec = b.clone();
44 vec->set(b);
45 auto obj = ROL::makePtr<ROL::QuadraticObjective<Real>>(op,vec);
46 return obj;
47}
48
49typedef double RealT;
50
51int main(int argc, char *argv[]) {
52
53// typedef ROL::Vector<RealT> V;
54 typedef ROL::StdVector<RealT> SV;
55
56
57
58 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
59
60 // This little trick lets us print to std::cout only if a
61 // (dummy) command-line argument is provided.
62 int iprint = argc - 1;
63 ROL::Ptr<std::ostream> outStream;
64 ROL::nullstream bhs; // outputs nothing
65 if (iprint > 0)
66 outStream = ROL::makePtrFromRef(std::cout);
67 else
68 outStream = ROL::makePtrFromRef(bhs);
69
70 int errorFlag = 0;
71
72 try {
73
74 auto parlist = ROL::getParametersFromXmlFile("binary_constraint.xml");
75
76 // Penalty parameter
77 RealT gamma = 1.0;
78
79 RealT INF = ROL::ROL_INF<RealT>();
80 RealT NINF = ROL::ROL_NINF<RealT>();
81
82 /*----- Optimization Vector -----*/
83
84 // Initial guess
85 auto x0_ptr = ROL::makePtr<std::vector<RealT>>(4);
86 auto x0 = ROL::makePtr<SV>(x0_ptr);
88
89 auto x = x0->clone(); x->set(*x0);
90
91 /*----- Objective Function -----*/
92
93 // Diagonal quadratic objective scaling vector
94 auto d_ptr = ROL::makePtr<std::vector<RealT>>(4);
95 auto d = ROL::makePtr<SV>(d_ptr);
96
97 // Quadratic objective offset vector
98 auto b_ptr = ROL::makePtr<std::vector<RealT>>(4);
99 auto b = ROL::makePtr<SV>(b_ptr);
100
101 // Set values for objective
102 (*b_ptr)[0] = 1.0; (*b_ptr)[1] = 1.0;
103 (*b_ptr)[2] = 1.0; (*b_ptr)[3] = 1.0;
104
105 (*d_ptr)[0] = 1.0; (*d_ptr)[1] = 2.0;
106 (*d_ptr)[2] = 4.0; (*d_ptr)[3] = 8.0;
107
108 auto obj = createDiagonalQuadraticObjective( *d, *b );
109
110 // Unconstrained minimizer: x = [ 1.0, 0.5, 0.25, 0.125 ]
111
112
113 /*----- Bound Constraint -----*/
114
115 // Lower bound vector
116 auto xl_ptr = ROL::makePtr<std::vector<RealT>>(4);
117 auto xl = ROL::makePtr<SV>(xl_ptr);
118
119 // Upper bound vector
120 auto xu_ptr = ROL::makePtr<std::vector<RealT>>(4);
121 auto xu = ROL::makePtr<SV>(xu_ptr);
122
123 // Set bounds
124 (*xl_ptr)[0] = 0.0; (*xl_ptr)[1] = 0.0;
125 (*xl_ptr)[2] = NINF; (*xl_ptr)[3] = NINF;
126
127 (*xu_ptr)[0] = 1.0; (*xu_ptr)[1] = INF;
128 (*xu_ptr)[2] = 1.0; (*xu_ptr)[3] = INF;
129
130// ROL::BoundConstraint<RealT> bnd(xl,xu);
131 auto bnd = ROL::makePtr<ROL::Bounds<RealT>>(xl,xu);
132
133 /*---- Constraint and Lagrange Multiplier -----*/
134
135 auto con = ROL::makePtr<ROL::BinaryConstraint<RealT>>( bnd, gamma );
136
137 // Lagrange multiplier
138 auto l = x->dual().clone();
139 ROL::Elementwise::Fill<RealT> FillWithOnes(1.0);
140 l->applyUnary( ROL::Elementwise::Fill<RealT>(1.0) );
141
142 // Constrained minimizer set X = { [ 0, 0, 1, 0.125 ], [ 1, 0, 1, 0.125 ] }
143
144 // Create Optimization problems
145 ROL::OptimizationProblem<RealT> problem_E( obj, x, ROL::nullPtr, con, l );
146 ROL::OptimizationProblem<RealT> problem_EB( obj, x, bnd, con, l );
147
148 // Perform linear algebra and finite difference checks
149 problem_E.check( *outStream );
150
151
152 // Solve using Composite Step where the bound is not enforced and
153 // equality constraints are satisfied asymptotically
154 parlist->sublist("Step").set("Type","Composite Step");
155 ROL::OptimizationSolver<RealT> solver_cs( problem_E, *parlist );
156 solver_cs.solve( *outStream );
157 *outStream << "\n\nFinal optimization vector:";
158 x->print(*outStream);
159
160 // Reset optimization vector and Lagrange multiplier to initial values
161 x->set(*x0); l->applyUnary(FillWithOnes);
162
163
164 // Solve using Augmented Lagrangian where the bound is enforced explicitly
165 // and equality constraints are enforced through penalization
166 parlist->sublist("Step").set("Type","Augmented Lagrangian");
167 ROL::OptimizationSolver<RealT> solver_al( problem_EB, *parlist );
168 solver_al.solve( *outStream );
169 *outStream << "\n\nFinal optimization vector:";
170 x->print(*outStream);
171
172
173 // Reset optimization vector and Lagrange multiplier to initial values
174 x->set(*x0); l->applyUnary(FillWithOnes);
175
176
177 // Solve using Moreau-Yosida where the bound is enforced through penalization
178 // and equality constraints are satisfied asymptotically
179 parlist->sublist("Step").set("Type","Moreau-Yosida Penalty");
180 ROL::OptimizationSolver<RealT> solver_my( problem_EB, *parlist );
181 solver_my.solve( *outStream );
182 *outStream << "\n\nFinal optimization vector:";
183 x->print(*outStream);
184
185
186 }
187
188 catch (std::logic_error& err) {
189 *outStream << err.what() << "\n";
190 errorFlag = -1000;
191 }; // end try
192
193 if (errorFlag != 0)
194 std::cout << "End Result: TEST FAILED\n";
195 else
196 std::cout << "End Result: TEST PASSED\n";
197
198 return 0;
199}
200
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Provides a simplified interface for solving a wide range of optimization problems.
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
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 ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
int main(int argc, char *argv[])
ROL::Ptr< ROL::Objective< Real > > createDiagonalQuadraticObjective(const ROL::Vector< Real > &a, const ROL::Vector< Real > &b)
double RealT
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,...