Scheduler
stateSpaceBuilder.cpp
Go to the documentation of this file.
1 
10 #include "stateSpaceBuilder.h"
11 
12 #include <cassert>
13 #include <iostream>
14 #include <stdexcept>
15 
16 #include "priorityState.h"
17 #include "stateSpace.h"
18 #include "stateSpaceDimension.h"
19 
20 using namespace Mdp;
21 
22 std::shared_ptr<StateSpace> StateSpaceBuilder::getStateSpace()
23 {
24  throwIfNoModel();
25  throwIfInconsistentNumberOfStates();
26  /*if nbOfStates is 1, it means no dimension has been added to the state space, and therefore we only have priority states.*/
27  if (nbOfStates == 1)
28  nbOfStates = 0;
29  std::shared_ptr<StateSpace> stateSpace(new StateSpace(nbOfStates, nbOfPriorityStates, dimensions, priorityStates));
30  stateSpace->domainModel = domainModel; /*TODO: is this necessary?*/
31  return stateSpace;
32 }
33 
35 {
36  /*TODO: check that this dimension was not added twice*/
37  throwIfNoModel();
38  dim->domainModel = domainModel;
39  dim->setIndex(nbOfDimensions++);
40  nbOfStates *= dim->getNumberOfPositions();
41  assert(nbOfStates >= 0); //getNumberOfPositions returns -1 if not defined
42  dimensions.push_back(dim);
43 }
44 void StateSpaceBuilder::setDomainModel(std::shared_ptr<DomainModel> model)
45 {
46  domainModel = model;
47  if (domainModel == nullptr)
48  throw std::runtime_error("domainModel is null");
49 }
50 
51 void StateSpaceBuilder::throwIfInconsistentNumberOfStates()
52 {
53  if (nbOfStates == 1 && nbOfDimensions == 0)
54  {
55  if (nbOfPriorityStates == 0)
56  throw std::runtime_error("StateSpace has no states defined");
57  if (nbOfPriorityStates == 1)
58  throw std::runtime_error("State space has no dimensions and only one priority state");
59  }
60  if (nbOfStates > 1 && nbOfDimensions > 0)
61  {
62  return;
63  }
64  if (nbOfStates == 1 && nbOfDimensions > 0)
65  {
66  throw std::runtime_error("The state space has dimensions but all have only one state position");
67  }
68  if (nbOfStates == 0 && nbOfDimensions > 0)
69  {
70  throw std::runtime_error("A statespace dimension has zero state positions");
71  }
72 }
73 
74 void StateSpaceBuilder::throwIfNoModel()
75 {
76  if (domainModel == nullptr)
77  {
78  throw std::runtime_error("no domain model specified");
79  }
80 }
81 
83 {
84  /*TODO check this state was not added twice*/
85  throwIfNoModel();
86  prio->domainModel = domainModel;
87  nbOfPriorityStates++;
88  priorityStates.push_back(prio);
89 }
void setDomainModel(std::shared_ptr< DomainModel > model)
sets the domain model used by the state space to determine current state
std::shared_ptr< DomainModel > domainModel
std::shared_ptr< StateSpace > getStateSpace()
call this function last!
void addDimension(StateSpaceDimension *dimension)
adds a dimension to the state space
virtual size_t getNumberOfPositions()=0
returns the number of possible positions along that dimension
Definition: action.h:18
void addPriorityState(PriorityState *)
adds single states to the state space
std::shared_ptr< DomainModel > domainModel
Definition: priorityState.h:53
a single state of the state space
Definition: priorityState.h:31
this models a dimension in the state space.