Debian Codegraph @

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