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