HOG2
MNAgentPuzzle.cpp
Go to the documentation of this file.
1 /*
2  * MNAgentPuzzle.cpp
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 "MNAgentPuzzle.h"
11 #include <assert.h>
12 
13 void MNAgentEnvironment::GetSuccessors(const MNAgentPuzzleState &nodeID, std::vector<MNAgentPuzzleState> &neighbors) const
14 {
15  neighbors.resize(0);
16  std::vector<tAgentAction> actions;
17  GetActions(nodeID, actions);
18  for (unsigned int x = 0; x < actions.size(); x++)
19  {
20  MNAgentPuzzleState s = nodeID;
21  ApplyAction(s, actions[x]);
22  neighbors.push_back(s);
23  }
24 }
25 
26 void MNAgentEnvironment::GetActions(const MNAgentPuzzleState &s, std::vector<tAgentAction> &actions) const
27 {
28  int where = -1;
29  for (unsigned int w = 0; w < s.locations.size(); w++)
30  if (((s.locations[w]>>s.currentAgent)&0x1) != 0)
31  {
32  where = w;
33  break;
34  }
35  assert(where != -1);
36 
37  uint64_t mask = (1<<s.currentAgent)-1;
38  if ((where-s.width > 0) && (s.locations[where-s.width]&mask) == 0)
39  {
40  actions.push_back(kAgentUp);
41  }
42  if ((where+s.width < s.locations.size()) && (s.locations[where+s.width]&mask) == 0)
43  {
44  actions.push_back(kAgentDown);
45  }
46  if ((where%s.width > 0) && (s.locations[where-1]&mask) == 0)
47  {
48  actions.push_back(kAgentLeft);
49  }
50  if ((where%s.width != s.width-1) && (s.locations[where+1]&mask) == 0)
51  {
52  actions.push_back(kAgentRight);
53  }
54  if ((s.locations[where]&mask) == 0)
55  {
56  actions.push_back(kAgentStay);
57  }
58 }
59 
61 {
62  assert(false);
63 }
64 
66 {
67  uint64_t mask1 = (uint64_t)1<<s.currentAgent;
68  uint64_t mask2 = ~mask1;
69 
70  int where = -1;
71  for (unsigned int w = 0; w < s.locations.size(); w++)
72  if (((s.locations[w]>>s.currentAgent)&0x1) != 0)
73  {
74  where = w;
75  break;
76  }
77  assert(where != -1);
78 
79  s.locations[where]&=mask2;
80  // move
81  switch (a)
82  {
83  case kAgentStay: s.locations[where]|=mask1; break;
84  case kAgentLeft: s.locations[where-1]|=mask1; break;
85  case kAgentRight:s.locations[where+1]|=mask1; break;
86  case kAgentDown: s.locations[where+s.width]|=mask1; break;
87  case kAgentUp: s.locations[where-s.width]|=mask1; break;
88  }
90 }
91 
93 {
94  s2 = s1;
95  ApplyAction(s2, a);
96 }
97 
99 {
100  assert(false);
101 }
102 
104 double MNAgentEnvironment::HCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const
105 {
106  // for now we don't need this
107  return 1;
108 }
109 
110 double MNAgentEnvironment::GCost(const MNAgentPuzzleState &node1, const MNAgentPuzzleState &node2) const
111 { return 1; }
113 { return 1; }
114 
116 {
117  return (node == goal);
118 }
119 
121 {
122  int base = node.numAgents;
123  uint64_t hash = 0;
124  for (unsigned int x = 0; x < node.locations.size(); x++)
125  {
126  int which = -1;
127  for (unsigned int w = 0; w < node.numAgents; w++)
128  {
129  if (((node.locations[x]>>w)&0x1) != 0)
130  {
131  which = w;
132  break;
133  }
134  }
135  which += 1;
136  if ((node.locations[x] != 0) && (node.locations[x] != filled))
137  {
138  hash = hash*base+which;
139  }
140  }
141  return hash;
142 }
143 
145 {
146  return act;
147 }
148 
150 {
151 }
152 
154 {
155  glBegin(GL_QUADS);
156  glColor3f(0.5, 0.5, 0.5);
157  glVertex3f(-1, -1, 0.0);
158  glVertex3f( 1, -1, 0.0);
159  glVertex3f( 1, 1, 0.0);
160  glVertex3f(-1, 1, 0.0);
161  glEnd();
162 
163  double radius = max(s.height, s.width);
164  radius = 1/radius;
165  for (unsigned int x = 0; x < s.locations.size(); x++)
166  {
167  if (s.locations[x] == -1)
168  {
169  glColor3f(0.0, 0.0, 0.0);
170  DrawSphere(-1.0+radius+(x%s.width)*2.0*radius,
171  -1.0+radius+((double)((int)x/s.width))*2.0*radius,
172  0, radius);
173  }
174  else if (s.locations[x] > 0)
175  {
176  glColor3f(0.0, (double)s.locations[x]/(double)(1<<s.numAgents), 1-(double)s.locations[x]/(double)(1<<s.numAgents));
177  DrawSphere(-1.0+radius+(x%s.width)*2.0*radius,
178  -1.0+radius+((double)((int)x/s.width))*2.0*radius,
179  0, radius);
180 // glPushMatrix();
181 //
182 // GLdouble xx, yy, zz, rad;
183 // GLfloat r, g, b, t;
184 // GetColor(r, g, b, t);
185 // glColor4f(r, g, b, t);
186 //
187 // glTranslatef(xx-rad, yy+rad/2, zz-rad);
188 // glScalef(rad/(300.0), rad/300.0, 1);
189 // glRotatef(180, 0.0, 0.0, 1.0);
190 // glRotatef(180, 0.0, 1.0, 0.0);
191 // //glTranslatef((float)x/width-0.5, (float)y/height-0.5, 0);
192 // glDisable(GL_LIGHTING);
193 // glutStrokeCharacter(GLUT_STROKE_ROMAN, '0'+);
194 // glEnable(GL_LIGHTING);
195 // //glTranslatef(-x/width+0.5, -y/height+0.5, 0);
196 // glPopMatrix();
197 //
198  }
199  }
200 }
201 
204 {
205 }
206 
208 {
209 }
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
DrawSphere
void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:433
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
MNAgentPuzzleState::currentAgent
int currentAgent
Definition: MNAgentPuzzle.h:52
MNAgentEnvironment::InvertAction
virtual bool InvertAction(tAgentAction &a) const
Definition: MNAgentPuzzle.cpp:98
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
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
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
MNAgentPuzzle.h
node
Nodes to be stored within a Graph.
Definition: Graph.h:170