ROL
Loading...
Searching...
No Matches
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
17namespace ROL {
18
19template<class Real>
21private:
22 int begin_;
23 ROL::Ptr<BatchManager<Real> > bman_;
24 std::vector<std::vector<Real> > points_;
25 std::vector<Real> weights_;
26
27protected:
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
37public:
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) {
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 ROL_TEST_FOR_EXCEPTION( weights_.size() == 0, std::invalid_argument,
76 ">>> ERROR (ROL::SampleGenerator::numMySamples): No samples exist!");
77 return weights_.size();
78 }
79
80 virtual std::vector<Real> getMyPoint(const int i) const {
81 ROL_TEST_FOR_EXCEPTION( numMySamples() == 0, std::invalid_argument,
82 ">>> ERROR (ROL::SampleGenerator::getMyPoint): No samples exist!");
83 ROL_TEST_FOR_EXCEPTION( i < 0 || i >= numMySamples(), std::invalid_argument,
84 ">>> ERROR (ROL::SampleGenerator::getMyPoint): Sample index out of range!");
85 return points_[i];
86 }
87
88 virtual Real getMyWeight(const int i) const {
89 ROL_TEST_FOR_EXCEPTION( numMySamples() == 0, std::invalid_argument,
90 ">>> ERROR (ROL::SampleGenerator::getMyWeight): No samples exist!");
91 ROL_TEST_FOR_EXCEPTION( i < 0 || i >= numMySamples(), std::invalid_argument,
92 ">>> ERROR (ROL::SampleGenerator::getMyWeight): Sample index out range!");
93 return weights_[i];
94 }
95
96 int batchID(void) const {
97 return bman_->batchID();
98 }
99
100 int numBatches(void) const {
101 return bman_->numBatches();
102 }
103
104 void sumAll(Real *input, Real *output, int dim) const {
105 bman_->sumAll(input, output, dim);
106 }
107
108 void sumAll(Vector<Real> &input, Vector<Real> &output) const {
109 bman_->sumAll(input,output);
110 }
111
112 void broadcast(Real *input, int cnt, int root) const {
113 bman_->broadcast(input,cnt,root);
114 }
115
116 void barrier(void) const {
117 bman_->barrier();
118 }
119
120 const ROL::Ptr<BatchManager<Real>> getBatchManager(void) const {
121 return bman_;
122 }
123
124 void print(const std::string &filename = "samples",
125 const int prec = 12) const {
126 int width = prec + 5 + 4;
127 std::stringstream name;
128 name << filename << "_" << batchID() << ".txt";
129 std::ofstream file(name.str().c_str());
130 if (file.is_open()) {
131 file << std::scientific << std::setprecision(prec);
132 for (int i = 0; i < numMySamples(); ++i) {
133 std::vector<Real> pt = getMyPoint(i);
134 Real wt = getMyWeight(i);
135 for (int j = 0; j < static_cast<int>(pt.size()); ++j) {
136 file << std::setw(width) << std::left << pt[j];
137 }
138 file << std::setw(width) << std::left << wt << std::endl;
139 }
140 file.close();
141 }
142 else {
143 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
144 ">>> (ROL::SampleGenerator::print): Unable to open file!");
145 }
146 }
147
148};
149
150}
151
152#endif
virtual int numMySamples(void) const
virtual std::vector< Real > getMyPoint(const int i) const
std::vector< std::vector< Real > > points_
virtual int numGlobalSamples(void) const
void setPoints(std::vector< std::vector< Real > > &p)
SampleGenerator(const ROL::Ptr< BatchManager< Real > > &bman)
void setWeights(std::vector< Real > &w)
void broadcast(Real *input, int cnt, int root) const
void sumAll(Vector< Real > &input, Vector< Real > &output) const
virtual Real computeError(std::vector< ROL::Ptr< Vector< Real > > > &vals, const Vector< Real > &x)
std::vector< Real > weights_
virtual Real computeError(std::vector< Real > &vals)
const ROL::Ptr< BatchManager< Real > > getBatchManager(void) const
virtual void refine(void)
void sumAll(Real *input, Real *output, int dim) const
SampleGenerator(const SampleGenerator< Real > &sampler)
virtual void update(const Vector< Real > &x)
virtual void setSamples(bool inConstructor=false)
ROL::Ptr< BatchManager< Real > > bman_
void print(const std::string &filename="samples", const int prec=12) const
virtual Real getMyWeight(const int i) const
Defines the linear algebra or vector space interface.
constexpr auto dim