HOG2
BurnedPancakePuzzle.h
Go to the documentation of this file.
1 #ifndef BURNEDPANCAKE_H
2 #define BURNEDPANCAKE_H
3 
4 #include <stdint.h>
5 #include <iostream>
6 #include "SearchEnvironment.h"
7 #include <sstream>
8 
10 public:
12 
13  BurnedPancakePuzzleState(unsigned int puzzle_size) {
14  puzzle.resize(puzzle_size);
15  for (unsigned int x = 0; x < puzzle.size(); x++)
16  puzzle[x] = x+1;
17  }
18  std::vector<int> puzzle;
19 };
20 
21 static std::ostream& operator <<(std::ostream & out, const BurnedPancakePuzzleState &loc)
22 {
23  for (unsigned int x = 0; x < loc.puzzle.size(); x++)
24  {
25  out << loc.puzzle[x] << " ";
26  }
27  return out;
28 }
29 
31 {
32  if (l1.puzzle.size() != l2.puzzle.size())
33  return false;
34 
35  for (unsigned int x = 0; x < l1.puzzle.size(); x++)
36  if (l1.puzzle[x] != l2.puzzle[x])
37  return false;
38  return true;
39 }
40 
41 class BurnedPancakePuzzle : public SearchEnvironment<BurnedPancakePuzzleState, unsigned> {
42 public:
43  BurnedPancakePuzzle(unsigned s);
44  BurnedPancakePuzzle(unsigned size, const std::vector<unsigned> op_order); // used to set action order
45 
47  void GetSuccessors(const BurnedPancakePuzzleState &state, std::vector<BurnedPancakePuzzleState> &neighbors) const;
48  void GetActions(const BurnedPancakePuzzleState &state, std::vector<unsigned> &actions) const;
49  unsigned GetAction(const BurnedPancakePuzzleState &s1, const BurnedPancakePuzzleState &s2) const;
50  void ApplyAction(BurnedPancakePuzzleState &s, unsigned a) const;
51  bool InvertAction(unsigned &a) const;
52 
53  virtual uint64_t GetStateHash(const BurnedPancakePuzzleState &s) const;
54 
55  double HCost(const BurnedPancakePuzzleState &state1, const BurnedPancakePuzzleState &state2) const;
56  double Memory_Free_HCost(const BurnedPancakePuzzleState &state1, const std::vector<int> &goal_locs) const;
57  double HCost(const BurnedPancakePuzzleState &state1) const;
58 
59  double GCost(const BurnedPancakePuzzleState &, const BurnedPancakePuzzleState &) const {return 1.0;}
60  double GCost(const BurnedPancakePuzzleState &, const unsigned &) const { return 1.0; }
61 
62  bool GoalTest(const BurnedPancakePuzzleState &state, const BurnedPancakePuzzleState &goal) const;
63 
64  bool GoalTest(const BurnedPancakePuzzleState &s) const;
65 
66  uint64_t GetActionHash(unsigned act) const;
67  void StoreGoal(BurnedPancakePuzzleState &); // stores the locations for the given goal state
68 
69 // virtual const std::string GetName();
70  std::vector<unsigned> Get_Op_Order(){return operators;}
71 
74  if (!goal_stored) {
75  fprintf(stderr, "ERROR: Call to Get_Goal when no goal stored\n");
76  exit(1);
77  }
78  return goal;
79  }
80 
81  void ClearGoal(){} // clears the current stored information of the goal
82 
83  bool IsGoalStored() const {return goal_stored;} // returns if a goal is stored or not
84 
88  void Change_Op_Order(const std::vector<unsigned> op_order);
89 
90  // currently not drawing anything
91  void OpenGLDraw() const{}
92  void OpenGLDraw(const BurnedPancakePuzzleState &) const {}
93  void OpenGLDraw(const BurnedPancakePuzzleState &, const unsigned &) const {}
94  void OpenGLDraw(const BurnedPancakePuzzleState&, const BurnedPancakePuzzleState&, float) const {}
95 
98 // static void Create_Random_BurnedPancake_Puzzles(std::vector<BurnedPancakePuzzleState> &puzzle_vector, unsigned size, unsigned num_puzzles);
99 //
100 // static int read_in_pancake_puzzles(const char *filename, bool first_counter, unsigned size, unsigned max_puzzles, std::vector<BurnedPancakePuzzleState> &puzzle_vector);
101 //
102 // bool State_Check(const BurnedPancakePuzzleState &to_check) {
103 // if (to_check.puzzle.size() != size)
104 // return false;
105 //
106 // return true;
107 // }
108 //
109 // bool Path_Check(BurnedPancakePuzzleState start, BurnedPancakePuzzleState goal, std::vector<unsigned> &actions);
110 
118  static std::vector<unsigned> Get_Puzzle_Order(int64_t order_num, unsigned num_pancakes);
119 
120  void Set_Use_Memory_Free_Heuristic(bool to_use){use_memory_free = to_use;}
121 
122 private:
123 
124  std::vector<unsigned> operators;
125  bool goal_stored; // whether a goal is stored or not
127 
129  std::vector<int> goal_locations;
130  unsigned size;
131 
132 
133  uint64_t Factorial(int val) const
134  {
135  static uint64_t table[21] =
136  { 1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll, 39916800ll, 479001600ll,
137  6227020800ll, 87178291200ll, 1307674368000ll, 20922789888000ll, 355687428096000ll,
138  6402373705728000ll, 121645100408832000ll, 2432902008176640000ll };
139  if (val > 20)
140  return (uint64_t)-1;
141  return table[val];
142  }
143 };
144 
145 //typedef UnitSimulation<BurnedPancakePuzzleState, unsigned, BurnedPancake> BurnedPancakeSimulation;
146 #endif
BurnedPancakePuzzle::InvertAction
bool InvertAction(unsigned &a) const
Definition: BurnedPancakePuzzle.cpp:143
BurnedPancakePuzzle::operators
std::vector< unsigned > operators
Definition: BurnedPancakePuzzle.h:124
BurnedPancakePuzzle::IsGoalStored
bool IsGoalStored() const
Definition: BurnedPancakePuzzle.h:83
BurnedPancakePuzzle::Get_Puzzle_Order
static std::vector< unsigned > Get_Puzzle_Order(int64_t order_num, unsigned num_pancakes)
Returns a possible ordering of the operators.
BurnedPancakePuzzle::Factorial
uint64_t Factorial(int val) const
Definition: BurnedPancakePuzzle.h:133
BurnedPancakePuzzle::GCost
double GCost(const BurnedPancakePuzzleState &, const unsigned &) const
Definition: BurnedPancakePuzzle.h:60
BurnedPancakePuzzle::Change_Op_Order
void Change_Op_Order(const std::vector< unsigned > op_order)
Changes the ordering of operators to the new inputted order.
Definition: BurnedPancakePuzzle.cpp:314
BurnedPancakePuzzle::HCost
double HCost(const BurnedPancakePuzzleState &state1, const BurnedPancakePuzzleState &state2) const
Heuristic value between two arbitrary nodes.
Definition: BurnedPancakePuzzle.cpp:192
BurnedPancakePuzzle::GCost
double GCost(const BurnedPancakePuzzleState &, const BurnedPancakePuzzleState &) const
Definition: BurnedPancakePuzzle.h:59
BurnedPancakePuzzle::BurnedPancakePuzzle
BurnedPancakePuzzle(unsigned s)
Definition: BurnedPancakePuzzle.cpp:4
BurnedPancakePuzzle::goal_stored
bool goal_stored
Definition: BurnedPancakePuzzle.h:125
BurnedPancakePuzzle::GetActions
void GetActions(const BurnedPancakePuzzleState &state, std::vector< unsigned > &actions) const
Definition: BurnedPancakePuzzle.cpp:86
BurnedPancakePuzzle::use_memory_free
bool use_memory_free
Definition: BurnedPancakePuzzle.h:126
loc
Definition: MapGenerators.cpp:296
BurnedPancakePuzzle::OpenGLDraw
void OpenGLDraw(const BurnedPancakePuzzleState &, const unsigned &) const
Definition: BurnedPancakePuzzle.h:93
BurnedPancakePuzzle::Get_Op_Order
std::vector< unsigned > Get_Op_Order()
Definition: BurnedPancakePuzzle.h:70
BurnedPancakePuzzle::goal_locations
std::vector< int > goal_locations
Definition: BurnedPancakePuzzle.h:129
BurnedPancakePuzzleState
Definition: BurnedPancakePuzzle.h:9
BurnedPancakePuzzle::Get_Goal
BurnedPancakePuzzleState Get_Goal()
Returns stored goal state if it is stored.
Definition: BurnedPancakePuzzle.h:73
BurnedPancakePuzzle::GetSuccessors
void GetSuccessors(const BurnedPancakePuzzleState &state, std::vector< BurnedPancakePuzzleState > &neighbors) const
Definition: BurnedPancakePuzzle.cpp:73
BurnedPancakePuzzle::StoreGoal
void StoreGoal(BurnedPancakePuzzleState &)
Stores the goal for use by single-state HCost.
Definition: BurnedPancakePuzzle.cpp:300
BurnedPancakePuzzle::GetActionHash
uint64_t GetActionHash(unsigned act) const
Definition: BurnedPancakePuzzle.cpp:295
BurnedPancakePuzzleState::puzzle
std::vector< int > puzzle
Definition: BurnedPancakePuzzle.h:18
BurnedPancakePuzzleState::BurnedPancakePuzzleState
BurnedPancakePuzzleState(unsigned int puzzle_size)
Definition: BurnedPancakePuzzle.h:13
BurnedPancakePuzzle::OpenGLDraw
void OpenGLDraw() const
Definition: BurnedPancakePuzzle.h:91
BurnedPancakePuzzle::~BurnedPancakePuzzle
~BurnedPancakePuzzle()
Definition: BurnedPancakePuzzle.cpp:27
BurnedPancakePuzzle::GoalTest
bool GoalTest(const BurnedPancakePuzzleState &state, const BurnedPancakePuzzleState &goal) const
Definition: BurnedPancakePuzzle.cpp:275
operator<<
static std::ostream & operator<<(std::ostream &out, const BurnedPancakePuzzleState &loc)
Definition: BurnedPancakePuzzle.h:21
operator==
static bool operator==(const BurnedPancakePuzzleState &l1, const BurnedPancakePuzzleState &l2)
Definition: BurnedPancakePuzzle.h:30
BurnedPancakePuzzle::size
unsigned size
Definition: BurnedPancakePuzzle.h:130
BurnedPancakePuzzle::goal
BurnedPancakePuzzleState goal
Definition: BurnedPancakePuzzle.h:128
BurnedPancakePuzzleState::BurnedPancakePuzzleState
BurnedPancakePuzzleState()
Definition: BurnedPancakePuzzle.h:11
BurnedPancakePuzzle::GetAction
unsigned GetAction(const BurnedPancakePuzzleState &s1, const BurnedPancakePuzzleState &s2) const
Definition: BurnedPancakePuzzle.cpp:97
BurnedPancakePuzzle::OpenGLDraw
void OpenGLDraw(const BurnedPancakePuzzleState &, const BurnedPancakePuzzleState &, float) const
Draw the transition at some percentage 0...1 between two states.
Definition: BurnedPancakePuzzle.h:94
BurnedPancakePuzzle::Memory_Free_HCost
double Memory_Free_HCost(const BurnedPancakePuzzleState &state1, const std::vector< int > &goal_locs) const
Definition: BurnedPancakePuzzle.cpp:233
BurnedPancakePuzzle::OpenGLDraw
void OpenGLDraw(const BurnedPancakePuzzleState &) const
Definition: BurnedPancakePuzzle.h:92
BurnedPancakePuzzle::Set_Use_Memory_Free_Heuristic
void Set_Use_Memory_Free_Heuristic(bool to_use)
Definition: BurnedPancakePuzzle.h:120
BurnedPancakePuzzle
Definition: BurnedPancakePuzzle.h:41
BurnedPancakePuzzle::ClearGoal
void ClearGoal()
Clears the goal from memory.
Definition: BurnedPancakePuzzle.h:81
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
SearchEnvironment
Definition: SearchEnvironment.h:30
SearchEnvironment.h