Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Mpp
MLUQ
Commits
66dbb6ed
Commit
66dbb6ed
authored
May 12, 2021
by
niklas.baumgarten
Browse files
refactoring
parent
185349ef
Pipeline
#149212
failed with stages
in 4 minutes and 9 seconds
Changes
12
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
mlmc/src/Main.hpp
View file @
66dbb6ed
...
...
@@ -6,7 +6,7 @@
#include "MultilevelMonteCarlo.hpp"
#include "MonteCarlo.hpp"
#include "
I
Estimator.hpp"
#include "Estimator.hpp"
bool
PowerOfTwo
(
int
n
)
{
...
...
@@ -14,7 +14,7 @@ bool PowerOfTwo(int n) {
return
(
ceil
(
log2
(
n
))
==
floor
(
log2
(
n
)));
}
std
::
unique_ptr
<
I
Estimator
>
CreateEstimator
(
const
std
::
string
&
estimator
)
{
std
::
unique_ptr
<
Estimator
>
CreateEstimator
(
const
std
::
string
&
estimator
)
{
if
(
estimator
==
"MultilevelStochasticCollocation"
)
return
std
::
make_unique
<
MultilevelStochasticCollocation
>
();
if
(
estimator
==
"StochasticCollocation"
)
...
...
@@ -33,7 +33,7 @@ private:
std
::
string
estimatorName
=
"MultilevelMonteCarlo"
;
public:
std
::
unique_ptr
<
I
Estimator
>
estimator
;
std
::
unique_ptr
<
Estimator
>
estimator
;
MainProgram
()
{
config
.
get
(
"MainVerbose"
,
verbose
);
...
...
mlmc/src/estimators/CMakeLists.txt
View file @
66dbb6ed
add_library
(
MONTECARLO STATIC
MonteCarlo.cpp
MultilevelMonteCarlo.cpp
datastructure/Errors.cpp
datastructure/
Multilevel
Errors.cpp
datastructure/Exponents.cpp
datastructure/WelfordAggregate.cpp
datastructure/EmpiricMeasureLevelMaps.cpp
...
...
mlmc/src/estimators/
I
Estimator.hpp
→
mlmc/src/estimators/Estimator.hpp
View file @
66dbb6ed
#ifndef
I
ESTIMATOR_HPP
#define
I
ESTIMATOR_HPP
#ifndef ESTIMATOR_HPP
#define ESTIMATOR_HPP
#include "
Config
.hpp"
#include "
WelfordAggregate
.hpp"
//
#include "Errors.hpp"
#include "Errors.hpp"
struct
Exponents
;
class
I
Estimator
{
class
Estimator
{
protected:
double
epsilon
=
0.0
;
//
Errors errors;
Errors
errors
;
public:
IEstimator
()
{
WelfordAggregate
aggregate
;
Estimator
()
{
config
.
get
(
"epsilon"
,
epsilon
);
}
...
...
@@ -24,11 +26,11 @@ public:
double
Epsilon
()
const
{
return
epsilon
;
}
virtual
void
Method
()
=
0
;
virtual
void
Method
()
{}
;
virtual
std
::
string
Name
()
const
=
0
;
virtual
std
::
string
Name
()
const
{
return
"Estimator"
;
}
;
virtual
void
EstimatorResults
()
const
=
0
;
virtual
void
EstimatorResults
()
const
{}
;
virtual
void
MultilevelResults
()
const
{};
...
...
@@ -36,4 +38,4 @@ public:
};
#endif //
I
ESTIMATOR_HPP
#endif //ESTIMATOR_HPP
mlmc/src/estimators/MonteCarlo.hpp
View file @
66dbb6ed
...
...
@@ -3,12 +3,12 @@
#include "WelfordAggregate.hpp"
#include "PDESolverCreator.hpp"
#include "
I
Estimator.hpp"
#include "Estimator.hpp"
#include "MeshesCreator.hpp"
class
MonteCarlo
:
public
I
Estimator
{
class
MonteCarlo
:
public
Estimator
{
protected:
int
verbose
=
1
;
...
...
mlmc/src/estimators/MultilevelMonteCarlo.hpp
View file @
66dbb6ed
...
...
@@ -4,17 +4,15 @@
#include "EmpiricMeasureLevelMaps.hpp"
#include "MonteCarlo.hpp"
#include "Exponents.hpp"
#include "Errors.hpp"
#include "
Multilevel
Errors.hpp"
#include "Utilities.hpp"
#include "
I
Estimator.hpp"
#include "Estimator.hpp"
class
MultilevelMonteCarlo
:
public
I
Estimator
{
class
MultilevelMonteCarlo
:
public
Estimator
{
private:
int
verbose
=
1
;
bool
mcOnly
=
false
;
// Levels init{3, 4, 5};
std
::
vector
<
int
>
initLevels
{
3
,
4
,
5
};
...
...
@@ -22,20 +20,16 @@ private:
std
::
vector
<
int
>
initSampleAmount
{
12
,
6
,
3
};
public:
double
epsilon
=
0.0
;
MonteCarloMap
mcMap
;
// Here Could be EstimatorMap
EstimatorMap
mcMap
;
MultilevelData
data
;
Exponents
exponents
;
Errors
errors
;
Multilevel
Errors
errors
;
MultilevelMonteCarlo
()
:
I
Estimator
()
{
MultilevelMonteCarlo
()
:
Estimator
()
{
config
.
get
(
"MLMCVerbose"
,
verbose
);
config
.
get
(
"mcOnly"
,
mcOnly
);
config
.
get
(
"initLevels"
,
initLevels
);
config
.
get
(
"initSampleAmount"
,
initSampleAmount
);
...
...
mlmc/src/estimators/MultilevelStochasticCollocation.hpp
View file @
66dbb6ed
#ifndef MULTILEVELSTOCHASTICCOLLOCATION_HPP
#define MULTILEVELSTOCHASTICCOLLOCATION_HPP
#include "
I
Estimator.hpp"
#include "Estimator.hpp"
class
MultilevelStochasticCollocation
:
public
I
Estimator
{
class
MultilevelStochasticCollocation
:
public
Estimator
{
private:
public:
...
...
mlmc/src/estimators/StochasticCollocation.hpp
View file @
66dbb6ed
#ifndef STOCHASTICCOLLOCATION_HPP
#define STOCHASTICCOLLOCATION_HPP
#include "
I
Estimator.hpp"
#include "Estimator.hpp"
class
StochasticCollocation
:
public
I
Estimator
{
class
StochasticCollocation
:
public
Estimator
{
private:
public:
...
...
mlmc/src/estimators/datastructure/EmpiricMeasureLevelMaps.cpp
View file @
66dbb6ed
...
...
@@ -2,7 +2,7 @@
#include "Exponents.hpp"
void
MonteCarlo
Map
::
UpdateSampleCounter
(
double
epsilon
)
{
void
Estimator
Map
::
UpdateSampleCounter
(
double
epsilon
)
{
int
optimalM
;
double
factor
=
0.0
;
...
...
@@ -17,7 +17,7 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) {
}
}
void
MonteCarlo
Map
::
AppendLevel
(
double
epsilon
,
Exponents
exponents
,
int
newLevel
)
{
void
Estimator
Map
::
AppendLevel
(
double
epsilon
,
Exponents
exponents
,
int
newLevel
)
{
if
(
newLevel
<=
maxLevel
)
{
auto
oldLevel
=
this
->
rbegin
();
double
sVarY
=
oldLevel
->
second
.
aggregate
.
sVar
.
Y
/
pow
(
2.0
,
exponents
.
beta
);
...
...
@@ -29,13 +29,14 @@ void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, int newLeve
_levelMap
.
find
(
newLevel
)
->
second
.
aggregate
.
sVar
.
Y
=
sVarY
;
_levelMap
.
find
(
newLevel
)
->
second
.
aggregate
.
mean
.
C
=
avgsCost
;
this
->
UpdateSampleCounter
(
epsilon
);
}
else
{
Exit
(
"Maximum level has been reached."
)
}
}
void
SampleCounterMap
::
Update
(
const
MonteCarlo
Map
&
mcMap
)
{
void
SampleCounterMap
::
Update
(
const
Estimator
Map
&
mcMap
)
{
for
(
auto
&
[
level
,
mc
]
:
mcMap
)
_levelMap
[
level
]
=
SampleCounter
(
mc
.
aggregate
.
ctr
);
}
...
...
@@ -50,7 +51,7 @@ bool SampleCounterMap::NoSamplesLeft() {
return
noSamplesLeft
;
}
void
MeanMap
::
Update
(
const
MonteCarlo
Map
&
mcMap
)
{
void
MeanMap
::
Update
(
const
Estimator
Map
&
mcMap
)
{
for
(
auto
&&
[
level
,
mc
]
:
mcMap
)
{
_levelMap
[
level
]
=
mc
.
aggregate
.
mean
;
Q
+=
mc
.
aggregate
.
mean
.
Y
;
...
...
@@ -58,12 +59,12 @@ void MeanMap::Update(const MonteCarloMap &mcMap) {
}
}
void
SVarMap
::
Update
(
const
MonteCarlo
Map
&
mcMap
)
{
void
SVarMap
::
Update
(
const
Estimator
Map
&
mcMap
)
{
for
(
auto
&
[
level
,
mc
]
:
mcMap
)
_levelMap
[
level
]
=
mc
.
aggregate
.
sVar
;
}
void
KurtosisMap
::
Update
(
const
MonteCarlo
Map
&
mcMap
)
{
void
KurtosisMap
::
Update
(
const
Estimator
Map
&
mcMap
)
{
for
(
auto
&
[
level
,
mc
]
:
mcMap
)
_levelMap
[
level
]
=
mc
.
aggregate
.
kurtosis
;
}
mlmc/src/estimators/datastructure/EmpiricMeasureLevelMaps.hpp
View file @
66dbb6ed
...
...
@@ -10,16 +10,16 @@
struct
Exponents
;
struct
MonteCarlo
Map
:
public
LevelMap
<
MonteCarlo
>
{
struct
Estimator
Map
:
public
LevelMap
<
Estimator
>
{
int
maxLevel
=
10
;
MonteCarlo
Map
()
{
Estimator
Map
()
{
config
.
get
(
"maxLevel"
,
maxLevel
);
};
MonteCarlo
Map
(
std
::
initializer_list
<
std
::
pair
<
int
,
MonteCarlo
>>
mcMap
)
:
LevelMap
<
MonteCarlo
>
(
mcMap
)
{
Estimator
Map
(
std
::
initializer_list
<
std
::
pair
<
int
,
Estimator
>>
mcMap
)
:
LevelMap
<
Estimator
>
(
mcMap
)
{
config
.
get
(
"maxLevel"
,
maxLevel
);
};
...
...
@@ -35,7 +35,7 @@ struct SampleCounterMap : public LevelMap<SampleCounter> {
SampleCounterMap
(
std
::
initializer_list
<
std
::
pair
<
int
,
SampleCounter
>>
ctrs
)
:
LevelMap
<
SampleCounter
>
(
ctrs
)
{};
void
Update
(
const
MonteCarlo
Map
&
mcMap
);
void
Update
(
const
Estimator
Map
&
mcMap
);
bool
NoSamplesLeft
();
};
...
...
@@ -49,11 +49,11 @@ struct MeanMap : public LevelMap<Mean> {
MeanMap
(
std
::
initializer_list
<
std
::
pair
<
int
,
Mean
>>
avgs
)
:
LevelMap
<
Mean
>
(
avgs
)
{};
MeanMap
(
const
MonteCarlo
Map
&
mcMap
)
{
MeanMap
(
const
Estimator
Map
&
mcMap
)
{
Update
(
mcMap
);
};
void
Update
(
const
MonteCarlo
Map
&
mcMap
);
void
Update
(
const
Estimator
Map
&
mcMap
);
};
struct
SVarMap
:
public
LevelMap
<
SVar
>
{
...
...
@@ -63,11 +63,11 @@ struct SVarMap : public LevelMap<SVar> {
SVarMap
(
std
::
initializer_list
<
std
::
pair
<
int
,
SVar
>>
vars
)
:
LevelMap
<
SVar
>
(
vars
)
{};
SVarMap
(
const
MonteCarlo
Map
&
mcMap
)
{
SVarMap
(
const
Estimator
Map
&
mcMap
)
{
Update
(
mcMap
);
};
void
Update
(
const
MonteCarlo
Map
&
mcMap
);
void
Update
(
const
Estimator
Map
&
mcMap
);
};
struct
KurtosisMap
:
LevelMap
<
Kurtosis
>
{
...
...
@@ -77,11 +77,11 @@ struct KurtosisMap : LevelMap<Kurtosis> {
KurtosisMap
(
std
::
initializer_list
<
std
::
pair
<
int
,
Kurtosis
>>
kurtosis
)
:
LevelMap
<
Kurtosis
>
(
kurtosis
)
{};
KurtosisMap
(
const
MonteCarlo
Map
&
mcMap
)
{
KurtosisMap
(
const
Estimator
Map
&
mcMap
)
{
Update
(
mcMap
);
};
void
Update
(
const
MonteCarlo
Map
&
mcMap
);
void
Update
(
const
Estimator
Map
&
mcMap
);
};
struct
MultilevelData
{
...
...
@@ -95,7 +95,7 @@ struct MultilevelData {
MultilevelData
()
{};
MultilevelData
(
const
MonteCarlo
Map
&
mcMap
)
{
MultilevelData
(
const
Estimator
Map
&
mcMap
)
{
ExtractDataFrom
(
mcMap
);
}
...
...
@@ -103,7 +103,7 @@ struct MultilevelData {
const
SVarMap
&
vars
,
const
KurtosisMap
&
kurtosis
)
:
ctrs
(
ctrs
),
means
(
avgs
),
sVars
(
vars
),
kurtosis
(
kurtosis
)
{};
void
ExtractDataFrom
(
const
MonteCarlo
Map
&
mcMap
)
{
void
ExtractDataFrom
(
const
Estimator
Map
&
mcMap
)
{
ctrs
.
Update
(
mcMap
);
means
.
Update
(
mcMap
);
sVars
.
Update
(
mcMap
);
...
...
@@ -130,7 +130,7 @@ struct MultilevelData {
<<
"kurtosis="
<<
data
.
kurtosis
.
GetYVector
()
<<
endl
<<
"E(cost)="
<<
data
.
means
.
GetCostVector
()
<<
endl
<<
"Used Levels="
<<
data
.
means
.
GetLevelVector
()
<<
endl
<<
"Used Samples"
<<
data
.
ctrs
.
GetMVector
()
<<
endl
;
<<
"Used Samples
=
"
<<
data
.
ctrs
.
GetMVector
()
<<
endl
;
}
};
...
...
mlmc/src/estimators/datastructure/Exponents.cpp
View file @
66dbb6ed
#include "Exponents.hpp"
void
Exponents
::
ComputeExponents
(
const
MonteCarlo
Map
&
mcMap
)
{
void
Exponents
::
ComputeExponents
(
const
Estimator
Map
&
mcMap
)
{
ComputeExponents
(
MultilevelData
(
mcMap
));
}
...
...
mlmc/src/estimators/datastructure/Exponents.hpp
View file @
66dbb6ed
...
...
@@ -11,7 +11,7 @@ struct Exponents {
Exponents
()
{};
Exponents
(
const
MonteCarlo
Map
&
mcMap
)
{
Exponents
(
const
Estimator
Map
&
mcMap
)
{
ComputeExponents
(
mcMap
);
};
...
...
@@ -21,7 +21,7 @@ struct Exponents {
void
ComputeExponents
(
const
MultilevelData
&
data
);
void
ComputeExponents
(
const
MonteCarlo
Map
&
mcMap
);
void
ComputeExponents
(
const
Estimator
Map
&
mcMap
);
void
ComputeExponents
(
const
MeanMap
&
mean
,
const
SVarMap
&
sVar
);
...
...
mlmc/src/estimators/datastructure/Errors.cpp
→
mlmc/src/estimators/datastructure/
Multilevel
Errors.cpp
View file @
66dbb6ed
#include "Errors.hpp"
#include "
Multilevel
Errors.hpp"
void
Errors
::
Estimate
(
const
MonteCarlo
Map
&
mcMap
)
{
void
Multilevel
Errors
::
Estimate
(
const
Estimator
Map
&
mcMap
)
{
Estimate
(
MultilevelData
(
mcMap
));
}
void
Errors
::
Estimate
(
const
MultilevelData
&
data
)
{
void
Multilevel
Errors
::
Estimate
(
const
MultilevelData
&
data
)
{
exponents
.
ComputeExponents
(
data
);
numeric
=
EstimateNumeric
(
data
);
stochastic
=
EstimateStochastic
(
data
);
total
=
numeric
+
stochastic
;
}
double
Errors
::
EstimateNumeric
(
const
MultilevelData
&
data
)
{
double
Multilevel
Errors
::
EstimateNumeric
(
const
MultilevelData
&
data
)
{
exponents
.
ComputeExponents
(
data
);
return
EstimateNumeric
(
data
.
means
);
}
double
Errors
::
EstimateNumeric
(
const
MeanMap
&
avg
s
)
{
auto
_levels
=
avg
s
.
GetLevelVector
();
auto
_avgs
=
avg
s
.
GetYVector
();
double
Multilevel
Errors
::
EstimateNumeric
(
const
MeanMap
&
mean
s
)
{
auto
_levels
=
mean
s
.
GetLevelVector
();
auto
_avgs
=
mean
s
.
GetYVector
();
double
alpha
=
exponents
.
alpha
;
if
(
alpha
==
0.0
)
alpha
=
exponents
.
ComputeAlpha
(
avg
s
);
if
(
alpha
==
0.0
)
alpha
=
exponents
.
ComputeAlpha
(
mean
s
);
double
err
=
0.0
;
int
integer
=
avg
s
.
size
()
-
2
;
int
integer
=
mean
s
.
size
()
-
2
;
for
(
int
i
=
1
;
i
<
_levels
.
size
();
i
++
)
{
err
=
max
(
_avgs
[
i
]
/
(
pow
(
2.0
,
alpha
)
-
1
)
/
(
pow
(
2.0
,
integer
)),
err
);
integer
--
;
...
...
@@ -34,13 +34,13 @@ double Errors::EstimateNumeric(const MeanMap &avgs) {
return
err
;
}
double
Errors
::
EstimateStochastic
(
const
MultilevelData
&
data
)
{
double
Multilevel
Errors
::
EstimateStochastic
(
const
MultilevelData
&
data
)
{
return
EstimateStochastic
(
data
.
ctrs
,
data
.
sVars
);
}
double
Errors
::
EstimateStochastic
(
const
SampleCounterMap
&
ctrs
,
const
SVarMap
&
v
ars
)
{
double
Multilevel
Errors
::
EstimateStochastic
(
const
SampleCounterMap
&
ctrs
,
const
SVarMap
&
sV
ars
)
{
auto
_ctrs
=
ctrs
.
GetMVector
();
auto
_vars
=
v
ars
.
GetYVector
();
auto
_vars
=
sV
ars
.
GetYVector
();
double
err
=
0.0
;
for
(
int
i
=
0
;
i
<
_ctrs
.
size
();
i
++
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment