aGrUM  0.14.2
abstractSimulator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // =====================================================================
28 #include <cstdlib>
29 #include <random>
30 //======================================================================
32 //======================================================================
33 
34 namespace gum {
35 
36  // ===========================================================================
37  // Constructors, Destructors.
38  // ===========================================================================
39 
40  /*
41  * Default constructor.
42  */
44 
45  /*
46  * Default destructor.
47  */
49 
50 
51  // ===========================================================================
52  //
53  // ===========================================================================
54 
56  bool hre = true;
57  while (hre) {
59  hre = hasReachEnd();
60  }
61  }
62 
64  Instantiation retState;
65  for (auto varIter = this->beginVariables(); varIter != this->endVariables();
66  ++varIter) {
67  retState.add(**varIter);
68  retState.chgVal(*varIter,
69  (Idx)(((double)std::rand() / (double)RAND_MAX)
70  * (double)(*varIter)->domainSize()));
71  }
72  return retState;
73  }
74 
77  if (_endState.empty()) return false;
78 
79  for (auto varIter = _endState.variablesSequence().beginSafe();
80  varIter != _endState.variablesSequence().endSafe();
81  ++varIter)
82  if (_endState.val(**varIter) != _currentState.val(**varIter)) return false;
83  return true;
84  }
85 
86 
87 } // End of namespace gum
virtual bool hasReachEnd()
Tests if end state has been reached.
<agrum/FMDP/simulation/abstractSimulator.h>
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Instantiation _currentState
Tha state in which the system currently is.
virtual ~AbstractSimulator()
Default destructor.
void setInitialStateRandomly()
Sets the intial statefrom which we begun the simulation.
virtual SequenceIteratorSafe< const DiscreteVariable *> beginVariables()=0
Iteration over the variables of the simulated probleme.
Class for simulating a discrete time stochastic process.
virtual bool empty() const final
Returns true if the instantiation is empty.
virtual Instantiation _randomState()
Choses a random state as the first test for a run.
virtual SequenceIteratorSafe< const DiscreteVariable *> endVariables()=0
Iteration over the variables of the simulated probleme.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
Size Idx
Type for indexes.
Definition: types.h:50
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
AbstractSimulator()
Default constructor.