Mungojerrie  1.1
Mungojerrie
Model.hh
Go to the documentation of this file.
1 #ifndef MODEL_HH_
2 #define MODEL_HH_
3 
46 #include <vector>
47 #include <set>
48 #include <string>
49 #include "Set.hh"
50 #include "Util.hh"
51 #include "cuddObj.hh"
52 #include "config.h"
53 #include "Parity.hh"
54 #include "Verbosity.hh"
55 #include "ModelOptions.hh"
56 
58 using Node = unsigned long;
63 using Priority = short int;
68 using Player = int;
73 using Action = short int;
75 using Reward = double;
77 using Probability = double;
78 
82 class Model {
83 public:
84  using Index = unsigned;
85  using Propositions = std::set<BDD, Util::BddCompare>;
86 private:
87 
89  struct Transition {
90  Transition(Node s, Node d, Action a, Probability p, Reward r, Priority pi = 0) :
91  source(s), destination(d), action(a), priority(pi), probability(p), reward(r) {}
92  Node source;
93  Node destination;
94  Action action;
95  Priority priority;
96  Probability probability;
97  Reward reward;
98  };
99 
100  using Edges = std::vector<Index>;
101 
103  struct NodeAttributes{
104  NodeAttributes(std::string nm, Player pl) :
105  name(nm), player(pl) {}
106  std::string name;
107  Propositions propositions;
108  Player player;
109  Edges outEdges;
110  Edges inEdges;
111  };
112 
113  using AttributeMap = std::unordered_map<Node,NodeAttributes>;
114  using LabelMap = std::map<std::string, BDD>;
115  using TransitionList = std::vector<Transition>;
116  using NodeCounts = std::map<Player, size_t>;
117 
118 public:
120  using const_iterator = AttributeMap::const_iterator;
122  const_iterator cbegin() { return attributes.cbegin(); }
124  const_iterator cend() { return attributes.cend(); }
125 
128  std::unordered_map<Node, Action> strategy;
129  std::unordered_map<Node, double> vals;
130  };
131 
134  std::set<Node> attr;
135  std::map<Node, Action> strategy;
136  };
137 
139  struct PlayerSets{
140  std::set<Node> w0;
141  std::set<Node> w1;
142  std::map<Node, Action> strategy;
143  };
144 
146  Model(Cudd mgr, ModelOptions options = ModelOptions{});
152  Model(Cudd mgr, std::string const & filename,
153  Verbosity::Level pverbosity = Verbosity::Silent,
154  ModelOptions options = ModelOptions{});
156  Model(Model const & model, Parity const & automaton, ModelOptions options = ModelOptions{});
158  void parseOptions(ModelOptions options);
160  ModelOptions getOptions(void) const;
162  size_t numNodes(void) const { return attributes.size(); }
164  size_t numDecisionNodes(void) const;
166  NodeCounts getNodeCounts(void) const;
168  bool isGame(void) const;
170  bool isBDP() const {return BDP;}
172  void setBDP() {BDP = true;}
174  int getNumOfStrategicPlayers(void) const;
176  bool isNode(Node node) const;
178  bool isDecisionNode(Node node) const;
180  size_t numProbabilisticNodes(void) const;
182  Cudd getManager(void) const { return mgr; }
184  void addDecisionNode(Node node, std::string name, Player player = 0);
186  void addProbabilisticNode(Node node, std::string name = std::string(""));
188  std::string getNodeName(Node node) const;
190  void addLabel(Node node, BDD proposition);
192  LabelMap::const_iterator lookUpLabel(std::string const & label) const;
194  void addDecisionTransition(Node source, Node destination, Action a,
195  Reward reward = 0.0, Priority p = 0);
197  void addProbabilisticTransition(Node source, Node destination,
198  Probability p, Reward r = 0.0);
202  Probability getTransitionProbability(Node source, Node destination) const;
206  void addTransitionProbability(Node source, Node destination,
207  Probability probability, Reward reward = 0.0);
212  void addStateReward(Node node, Reward reward);
215  void addActionStateReward(Node node, Action action, Reward reward);
217  void makeInitial(Node node);
219  Node getInitial(void) const { return initialState; }
221  void addAtomicProposition(BDD proposition, std::string name);
223  std::string getPropositionName(BDD proposition) const;
225  Propositions getAtomicPropositions(void) const {
226  return varSet;
227  }
229  BDD getNodeLetter(Node decisionnode) const;
231  std::string getActionName(Action action) const;
233  void setActionName(Action action, std::string const & name);
235  Player getNodePlayer(Node state) const;
237  void setNodePlayer(Node decisionnode, Player player);
239  std::vector<Node> getDecisionNodes(Player player = 0) const;
241  std::vector<Action> getActions(Node state) const;
243  std::pair<Node, Priority> getSuccessor(Node state, Action action) const;
245  std::map<Node, double> getSuccessors(Node state, Action action) const;
247  Reward getActionStateReward(Node state, Action action) const;
249  friend std::ostream & operator<<(std::ostream & os, Model const & l);
251  Verbosity::Level readVerbosity(void) const { return verbosity; }
253  void setVerbosity(Verbosity::Level v) { verbosity = v; }
257  void sortEdgesByPriority(void);
259  void sanityCheck(void) const;
263  std::unordered_map<Node, Action> loadStrategyCSV(std::string filename) const;
265  std::string prettyPrintState(int n) const;
270  void printStrategyCSV(std::unordered_map<Node, Action>, std::string filename) const;
274  void printActionsCSV(std::string filename) const;
276  void printDot(std::string graphname = std::string("Model"), std::string filename = std::string("-")) const;
278  void printPrism(std::string const & modulename = std::string("m"),
279  std::string filename = std::string("-")) const;
286  Model pruneToStrategy(std::unordered_map<Node, Action> const & strategy,
287  bool removeUnreachable,
288  bool makeMC) const;
290  void invertProperty(void);
291 
292  // Model Checking
298  std::unordered_map<Node, double> getProbabilityOfSat(void) const;
302  std::unordered_map<Node, double> getProbabilityOfSat1Player(void) const;
307  Model::StrategyResults getStrategyReach1Player(std::set<Node> const & target,
308  std::set<Node> const & restriction,
309  std::set<Index> const & forbiddenEdges) const;
319 
321  std::vector< std::set<Node> > getWECs(std::set<Node> const & restriction,
322  std::set<std::pair<Node, Action>> const & forbiddenEdges) const;
324  std::vector< std::set<Node> > getWECs(std::set<Node> const & restriction) const;
326  std::vector< std::set<Node> > getWECs(void) const;
328  std::vector< std::set<Node> > getMECs(std::set<Node> const & restriction,
329  std::set<std::pair<Node, Action>> const & forbiddenEdges) const;
331  std::vector< std::set<Node> > getMECs(std::set<Node> const & restriction) const;
333  std::vector< std::set<Node> > getMECs(void) const;
336  std::unordered_set<Node> getAttractor(std::set<Node> const & target,
337  Player targetPlayer,
338  std::set<Node> const & restriction,
339  std::set<std::pair<Node, Action>> const & forbiddenEdges) const;
342  std::unordered_set<Node> getAttractor(std::set<Node> const & target,
343  Player targetPlayer,
344  std::set<Node> const & restriction) const;
347  std::unordered_set<Node> getAttractor(std::set<Node> const & target,
348  Player targetPlayer) const;
349  std::vector<std::set<Node>> getSCCs(std::set<Node> const & init,
350  std::set<Node> const & restriction,
351  std::set<Index> const & forbiddenEdges) const;
353  std::vector<std::set<Node>> getSCCs(std::set<Node> const & init,
354  std::set<Node> const & restriction) const;
356  std::vector<std::set<Node>> getSCCs(std::set<Node> const & restriction) const;
358  std::vector<std::set<Node>> getSCCs(void) const;
360  Model::AttractorResults getAttractorWithStrat(std::set<Player> targetPlayers, std::set<Node> const & target,
361  std::set<Node> const & restriction,
362  std::set<Index> const & forbiddenEdges) const;
364  Model::AttractorResults getUltraWeakAttr(Player targetPlayer, std::set<Node> const & target,
365  std::set<Node> const & restriction,
366  std::set<Index> const & forbiddenEdges) const;
368  Model::AttractorResults getStrongAttr(Player targetPlayer, std::set<Node> const & target,
369  std::set<Node> const & restriction,
370  std::set<Index> const & forbiddenEdges) const;
372  Model::AttractorResults getWeakAttr(Player targetPlayer, std::set<Node> const & target,
373  std::set<Node> const & restriction,
374  std::set<Index> const & forbiddenEdges) const;
378  Model::PlayerSets probMcNaughton(void) const;
382  Model::PlayerSets probMcNaughton(std::set<Node> const & restriction,
383  std::set<Index> const & forbiddenEdges) const;
387  Model::PlayerSets probMcNaughton(std::map<Node, Priority> pri,
388  std::set<Node> const & restriction,
389  std::set<Index> const & forbiddenEdges) const;
397  Model::StrategyResults getStrategy2Player(std::set<Node> const & restriction,
398  std::set<Index> const & forbiddenEdges) const;
402  Model::StrategyResults getStrategy2Player(std::map<Node, Priority> pri,
403  std::set<Node> const & restriction,
404  std::set<Index> const & forbiddenEdges) const;
405 private:
407  std::vector< std::set<Node> > getWECsNoCheck(std::set<Node> const & restriction,
408  std::set<Index> const & forbiddenEdges) const;
410  std::vector< std::set<Node> > getMECsNoCheck(std::set<Node> const & restriction,
411  std::set<Index> const & forbiddenEdges) const;
413  std::unordered_set<Node> getAttractorNoCheck(std::set<Node> const & target,
414  Player targetPlayer,
415  std::set<Node> const & restriction,
416  std::set<Index> const & forbiddenEdges) const;
418  std::unordered_map<Node, Action> getStrategyWECs(std::vector< std::set<Node> > const & WECs) const;
420  std::unordered_map<Node, Action> getStrategyWECs(void) const;
425  std::unordered_map<Node, double> getSSP(std::set<Node> const & target,
426  std::set<Node> const & restriction,
427  std::set<Index> const & forbiddenEdges) const;
432  std::unordered_map<Node, double> getSSPORTools(std::set<Node> const & target,
433  std::set<Node> const & restriction,
434  std::set<Index> const & forbiddenEdges) const;
439  std::unordered_map<Node, double> getSSPIncr(std::set<Node> const & target,
440  std::set<Node> const & restriction,
441  std::set<Index> const & forbiddenEdges) const;
446  std::unordered_map<Node, double> getSSPPoly(std::set<Node> const & target,
447  std::set<Node> const & restriction,
448  std::set<Index> const & forbiddenEdges) const;
450  std::unordered_map<Node, double> getProbReachabilityORTools(std::set<Node> const & target,
451  std::set<Node> const & restriction,
452  std::set<Index> const & forbiddenEdges) const;
454  std::unordered_map<Node, double> getProbReachabilityIncr(std::set<Node> const & target,
455  std::set<Node> const & restriction,
456  std::set<Index> const & forbiddenEdges) const;
457 
459  std::unordered_map<Node, double> getBDPValueUsingORTools() const;
460 
462  void addTransition(Transition const & tran);
464  bool isTrivial(std::set<Node> const & scc) const;
466  void incrementCount(Player player);
468  void decrementCount(Player player);
469 
470 
471  Verbosity::Level verbosity;
472  Cudd mgr;
473  double epsilon; // tolerance for probability computations
474  double tranEpsilon; // tolerance for sum of transistion probabilities to be considered 1
475  ModelOptions::ReachType reachSolver; // solver for maximal reachability probability
476  ModelOptions::SSPType sspSolver; // solver for stochastic shortest path
477  NodeCounts counts; // node count for each player
478  AttributeMap attributes; // from a node's index to its attributes
479  TransitionList transitions; // list of all model transitions
480  Node initialState;
481  Propositions varSet; // set of propositional variable BDDs
482  LabelMap labelMap; // from label string to BDD
483  std::map<Action, std::string> actionNames;
484  bool sortedLists; // are the edge lists sorted?
485  static Action constexpr invalid = -1;
486  static Player constexpr probPlayer = -1;
487  bool BDP = false; // true if the PRISM model is to be interpreted as a BDP
488 
489  class SccAnalyzer;
490 };
491 
492 #endif
Model::PlayerSets
Class to store return value of probMcNaughton.
Definition: Model.hh:139
Model::getSuccessors
std::map< Node, double > getSuccessors(Node state, Action action) const
Returns a the decision node successors of state with probabilities.
Definition: Model.cc:788
Action
short int Action
Type of actions.
Definition: Model.hh:73
Model::addStateReward
void addStateReward(Node node, Reward reward)
Adds reward to existing reward for all transitions from a decision node.
Definition: Model.cc:553
Model::setNodePlayer
void setNodePlayer(Node decisionnode, Player player)
Sets the player of a decision node.
Definition: Model.cc:696
Model::getActionName
std::string getActionName(Action action) const
Returns the name of an action number.
Definition: Model.cc:671
Model::printDot
void printDot(std::string graphname=std::string("Model"), std::string filename=std::string("-")) const
Write model in dot format.
Definition: Model.cc:1230
ModelOptions
Class to pass options to Model.
Definition: ModelOptions.hh:52
Model::getNodeLetter
BDD getNodeLetter(Node decisionnode) const
Returns the letter labeling a decision node.
Definition: Model.cc:649
Util.hh
Utility functions.
Parity.hh
Parity automata.
Model::getMECs
std::vector< std::set< Node > > getMECs(void) const
Computes the maximal-end-components of model.
Definition: ModelCheck.cc:1701
Model::getNodePlayer
Player getNodePlayer(Node state) const
Gets the player of a node.
Definition: Model.cc:686
Model::setBDP
void setBDP()
set model to be a branchign decision process.
Definition: Model.hh:172
Player
int Player
Type of players.
Definition: Model.hh:68
Model::addDecisionTransition
void addDecisionTransition(Node source, Node destination, Action a, Reward reward=0.0, Priority p=0)
Adds a transition to a decision node.
Definition: Model.cc:469
Model::parseOptions
void parseOptions(ModelOptions options)
Parses ModelOptions.
Definition: Model.cc:272
Model::getNumOfStrategicPlayers
int getNumOfStrategicPlayers(void) const
Returns number of strategic players.
Definition: Model.cc:319
Model::getManager
Cudd getManager(void) const
Returns the BDD manager of the model.
Definition: Model.hh:182
Model::getPropositionName
std::string getPropositionName(BDD proposition) const
Returns name of an atomic proposition.
Definition: Model.cc:635
Model::addDecisionNode
void addDecisionNode(Node node, std::string name, Player player=0)
Adds a decision node to the model.
Definition: Model.cc:345
Model::pruneToStrategy
Model pruneToStrategy(std::unordered_map< Node, Action > const &strategy, bool removeUnreachable, bool makeMC) const
Produces a model with all non-strategic actions removed.
Definition: Model.cc:975
Model::printStrategyCSV
void printStrategyCSV(std::unordered_map< Node, Action >, std::string filename) const
Write strategy to CSV in the given filename.
Definition: Model.cc:1117
Model::cbegin
const_iterator cbegin()
Returns a const_iterator pointing to the first node.
Definition: Model.hh:122
Model::isNode
bool isNode(Node node) const
Checks whether a node exists with a given index.
Definition: Model.cc:330
Model::invertProperty
void invertProperty(void)
Inverts property by incrementing all priorities by 1.
Definition: Model.cc:1060
Model::getStrategyReach1Player
Model::StrategyResults getStrategyReach1Player(std::set< Node > const &target, std::set< Node > const &restriction, std::set< Index > const &forbiddenEdges) const
Computes strategy to reach target for 1-1/2 player model.
Definition: ModelCheck.cc:1322
Model::addAtomicProposition
void addAtomicProposition(BDD proposition, std::string name)
Adds atomic proposition to model.
Definition: Model.cc:622
Model::getSuccessor
std::pair< Node, Priority > getSuccessor(Node state, Action action) const
Returns the next decision node and priority given an action through sampling.
Definition: Model.cc:753
ModelOptions.hh
Option structure for models.
Model::getStrategy1Player
Model::StrategyResults getStrategy1Player(void) const
Computes strategy for WECs and to reach them for 1-1/2 player models.
Definition: ModelCheck.cc:1393
Model::getOptions
ModelOptions getOptions(void) const
Returns current options.
Definition: Model.cc:286
Model::numDecisionNodes
size_t numDecisionNodes(void) const
Returns the number of decision nodes in the model.
Definition: Model.cc:309
Model::isBDP
bool isBDP() const
Returns 1 if the model is a brnaching decision process.
Definition: Model.hh:170
Model::printPrism
void printPrism(std::string const &modulename=std::string("m"), std::string filename=std::string("-")) const
Write model in PRISM format.
Definition: Model.cc:1336
Model::getWECs
std::vector< std::set< Node > > getWECs(void) const
Computes the winning-end-components of model.
Definition: ModelCheck.cc:1553
Model::numProbabilisticNodes
size_t numProbabilisticNodes(void) const
Returns the number of probabilistic nodes in the model.
Definition: Model.cc:298
Node
unsigned long Node
Type of nodes.
Definition: Model.hh:58
Model::cend
const_iterator cend()
Returns a const_iterator pointing "just past the last" node.
Definition: Model.hh:124
Model::getActionStateReward
Reward getActionStateReward(Node state, Action action) const
Returns reward from transition from a node enabled by an action.
Definition: Model.cc:739
Model::setActionName
void setActionName(Action action, std::string const &name)
Sets the name of an action.
Definition: Model.cc:681
Model::addTransitionProbability
void addTransitionProbability(Node source, Node destination, Probability probability, Reward reward=0.0)
Adds probability to a probabilistic transition.
Definition: Model.cc:516
Model::addProbabilisticNode
void addProbabilisticNode(Node node, std::string name=std::string(""))
Adds a probabilistic node to the model.
Definition: Model.cc:358
Model::getAttractorWithStrat
Model::AttractorResults getAttractorWithStrat(std::set< Player > targetPlayers, std::set< Node > const &target, std::set< Node > const &restriction, std::set< Index > const &forbiddenEdges) const
Computes the attractor with computed strategy in return value.
Definition: ModelGames.cc:59
Model::operator<<
friend std::ostream & operator<<(std::ostream &os, Model const &l)
Stream insertion operator overload.
Model::printActionsCSV
void printActionsCSV(std::string filename) const
Write all available actions to CSV in the given filename.
Definition: Model.cc:1177
Model::getNodeCounts
NodeCounts getNodeCounts(void) const
Returns the node counts map without probPlayer.
Definition: Model.cc:91
Model::addProbabilisticTransition
void addProbabilisticTransition(Node source, Node destination, Probability p, Reward r=0.0)
Adds a transition to a probabilistic node.
Definition: Model.cc:437
Probability
double Probability
Type of probabilities.
Definition: Model.hh:77
Model::replaceUnitProbabilityTransitions
void replaceUnitProbabilityTransitions()
Replaces probabilistic transitions with deterministic ones.
Definition: Model.cc:820
Model::getAttractor
std::unordered_set< Node > getAttractor(std::set< Node > const &target, Player targetPlayer, std::set< Node > const &restriction, std::set< std::pair< Node, Action >> const &forbiddenEdges) const
Computes the attractor for target set and player.
Model::getSCCs
std::vector< std::set< Node > > getSCCs(void) const
Computes the SCCs of the model.
Definition: ModelCheck.cc:270
Set.hh
Set manipulation functions.
Model::AttractorResults
Class for return value of getAttractorWithStrat.
Definition: Model.hh:133
Model::lookUpLabel
LabelMap::const_iterator lookUpLabel(std::string const &label) const
Looks up proposition from label.
Definition: Model.cc:396
Model::StrategyResults
Class for return value of getStrategy.
Definition: Model.hh:127
Model::getStrongAttr
Model::AttractorResults getStrongAttr(Player targetPlayer, std::set< Node > const &target, std::set< Node > const &restriction, std::set< Index > const &forbiddenEdges) const
Computes the strong attractor (2 player attractor where probabilistic nodes helps targetPlayer).
Definition: ModelGames.cc:128
Model::getProbabilityOfSat1Player
std::unordered_map< Node, double > getProbabilityOfSat1Player(void) const
Computes maximum probabilities of reaching WECs for 1-1/2 player model.
Definition: ModelCheck.cc:1284
Model::const_iterator
AttributeMap::const_iterator const_iterator
Constant iterator type.
Definition: Model.hh:120
Model::getInitial
Node getInitial(void) const
Returns the initial state of the model.
Definition: Model.hh:219
Model::getDecisionNodes
std::vector< Node > getDecisionNodes(Player player=0) const
Returns the set of decision nodes in the model for one player.
Definition: Model.cc:714
Model::setVerbosity
void setVerbosity(Verbosity::Level v)
Sets the verbosity level of the model.
Definition: Model.hh:253
Model::getWeakAttr
Model::AttractorResults getWeakAttr(Player targetPlayer, std::set< Node > const &target, std::set< Node > const &restriction, std::set< Index > const &forbiddenEdges) const
Computes the weak attractor (attractor that targetPlayer can attract almost surely).
Definition: ModelGames.cc:141
Model::numNodes
size_t numNodes(void) const
Returns the total number of nodes in the model.
Definition: Model.hh:162
Verbosity.hh
Verbosity-related definitions.
Model::prettyPrintState
std::string prettyPrintState(int n) const
Processes the name of the product state.
Definition: Model.cc:1321
Model::getStrategy2Player
Model::StrategyResults getStrategy2Player(void) const
Computes strategy and winning sets for quantitative parity games.
Definition: ModelGames.cc:503
Model
Class for MDPs and related structures.
Definition: Model.hh:82
Model::getNodeName
std::string getNodeName(Node node) const
Returns node name.
Definition: Model.cc:368
Model::sortEdgesByPriority
void sortEdgesByPriority(void)
Sorts edges into and out of nodes by their priorities.
Definition: Model.cc:251
Model::getStrategy
Model::StrategyResults getStrategy(void) const
Computes getStrategy1Player or getStrategy2Player depending on whether the model is 1-1/2 players or ...
Definition: ModelCheck.cc:1426
Model::getUltraWeakAttr
Model::AttractorResults getUltraWeakAttr(Player targetPlayer, std::set< Node > const &target, std::set< Node > const &restriction, std::set< Index > const &forbiddenEdges) const
Computes the ultra-weak attractor (2 player attractor where probabilistic nodes are adversarial).
Definition: ModelGames.cc:117
Model::getBDPStrategy
Model::StrategyResults getBDPStrategy(void) const
Computes optimal strategy for a BDP.
Definition: ModelCheck.cc:2130
Model::sanityCheck
void sanityCheck(void) const
Checks sanity of model.
Definition: Model.cc:864
Model::isDecisionNode
bool isDecisionNode(Node node) const
Checks whether a decision node exists with a given index.
Definition: Model.cc:336
Model::getAtomicPropositions
Propositions getAtomicPropositions(void) const
Returns the set of atomic propositions.
Definition: Model.hh:225
Model::makeInitial
void makeInitial(Node node)
Makes a decision node the initial node of the model.
Definition: Model.cc:609
Reward
double Reward
Type of rewards.
Definition: Model.hh:75
Model::isGame
bool isGame(void) const
Returns 1 if the model is a stochastic game.
Definition: Model.cc:314
Model::getProbabilityOfSat
std::unordered_map< Node, double > getProbabilityOfSat(void) const
Computes maximum probabilities of reaching WECs for 1-1/2 player models and probabilities for parity ...
Definition: ModelCheck.cc:1270
Model::addActionStateReward
void addActionStateReward(Node node, Action action, Reward reward)
Adds reward to existing reward for all transitions from a node enabled by an action.
Definition: Model.cc:580
Parity
Class of parity automata.
Definition: Parity.hh:85
Model::getTransitionProbability
Probability getTransitionProbability(Node source, Node destination) const
Returns the probability of a probabilistic transition.
Definition: Model.cc:490
Model::getActions
std::vector< Action > getActions(Node state) const
Returns a vector of actions possible from the given node.
Definition: Model.cc:725
Model::probMcNaughton
Model::PlayerSets probMcNaughton(void) const
Computes strategy and winning sets for qualitative parity games.
Definition: ModelGames.cc:207
Model::loadStrategyCSV
std::unordered_map< Node, Action > loadStrategyCSV(std::string filename) const
Loads pure strategy from CSV.
Definition: Model.cc:1067
Model::Model
Model(Cudd mgr, ModelOptions options=ModelOptions{})
Constructor.
Definition: Model.cc:98
Model::addLabel
void addLabel(Node node, BDD proposition)
Adds an atomic proposition to a node.
Definition: Model.cc:378
Model::readVerbosity
Verbosity::Level readVerbosity(void) const
Returns the verbosity level of the model.
Definition: Model.hh:251