19 size_t size()
const {
return N; }
23 for (
unsigned int x = 0; x < N; x++)
32 for (
unsigned int x = 0; x <
loc.size(); x++)
33 out << +
loc.puzzle[x] <<
" ";
40 for (
unsigned int x = 0; x < l1.
size(); x++)
77 if (!
real)
return 1.0;
84 if (!
real)
return 1.0;
86 return 1.0+0.1*(
static_cast<double>(a)/
static_cast<double>(N));
96 virtual const std::string
GetName();
102 fprintf(stderr,
"ERROR: Call to Get_Goal when no goal stored\n");
138 if (to_check.
size() != N)
153 static std::vector<PancakePuzzleAction>
Get_Puzzle_Order(int64_t order_num,
unsigned num_pancakes);
194 for (
unsigned i = N; i >= 2; i--)
223 std::string s =
"Pancake("+std::to_string(N)+
")";
270 for (
unsigned i = 0; i < operators.size(); i++)
272 children.push_back(parent);
273 ApplyAction(children.back(), operators[i]);
277 GetActions(parent, actCache);
278 for (
unsigned i = 0; i < actCache.size(); i++)
280 children.push_back(parent);
281 ApplyAction(children.back(), actCache[i]);
293 for (
unsigned i = 0; i < operators.size(); i++)
295 actions.push_back(operators[i]);
300 for (
unsigned i = N; i >= 2; i--)
302 if (s.
puzzle[i-1] == i-1 && skip)
305 actions.push_back(i);
314 bool are_equal =
false;
316 assert(child.
size() == N);
317 assert(parent.
size() == N);
319 for (
unsigned i = 0; i < operators.size(); i++)
321 current_action = operators[i];
322 ApplyAction(parentCopy, current_action);
323 if (parentCopy == child)
325 InvertAction(current_action);
326 ApplyAction(parentCopy, current_action);
331 fprintf(stderr,
"ERROR: GetAction called with non-adjacent states\n");
339 assert(s.
size() == N);
340 assert(action > 1 && action <= N);
343 int lower = action - 1;
360 assert(a > 1 && a <= N);
369 fprintf(stderr,
"ERROR: HCost called with a single state and goal is not stored.\n");
391 h_cost =
std::max(DefaultH(state, goal_locations), h_cost);
442 static std::vector<int> goal_locs(N);
443 for (
unsigned i = 0; i < N; i++)
445 goal_locs[goal_state.
puzzle[i]] = i;
447 return DefaultH(state, goal_locs);
450 if (state == goal_state)
458 return DefaultH(state, goal_locations);
470 double h_count = 0.0;
472 for (; i < N - 1; i++)
474 if ((goal_locs[state.
puzzle[i]] < gap) || (goal_locs[state.
puzzle[i+1]] < gap))
476 int diff = goal_locs[state.
puzzle[i]] - goal_locs[state.
puzzle[i+1]];
477 if (diff > 1 || diff < -1)
480 if ((
unsigned) goal_locs[state.
puzzle[i]]!= N -1)
489 return (state == theGoal);
497 fprintf(stderr,
"ERROR: GoalTest called with a single state and goal is not stored.\n");
502 fprintf(stderr,
"ERROR: GoalTest called with a single state with wrong size.\n");
511 return (uint64_t) act;
522 goal_locations.resize(N);
523 for (
unsigned i = 0; i < N; i++)
525 goal_locations[goal.puzzle[i]] = i;
534 if (op_order.size() != N - 1)
536 fprintf(stderr,
"ERROR: Not enough operators in operator sequence for construction of PancakePuzzle\n");
540 bool all_ops[op_order.size()];
542 for (
unsigned i = 0; i < N; i++)
547 for (
unsigned i = 0; i < op_order.size(); i++)
549 if (op_order[i] < 2 || op_order[i] > N)
551 fprintf(stderr,
"ERROR: Invalid operator included in construction of PancakePuzzle\n");
554 all_ops[op_order[i] - 2] =
true;
557 for (
unsigned i = 0; i < op_order.size(); i++)
561 fprintf(stderr,
"ERROR: Missing operator %u in construction of PancakePuzzle\n", i+2);
566 for (
unsigned i = 0; i < op_order.size(); i++)
567 operators.push_back(op_order[i]);
575 std::map<uint64_t, uint64_t> puzzle_map;
581 std::vector<int> perm;
583 while (count < num_puzzles)
588 for (
unsigned i = 0; i < N; i++)
590 potential_puzz.
puzzle[i] = perm[i];
593 uint64_t next_hash = my_puzz.
GetStateHash(potential_puzz);
597 if (puzzle_map.find(next_hash) != puzzle_map.end())
602 puzzle_map[next_hash] = next_hash;
603 puzzle_vector.push_back(potential_puzz);
613 std::vector<std::vector<int> > permutations;
617 for (
unsigned i = 0; i < permutations.size(); i++)
621 for (
unsigned j = 0; j < N; j++)
623 new_state.
puzzle[j] = permutations[i][j];
625 puzzles.push_back(new_state);
633 if (start.
size() != N || theGoal.
size() != N)
636 for (
unsigned i = 0; i < actions.size(); i++)
638 if (actions[i] < 2 || actions[i] > N)
640 ApplyAction(start, actions[i]);
643 if (start == theGoal)
652 std::vector<unsigned> ops;
653 assert(order_num >= 0);
654 assert(num_pancakes > 0);
657 std::vector<int64_t> op_nums(num_pancakes -1);
659 int64_t num_left = 1;
660 for (int64_t x = num_pancakes - 2; x >= 0; x--)
662 op_nums[x] = order_num % num_left;
663 order_num /= num_left;
666 for (int64_t y = x+1; y < num_pancakes-1; y++)
668 if (op_nums[y] >= op_nums[x])
675 std::vector<bool> actions;
677 for (
unsigned i = 0; i < num_pancakes - 1; i++)
679 actions.push_back(
false);
682 for (
unsigned i = 0; i < num_pancakes - 1; i++)
684 ops.push_back(op_nums[i] + 2);
685 actions[op_nums[i]] =
true;
688 for (
unsigned i = 0; i < num_pancakes - 1; i++)
719 double count = pps.
size();
720 double widthUnit = 1.5/count;
722 for (
size_t y = 0; y < pps.
size(); y++)
724 for (
int x = 0; x <= pps.
puzzle[y]; x++)
726 glColor3f(pps.
puzzle[y]/count, 0, 1-pps.
puzzle[y]/count);
728 -1+widthUnit*y+widthUnit/2, 0,
743 for (
int x = 0; x < N; x++)
745 float t = 0.6*s.
puzzle[x]/(N-1.0f)+0.1;
746 float v = 1.0f/(N-1.0f);
763 float v = 1.0f/(N-1.0f);
770 float v = 1.0f/(N-1.0f);
771 for (
int x = 0; x < N; x++)
773 float t = 0.6*s.
puzzle[x]/(N-1.0f)+0.1;
782 float v = 1.0f/(N-1.0f);
803 float v = 1.0f/(N-1.0f);
804 for (
int x = 0; x < N; x++)
806 float t = 0.6*from.
puzzle[x]/(N-1.0f)+0.1;
808 fromRect[from.
puzzle[x]] = r;
810 t = 0.6*to.
puzzle[x]/(N-1.0f)+0.1;
815 for (
int x = 0; x < N; x++)
817 float t = 0.6*x/(N-1.0f)+0.1;
818 fromRect[x].
lerp(toRect[x], p);