aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
fmdpSimulator.cpp
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/**
23
* @file
24
* @brief Sources of
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*
29
*/
30
// =====================================================================
31
#
include
<
cstdlib
>
32
#
include
<
random
>
33
//======================================================================
34
#
include
<
agrum
/
FMDP
/
io
/
dat
/
fmdpDatReader
.
h
>
35
#
include
<
agrum
/
FMDP
/
simulation
/
fmdpSimulator
.
h
>
36
//======================================================================
37
38
namespace
gum
{
39
40
// ===========================================================================
41
// Constructors, Destructors.
42
// ===========================================================================
43
44
/*
45
* Default constructor.
46
*/
47
FMDPSimulator
::
FMDPSimulator
(
const
FMDP
<
double
>*
fmdp
) :
48
AbstractSimulator
(),
fmdp__
(
const_cast
<
FMDP
<
double
>* >(
fmdp
)),
49
loaded__
(
false
) {
50
GUM_CONSTRUCTOR
(
FMDPSimulator
);
51
}
52
53
FMDPSimulator
::
FMDPSimulator
(
const
std
::
string
&
ressource
) :
54
AbstractSimulator
(),
loaded__
(
true
) {
55
GUM_CONSTRUCTOR
(
FMDPSimulator
);
56
57
fmdp__
=
new
FMDP
<
double
>(
true
);
58
FMDPDatReader
<
double
>
reader
(
fmdp__
,
ressource
);
59
reader
.
trace
(
false
);
60
reader
.
proceed
();
61
}
62
63
/*
64
* Default destructor.
65
*/
66
FMDPSimulator
::~
FMDPSimulator
() {
67
GUM_DESTRUCTOR
(
FMDPSimulator
);
68
if
(
loaded__
)
delete
fmdp__
;
69
}
70
71
72
// ===========================================================================
73
//
74
// ===========================================================================
75
76
void
FMDPSimulator
::
perform
(
Idx
actionId
) {
77
Instantiation
newState
;
78
for
(
auto
varIter
=
this
->
beginVariables
();
varIter
!=
this
->
endVariables
();
79
++
varIter
) {
80
newState
.
add
(**
varIter
);
81
Instantiation
transit
(
currentState_
);
82
transit
.
add
(*(
this
->
primeVar
(*
varIter
)));
83
84
double
proba
= (
double
)
std
::
rand
() / (
double
)RAND_MAX;
85
double
cdd
= 0.0;
86
for
(
transit
.
setFirstOut
(
currentState_
); !
transit
.
end
();
87
transit
.
incOut
(
currentState_
)) {
88
cdd
+=
this
->
transitionProbability_
(*
varIter
,
transit
,
actionId
);
89
if
(
proba
<=
cdd
) {
90
newState
.
chgVal
(**
varIter
,
transit
.
val
(*(
this
->
primeVar
(*
varIter
))));
91
break
;
92
}
93
}
94
}
95
currentState_
=
newState
;
96
}
97
98
99
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669