HOG2
PancakePuzzle.h
Go to the documentation of this file.
1 #ifndef PANCAKE_H
2 #define PANCAKE_H
3 
4 #include <stdint.h>
5 #include <iostream>
6 #include "SearchEnvironment.h"
8 #include <sstream>
9 #include "Permutations.h"
10 
11 typedef unsigned PancakePuzzleAction;
12 
13 template <int N>
15 public:
17  Reset();
18  }
19  size_t size() const { return N; }
20  void FinishUnranking() {}
21  void Reset()
22  {
23  for (unsigned int x = 0; x < N; x++)
24  puzzle[x] = x;
25  }
26  int puzzle[N];
27 };
28 
29 template <int N>
30 static std::ostream& operator <<(std::ostream & out, const PancakePuzzleState<N> &loc)
31 {
32  for (unsigned int x = 0; x < loc.size(); x++)
33  out << +loc.puzzle[x] << " ";
34  return out;
35 }
36 
37 template <int N>
38 static bool operator==(const PancakePuzzleState<N> &l1, const PancakePuzzleState<N> &l2)
39 {
40  for (unsigned int x = 0; x < l1.size(); x++)
41  if (l1.puzzle[x] != l2.puzzle[x])
42  return false;
43  return true;
44 }
45 
46 template <int N>
47 static bool operator!=(const PancakePuzzleState<N> &l1, const PancakePuzzleState<N> &l2)
48 {
49  return !(l1==l2);
50 }
51 
52 template <int N>
53 class PancakePuzzle : public PermutationPuzzle::PermutationPuzzleEnvironment<PancakePuzzleState<N>, PancakePuzzleAction> {
54 public:
55  PancakePuzzle(int gap = 0);
56  PancakePuzzle(const std::vector<unsigned> op_order); // used to set action order
57 
59  void GetSuccessors(const PancakePuzzleState<N> &state, std::vector<PancakePuzzleState<N>> &neighbors) const;
60  void GetActions(const PancakePuzzleState<N> &state, std::vector<unsigned> &actions) const;
64  bool InvertAction(PancakePuzzleAction &a) const;
65 
66  virtual uint64_t GetMaxHash() const;
67  virtual uint64_t GetStateHash(const PancakePuzzleState<N> &node);
68  virtual void GetStateFromHash(uint64_t parent, PancakePuzzleState<N> &s) const;
69 
70  double HCost(const PancakePuzzleState<N> &state1, const PancakePuzzleState<N> &state2) const;
71  double DefaultH(const PancakePuzzleState<N> &state1) const;
72  double DefaultH(const PancakePuzzleState<N> &state1, const std::vector<int> &goal_locs) const;
73  double HCost(const PancakePuzzleState<N> &state1) const;
74 
75  double GCost(const PancakePuzzleState<N> &s1, const PancakePuzzleState<N> &s2) const
76  {
77  if (!real) return 1.0;
79  a = GetAction(s1, s2);
80  return GCost(s1, a);
81  }
82  double GCost(const PancakePuzzleState<N> &, const PancakePuzzleAction &a) const
83  {
84  if (!real) return 1.0;
85  else // bigger a is more pancakes - more expensive
86  return 1.0+0.1*(static_cast<double>(a)/static_cast<double>(N));
87  }
88 
89  bool GoalTest(const PancakePuzzleState<N> &state, const PancakePuzzleState<N> &goal) const;
90 
91  bool GoalTest(const PancakePuzzleState<N> &s) const;
92 
93  uint64_t GetActionHash(PancakePuzzleAction act) const;
94  void StoreGoal(PancakePuzzleState<N> &); // stores the locations for the given goal state
95 
96  virtual const std::string GetName();
97  std::vector<PancakePuzzleAction> Get_Op_Order(){return operators;}
98 
101  if (!goal_stored) {
102  fprintf(stderr, "ERROR: Call to Get_Goal when no goal stored\n");
103  exit(1);
104  }
105  return goal;
106  }
107 
108  void ClearGoal(){} // clears the current stored information of the goal
109 
110  bool IsGoalStored()const {return goal_stored;} // returns if a goal is stored or not
111 
115  void Change_Op_Order(const std::vector<PancakePuzzleAction> op_order);
116 
117  // currently not drawing anything
118  void OpenGLDraw() const{}
119  void OpenGLDraw(const PancakePuzzleState<N> &) const;
120  void OpenGLDraw(const PancakePuzzleState<N> &, const PancakePuzzleAction &) const {}
121  void OpenGLDraw(const PancakePuzzleState<N>&, const PancakePuzzleState<N>&, float) const {}
122 
123  void Draw(Graphics::Display &display) const;
124  void Draw(Graphics::Display &display, const PancakePuzzleState<N>&) const;
125  void Draw(Graphics::Display &display, const PancakePuzzleAction&) const;
126  void Draw(Graphics::Display &display,
127  const PancakePuzzleState<N> &from,
128  const PancakePuzzleState<N> &to, float p) const;
129 
132  static void Create_Random_Pancake_Puzzles(std::vector<PancakePuzzleState<N>> &puzzle_vector, unsigned num_puzzles);
133 
134  static int read_in_pancake_puzzles(const char *filename, bool first_counter, unsigned max_puzzles, std::vector<PancakePuzzleState<N>> &puzzle_vector);
135 
136  bool State_Check(const PancakePuzzleState<N> &to_check)
137  {
138  if (to_check.size() != N)
139  return false;
140 
141  return true;
142  }
143 
144  bool Path_Check(PancakePuzzleState<N> start, PancakePuzzleState<N> goal, std::vector<PancakePuzzleAction> &actions);
145 
153  static std::vector<PancakePuzzleAction> Get_Puzzle_Order(int64_t order_num, unsigned num_pancakes);
154 
155  void Set_Use_Memory_Free_Heuristic(bool to_use){use_memory_free = to_use;}
156  void Set_Use_Dual_Lookup( bool to_use ) { use_dual_lookup = to_use; };
157  void SetUseRealValueEdges(bool use) { real = use; }
159 private:
160  bool real;
161  std::vector<PancakePuzzleAction> operators;
162  mutable std::vector<PancakePuzzleAction> actCache;
163  bool goal_stored; // whether a goal is stored or not
166  int gap;
168  std::vector<int> goal_locations;
169  //unsigned size;
170 };
171 
172 
173 namespace std {
174 
175  template <int N>
176  struct hash<PancakePuzzleState<N>>
177  {
178  std::size_t operator()(const PancakePuzzleState<N>& p) const
179  {
180  return PancakePuzzle<N>::Hash(p);
181  }
182  };
183 
184 }
185 
186 
187 template <int N>
189 :gap(gap)
190 {
191  assert(N >= 2);
192 
193  // assign the default operator ordering
194  for (unsigned i = N; i >= 2; i--)
195  operators.push_back(i);
196 
197  goal_stored = false;
198  use_memory_free = true;
199  use_dual_lookup = true;
200  pruneActions = false;
201 }
202 
203 template <int N>
204 PancakePuzzle<N>::PancakePuzzle(const std::vector<PancakePuzzleAction> op_order)
205 :gap(0)
206 {
207  Change_Op_Order(op_order);
208 
209  goal_stored = false;
210  use_memory_free = false;
211  use_dual_lookup = true;
212  pruneActions = false;
213 }
214 
215 template <int N>
217 {
218  ClearGoal();
219 }
220 
221 template <int N>
222 const std::string PancakePuzzle<N>::GetName(){
223  std::string s = "Pancake("+std::to_string(N)+")";
224  return s;
225 // std::stringstream name;
226 // name << std::to_string(N);
227 // name << "Pancake(" ;
228 
229 // if (PDB_distincts.size() > 0)
230 // {
231 // name << ", PDBS:";
232 // for (unsigned i = 0; i < PDB_distincts.size(); i++)
233 // {
234 // name << " <";
235 // for (unsigned j = 0; j < PDB_distincts[i].size() - 1; j++) {
236 // name << PDB_distincts[i][j];
237 // name << ", ";
238 // }
239 // name << PDB_distincts[i].back();
240 // name << ">";
241 // }
242 // if (use_memory_free)
243 // name << ", Memory-Free Heuristic";
244 // }
245 // if (use_memory_free)
246 // {
247 // name << ", Memory-Free Heuristic";
248 // }
249 // else {
250 // name << ",No Heuristic";
251 // }
252 
253 // name << ", Op Order: ";
254 // for (unsigned op_num = 0; op_num < operators.size() - 1; op_num++){
255 // name << operators[op_num];
256 // name << ", ";
257 // }
258 // name << operators.back();
259 // return name.str();
260 }
261 
262 template <int N>
264  std::vector<PancakePuzzleState<N>> &children) const
265 {
266  children.resize(0);
267  if (!pruneActions)
268  {
269  // all operators are applicable in all states
270  for (unsigned i = 0; i < operators.size(); i++)
271  {
272  children.push_back(parent); // adds a copy of the state to the stack
273  ApplyAction(children.back(), operators[i]);
274  }
275  }
276  else {
277  GetActions(parent, actCache);
278  for (unsigned i = 0; i < actCache.size(); i++)
279  {
280  children.push_back(parent); // adds a copy of the state to the stack
281  ApplyAction(children.back(), actCache[i]);
282  }
283  }
284 }
285 
286 template <int N>
287 void PancakePuzzle<N>::GetActions(const PancakePuzzleState<N> &s, std::vector<PancakePuzzleAction> &actions) const
288 {
289  actions.resize(0);
290  if (!pruneActions)
291  {
292  // all operators are applicable in all states
293  for (unsigned i = 0; i < operators.size(); i++)
294  {
295  actions.push_back(operators[i]);
296  }
297  }
298  else {
299  bool skip = true;
300  for (unsigned i = N; i >= 2; i--)
301  {
302  if (s.puzzle[i-1] == i-1 && skip)
303  continue;
304  skip = false;
305  actions.push_back(i);
306  }
307  }
308 }
309 
310 template <int N>
312 {
313  PancakePuzzleAction current_action;
314  bool are_equal = false;
315 
316  assert(child.size() == N);
317  assert(parent.size() == N);
318  PancakePuzzleState<N> parentCopy = parent;
319  for (unsigned i = 0; i < operators.size(); i++)
320  {
321  current_action = operators[i];
322  ApplyAction(parentCopy, current_action);
323  if (parentCopy == child)
324  are_equal = true;
325  InvertAction(current_action);
326  ApplyAction(parentCopy, current_action);
327 
328  if (are_equal)
329  return operators[i];
330  }
331  fprintf(stderr, "ERROR: GetAction called with non-adjacent states\n");
332  exit(1);
333  return 0;
334 }
335 
336 template <int N>
338 {
339  assert(s.size() == N);
340  assert(action > 1 && action <= N);
341 
342  int upper = 0;
343  int lower = action - 1;
344  int temp;
345  // performs pancake flipping
346  while(upper < lower)
347  {
348  temp = s.puzzle[upper];
349  s.puzzle[upper] = s.puzzle[lower];
350  s.puzzle[lower] = temp;
351  upper++;
352  lower--;
353  }
354 }
355 
356 template <int N>
358 {
359  // ever action is self-inverse
360  assert(a > 1 && a <= N);
361  return true;
362 }
363 
364 template <int N>
366 {
367  if (!goal_stored)
368  {
369  fprintf(stderr, "ERROR: HCost called with a single state and goal is not stored.\n");
370  exit(1);
371  }
372 // if (state.size() != N)
373 // {
374 // fprintf(stderr, "ERROR: HCost called with a single state with wrong size.\n");
375 // exit(1);
376 // }
377  double h_cost = 0;
378 
379  // // use PDB heuristic
380  // if (PDB.size() > 0) {
381  // if (PDB.size() != PDB_distincts.size()) {
382  // fprintf(stderr, "ERROR: HCost called with a single state, no use of memory free heuristic, and invalid setup of pattern databases.\n");
383  // exit(1);
384  // }
385  // h_cost = std::max(PDB_Lookup(state), h_cost);
386  // }
387 
388  // use memory-free heuristic
389  if (use_memory_free)
390  {
391  h_cost = std::max(DefaultH(state, goal_locations), h_cost);
392  }
393  // // if no heuristic
394  // else if (PDB.size()==0) {
395  // if (goal == state)
396  // return 0;
397  // else
398  // return 1;
399  // }
400 
401  return h_cost;
402 }
403 
404 template <int N>
405 double PancakePuzzle<N>::HCost(const PancakePuzzleState<N> &state, const PancakePuzzleState<N> &goal_state) const
406 {
407 // if (state.size() != N)
408 // {
409 // fprintf(stderr, "ERROR: HCost called with state with wrong size.\n");
410 // exit(1);
411 // }
412 // if (goal_state.size() != N)
413 // {
414 // fprintf(stderr, "ERROR: HCost called with goal with wrong size.\n");
415 // exit(1);
416 // }
417 
418  // if ( use_dual_lookup ) {
419  // // Note: This lookup only works if all PDB's have the same goal
420  // // (or alternatively there is only one PDB)
421  //
422  // // sanity check
423  // assert( IsGoalStored() );
424  //
425  // // beta is a remapping of the elements to the goal
426  // std::vector<int> beta;
427  // beta.resize( size );
428  // for ( unsigned int i = 0; i < size; i++ )
429  // beta[goal_state.puzzle[i]] = goal.puzzle[i];
430  //
431  // PancakePuzzleState<N> t = state;
432  // // remap t with respect to beta
433  // for ( unsigned int i = 0; i < size; i++ )
434  // t.puzzle[i] = beta[state.puzzle[i]];
435  //
436  // return PDB_Lookup( t );
437  // }
438 
439  if (use_memory_free)
440  {
441  //assert(!"This code allocates a vector; re-write to be more efficient");
442  static std::vector<int> goal_locs(N);
443  for (unsigned i = 0; i < N; i++)
444  {
445  goal_locs[goal_state.puzzle[i]] = i;
446  }
447  return DefaultH(state, goal_locs);
448  }
449 
450  if (state == goal_state)
451  return 0.0;
452  return 1.0;
453 }
454 
455 template <int N>
457 {
458  return DefaultH(state, goal_locations);
459 }
460 
461 template <int N>
462 double PancakePuzzle<N>::DefaultH(const PancakePuzzleState<N> &state, const std::vector<int> &goal_locs) const
463 {
464 // if (state.size() != N)
465 // {
466 // fprintf(stderr, "ERROR: HCost called with state with wrong size.\n");
467 // exit(1);
468 // }
469 
470  double h_count = 0.0;
471  unsigned i = 0;
472  for (; i < N - 1; i++)
473  {
474  if ((goal_locs[state.puzzle[i]] < gap) || (goal_locs[state.puzzle[i+1]] < gap))
475  continue;
476  int diff = goal_locs[state.puzzle[i]] - goal_locs[state.puzzle[i+1]];
477  if (diff > 1 || diff < -1)
478  h_count++;
479  }
480  if ((unsigned) goal_locs[state.puzzle[i]]!= N -1)
481  h_count++;
482 
483  return h_count;
484 }
485 
486 template <int N>
488 {
489  return (state == theGoal);
490 }
491 
492 template <int N>
494 {
495  if (!goal_stored)
496  {
497  fprintf(stderr, "ERROR: GoalTest called with a single state and goal is not stored.\n");
498  exit(1);
499  }
500  if (s.size() != N)
501  {
502  fprintf(stderr, "ERROR: GoalTest called with a single state with wrong size.\n");
503  exit(1);
504  }
505  return (s == goal);
506 }
507 
508 template <int N>
510 {
511  return (uint64_t) act;
512 }
513 
514 template <int N>
516 {
517  //assert(g.puzzle.size() == size);
518 
519  goal = g;
520  goal_stored = true;
521 
522  goal_locations.resize(N);
523  for (unsigned i = 0; i < N; i++)
524  {
525  goal_locations[goal.puzzle[i]] = i;
526  }
527 }
528 
529 template <int N>
530 void PancakePuzzle<N>::Change_Op_Order(const std::vector<PancakePuzzleAction> op_order)
531 {
532  operators.clear();
533 
534  if (op_order.size() != N - 1)
535  {
536  fprintf(stderr, "ERROR: Not enough operators in operator sequence for construction of PancakePuzzle\n");
537  exit(1);
538  }
539 
540  bool all_ops[op_order.size()];
541 
542  for (unsigned i = 0; i < N; i++)
543  {
544  all_ops[i] = false;
545  }
546 
547  for (unsigned i = 0; i < op_order.size(); i++)
548  {
549  if (op_order[i] < 2 || op_order[i] > N)
550  {
551  fprintf(stderr, "ERROR: Invalid operator included in construction of PancakePuzzle\n");
552  exit(1);
553  }
554  all_ops[op_order[i] - 2] = true;
555  }
556 
557  for (unsigned i = 0; i < op_order.size(); i++)
558  {
559  if (!all_ops[i])
560  {
561  fprintf(stderr, "ERROR: Missing operator %u in construction of PancakePuzzle\n", i+2);
562  exit(1);
563  }
564  }
565  // assign the default operator ordering
566  for (unsigned i = 0; i < op_order.size(); i++)
567  operators.push_back(op_order[i]);
568 }
569 
570 
571 template <int N>
572 void PancakePuzzle<N>::Create_Random_Pancake_Puzzles(std::vector<PancakePuzzleState<N>> &puzzle_vector, unsigned num_puzzles)
573 {
574 
575  std::map<uint64_t, uint64_t> puzzle_map; // used to ensure uniqueness
576 
577  PancakePuzzle my_puzz(N);
578 
579  unsigned count = 0;
580 
581  std::vector<int> perm;
582  PancakePuzzleState<N> potential_puzz(N);
583  while (count < num_puzzles)
584  {
586 
587  // construct puzzle
588  for (unsigned i = 0; i < N; i++)
589  {
590  potential_puzz.puzzle[i] = perm[i];
591  }
592 
593  uint64_t next_hash = my_puzz.GetStateHash(potential_puzz);
594 
595 
596  // make sure is not a duplicate
597  if (puzzle_map.find(next_hash) != puzzle_map.end())
598  {
599  continue;
600  }
601 
602  puzzle_map[next_hash] = next_hash;
603  puzzle_vector.push_back(potential_puzz);
604  count++;
605  }
606 
607 }
608 
609 template <int N>
610 int PancakePuzzle<N>::read_in_pancake_puzzles(const char *filename, bool puzz_num_start, unsigned max_puzzles, std::vector<PancakePuzzleState<N>> &puzzles)
611 {
612 
613  std::vector<std::vector<int> > permutations;
614  PermutationPuzzle::PermutationPuzzleEnvironment<PancakePuzzleState<N>, PancakePuzzleAction>::Read_In_Permutations(filename, N, max_puzzles, permutations, puzz_num_start);
615 
616  // convert permutations into PancakePuzzleState<N>s
617  for (unsigned i = 0; i < permutations.size(); i++)
618  {
619  PancakePuzzleState<N> new_state(N);
620 
621  for (unsigned j = 0; j < N; j++)
622  {
623  new_state.puzzle[j] = permutations[i][j];
624  }
625  puzzles.push_back(new_state);
626  }
627  return 0;
628 }
629 
630 template <int N>
631 bool PancakePuzzle<N>::Path_Check(PancakePuzzleState<N> start, PancakePuzzleState<N> theGoal, std::vector<PancakePuzzleAction> &actions)
632 {
633  if (start.size() != N || theGoal.size() != N)
634  return false;
635 
636  for (unsigned i = 0; i < actions.size(); i++)
637  {
638  if (actions[i] < 2 || actions[i] > N)
639  return false;
640  ApplyAction(start, actions[i]);
641  }
642 
643  if (start == theGoal)
644  return true;
645 
646  return false;
647 }
648 
649 template <int N>
650 std::vector<unsigned> PancakePuzzle<N>::Get_Puzzle_Order(int64_t order_num, unsigned num_pancakes)
651 {
652  std::vector<unsigned> ops;
653  assert(order_num >= 0);
654  assert(num_pancakes > 0);
655 
656 
657  std::vector<int64_t> op_nums(num_pancakes -1);
658 
659  int64_t num_left = 1;
660  for (int64_t x = num_pancakes - 2; x >= 0; x--)
661  {
662  op_nums[x] = order_num % num_left;
663  order_num /= num_left;
664  num_left++;
665 
666  for (int64_t y = x+1; y < num_pancakes-1; y++)
667  {
668  if (op_nums[y] >= op_nums[x])
669  {
670  op_nums[y]++;
671  }
672  }
673  }
674 
675  std::vector<bool> actions;
676 
677  for (unsigned i = 0; i < num_pancakes - 1; i++)
678  {
679  actions.push_back(false);
680  }
681 
682  for (unsigned i = 0; i < num_pancakes - 1; i++)
683  {
684  ops.push_back(op_nums[i] + 2);
685  actions[op_nums[i]] = true;
686  }
687 
688  for (unsigned i = 0; i < num_pancakes - 1; i++)
689  {
690  assert(actions[i]);
691  }
692  return ops;
693 }
694 
695 template <int N>
697 {
698  Permutations<N> c;
699  return c.MaxRank();
700 }
701 
702 template <int N>
704 {
705  Permutations<N> c;
706  return c.Rank(node.puzzle);
707 }
708 
709 template <int N>
711 {
712  Permutations<N> c;
713  return c.Unrank(parent, s.puzzle);
714 }
715 
716 template <int N>
718 {
719  double count = pps.size();
720  double widthUnit = 1.5/count;
721 
722  for (size_t y = 0; y < pps.size(); y++)
723  {
724  for (int x = 0; x <= pps.puzzle[y]; x++)
725  {
726  glColor3f(pps.puzzle[y]/count, 0, 1-pps.puzzle[y]/count);
727  DrawBox(-pps.puzzle[y]*widthUnit/4+x*widthUnit/2,
728  -1+widthUnit*y+widthUnit/2, 0,
729  widthUnit/2);
730  }
731  }
732 }
733 
734 template <int N>
736 {
737 // if (p.y > -0.5 && p.y < 0.5)
738 // {
739 // return N-(1-p.y-0.5)*N+1;
740 // }
741 // return 0;
742 //
743  for (int x = 0; x < N; x++)
744  {
745  float t = 0.6*s.puzzle[x]/(N-1.0f)+0.1;
746  float v = 1.0f/(N-1.0f);
747  Graphics::rect r(-t, -0.5f+(x)*v, t, -0.5f+(x+1)*v);
748  if (PointInRect(p, r))
749  {
750  return x+1;
751  }
752 // else {
753 // std::cout << "[" << x << "]" << p << " missed [" << r << "]\n";
754 // }
755  }
756  return 0;
757 }
758 
759 template <int N>
761 {
762  // no baseline drawing
763  float v = 1.0f/(N-1.0f);
764  display.FillRect(Graphics::rect(-1, -0.5f+(N+1)*v, 1, -0.5f+N*v), Colors::darkgray);
765 }
766 
767 template <int N>
769 {
770  float v = 1.0f/(N-1.0f);
771  for (int x = 0; x < N; x++)
772  {
773  float t = 0.6*s.puzzle[x]/(N-1.0f)+0.1;
774  display.FillRect(Graphics::rect(-t, -0.5f+(x+1)*v, t, -0.5f+x*v), rgbColor(1, t, 0));
775  display.FrameRect(Graphics::rect(-t, -0.5f+(x+1)*v, t, -0.5f+x*v), Colors::black, v*0.1f);
776  }
777 }
778 
779 template <int N>
781 {
782  float v = 1.0f/(N-1.0f);
783  Graphics::point p1(0, -0.5+v/2);
784  Graphics::point p2(0, -0.5f+(a)*v-v/2);
785 // display.FillRect(Graphics::rect(-t, -0.5f+(x+1)*v, t, -0.5f+x*v), rgbColor(1, t, 0));
786 // display.FrameRect(Graphics::rect(-t, -0.5f+(x+1)*v, t, -0.5f+x*v), Colors::black, v*0.1f);
787  display.DrawArrow(p2, p1, v*0.2, Colors::blue);
788  Graphics::point p3(-0.7, -0.5f+(a)*v);
789  Graphics::point p4(0.7, -0.5f+(a)*v);
790  Graphics::point p5(0.7+0.3, -0.5f+(a)*v-0.3);
791  display.DrawLine(p3, p4, v*0.3, Colors::black);
792  display.DrawLine(p4, p5, v*0.3, Colors::black);
793 }
794 
795 template <int N>
797  const PancakePuzzleState<N> &from,
798  const PancakePuzzleState<N> &to, float p) const
799 {
800  Graphics::rect fromRect[N];
801  Graphics::rect toRect[N];
802 
803  float v = 1.0f/(N-1.0f);
804  for (int x = 0; x < N; x++)
805  {
806  float t = 0.6*from.puzzle[x]/(N-1.0f)+0.1;
807  Graphics::rect r(-t, -0.5f+(x)*v, t, -0.5f+(x+1)*v);
808  fromRect[from.puzzle[x]] = r;
809 
810  t = 0.6*to.puzzle[x]/(N-1.0f)+0.1;
811  v = 1.0f/(N-1.0f);
812  r = Graphics::rect(-t, -0.5f+(x)*v, t, -0.5f+(x+1)*v);
813  toRect[to.puzzle[x]] = r;
814  }
815  for (int x = 0; x < N; x++)
816  {
817  float t = 0.6*x/(N-1.0f)+0.1;
818  fromRect[x].lerp(toRect[x], p);
819  display.FillRect(fromRect[x], rgbColor(1, t, 0));
820  display.FrameRect(fromRect[x], Colors::black, v*0.1f);
821  }
822 }
823 
824 
825 #endif
Graphics::Display::DrawLine
void DrawLine(point start, point end, float lineWidth, rgbColor c)
Definition: Graphics.cpp:184
Graphics::point
Definition: Graphics.h:32
PancakePuzzleState::PancakePuzzleState
PancakePuzzleState()
Definition: PancakePuzzle.h:16
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
PancakePuzzle::~PancakePuzzle
~PancakePuzzle()
Definition: PancakePuzzle.h:216
PancakePuzzle::State_Check
bool State_Check(const PancakePuzzleState< N > &to_check)
Checks that the given state is a valid state for this domain.
Definition: PancakePuzzle.h:136
PancakePuzzle::Create_Random_Pancake_Puzzles
static void Create_Random_Pancake_Puzzles(std::vector< PancakePuzzleState< N >> &puzzle_vector, unsigned num_puzzles)
Definition: PancakePuzzle.h:572
PancakePuzzle::Get_Op_Order
std::vector< PancakePuzzleAction > Get_Op_Order()
Definition: PancakePuzzle.h:97
PancakePuzzle::HCost
double HCost(const PancakePuzzleState< N > &state1, const PancakePuzzleState< N > &state2) const
Definition: PancakePuzzle.h:405
PancakePuzzle::GetAction
PancakePuzzleAction GetAction(const PancakePuzzleState< N > &s1, const PancakePuzzleState< N > &s2) const
Definition: PancakePuzzle.h:311
PancakePuzzle::GetStateHash
virtual uint64_t GetStateHash(const PancakePuzzleState< N > &node)
Definition: PancakePuzzle.h:703
PancakePuzzle::GetName
virtual const std::string GetName()
Definition: PancakePuzzle.h:222
PancakePuzzleState::puzzle
int puzzle[N]
Definition: PancakePuzzle.h:26
std::hash< PancakePuzzleState< N > >::operator()
std::size_t operator()(const PancakePuzzleState< N > &p) const
Definition: PancakePuzzle.h:178
PancakePuzzle::Path_Check
bool Path_Check(PancakePuzzleState< N > start, PancakePuzzleState< N > goal, std::vector< PancakePuzzleAction > &actions)
Definition: PancakePuzzle.h:631
Graphics::rect
Definition: Graphics.h:94
operator<<
static std::ostream & operator<<(std::ostream &out, const PancakePuzzleState< N > &loc)
Definition: PancakePuzzle.h:30
PancakePuzzle::StoreGoal
void StoreGoal(PancakePuzzleState< N > &)
Definition: PancakePuzzle.h:515
Permutations::MaxRank
uint64_t MaxRank() const
Definition: Permutations.h:18
PancakePuzzle::use_dual_lookup
bool use_dual_lookup
Definition: PancakePuzzle.h:165
PancakePuzzle::GetActions
void GetActions(const PancakePuzzleState< N > &state, std::vector< unsigned > &actions) const
Definition: PancakePuzzle.h:287
PancakePuzzle::GetSuccessors
void GetSuccessors(const PancakePuzzleState< N > &state, std::vector< PancakePuzzleState< N >> &neighbors) const
Definition: PancakePuzzle.h:263
PancakePuzzle::use_memory_free
bool use_memory_free
Definition: PancakePuzzle.h:164
PancakePuzzleState
Definition: PancakePuzzle.h:14
Graphics::PointInRect
bool PointInRect(const point &p, const rect &r)
Definition: Graphics.cpp:17
PancakePuzzleState::size
size_t size() const
Definition: PancakePuzzle.h:19
PancakePuzzle::PancakePuzzle
PancakePuzzle(int gap=0)
Definition: PancakePuzzle.h:188
PancakePuzzle::SetUseRealValueEdges
void SetUseRealValueEdges(bool use)
Definition: PancakePuzzle.h:157
loc
Definition: MapGenerators.cpp:296
PancakePuzzle::GCost
double GCost(const PancakePuzzleState< N > &s1, const PancakePuzzleState< N > &s2) const
Definition: PancakePuzzle.h:75
PermutationPuzzleEnvironment.h
PancakePuzzle::OpenGLDraw
void OpenGLDraw() const
Definition: PancakePuzzle.h:118
PancakePuzzle::read_in_pancake_puzzles
static int read_in_pancake_puzzles(const char *filename, bool first_counter, unsigned max_puzzles, std::vector< PancakePuzzleState< N >> &puzzle_vector)
Definition: PancakePuzzle.h:610
PancakePuzzle::InvertAction
bool InvertAction(PancakePuzzleAction &a) const
Definition: PancakePuzzle.h:357
Colors::black
const rgbColor black
Definition: Colors.h:119
PancakePuzzle::ClearGoal
void ClearGoal()
Definition: PancakePuzzle.h:108
point3d
#define point3d
Definition: GLUtil.h:123
Graphics::Display
Definition: Graphics.h:146
PancakePuzzle::Get_Goal
PancakePuzzleState< N > Get_Goal()
Returns stored goal state if it is stored.
Definition: PancakePuzzle.h:100
Colors::darkgray
const rgbColor darkgray
Definition: Colors.h:123
PancakePuzzleState::Reset
void Reset()
Definition: PancakePuzzle.h:21
PancakePuzzle::pruneActions
bool pruneActions
Definition: PancakePuzzle.h:158
PancakePuzzle::Set_Use_Dual_Lookup
void Set_Use_Dual_Lookup(bool to_use)
Definition: PancakePuzzle.h:156
Permutations::Unrank
void Unrank(uint64_t hash, int *items) const
Definition: Permutations.h:41
PancakePuzzle
Definition: PancakePuzzle.h:53
PancakePuzzleAction
unsigned PancakePuzzleAction
Definition: PancakePuzzle.h:11
Permutations::Rank
uint64_t Rank(const int *items) const
Definition: Permutations.h:19
PancakePuzzle::Change_Op_Order
void Change_Op_Order(const std::vector< PancakePuzzleAction > op_order)
Changes the ordering of operators to the new inputted order.
Definition: PancakePuzzle.h:530
PermutationPuzzle::PermutationPuzzleEnvironment
Note, assumes that state has a public vector<int> called puzzle in which the permutation is held.
Definition: PermutationPuzzleEnvironment.h:26
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
PancakePuzzle::actCache
std::vector< PancakePuzzleAction > actCache
Definition: PancakePuzzle.h:162
PancakePuzzle::goal
PancakePuzzleState< N > goal
Definition: PancakePuzzle.h:167
Permutations
Definition: Permutations.h:16
Graphics::Display::DrawArrow
void DrawArrow(point start, point end, float lineWidth, rgbColor c)
Definition: Graphics.cpp:193
PancakePuzzleState::FinishUnranking
void FinishUnranking()
Definition: PancakePuzzle.h:20
DrawBox
void DrawBox(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
Definition: GLUtil.cpp:285
PancakePuzzle::DefaultH
double DefaultH(const PancakePuzzleState< N > &state1) const
Definition: PancakePuzzle.h:456
PancakePuzzle::GoalTest
bool GoalTest(const PancakePuzzleState< N > &state, const PancakePuzzleState< N > &goal) const
Definition: PancakePuzzle.h:487
PancakePuzzle::operators
std::vector< PancakePuzzleAction > operators
Definition: PancakePuzzle.h:161
operator==
static bool operator==(const PancakePuzzleState< N > &l1, const PancakePuzzleState< N > &l2)
Definition: PancakePuzzle.h:38
std
Definition: CanonicalGraphEnvironment.h:26
Graphics::Display::FrameRect
void FrameRect(rect r, rgbColor c, float lineWidth)
Definition: Graphics.cpp:73
operator!=
static bool operator!=(const PancakePuzzleState< N > &l1, const PancakePuzzleState< N > &l2)
Definition: PancakePuzzle.h:47
PancakePuzzle::GetActionHash
uint64_t GetActionHash(PancakePuzzleAction act) const
Definition: PancakePuzzle.h:509
PermutationPuzzle::PermutationPuzzleEnvironment< PancakePuzzleState< N >, PancakePuzzleAction >::Hash
static uint64_t Hash(const PancakePuzzleState< N > &s)
Definition: PermutationPuzzleEnvironment.h:68
PancakePuzzle::OpenGLDraw
void OpenGLDraw(const PancakePuzzleState< N > &, const PancakePuzzleState< N > &, float) const
Definition: PancakePuzzle.h:121
PancakePuzzle::GetMaxHash
virtual uint64_t GetMaxHash() const
Definition: PancakePuzzle.h:696
Colors::blue
const rgbColor blue
Definition: Colors.h:142
PancakePuzzle::ApplyAction
void ApplyAction(PancakePuzzleState< N > &s, PancakePuzzleAction a) const
Definition: PancakePuzzle.h:337
PancakePuzzle::goal_locations
std::vector< int > goal_locations
Definition: PancakePuzzle.h:168
PancakePuzzle::gap
int gap
Definition: PancakePuzzle.h:166
PancakePuzzle::goal_stored
bool goal_stored
Definition: PancakePuzzle.h:163
PancakePuzzle::real
bool real
Definition: PancakePuzzle.h:160
PancakePuzzle::GetStateFromHash
virtual void GetStateFromHash(uint64_t parent, PancakePuzzleState< N > &s) const
Definition: PancakePuzzle.h:710
Graphics::rect::lerp
void lerp(const rect &val, float percentage)
Definition: Graphics.h:121
Graphics::Display::FillRect
void FillRect(rect r, rgbColor c)
Definition: Graphics.cpp:101
PancakePuzzle::Get_Puzzle_Order
static std::vector< PancakePuzzleAction > Get_Puzzle_Order(int64_t order_num, unsigned num_pancakes)
Returns a possible ordering of the operators.
Definition: PancakePuzzle.h:650
PancakePuzzle::IsGoalStored
bool IsGoalStored() const
Definition: PancakePuzzle.h:110
PancakePuzzle::OpenGLDraw
void OpenGLDraw(const PancakePuzzleState< N > &, const PancakePuzzleAction &) const
Definition: PancakePuzzle.h:120
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
PancakePuzzle::GCost
double GCost(const PancakePuzzleState< N > &, const PancakePuzzleAction &a) const
Definition: PancakePuzzle.h:82
Permutations.h
PancakePuzzle::Set_Use_Memory_Free_Heuristic
void Set_Use_Memory_Free_Heuristic(bool to_use)
Definition: PancakePuzzle.h:155
PancakePuzzle::Draw
void Draw(Graphics::Display &display) const
Definition: PancakePuzzle.h:760