aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
abstractSimulator.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
/
simulation
/
abstractSimulator
.
h
>
35
//======================================================================
36
37
namespace
gum
{
38
39
// ===========================================================================
40
// Constructors, Destructors.
41
// ===========================================================================
42
43
/*
44
* Default constructor.
45
*/
46
AbstractSimulator
::
AbstractSimulator
() {
47
GUM_CONSTRUCTOR
(
AbstractSimulator
);
48
;
49
}
50
51
/*
52
* Default destructor.
53
*/
54
AbstractSimulator
::~
AbstractSimulator
() {
55
GUM_DESTRUCTOR
(
AbstractSimulator
);
56
;
57
}
58
59
60
// ===========================================================================
61
//
62
// ===========================================================================
63
64
void
AbstractSimulator
::
setInitialStateRandomly
() {
65
bool
hre
=
true
;
66
while
(
hre
) {
67
currentState_
=
randomState_
();
68
hre
=
hasReachEnd
();
69
}
70
}
71
72
Instantiation
AbstractSimulator
::
randomState_
() {
73
Instantiation
retState
;
74
for
(
auto
varIter
=
this
->
beginVariables
();
varIter
!=
this
->
endVariables
(); ++
varIter
) {
75
retState
.
add
(**
varIter
);
76
retState
.
chgVal
(
77
*
varIter
,
78
(
Idx
)(((
double
)
std
::
rand
() / (
double
)RAND_MAX) * (
double
)(*
varIter
)->
domainSize
()));
79
}
80
return
retState
;
81
}
82
83
///
84
bool
AbstractSimulator
::
hasReachEnd
() {
85
if
(
endState_
.
empty
())
return
false
;
86
87
for
(
auto
varIter
=
endState_
.
variablesSequence
().
beginSafe
();
88
varIter
!=
endState_
.
variablesSequence
().
endSafe
();
89
++
varIter
)
90
if
(
endState_
.
val
(**
varIter
) !=
currentState_
.
val
(**
varIter
))
return
false
;
91
return
true
;
92
}
93
94
95
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643