ROL
Loading...
Searching...
No Matches
ROL_BoundConstraint_Partitioned.hpp
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
10#ifndef ROL_BOUND_CONSTRAINT_PARTITIONED_H
11#define ROL_BOUND_CONSTRAINT_PARTITIONED_H
12
15#include "ROL_Types.hpp"
16#include <iostream>
17
24namespace ROL {
25
26template<typename Real>
28
29 typedef Vector<Real> V;
31 typedef typename std::vector<Real>::size_type uint;
32
33private:
34 std::vector<Ptr<BoundConstraint<Real>>> bnd_;
35
36 using BoundConstraint<Real>::lower_;
37 using BoundConstraint<Real>::upper_;
38 Ptr<V> l_;
39 Ptr<V> u_;
40
42
45
46public:
48
50 const std::vector<Ptr<Vector<Real>>> &x)
51 : bnd_(bnd), dim_(bnd.size()), hasLvec_(true), hasUvec_(true) {
53 for( uint k=0; k<dim_; ++k ) {
54 if( bnd_[k]->isActivated() ) {
56 break;
57 }
58 }
59 std::vector<Ptr<Vector<Real>>> lp(dim_);
60 std::vector<Ptr<Vector<Real>>> up(dim_);
61 for( uint k=0; k<dim_; ++k ) {
62 try {
63 lp[k] = x[k]->clone();
64 if (bnd_[k]->isLowerActivated()) {
65 lp[k]->set(*bnd_[k]->getLowerBound());
66 }
67 else {
68 lp[k]->setScalar(-BoundConstraint<Real>::computeInf(*x[k]));
69 }
70 }
71 catch (std::exception &e1) {
72 try {
73 lp[k] = x[k]->clone();
74 lp[k]->setScalar(-BoundConstraint<Real>::computeInf(*x[k]));
75 }
76 catch (std::exception &e2) {
77 lp[k] = nullPtr;
78 hasLvec_ = false;
79 }
80 }
81 try {
82 up[k] = x[k]->clone();
83 if (bnd_[k]->isUpperActivated()) {
84 up[k]->set(*bnd_[k]->getUpperBound());
85 }
86 else {
87 up[k]->setScalar( BoundConstraint<Real>::computeInf(*x[k]));
88 }
89 }
90 catch (std::exception &e1) {
91 try {
92 up[k] = x[k]->clone();
93 up[k]->setScalar( BoundConstraint<Real>::computeInf(*x[k]));
94 }
95 catch (std::exception &e2) {
96 up[k] = nullPtr;
97 hasUvec_ = false;
98 }
99 }
100 }
101 if (hasLvec_) {
102 lower_ = makePtr<PV>(lp);
103 }
104 if (hasUvec_) {
105 upper_ = makePtr<PV>(up);
106 }
107 }
108
109 void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
110 }
111
113 PV &xpv = dynamic_cast<PV&>(x);
114 for( uint k=0; k<dim_; ++k ) {
115 if( bnd_[k]->isActivated() ) {
116 bnd_[k]->project(*xpv.get(k));
117 }
118 }
119 }
120
122 PV &xpv = dynamic_cast<PV&>(x);
123 for( uint k=0; k<dim_; ++k ) {
124 if( bnd_[k]->isActivated() ) {
125 bnd_[k]->projectInterior(*xpv.get(k));
126 }
127 }
128 }
129
130 void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
131 PV &vpv = dynamic_cast<PV&>(v);
132 const PV &xpv = dynamic_cast<const PV&>(x);
133 for( uint k=0; k<dim_; ++k ) {
134 if( bnd_[k]->isActivated() ) {
135 bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(xpv.get(k)),eps);
136 }
137 }
138 }
139
140 void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0)) {
141 PV &vpv = dynamic_cast<PV&>(v);
142 const PV &gpv = dynamic_cast<const PV&>(g);
143 const PV &xpv = dynamic_cast<const PV&>(x);
144 for( uint k=0; k<dim_; ++k ) {
145 if( bnd_[k]->isActivated() ) {
146 bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),xeps,geps);
147 }
148 }
149 }
150
151 void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
152 PV &vpv = dynamic_cast<PV&>(v);
153 const PV &xpv = dynamic_cast<const PV&>(x);
154 for( uint k=0; k<dim_; ++k ) {
155 if( bnd_[k]->isActivated() ) {
156 bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(xpv.get(k)),eps);
157 }
158 }
159 }
160
161 void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0) ) {
162 PV &vpv = dynamic_cast<PV&>(v);
163 const PV &gpv = dynamic_cast<const PV&>(g);
164 const PV &xpv = dynamic_cast<const PV&>(x);
165 for( uint k=0; k<dim_; ++k ) {
166 if( bnd_[k]->isActivated() ) {
167 bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),xeps,geps);
168 }
169 }
170 }
171
172 bool isFeasible( const Vector<Real> &v ) {
173 bool feasible = true;
174 const PV &vs = dynamic_cast<const PV&>(v);
175 for( uint k=0; k<dim_; ++k ) {
176 if(bnd_[k]->isActivated()) {
177 feasible = feasible && bnd_[k]->isFeasible(*(vs.get(k)));
178 }
179 }
180 return feasible;
181 }
182
183 void applyInverseScalingFunction(Vector<Real> &dv, const Vector<Real> &v, const Vector<Real> &x, const Vector<Real> &g) const {
184 PV &dvpv = dynamic_cast<PV&>(dv);
185 const PV &vpv = dynamic_cast<const PV&>(v);
186 const PV &xpv = dynamic_cast<const PV&>(x);
187 const PV &gpv = dynamic_cast<const PV&>(g);
188 for( uint k=0; k<dim_; ++k ) {
189 if( bnd_[k]->isActivated() ) {
190 bnd_[k]->applyInverseScalingFunction(*(dvpv.get(k)),*(vpv.get(k)),*(xpv.get(k)),*(gpv.get(k)));
191 }
192 }
193 }
194
196 PV &dvpv = dynamic_cast<PV&>(dv);
197 const PV &vpv = dynamic_cast<const PV&>(v);
198 const PV &xpv = dynamic_cast<const PV&>(x);
199 const PV &gpv = dynamic_cast<const PV&>(g);
200 for( uint k=0; k<dim_; ++k ) {
201 if( bnd_[k]->isActivated() ) {
202 bnd_[k]->applyScalingFunctionJacobian(*(dvpv.get(k)),*(vpv.get(k)),*(xpv.get(k)),*(gpv.get(k)));
203 }
204 }
205 }
206}; // class BoundConstraint_Partitioned
207
208
209
210template<typename Real>
211Ptr<BoundConstraint<Real>>
213 const Ptr<BoundConstraint<Real>> &bnd2 ) {
214
215
216 typedef BoundConstraint<Real> BND;
218 Ptr<BND> temp[] = {bnd1, bnd2};
219 return makePtr<BNDP>( std::vector<Ptr<BND>>(temp,temp+2) );
220}
221
222
223} // namespace ROL
224
225#endif
Contains definitions of custom data types in ROL.
A composite composite BoundConstraint formed from bound constraints on subvectors of a PartitionedVec...
BoundConstraint_Partitioned(const std::vector< Ptr< BoundConstraint< Real > > > &bnd, const std::vector< Ptr< Vector< Real > > > &x)
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0))
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
std::vector< Ptr< BoundConstraint< Real > > > bnd_
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0))
void applyScalingFunctionJacobian(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
void applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Provides the interface to apply upper and lower bound constraints.
Real computeInf(const Vector< Real > &x) const
void deactivate(void)
Turn off bounds.
void activate(void)
Turn on bounds.
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Ptr< const Vector< Real > > get(size_type i) const
virtual const Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
bool isLowerActivated(void) const
Check if lower bound are on.
virtual const Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
bool isActivated(void) const
Check if bounds are on.
bool isUpperActivated(void) const
Check if upper bound are on.
Defines the linear algebra or vector space interface.
Ptr< BoundConstraint< Real > > CreateBoundConstraint_Partitioned(const Ptr< BoundConstraint< Real > > &bnd1, const Ptr< BoundConstraint< Real > > &bnd2)