ROL
ROL_SampleGenerator.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_SAMPLEGENERATOR_HPP
11 #define ROL_SAMPLEGENERATOR_HPP
12 
13 #include "ROL_BatchManager.hpp"
14 #include "ROL_Vector.hpp"
15 #include <fstream>
16 
17 namespace ROL {
18 
19 template<class Real>
21 private:
22  int begin_;
23  ROL::Ptr<BatchManager<Real> > bman_;
24  std::vector<std::vector<Real> > points_;
25  std::vector<Real> weights_;
26 
27 protected:
28  void setPoints(std::vector<std::vector<Real> > &p) {
29  points_.clear();
30  points_.assign(p.begin(),p.end());
31  }
32  void setWeights(std::vector<Real> &w) {
33  weights_.clear();
34  weights_.assign(w.begin(),w.end());
35  }
36 
37 public:
38  virtual ~SampleGenerator() {}
39  SampleGenerator(const ROL::Ptr<BatchManager<Real> > &bman)
40  : begin_(0), bman_(bman) {}
42  : begin_(sampler.begin_), bman_(sampler.bman_),
43  points_(sampler.points_), weights_(sampler.weights_) {}
44 
45  virtual void update(const Vector<Real> &x) {
46  begin_ = 0;
47  }
48 
49  virtual int start(void) {
50  return begin_;
51  }
52 
53  virtual Real computeError(std::vector<Real> &vals) {
54  return 0.0;
55  }
56 
57  virtual Real computeError(std::vector<ROL::Ptr<Vector<Real> > > &vals, const Vector<Real> &x) {
58  return 0.0;
59  }
60 
61  virtual void refine(void) {
62  begin_ = numMySamples();
63  }
64 
65  virtual void setSamples(bool inConstructor = false) {}
66 
67  virtual int numGlobalSamples(void) const {
68  Real nms = static_cast<Real>(numMySamples());
69  Real gnms = static_cast<Real>(0);
70  sumAll(&nms,&gnms,1);
71  return static_cast<int>(gnms);
72  }
73 
74  virtual int numMySamples(void) const {
75  return weights_.size();
76  }
77 
78  virtual std::vector<Real> getMyPoint(const int i) const {
79  return points_[i];
80  }
81 
82  virtual Real getMyWeight(const int i) const {
83  return weights_[i];
84  }
85 
86  int batchID(void) const {
87  return bman_->batchID();
88  }
89 
90  int numBatches(void) const {
91  return bman_->numBatches();
92  }
93 
94  void sumAll(Real *input, Real *output, int dim) const {
95  bman_->sumAll(input, output, dim);
96  }
97 
98  void sumAll(Vector<Real> &input, Vector<Real> &output) const {
99  bman_->sumAll(input,output);
100  }
101 
102  void broadcast(Real *input, int cnt, int root) const {
103  bman_->broadcast(input,cnt,root);
104  }
105 
106  void barrier(void) const {
107  bman_->barrier();
108  }
109 
110  const ROL::Ptr<BatchManager<Real>> getBatchManager(void) const {
111  return bman_;
112  }
113 
114  void print(const std::string &filename = "samples",
115  const int prec = 12) const {
116  int width = prec + 5 + 4;
117  std::stringstream name;
118  name << filename << "_" << batchID() << ".txt";
119  std::ofstream file(name.str().c_str());
120  if (file.is_open()) {
121  file << std::scientific << std::setprecision(prec);
122  for (int i = 0; i < numMySamples(); ++i) {
123  std::vector<Real> pt = getMyPoint(i);
124  Real wt = getMyWeight(i);
125  for (int j = 0; j < static_cast<int>(pt.size()); ++j) {
126  file << std::setw(width) << std::left << pt[j];
127  }
128  file << std::setw(width) << std::left << wt << std::endl;
129  }
130  file.close();
131  }
132  else {
133  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
134  ">>> (ROL::SampleGenerator::print): Unable to open file!");
135  }
136  }
137 
138 };
139 
140 }
141 
142 #endif
void sumAll(Vector< Real > &input, Vector< Real > &output) const
virtual void update(const Vector< Real > &x)
virtual int numGlobalSamples(void) const
virtual void setSamples(bool inConstructor=false)
virtual std::vector< Real > getMyPoint(const int i) const
void broadcast(Real *input, int cnt, int root) const
virtual Real getMyWeight(const int i) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual int numMySamples(void) const
void sumAll(Real *input, Real *output, int dim) const
void print(const std::string &filename="samples", const int prec=12) const
ROL::Ptr< BatchManager< Real > > bman_
virtual void refine(void)
virtual Real computeError(std::vector< ROL::Ptr< Vector< Real > > > &vals, const Vector< Real > &x)
std::vector< Real > weights_
const ROL::Ptr< BatchManager< Real > > getBatchManager(void) const
SampleGenerator(const SampleGenerator< Real > &sampler)
std::vector< std::vector< Real > > points_
int numBatches(void) const
virtual Real computeError(std::vector< Real > &vals)
void setPoints(std::vector< std::vector< Real > > &p)
constexpr auto dim
void setWeights(std::vector< Real > &w)
SampleGenerator(const ROL::Ptr< BatchManager< Real > > &bman)