HOG2
CanonicalGraphEnvironment.h
Go to the documentation of this file.
1 //
2 // CanonicalGraphEnvironment.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 4/11/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #ifndef CanonicalGraphEnvironment_h
10 #define CanonicalGraphEnvironment_h
11 
12 #include <stdio.h>
13 #include <unordered_map>
14 #include "TemplateAStar.h"
15 #include "Graph.h"
16 #include "GraphEnvironment.h"
17 
18 struct canGraphState {
21 };
22 
23 bool operator==(const canGraphState &s1, const canGraphState &s2);
24 std::ostream &operator<<(std::ostream &out, const canGraphState &s1);
25 
26 namespace std
27 {
28  template<> struct hash<std::pair<graphState, graphState>>
29  {
30  inline size_t operator()(const pair<graphState, graphState> & v) const
31  {
32  return (v.first<<32)|v.second;
33  }
34  };
35 
36  template<> struct hash<canGraphState>
37  {
38  inline size_t operator()(const canGraphState & v) const
39  {
40  return (v.parent<<32)|v.s;
41  }
42  };
43 }
44 
45 class CanonicalGraphEnvironment : public SearchEnvironment<canGraphState, graphMove> {
46 public:
49  void GetSuccessors(const canGraphState &stateID, std::vector<canGraphState> &neighbors) const;
50 // int GetNumSuccessors(const canGraphState &stateID) const;
51  void GetActions(const canGraphState &stateID, std::vector<graphMove> &actions) const;
52  graphMove GetAction(const canGraphState &s1, const canGraphState &s2) const;
53  void ApplyAction(canGraphState &s, graphMove a) const;
54  bool InvertAction(graphMove &a) const;
55 
56  void SetDirected(bool b) { directed = b; }
57 
58  double HCost(const canGraphState &state1, const canGraphState &state2) const;
59  double GCost(const canGraphState &state1, const canGraphState &state2) const;
60  double GCost(const canGraphState &state1, const graphMove &state2) const;
61  bool GoalTest(const canGraphState &state, const canGraphState &goal) const;
62  uint64_t GetMaxHash() const;
63  uint64_t GetStateHash(const canGraphState &state) const;
64  uint64_t GetActionHash(graphMove act) const;
65  void OpenGLDraw() const;
66  void OpenGLDraw(const canGraphState &s) const;
67  void OpenGLDraw(const canGraphState &s, const graphMove &gm) const;
68  void OpenGLDraw(const canGraphState &s, const canGraphState&, float) const { OpenGLDraw(s); }
69  void GLDrawLine(const canGraphState &x, const canGraphState &y) const;
70 
71  Graph *GetGraph() { return g; };
72 
74  void ClearGoal() {}
75  bool IsGoalStored() const {return false;}
76 
77  double HCost(const canGraphState &) const {
78  fprintf(stderr, "ERROR: Single State HCost not implemented for CanonicalGraphEnvironment\n");
79  exit(1); return -1.0;}
80 
81  bool GoalTest(const canGraphState &) const {
82  fprintf(stderr, "ERROR: Single State Goal Test not implemented for CanonicalGraphEnvironment\n");
83  exit(1); return false;
84  }
85  void SetDrawEdgeCosts(bool val) { drawEdgeCosts = val; }
86  void SetDrawNodeLabels(bool val) { drawNodeLabels = val; }
87  void SetNodeScale(double v) { nodeScale = v; }
88  void ComputeOrdering();
89 protected:
90  std::unordered_map<canGraphState, uint8_t> canonicalOrdering;
91  bool directed;
92  Graph *g;
95  double nodeScale;
96 };
97 
98 
99 #endif /* CanonicalGraphEnvironment_hpp */
CanonicalGraphEnvironment::GetGraph
Graph * GetGraph()
Definition: CanonicalGraphEnvironment.h:71
Graph.h
graphMove
Definition: GraphEnvironment.h:34
CanonicalGraphEnvironment::GetActionHash
uint64_t GetActionHash(graphMove act) const
Definition: CanonicalGraphEnvironment.cpp:161
graphState
unsigned long graphState
Definition: GraphEnvironment.h:32
CanonicalGraphEnvironment::GetStateHash
uint64_t GetStateHash(const canGraphState &state) const
Definition: CanonicalGraphEnvironment.cpp:156
CanonicalGraphEnvironment::InvertAction
bool InvertAction(graphMove &a) const
Definition: CanonicalGraphEnvironment.cpp:116
CanonicalGraphEnvironment::GCost
double GCost(const canGraphState &state1, const canGraphState &state2) const
Definition: CanonicalGraphEnvironment.cpp:132
std::hash< std::pair< graphState, graphState > >::operator()
size_t operator()(const pair< graphState, graphState > &v) const
Definition: CanonicalGraphEnvironment.h:30
operator==
bool operator==(const canGraphState &s1, const canGraphState &s2)
Definition: CanonicalGraphEnvironment.cpp:12
CanonicalGraphEnvironment::SetDirected
void SetDirected(bool b)
Definition: CanonicalGraphEnvironment.h:56
Graph
A generic Graph class.
Definition: Graph.h:66
canGraphState::s
graphState s
Definition: CanonicalGraphEnvironment.h:20
CanonicalGraphEnvironment::drawNodeLabels
bool drawNodeLabels
Definition: CanonicalGraphEnvironment.h:94
CanonicalGraphEnvironment::GetActions
void GetActions(const canGraphState &stateID, std::vector< graphMove > &actions) const
Definition: CanonicalGraphEnvironment.cpp:99
CanonicalGraphEnvironment::ApplyAction
void ApplyAction(canGraphState &s, graphMove a) const
Definition: CanonicalGraphEnvironment.cpp:110
canGraphState::parent
graphState parent
Definition: CanonicalGraphEnvironment.h:19
CanonicalGraphEnvironment::HCost
double HCost(const canGraphState &) const
Heuristic value between node and the stored goal.
Definition: CanonicalGraphEnvironment.h:77
CanonicalGraphEnvironment::GoalTest
bool GoalTest(const canGraphState &) const
Goal Test if the goal is stored.
Definition: CanonicalGraphEnvironment.h:81
CanonicalGraphEnvironment::directed
bool directed
Definition: CanonicalGraphEnvironment.h:91
CanonicalGraphEnvironment::drawEdgeCosts
bool drawEdgeCosts
Definition: CanonicalGraphEnvironment.h:93
CanonicalGraphEnvironment::SetDrawEdgeCosts
void SetDrawEdgeCosts(bool val)
Definition: CanonicalGraphEnvironment.h:85
CanonicalGraphEnvironment::GetAction
graphMove GetAction(const canGraphState &s1, const canGraphState &s2) const
Definition: CanonicalGraphEnvironment.cpp:104
TemplateAStar.h
CanonicalGraphEnvironment::GetSuccessors
void GetSuccessors(const canGraphState &stateID, std::vector< canGraphState > &neighbors) const
Definition: CanonicalGraphEnvironment.cpp:41
CanonicalGraphEnvironment::GoalTest
bool GoalTest(const canGraphState &state, const canGraphState &goal) const
Definition: CanonicalGraphEnvironment.cpp:146
CanonicalGraphEnvironment::ComputeOrdering
void ComputeOrdering()
Definition: CanonicalGraphEnvironment.cpp:320
CanonicalGraphEnvironment::GLDrawLine
void GLDrawLine(const canGraphState &x, const canGraphState &y) const
Definition: CanonicalGraphEnvironment.cpp:291
CanonicalGraphEnvironment::SetDrawNodeLabels
void SetDrawNodeLabels(bool val)
Definition: CanonicalGraphEnvironment.h:86
CanonicalGraphEnvironment::StoreGoal
void StoreGoal(canGraphState &)
Stores the goal for use by single-state HCost.
Definition: CanonicalGraphEnvironment.h:73
std::hash< canGraphState >::operator()
size_t operator()(const canGraphState &v) const
Definition: CanonicalGraphEnvironment.h:38
CanonicalGraphEnvironment::CanonicalGraphEnvironment
CanonicalGraphEnvironment(Graph *g)
Definition: CanonicalGraphEnvironment.cpp:26
CanonicalGraphEnvironment::g
Graph * g
Definition: CanonicalGraphEnvironment.h:92
operator<<
std::ostream & operator<<(std::ostream &out, const canGraphState &s1)
Definition: CanonicalGraphEnvironment.cpp:18
CanonicalGraphEnvironment::ClearGoal
void ClearGoal()
Clears the goal from memory.
Definition: CanonicalGraphEnvironment.h:74
CanonicalGraphEnvironment::OpenGLDraw
void OpenGLDraw() const
Definition: CanonicalGraphEnvironment.cpp:167
std
Definition: CanonicalGraphEnvironment.h:26
CanonicalGraphEnvironment::HCost
double HCost(const canGraphState &state1, const canGraphState &state2) const
Heuristic value between two arbitrary nodes.
Definition: CanonicalGraphEnvironment.cpp:127
canGraphState
Definition: CanonicalGraphEnvironment.h:18
CanonicalGraphEnvironment::canonicalOrdering
std::unordered_map< canGraphState, uint8_t > canonicalOrdering
Definition: CanonicalGraphEnvironment.h:90
CanonicalGraphEnvironment::OpenGLDraw
void OpenGLDraw(const canGraphState &s, const canGraphState &, float) const
Draw the transition at some percentage 0...1 between two states.
Definition: CanonicalGraphEnvironment.h:68
CanonicalGraphEnvironment::~CanonicalGraphEnvironment
~CanonicalGraphEnvironment()
Definition: CanonicalGraphEnvironment.cpp:36
CanonicalGraphEnvironment::GetMaxHash
uint64_t GetMaxHash() const
Definition: CanonicalGraphEnvironment.cpp:151
CanonicalGraphEnvironment
Definition: CanonicalGraphEnvironment.h:45
CanonicalGraphEnvironment::nodeScale
double nodeScale
Definition: CanonicalGraphEnvironment.h:95
SearchEnvironment
Definition: SearchEnvironment.h:30
CanonicalGraphEnvironment::IsGoalStored
bool IsGoalStored() const
Definition: CanonicalGraphEnvironment.h:75
GraphEnvironment.h
CanonicalGraphEnvironment::SetNodeScale
void SetNodeScale(double v)
Definition: CanonicalGraphEnvironment.h:87