ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
sol
sampler
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>
20
class
SampleGenerator
{
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) {}
41
SampleGenerator
(
const
SampleGenerator<Real>
&sampler)
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
ROL_BatchManager.hpp
ROL_Vector.hpp
ROL::BatchManager
Definition
ROL_BatchManager.hpp:19
ROL::SampleGenerator::numMySamples
virtual int numMySamples(void) const
Definition
ROL_SampleGenerator.hpp:74
ROL::SampleGenerator::getMyPoint
virtual std::vector< Real > getMyPoint(const int i) const
Definition
ROL_SampleGenerator.hpp:78
ROL::SampleGenerator::points_
std::vector< std::vector< Real > > points_
Definition
ROL_SampleGenerator.hpp:24
ROL::SampleGenerator::numGlobalSamples
virtual int numGlobalSamples(void) const
Definition
ROL_SampleGenerator.hpp:67
ROL::SampleGenerator::batchID
int batchID(void) const
Definition
ROL_SampleGenerator.hpp:86
ROL::SampleGenerator::setPoints
void setPoints(std::vector< std::vector< Real > > &p)
Definition
ROL_SampleGenerator.hpp:28
ROL::SampleGenerator::SampleGenerator
SampleGenerator(const ROL::Ptr< BatchManager< Real > > &bman)
Definition
ROL_SampleGenerator.hpp:39
ROL::SampleGenerator::setWeights
void setWeights(std::vector< Real > &w)
Definition
ROL_SampleGenerator.hpp:32
ROL::SampleGenerator::barrier
void barrier(void) const
Definition
ROL_SampleGenerator.hpp:106
ROL::SampleGenerator::broadcast
void broadcast(Real *input, int cnt, int root) const
Definition
ROL_SampleGenerator.hpp:102
ROL::SampleGenerator::sumAll
void sumAll(Vector< Real > &input, Vector< Real > &output) const
Definition
ROL_SampleGenerator.hpp:98
ROL::SampleGenerator::computeError
virtual Real computeError(std::vector< ROL::Ptr< Vector< Real > > > &vals, const Vector< Real > &x)
Definition
ROL_SampleGenerator.hpp:57
ROL::SampleGenerator::weights_
std::vector< Real > weights_
Definition
ROL_SampleGenerator.hpp:25
ROL::SampleGenerator::computeError
virtual Real computeError(std::vector< Real > &vals)
Definition
ROL_SampleGenerator.hpp:53
ROL::SampleGenerator::~SampleGenerator
virtual ~SampleGenerator()
Definition
ROL_SampleGenerator.hpp:38
ROL::SampleGenerator::begin_
int begin_
Definition
ROL_SampleGenerator.hpp:22
ROL::SampleGenerator::getBatchManager
const ROL::Ptr< BatchManager< Real > > getBatchManager(void) const
Definition
ROL_SampleGenerator.hpp:110
ROL::SampleGenerator::refine
virtual void refine(void)
Definition
ROL_SampleGenerator.hpp:61
ROL::SampleGenerator::start
virtual int start(void)
Definition
ROL_SampleGenerator.hpp:49
ROL::SampleGenerator::sumAll
void sumAll(Real *input, Real *output, int dim) const
Definition
ROL_SampleGenerator.hpp:94
ROL::SampleGenerator::SampleGenerator
SampleGenerator(const SampleGenerator< Real > &sampler)
Definition
ROL_SampleGenerator.hpp:41
ROL::SampleGenerator::update
virtual void update(const Vector< Real > &x)
Definition
ROL_SampleGenerator.hpp:45
ROL::SampleGenerator::setSamples
virtual void setSamples(bool inConstructor=false)
Definition
ROL_SampleGenerator.hpp:65
ROL::SampleGenerator::bman_
ROL::Ptr< BatchManager< Real > > bman_
Definition
ROL_SampleGenerator.hpp:23
ROL::SampleGenerator::print
void print(const std::string &filename="samples", const int prec=12) const
Definition
ROL_SampleGenerator.hpp:114
ROL::SampleGenerator::getMyWeight
virtual Real getMyWeight(const int i) const
Definition
ROL_SampleGenerator.hpp:82
ROL::SampleGenerator::numBatches
int numBatches(void) const
Definition
ROL_SampleGenerator.hpp:90
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL
Definition
ROL_ElementwiseVector.hpp:27
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0