Debian Codegraph @

packages/rol/adapters/teuchos/src/pebbl/ROL_PEBBL_TeuchosBranchHelper.hpp

raw
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_PEBBL_TEUCHOSBRANCHHELPER_H
11 #define ROL_PEBBL_TEUCHOSBRANCHHELPER_H
12
13 #include "ROL_TeuchosVector.hpp"
14 #include "ROL_PEBBL_BranchHelper.hpp"
15 #include "ROL_PEBBL_TeuchosIntegerTransformation.hpp"
16
17 /** @ingroup func_group
18 \class ROL::PEBBL::TeuchosBranchHelper
19 \brief Defines the pebbl branch index interface for TeuchosVectors.
20
21 ---
22 */
23
24
25 namespace ROL {
26 namespace PEBBL {
27
28 template <class Ordinal, class Real>
29 class TeuchosBranchHelper : public BranchHelper<Real> {
30 private:
31 const Real tol_;
32 const int method_;
33
34 using BranchHelper<Real>::getIntegerVector;
35 Ptr<const Teuchos::SerialDenseVector<Ordinal,Real>> getConstData(const Vector<Real> &x) const {
36 return dynamic_cast<const TeuchosVector<Ordinal,Real>&>(*getIntegerVector(x)).getVector();
37 }
38
39 // Branching based on distance to integer
40 int getIndex_D(const Vector<Real> &x, const Vector<Real> &g) const {
41 // Get index closest to 0.5
42 Ptr<const Teuchos::SerialDenseVector<Ordinal,Real>> xval = getConstData(x);
43 int index = 0;
44 Real minD(0), minX(ROL_INF<Real>()), half(0.5);
45 int size = xval->length();
46 for (int i = 0; i < size; ++i) {
47 Real x = (*xval)(i);
48 Real fx = std::floor(x);
49 Real cx = std::ceil(x);
50 minD = std::min(x-fx,cx-x);
51 if (std::abs(minD-half) < minX) {
52 minX = std::abs(minD-half);
53 index = i;
54 }
55 }
56 return index;
57 }
58
59 // Branching based on directional derivatives (similar to pseudo costs).
60 int getIndex_PC(const Vector<Real> &x, const Vector<Real> &g) const {
61 Ptr<const Teuchos::SerialDenseVector<Ordinal,Real>> xval = getConstData(x);
62 Ptr<const Teuchos::SerialDenseVector<Ordinal,Real>> gval = getConstData(g);
63 Real maxD(ROL_NINF<Real>()), Li(0), Ui(0), mini(0);
64 int index = 0, size = gval->length();
65 for (int i = 0; i < size; ++i) {
66 Li = (*gval)[i] * (std::floor((*xval)[i]) - (*xval)[i]);
67 Ui = (*gval)[i] * (std::ceil((*xval)[i]) - (*xval)[i]);
68 mini = std::min(std::abs(Li),std::abs(Ui));
69 if (mini > maxD) {
70 maxD = mini;
71 index = i;
72 }
73 }
74 return index;
75 }
76
77 public:
78 TeuchosBranchHelper(const Real tol = 1e-6, const int method = 0)
79 : tol_(tol), method_(method) {}
80
81 TeuchosBranchHelper(const TeuchosBranchHelper &BH)
82 : tol_(BH.tol_), method_(BH.method_) {}
83
84 int getIndex(const Vector<Real> &x, const Vector<Real> &g) const {
85 int index(0);
86 if (method_ == 1) index = getIndex_D(x,g);
87 else index = getIndex_PC(x,g);
88 return index;
89 }
90
91 void getNumFrac(int &nfrac, Real &integralityMeasure,
92 const Vector<Real> &x) const {
93 // Return number of fractional variables and the
94 // sum of the distance to integer for the input vector
95 Ptr<const Teuchos::SerialDenseVector<Ordinal,Real>> xval = getConstData(x);
96 nfrac = 0;
97 integralityMeasure = static_cast<Real>(0);
98 Real minD(0);
99 int size = xval->length();
100 for (int i = 0; i < size; ++i) {
101 Real x = (*xval)(i);
102 Real fx = std::floor(x);
103 Real cx = std::ceil(x);
104 minD = std::min(x-fx,cx-x);
105 integralityMeasure += minD;
106 if (minD > tol_) {
107 nfrac++;
108 }
109 }
110 }
111
112 Ptr<IntegerTransformation<Real>> createTransform(void) const {
113 return makePtr<TeuchosIntegerTransformation<Ordinal,Real>>();
114 }
115
116 }; // class StdBranchHelper_PEBBL
117
118 } // namespace PEBBL
119 } // namespace ROL
120
121 #endif