HOG2
SFBDPancakePuzzle.cpp
Go to the documentation of this file.
1 /*
2  * SFBDPancakePuzzle.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 1/14/10.
6  * Copyright 2010 NS Software. All rights reserved.
7  *
8  */
9 
10 #include "SFBDPancakePuzzle.h"
11 
13 {
14  pan = new BurnedPancakePuzzle(theSize);
15 }
16 
18 {
19  delete pan;
20 }
21 
22 void SFBDPancakeEnvironment::GetSuccessors(const pancakeStatePair &stateID, std::vector<pancakeStatePair> &neighbors) const
23 {
24  assert(false);
25 }
26 
27 void SFBDPancakeEnvironment::GetActions(const pancakeStatePair &stateID, std::vector<pancakeMovePair> &actions) const
28 {
29  std::vector<pancakeMovePair> acts;
30  bool fromStart = true;
31 
32  int startScore = MeasureLocalHeuristic(stateID.start, stateID.goal);
33  int goalScore = MeasureLocalHeuristic(stateID.goal, stateID.start);
34 
35  if (startScore > goalScore)
36  {
37  fromStart = false;
38 // assert(pan->HCost(stateID.start, stateID.goal)==pan->HCost(stateID.goal, stateID.start));
39 // std::cout << "Start score: [" << startScore << "] " << stateID.start << std::endl;
40 // std::cout << "Goal score: [" << goalScore << "] " << stateID.goal << std::endl;
41  }
42 
43  actions.clear();
44  for (unsigned i = stateID.start.puzzle.size(); i >= 2; i--)
45  actions.push_back(pancakeMovePair(i, fromStart));
46 // for (unsigned i = 2; i < stateID.start.puzzle.size(); i++)
47 // actions.push_back(pancakeMovePair(i, fromStart));
48 }
49 
51 {
52  double hc = pan->HCost(a, b);
53  int result = 0;
54  std::vector<unsigned> acts;
56  pan->GetActions(a1, acts);
57  for (unsigned int x = 0; x < acts.size(); x++)
58  {
59  pan->ApplyAction(a1, acts[x]);
60 // for (unsigned int y = 0; y < acts.size(); y++)
61 // {
62 // if (x==y) continue;
63 // pan->ApplyAction(a1, acts[y]);
64  double c = pan->HCost(a1, b);
65  result += (c>hc)?(1):0;
66 // pan->ApplyAction(a1, acts[y]);
67 // }
68  pan->ApplyAction(a1, acts[x]);
69  }
70  return -result;
71  // std::vector<int> goal_locs(a.puzzle.size());
72 // for (unsigned i = 0; i < a.puzzle.size(); i++) {
73 // goal_locs[abs(b.puzzle[i])] = i;
74 // }
75 //
76 // int total = 0;
77 // // int chunkSize = 1;
78 // for (unsigned int i = 2; i < a.puzzle.size()-1; i++) {
79 // int diff1 = abs(goal_locs[abs(a.puzzle[i])] - goal_locs[abs(a.puzzle[i+1])]);
80 // int diff2 = abs(goal_locs[abs(a.puzzle[i])] - goal_locs[abs(a.puzzle[i-1])]);
81 // if ((diff1 > 1) && (diff2 > 1)) total--;
82 // if (diff1 > 1) total--;
83 // // if (diff2 > 1) total++;
84 // }
85 // return total;
86 }
87 
88 // best so far, but needs to be cheaper (regular pancake)
89 // double hc = pan->HCost(a, b);
90 // int result = 0;
91 // std::vector<unsigned> acts;
92 // BurnedPancakePuzzleState a1 = a;
93 // pan->GetActions(a1, acts);
94 // for (unsigned int x = 0; x < acts.size(); x++)
95 // {
96 // pan->ApplyAction(a1, acts[x]);
97 // result += (pan->HCost(a1, b)>hc)?1:0;
98 // pan->ApplyAction(a1, acts[x]);
99 // }
100 // return result;
101 
102 // current best! (regular pancake)
103 //int SFBDPancakeEnvironment::MeasureLocalHeuristic(const PancakePuzzleState &a, const PancakePuzzleState &b) const
104 //{
105 // std::vector<int> goal_locs(a.puzzle.size());
106 // for (unsigned i = 0; i < a.puzzle.size(); i++) {
107 // goal_locs[b.puzzle[i]] = i;
108 // }
109 //
110 // int total = 0;
111 // // int chunkSize = 1;
112 // for (unsigned int i = 1; i < a.puzzle.size()-1; i++) {
113 // int diff1 = abs(goal_locs[a.puzzle[i]] - goal_locs[a.puzzle[i+1]]);
114 // // int diff2 = abs(goal_locs[a.puzzle[0]] - goal_locs[a.puzzle[i+1]]);
115 // if (diff1 > 1) total--;
116 // // if (diff2 > 1) total++;
117 // }
118 // return total;
119 //}
120 
121 // marginally better
122 //std::vector<int> goal_locs(a.puzzle.size());
123 //for (unsigned i = 0; i < a.puzzle.size(); i++) {
124 // goal_locs[b.puzzle[i]] = i;
125 //}
126 //
127 //int total = 0;
129 //for (unsigned int i = 2; i < a.puzzle.size()-1; i++) {
130 // int diff1 = abs(goal_locs[a.puzzle[i]] - goal_locs[a.puzzle[i+1]]);
131 // int diff2 = abs(goal_locs[a.puzzle[i]] - goal_locs[a.puzzle[i-1]]);
132 // if ((diff1 > 1) && (diff2 > 1)) total--;
133 // if (diff1 > 1) total--;
134 // // if (diff2 > 1) total++;
135 //}
136 //return total;
137 
138 
140 {
141  assert(false);
142  return pancakeMovePair(1, false);
143 }
144 
146 {
147  if (a.applyToStart)
149  else
150  pan->ApplyAction(s.goal, a.theAction);
151 }
152 
154 {
155  return true;
156 }
157 
159 {
160  return pan->HCost(state1.start, state1.goal);
161 }
162 
164 {
165  return 1;
166 }
167 
169 {
170  return 1;
171 }
172 
174 {
175  return (state.start == state.goal);
176 }
177 
179 {
180  return pan->GetStateHash(state.start)^pan->GetStateHash(state.goal);
181 }
182 
184 {
185  return (act.theAction<<1)|(act.applyToStart?1:0);
186 }
187 
189 {
190 }
191 
193 {
194 }
195 
197 {
198  // if we want to draw a set of moves we use this to do so
199 }
200 
SFBDPancakeEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: SFBDPancakePuzzle.cpp:188
SFBDPancakeEnvironment::SFBDPancakeEnvironment
SFBDPancakeEnvironment(int theSize)
Definition: SFBDPancakePuzzle.cpp:12
SFBDPancakeEnvironment::GetActionHash
virtual uint64_t GetActionHash(pancakeMovePair act) const
Definition: SFBDPancakePuzzle.cpp:183
SFBDPancakeEnvironment::GetActions
virtual void GetActions(const pancakeStatePair &stateID, std::vector< pancakeMovePair > &actions) const
Definition: SFBDPancakePuzzle.cpp:27
SFBDPancakeEnvironment::HCost
virtual double HCost(const pancakeStatePair &state1, const pancakeStatePair &state2) const
Heuristic value between two arbitrary nodes.
Definition: SFBDPancakePuzzle.cpp:158
BurnedPancakePuzzle::HCost
double HCost(const BurnedPancakePuzzleState &state1, const BurnedPancakePuzzleState &state2) const
Heuristic value between two arbitrary nodes.
Definition: BurnedPancakePuzzle.cpp:192
SFBDPancakeEnvironment::GCost
virtual double GCost(const pancakeStatePair &state1, const pancakeStatePair &state2) const
Definition: SFBDPancakePuzzle.cpp:168
BurnedPancakePuzzle::GetActions
void GetActions(const BurnedPancakePuzzleState &state, std::vector< unsigned > &actions) const
Definition: BurnedPancakePuzzle.cpp:86
SFBDPancakeEnvironment::MeasureLocalHeuristic
int MeasureLocalHeuristic(const BurnedPancakePuzzleState &a, const BurnedPancakePuzzleState &b) const
Definition: SFBDPancakePuzzle.cpp:50
SFBDPancakeEnvironment::GetStateHash
virtual uint64_t GetStateHash(const pancakeStatePair &state) const
Definition: SFBDPancakePuzzle.cpp:178
pancakeStatePair::goal
BurnedPancakePuzzleState goal
Definition: SFBDPancakePuzzle.h:25
SFBDPancakeEnvironment::GoalTest
virtual bool GoalTest(const pancakeStatePair &state, const pancakeStatePair &goal) const
Definition: SFBDPancakePuzzle.cpp:173
BurnedPancakePuzzleState
Definition: BurnedPancakePuzzle.h:9
SFBDPancakeEnvironment::ApplyAction
virtual void ApplyAction(pancakeStatePair &s, pancakeMovePair a) const
Definition: SFBDPancakePuzzle.cpp:145
a1
TemplateAStar< xyLoc, tDirection, MapEnvironment > a1
Definition: Sample.cpp:34
pancakeMovePair::applyToStart
bool applyToStart
Definition: SFBDPancakePuzzle.h:33
BurnedPancakePuzzleState::puzzle
std::vector< int > puzzle
Definition: BurnedPancakePuzzle.h:18
pancakeStatePair::start
BurnedPancakePuzzleState start
Definition: SFBDPancakePuzzle.h:24
pancakeStatePair
Definition: SFBDPancakePuzzle.h:16
pancakeMovePair
Definition: SFBDPancakePuzzle.h:28
SFBDPancakeEnvironment::GetSuccessors
virtual void GetSuccessors(const pancakeStatePair &stateID, std::vector< pancakeStatePair > &neighbors) const
Definition: SFBDPancakePuzzle.cpp:22
SFBDPancakeEnvironment::GetAction
virtual pancakeMovePair GetAction(const pancakeStatePair &s1, const pancakeStatePair &s2) const
Definition: SFBDPancakePuzzle.cpp:139
SFBDPancakePuzzle.h
BurnedPancakePuzzle
Definition: BurnedPancakePuzzle.h:41
SFBDPancakeEnvironment::~SFBDPancakeEnvironment
virtual ~SFBDPancakeEnvironment()
Definition: SFBDPancakePuzzle.cpp:17
BurnedPancakePuzzle::GetStateHash
virtual uint64_t GetStateHash(const BurnedPancakePuzzleState &s) const
Definition: BurnedPancakePuzzle.cpp:473
BurnedPancakePuzzle::ApplyAction
void ApplyAction(BurnedPancakePuzzleState &s, unsigned a) const
Definition: BurnedPancakePuzzle.cpp:122
pancakeMovePair::theAction
uint16_t theAction
Definition: SFBDPancakePuzzle.h:32
SFBDPancakeEnvironment::pan
BurnedPancakePuzzle * pan
Definition: SFBDPancakePuzzle.h:91
SFBDPancakeEnvironment::InvertAction
virtual bool InvertAction(pancakeMovePair &a) const
Definition: SFBDPancakePuzzle.cpp:153