HOG2
MNAgentPuzzle.h
Go to the documentation of this file.
1 /*
2  * MNAgentPuzzle.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 9/25/10.
6  * Copyright 2010 University of Denver. All rights reserved.
7  *
8  */
9 
10 #include "SearchEnvironment.h"
11 #include <vector>
12 #include <string.h>
13 #include <iostream>
14 #include <iomanip>
15 
16 
17 #ifndef MNAGENTPUZZLE_H
18 #define MNAGENTPUZZLE_H
19 
24  kAgentUp = 3,
26 };
27 
28 const uint64_t filled = 0xFFFFFFFFFFFFFFFFll;
29 
31 public:
33  MNAgentPuzzleState(unsigned int _width, unsigned int _height)
34  :width(_width), height(_height), locations(height*width)
35  {
36  //locations.resize(height*width);
37  numAgents = 0;
38  currentAgent = 0;
39  }
40  void BlockCell(int x, int y)
41  {
42  assert(locations[y*width+x] == 0);
43  locations[y*width+x] = filled;
44  }
45  void AddAgent(int x, int y)
46  {
47  locations[y*width+x] |= (1<<numAgents);
48  numAgents++;
49  }
50  int width, height;
51  unsigned int numAgents;
53  std::vector<uint64_t> locations;
54 };
55 
56 static std::ostream& operator <<(std::ostream & out, const MNAgentPuzzleState &loc)
57 {
58  for (int y = 0; y < loc.height; y++)
59  {
60  for (int x = 0; x < loc.width; x++)
61  {
62  if (loc.locations[y*loc.width+x] == filled)
63  out << "X";
64  else if (loc.locations[y*loc.width+x] == 0)
65  out << ".";
66  else {
67  for (int t = 0; t < 64; t++)
68  {
69  if ((loc.locations[y*loc.width+x]&((uint64_t)1<<t)) != 0)
70  {
71  //out << std::setw (2) << t;
72  out << t;
73  break;
74  }
75  }
76  }
77  }
78  out << std::endl;
79  }
80  return out;
81 }
82 
83 static bool operator==(const MNAgentPuzzleState &l1, const MNAgentPuzzleState &l2) {
84  for (unsigned int x = 0; x < l1.locations.size(); x++)
85  if (l1.locations[x] != l2.locations[x])
86  return false;
87  return true;
88 }
89 
90 class MNAgentEnvironment : public SearchEnvironment<MNAgentPuzzleState, tAgentAction>
91 {
92 public:
94  virtual void GetSuccessors(const MNAgentPuzzleState &nodeID, std::vector<MNAgentPuzzleState> &neighbors) const;
95  virtual void GetActions(const MNAgentPuzzleState &nodeID, std::vector<tAgentAction> &actions) const;
96  //virtual int GetNumSuccessors(const MNAgentPuzzleState &stateID) const;
97  virtual tAgentAction GetAction(const MNAgentPuzzleState &s1, const MNAgentPuzzleState &s2) const;
98  virtual void ApplyAction(MNAgentPuzzleState &s, tAgentAction a) const;
99 
100  virtual void GetNextState(const MNAgentPuzzleState &, tAgentAction , MNAgentPuzzleState &) const;
101 
102  virtual bool InvertAction(tAgentAction &a) const;
103 
105  virtual double HCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const;
106 
109  virtual double HCost(const MNAgentPuzzleState &node) const
110  { assert(bValidSearchGoal); return HCost(node, searchGoal); }
111 
112  virtual double GCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const;
113  virtual double GCost(const MNAgentPuzzleState &node, const tAgentAction &act) const;
114  virtual bool GoalTest(const MNAgentPuzzleState &node, const MNAgentPuzzleState &goal) const;
115 
117  virtual bool GoalTest(const MNAgentPuzzleState &node) const
118  { return bValidSearchGoal&&(node == searchGoal); }
119 
121  virtual uint64_t GetStateHash(const MNAgentPuzzleState &node) const;
122  virtual uint64_t GetActionHash(tAgentAction act) const;
123 
124  //virtual double GetPathLength(std::vector<MNAgentPuzzleState> &neighbors);
125 
126  virtual void OpenGLDraw() const;
127  virtual void OpenGLDraw(const MNAgentPuzzleState&) const;
129  virtual void OpenGLDraw(const MNAgentPuzzleState&, const MNAgentPuzzleState&, float) const;
130  virtual void OpenGLDraw(const MNAgentPuzzleState&, const tAgentAction&) const;
131 private:
133  tAgentAction &curr,
134  std::vector<tAgentAction> &actions,
135  int realMoves,
136  int depth,
137  std::vector<bool> &moved) const;
139 };
140 
141 #endif
MNAgentEnvironment::GetSuccessors
virtual void GetSuccessors(const MNAgentPuzzleState &nodeID, std::vector< MNAgentPuzzleState > &neighbors) const
Definition: MNAgentPuzzle.cpp:13
MNAgentEnvironment::HCost
virtual double HCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const
Heuristic value between two arbitrary nodes.
Definition: MNAgentPuzzle.cpp:104
MNAgentPuzzleState::width
int width
Definition: MNAgentPuzzle.h:50
kAgentStay
@ kAgentStay
Definition: MNAgentPuzzle.h:21
MNAgentEnvironment::GetNextState
virtual void GetNextState(const MNAgentPuzzleState &, tAgentAction, MNAgentPuzzleState &) const
Definition: MNAgentPuzzle.cpp:92
MNAgentEnvironment::GetAction
virtual tAgentAction GetAction(const MNAgentPuzzleState &s1, const MNAgentPuzzleState &s2) const
Definition: MNAgentPuzzle.cpp:60
MNAgentEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: MNAgentPuzzle.cpp:149
MNAgentPuzzleState::height
int height
Definition: MNAgentPuzzle.h:50
kAgentUp
@ kAgentUp
Definition: MNAgentPuzzle.h:24
kAgentDown
@ kAgentDown
Definition: MNAgentPuzzle.h:25
tAgentAction
tAgentAction
Definition: MNAgentPuzzle.h:20
MNAgentEnvironment::GCost
virtual double GCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const
Definition: MNAgentPuzzle.cpp:110
MNAgentEnvironment::domainAbstractionSize
int domainAbstractionSize
Definition: MNAgentPuzzle.h:138
MNAgentPuzzleState::MNAgentPuzzleState
MNAgentPuzzleState(unsigned int _width, unsigned int _height)
Definition: MNAgentPuzzle.h:33
loc
Definition: MapGenerators.cpp:296
SearchEnvironment< MNAgentPuzzleState, tAgentAction >::searchGoal
MNAgentPuzzleState searchGoal
Definition: SearchEnvironment.h:113
MNAgentEnvironment::FindLegalMoves
void FindLegalMoves(MNAgentPuzzleState &s, tAgentAction &curr, std::vector< tAgentAction > &actions, int realMoves, int depth, std::vector< bool > &moved) const
SearchEnvironment< MNAgentPuzzleState, tAgentAction >::bValidSearchGoal
bool bValidSearchGoal
Definition: SearchEnvironment.h:112
MNAgentEnvironment::SetDomainAbstractionSize
void SetDomainAbstractionSize(int val)
Definition: MNAgentPuzzle.h:120
MNAgentPuzzleState::currentAgent
int currentAgent
Definition: MNAgentPuzzle.h:52
MNAgentPuzzleState::MNAgentPuzzleState
MNAgentPuzzleState()
Definition: MNAgentPuzzle.h:32
MNAgentEnvironment::InvertAction
virtual bool InvertAction(tAgentAction &a) const
Definition: MNAgentPuzzle.cpp:98
MNAgentEnvironment
Definition: MNAgentPuzzle.h:90
MNAgentEnvironment::HCost
virtual double HCost(const MNAgentPuzzleState &node) const
Heuristic value between node and the stored goal.
Definition: MNAgentPuzzle.h:109
MNAgentEnvironment::GoalTest
virtual bool GoalTest(const MNAgentPuzzleState &node, const MNAgentPuzzleState &goal) const
Definition: MNAgentPuzzle.cpp:115
MNAgentPuzzleState::numAgents
unsigned int numAgents
Definition: MNAgentPuzzle.h:51
kAgentRight
@ kAgentRight
Definition: MNAgentPuzzle.h:23
MNAgentPuzzleState
Definition: MNAgentPuzzle.h:30
MNAgentPuzzleState::AddAgent
void AddAgent(int x, int y)
Definition: MNAgentPuzzle.h:45
MNAgentEnvironment::GetActions
virtual void GetActions(const MNAgentPuzzleState &nodeID, std::vector< tAgentAction > &actions) const
Definition: MNAgentPuzzle.cpp:26
MNAgentEnvironment::ApplyAction
virtual void ApplyAction(MNAgentPuzzleState &s, tAgentAction a) const
Definition: MNAgentPuzzle.cpp:65
MNAgentEnvironment::GetStateHash
virtual uint64_t GetStateHash(const MNAgentPuzzleState &node) const
Definition: MNAgentPuzzle.cpp:120
MNAgentEnvironment::GetActionHash
virtual uint64_t GetActionHash(tAgentAction act) const
Definition: MNAgentPuzzle.cpp:144
kAgentLeft
@ kAgentLeft
Definition: MNAgentPuzzle.h:22
MNAgentPuzzleState::locations
std::vector< uint64_t > locations
Definition: MNAgentPuzzle.h:53
filled
const uint64_t filled
Definition: MNAgentPuzzle.h:28
MNAgentEnvironment::GoalTest
virtual bool GoalTest(const MNAgentPuzzleState &node) const
Goal Test if the goal is stored.
Definition: MNAgentPuzzle.h:117
operator<<
static std::ostream & operator<<(std::ostream &out, const MNAgentPuzzleState &loc)
Definition: MNAgentPuzzle.h:56
operator==
static bool operator==(const MNAgentPuzzleState &l1, const MNAgentPuzzleState &l2)
Definition: MNAgentPuzzle.h:83
MNAgentPuzzleState::BlockCell
void BlockCell(int x, int y)
Definition: MNAgentPuzzle.h:40
MNAgentEnvironment::MNAgentEnvironment
MNAgentEnvironment()
Definition: MNAgentPuzzle.h:93
SearchEnvironment
Definition: SearchEnvironment.h:30
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h