aGrUM
0.21.0
a C++ library for (probabilistic) graphical models
fmdpSimulator.cpp
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by 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
)),
_loaded_
(
false
) {
49
GUM_CONSTRUCTOR
(
FMDPSimulator
);
50
}
51
52
FMDPSimulator
::
FMDPSimulator
(
const
std
::
string
&
ressource
) :
AbstractSimulator
(),
_loaded_
(
true
) {
53
GUM_CONSTRUCTOR
(
FMDPSimulator
);
54
55
_fmdp_
=
new
FMDP
<
double
>(
true
);
56
FMDPDatReader
<
double
>
reader
(
_fmdp_
,
ressource
);
57
reader
.
trace
(
false
);
58
reader
.
proceed
();
59
}
60
61
/*
62
* Default destructor.
63
*/
64
FMDPSimulator
::~
FMDPSimulator
() {
65
GUM_DESTRUCTOR
(
FMDPSimulator
);
66
if
(
_loaded_
)
delete
_fmdp_
;
67
}
68
69
70
// ===========================================================================
71
//
72
// ===========================================================================
73
74
void
FMDPSimulator
::
perform
(
Idx
actionId
) {
75
Instantiation
newState
;
76
for
(
auto
varIter
=
this
->
beginVariables
();
varIter
!=
this
->
endVariables
(); ++
varIter
) {
77
newState
.
add
(**
varIter
);
78
Instantiation
transit
(
currentState_
);
79
transit
.
add
(*(
this
->
primeVar
(*
varIter
)));
80
81
double
proba
= (
double
)
std
::
rand
() / (
double
)RAND_MAX;
82
double
cdd
= 0.0;
83
for
(
transit
.
setFirstOut
(
currentState_
); !
transit
.
end
();
transit
.
incOut
(
currentState_
)) {
84
cdd
+=
this
->
transitionProbability_
(*
varIter
,
transit
,
actionId
);
85
if
(
proba
<=
cdd
) {
86
newState
.
chgVal
(**
varIter
,
transit
.
val
(*(
this
->
primeVar
(*
varIter
))));
87
break
;
88
}
89
}
90
}
91
currentState_
=
newState
;
92
}
93
94
95
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643