int puzzle(int N) { if (N == 1) return 1; if (N % 2 == 0) return puzzle(N/2); else return puzzle(3*N+1); }
int puzzle(int n) { printf("puzzle(%d)\n\t", n); count ++; if (n == 1) return 1; if (n % 2 == 0) return puzzle(n / 2); else return puzzle(3 * n + 1); }
int main (void) { won_mask = won = 0; srand (time (NULL)); puts ("Buttons\n-------\n" "Try to make the field of buttons look like the goal. You can press any\n" "button that is up by typing the number on the button, but when you press\n" "a button other buttons around it will change. Each button always changes\n" "the same buttons around it.\n" "Type zero ('0') as your move to quit\n" "There are several goals to accomplish. Try to get them all!\n\n" "Press ENTER to start..."); getche (); do puzzle (); while (want_more ()); puts ("Good bye!"); puts ("\n\n\n\n\n\n\n\n\tCymon's Games\n\n\thttp://WWW.CYMONSGAMES.COM\n\n\n" " This game and its code is a 2008 Cymon's Games game.\n" " If you have enjoyed this game or would just like to have a new game\n" " each week please visit:\n" " http://WWW.CYMONSGAMES.COM for C/C++ programming resources and programs\n" " for everyone, beginners and advanced users alike.\n\n"); printf ("\n\tPress ENTER to continue...\n\n\n\n\n\n"); getchar (); getchar(); exit (0); }
const PuzzleLayout* Test::runDifficultTest() { unsigned int arr_pieces[6][25] = { { 0,1,0,1,1, 0,1,1,1,1, 1,1,1,1,0, 0,1,1,1,0, 1,1,0,1,0 },{ 0,1,1,0,1, 0,1,1,1,1, 1,1,1,1,1, 1,1,1,1,0, 1,0,1,0,0 },{ 0,1,0,1,0, 0,1,1,1,0, 0,1,1,1,1, 1,1,1,1,0, 1,1,0,1,1 },{ 0,0,1,0,0, 0,1,1,1,1, 1,1,1,1,0, 0,1,1,1,1, 0,0,0,1,0 },{ 0,1,0,1,0, 0,1,1,1,0, 1,1,1,1,1, 0,1,1,1,1, 1,1,0,0,0 },{ 0,0,1,0,0, 1,1,1,1,0, 0,1,1,1,1, 0,1,1,1,1, 0,1,0,1,1 } }; std::vector<std::vector<unsigned int> > pieces = arrayToPuzzleVector(arr_pieces); Puzzle puzzle(5, pieces); std::cout << "Solving (difficult) puzzle:" << std::endl; std::cout << puzzle << std::endl; PuzzleLayout * solution = PuzzleSolver::solve_puzzle_with_flipping(puzzle); ASSERT(solution != NULL, "No solution found."); std::cout << "Puzzle solution:" << std::endl; std::cout << (*solution) << std::endl; return solution; }
int main(void) { int board[5][5]; int i,j; for (i=0; i<5; i++) { for (j=0; j<5; j++) { board[i][j] = 0; } } board[0][2] = 1; puzzle(board,0); printf("Antal losningar: %d",antal); return 0; }
int main(int argc, const char * argv[]) { std::cout << "Starting..." << std::endl; timeval time; gettimeofday(&time, NULL); long millis = (time.tv_sec * 1000) + (time.tv_usec / 1000); long inbetween_millis = millis; //Toy Story Color & breaks with median filter, needs filter() 48 pc // puzzle puzzle(input+"Toy Story/", 200, 22, false); //Toy Story back works w/ median filter 48pc // puzzle puzzle(input+"Toy Story back/", 200, 50); //Angry Birds color works with median, or filter 24 pc // puzzle puzzle(input+"Angry Birds/color/",300,30); //Angry Birds back works with median 24 pc // puzzle puzzle(input+"Angry Birds/Scanner Open/",300,30); //Horses back not numbered 104 pc // puzzle puzzle(input+"horses/", 380, 50); //Horses back numbered 104 pc puzzle puzzle(input+"horses numbered/", 380, 50); gettimeofday(&time, NULL); std::cout << std::endl << "time to initialize:" << (((time.tv_sec * 1000) + (time.tv_usec / 1000))-inbetween_millis)/1000.0 << std::endl; inbetween_millis = ((time.tv_sec * 1000) + (time.tv_usec / 1000)); puzzle.solve(); gettimeofday(&time, NULL); std::cout << std::endl << "time to solve:" << (((time.tv_sec * 1000) + (time.tv_usec / 1000))-inbetween_millis)/1000.0 << std::endl; inbetween_millis = ((time.tv_sec * 1000) + (time.tv_usec / 1000)); puzzle.save_image(output); gettimeofday(&time, NULL); std::cout << std::endl << "Time to draw:" << (((time.tv_sec * 1000) + (time.tv_usec / 1000))-inbetween_millis)/1000.0 << std::endl; gettimeofday(&time, NULL); std::cout << std::endl << "total time:" << (((time.tv_sec * 1000) + (time.tv_usec / 1000))-millis)/1000.0 << std::endl; system("/usr/bin/open /tmp/final/finaloutput.png"); return 0; }
Puzzle *Sheet::toPuzzle() const { std::auto_ptr<Puzzle> puzzle(new Puzzle); Grid &grid = puzzle->grid; State &state = puzzle->state; grid.height = grid_size.height(); grid.width = grid_size.width(); grid.vars.resize(layout()->count(), -1); int hgrp = -1; std::vector<int> vgrps(grid_size.width(), -1); for(int n = grid_size.width() + 1, c = 1; n < layout()->count(); ++n) { if(cellAt(n).open()) { if(hgrp == -1) { hgrp = state.grps++; state.sum.push_back(cellAt(n - 1).hsum()); state.mem.resize(state.grps); } if(vgrps[c] == -1) { vgrps[c] = state.grps++; state.sum.push_back(cellAt(n - grid_size.width()).vsum()); state.mem.resize(state.grps); } grid.vars[n] = state.vars; state.cand.push_back(cellAt(n).cands()); state.hgrp.push_back(hgrp); state.vgrp.push_back(vgrps[c]); state.mem[hgrp].push_back(state.vars); state.mem[vgrps[c]].push_back(state.vars); ++state.vars; } else { hgrp = vgrps[c] = -1; } if(++c == grid_size.width()) c = 0; } return puzzle.release(); }
int main(int argc, char *argv[]) { // Generate puzzle std::vector<std::vector<int>> puzzle(9, std::vector<int>(9)); int num_failures = gen_sudoku(puzzle); std::cout << "Number of failures: " << num_failures << std::endl; // Print puzzle for (int i=0; i<9; i++) { for (int j=0; j<9; j++) std::cout << puzzle[i][j] << " "; std::cout << std::endl << std::endl; } return 0; }
void task1p3(std::ifstream & inputfile, std::ofstream & outputfile) { std::string line; while ( getline (inputfile,line) ) { if (line.size() > MAX_LINE_SIZE) { printToOutput(outputfile,"LINE is Big!!"); } if (puzzle(line)) { printToOutput(outputfile,"YES"); } else { printToOutput(outputfile,"NO"); } } }
int main() { char str3[20]=".txt"; for(k=0;k<3;k++) { char str1[20]="Test_input"; str1[10]=k+48; strcat(str1,str3); printf("%s",str1); FILE* in_file = fopen( str1, "r"); // read only if (! in_file ) // equivalent to saying if ( in_file == NULL ) { printf("oops, file can't be read\n"); return 0; } // attempt to read the next line and store // the value in the "number" variable for (i = 0; i < 12; i++) { fscanf(in_file, "%d", &D1[i]); } j=0; i=0; while ( fscanf(in_file, "%d", & D2[i][j] ) == 1 ) { if(j==1) { i++;j=0; } else j++; } puzzle(); } return 0; }
int main(void) { int i; int max = 0; int num = 1; for(i = 1;i <= 1000;i ++) { count = 0; puzzle(i); if (count > max){ max = count; num = i; } printf("\n"); } printf("Max = %d, num = %d\n", max, num); return 0; }
/** * Starts a new game. */ void PuzzleManager::StartGame() { data_->gameState = PLAYING_STATE; auto candidates = NPuzzle::GenerateRandomCandidate(); NPuzzle::Puzzle puzzle(candidates); NPuzzle::Solver solver(puzzle); if (solver.Solve()) { data_->board = puzzle; } else { const auto EMPTY = NPuzzle::Puzzle::EMPTY; if (candidates[0] != EMPTY && candidates[1] != EMPTY) { std::swap(candidates[0], candidates[1]); } else { std::swap(candidates[2], candidates[3]); } data_->board = NPuzzle::Puzzle(candidates); } CoreManager::Instance()->SetNextState(MakeSharedState<PuzzleGameState>()); }
MainMenuDebug::MainMenuDebug(int id, ResourceCollection& resources, const MainMenuState* state): Menu(id, resources, false), mState(const_cast<MainMenuState*>(state)) { sf::Vector2f width = sf::Vector2f((float)baseWidth, 0.0f); sf::Vector2f height = sf::Vector2f(0.0f, (float)baseHeight); sf::Texture& buttonTexture = resources.getTexture("Assets/Menu/Button.png"); sf::Font& font = resources.getFont("Assets/Menu/24Janvier.otf"); sf::Text start("PlatformState", font, 30U); addButton(Button(START, width / 2.0f + height / 4.0f, buttonTexture, resources, start)); sf::Text puzzle("PuzzleState", font, 30U); addButton(Button(PUZZLE, width / 2.0f + height * (1/4.0f + 1/6.0f), buttonTexture, resources, puzzle)); sf::Text settings("Toggle Fullscreen", font, 30U); addButton(Button(FULLSCREEN, width / 2.0f + height * (1/4.0f + 2/6.0f), buttonTexture, resources, settings)); sf::Text exit("Exit", font, 30U); addButton(Button(EXIT, width / 2.0f + height * (1/4.0f + 3/6.0f), buttonTexture, resources, exit)); }
int main() { std::list<Puzzle> solutions; static std::stack<Puzzle> alternatives; char inputString[81]; std::string inputPuzzle = readAndVerify(inputString); Puzzle puzzle(inputPuzzle); puzzle.generatePossibleValues(); alternatives.push(puzzle); while (!alternatives.empty()) { puzzle = alternatives.top(); alternatives.pop(); //decide all immediately decideable cells puzzle.decideCells(); //try simplification strats bool simplificationFound = true; while (!puzzle.solved() && simplificationFound) { simplificationFound = false; do { simplificationFound = hiddenSingles(puzzle); } while (simplificationFound == true); //fall back to guessing if (!simplificationFound) { Puzzle alternative; alternative = *clone(puzzle); if ((simplificationFound = guess(puzzle, alternative))) { //record alternative if guess is wrong alternatives.push(alternative); } } //decide all immediately decidable cells before looking for further simplifications if (simplificationFound) { puzzle.decideCells(); } } //if solution is found or contradiction is found(no simplifications) if (puzzle.solved()) { solutions.push_back(puzzle); } } if (solutions.empty()) { std::cout << "No solutions.\n"; } if (!solutions. empty()) { while (!solutions.empty()) { solutions.front().printPuzzle(); solutions.pop_front(); } } std::exit(0); }//end main
void puzzle(int board[5][5], int blocks) { if (blocks > 8) { return; } if (solved(board)) { printf("Solution found! Blocks: %d \n",blocks); antal++; int i,j; for (i=0; i<5; i++) { for (j=0; j<5; j++) { printf("%d ",board[i][j]); } printf("\n"); } //return; exit(0); } else { int k,m; for (k=0; k<5; k++) { for (m=0; m<5; m++) { // prova bit A if (test(board,ABLOCK,k,m)) { //if (board[k][m+1] == 0 && board[k+1][m] == 0 && board[k+1][m+1] == 0) { board[k][m+1] = 1; board[k+1][m] = 1; board[k+1][m+1] = 1; puzzle(board,blocks+1); board[k][m+1] = 0; board[k+1][m] = 0; board[k+1][m+1] = 0; } // prova bit B //if (board[k][m] == 0 && board[k+1][m] == 0 && board[k+1][m+1] == 0) { if (test(board,BBLOCK,k,m)) { board[k][m] = 2; board[k+1][m] = 2; board[k+1][m+1] = 2; puzzle(board,blocks+1); board[k][m] = 0; board[k+1][m] = 0; board[k+1][m+1] = 0; } // prova bit C if (test(board,CBLOCK,k,m)) { //if (board[k][m] == 0 && board[k][m+1] == 0 && board[k+1][m+1] == 0) { board[k][m] = 3; board[k][m+1] = 3; board[k+1][m+1] = 3; puzzle(board,blocks+1); board[k][m] = 0; board[k][m+1] = 0; board[k+1][m+1] = 0; } // prova bit D if (test(board,DBLOCK,k,m)) { //if (board[k][m] == 0 && board[k][m+1] == 0 && board[k+1][m] == 0) { board[k][m] = 4; board[k][m+1] = 4; board[k+1][m] = 4; puzzle(board,blocks+1); board[k][m] = 0; board[k][m+1] = 0; board[k+1][m] = 0; } } } } }
void TVDemo::handleEvent(TEvent &event) { TApplication::handleEvent(event); if (event.what == evCommand) { switch (event.message.command) { case cmAboutCmd: // About Dialog Box aboutDlgBox(); break; case cmCalendarCmd: // Calendar Window calendar(); break; case cmAsciiCmd: // Ascii Table asciiTable(); break; case cmCalcCmd: // Calculator calculator(); break; case cmPuzzleCmd: // Puzzle puzzle(); break; case cmOpenCmd: // View a file openFile("*.*"); break; case cmChDirCmd: // Change directory changeDir(); break; case cmDOS_Cmd: // DOS shell shell(); break; case cmTile: // Tile current file windows tile(); break; case cmCascade: // Cascade current file windows cascade(); break; case cmMouseCmd: // Mouse control dialog box mouse(); break; case cmColorCmd: // Color control dialog box colors(); break; case cmSaveCmd: // Save current desktop saveDesktop(); break; case cmRestoreCmd: // Restore saved desktop retrieveDesktop(); break; default: // Unknown command return; } clearEvent (event); } }
int main() { auto p = puzzle(6); std::cout << p << std::endl; }
void TMyApp::handleEvent(TEvent& event) { TApplication::handleEvent(event); // act like base! if (event.what == evCommand) { switch (event.message.command) { case cmMyNewWin: myNewWindow(); break; case cmAboutCmd: aboutDlgBox(); break; case cmCalendarCmd: calendar(); break; case cmAsciiCmd: asciiTable(); break; case cmCalcCmd: calculator(); break; case cmPuzzleCmd: puzzle(); break; case cmTile: tile(); break; case cmCascade: cascade(); break; case cmCO128x75: setVideoMode(TScreen::smCO128x75); break; case cmCO128x60: setVideoMode(TScreen::smCO128x60); break; case cmCO128x37: setVideoMode(TScreen::smCO128x37); break; case cmCO113x75: setVideoMode(TScreen::smCO113x75); break; case cmCO113x37: setVideoMode(TScreen::smCO113x37); break; case cmCO102x30: setVideoMode(TScreen::smCO102x30); break; case cmCO85x37: setVideoMode(TScreen::smCO85x37); break; case cmCO85x26: setVideoMode(TScreen::smCO85x26); break; case cmCO85x25: setVideoMode(TScreen::smCO85x25); break; case cmCO85x22: setVideoMode(TScreen::smCO85x22); break; case cmCO73x20: setVideoMode(TScreen::smCO73x20); break; case cmCO64x18: setVideoMode(TScreen::smCO64x18); break; case cmCO64x16: setVideoMode(TScreen::smCO64x16); break; case cmCO40x22: setVideoMode(TScreen::smCO40x22); break; default: return; } clearEvent(event); // clear event after handling } }
int main(int argc,char** argv) { if (argc > 1 && strcmp(argv[1], "-stp") == 0) { bool showPath = false; int low = 1; int high = 100; if (argc > 3) { low = atoi(argv[2]); high = atoi(argv[3]); } if (argc > 4 && strcmp(argv[4], "-showpath") == 0) showPath = true; SlidingTilePuzzle puzzle(4,4); SlidingTilePuzzleState start(4, 4); SlidingTilePuzzleState goal(4, 4); ManhattanDistanceHeuristic heur; heur.SetGoal(goal); MyIDAStar<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic> *ida; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; //int i = atoi(argv[1]); for (int i = low - 1; i < high; i++) { startTime = clock(); GetSildingTileInstance(i, start); ida = new MyIDAStar<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic>(heur, 500); std::cout << "********************************\n" << "Puzzle " << i + 1 << " of 100\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<SlidingTilePuzzleAction> acs; puzzle = SlidingTilePuzzle(4, 4); if (ida->GetPath(puzzle, start, goal)) { std::cout << "IDA* found a path!\n"; acs = ida->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "IDA* did not find a path.\n"; std::cout << "nodes expanded: " << ida->GetNodesExpanded() << "\n"; delete ida; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << "time spent: " << timeInSeconds << " seconds\n"; } } else if (argc > 4 && strcmp(argv[1], "-nat") == 0) { bool showPath = false; NAryTree tree(atoi(argv[2]), atoi(argv[3])); NAryTreeState start = 0; NAryTreeState goal = atoi(argv[4]); NAryTreeHeuristic heur; heur.SetGoal(goal); if (argc > 5 && strcmp(argv[5], "-showpath") == 0) showPath = true; MyIDAStar<NAryTreeState, NAryTreeAction, NAryTree, NAryTreeHeuristic> *ida; ida = new MyIDAStar<NAryTreeState, NAryTreeAction, NAryTree, NAryTreeHeuristic>(heur, 1+tree.GetSolutionDepthUpperBound(start, goal)); std::cout << "********************************\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<NAryTreeAction> acs; if (ida->GetPath(tree, start, goal)) { std::cout << "IDA* found a path!\n"; acs = ida->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "IDA* did not find a path.\n"; std::cout << "nodes expanded: " << ida->GetNodesExpanded() << "\n"; delete ida; } else if (argc > 5 && strcmp(argv[1], "-gbm") == 0) { bool showPath = false; GridBasedMap map(atoi(argv[2]), atoi(argv[3])); GridBasedMapState start(0, 0); GridBasedMapState goal(atoi(argv[4]), atoi(argv[5])); GridBasedMapHeuristic heur; heur.SetGoal(goal); if (argc > 6 && strcmp(argv[6], "-showpath") == 0) showPath = true; MyIDAStar<GridBasedMapState, GridBasedMapAction, GridBasedMap, GridBasedMapHeuristic> *ida; //std::cout << "\nestimated upperbound: " << map.GetSolutionDepthUpperBound(start, goal) << "\n"; ida = new MyIDAStar<GridBasedMapState, GridBasedMapAction, GridBasedMap, GridBasedMapHeuristic>( heur, 1+map.GetSolutionDepthUpperBound(start, goal)); std::cout << "********************************\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<GridBasedMapAction> acs; if (ida->GetPath(map, start, goal)) { std::cout << "IDA* found a path!\n"; acs = ida->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "IDA* did not find a path.\n"; std::cout << "nodes expanded: " << ida->GetNodesExpanded() << "\n"; delete ida; } else { std::cout << "Usage: " << argv[0] << " -stp [low] [high] [-showpath]\n" << argv[0] << " -nat <branching factor> <depth> <goal> [-showpath]\n" << argv[0] << " -gbm <map X size> <map Y size> <goal X> <goal Y> [-showpath]\n"; } return 0; }
int main(int argc, char* argv[]){ auto start =boost::posix_time::microsec_clock::local_time(); // Declare the supported options. po::options_description desc("Allowed options"); desc.add_options() ("help", "prints this help message") ("time,t", po::value<int>(&timeArg)->default_value(86400), "set timeout (in seconds)") ("heuristic,h", po::value<string>(&heuristicArg)->default_value("obstacles0"), "set heuristic") ("algorithm,a", po::value<string>(&algorithmArg)->default_value("IDAstar"), "set algorithm") ("memory,m", po::value<int>(&memoryArg)->default_value(200), "set memory limit (in MB)") ("size,s", po::value<int>(&ttSizeArg)->default_value(10000000), "set TT size (in #entries)") ; po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); po::notify(vm); // cout<<timeArg<<" "<<memoryArg<<endl; if (vm.count("help")) { cout << desc << "\n"; return 1; } if(heuristicArg=="0"){ heuristic=zero; }else if(heuristicArg=="210"){ heuristic=twoonezero; }else if(heuristicArg=="obstacles0"){ heuristic=obstacles0; }else if(heuristicArg=="obstacles1"){ heuristic=obstacles1; }else if(heuristicArg=="obstacles2"){ heuristic=obstacles2; }else if(heuristicArg=="obstacles3"){ heuristic=obstacles3; }else if(heuristicArg=="relaxed1"){ heuristic=relaxed1; }else if(heuristicArg=="relaxed2"){ heuristic=relaxed2; }else{ cout << desc << "\n"; return EXIT_FAILURE; } _dummy_static_initializer _dummy; // cout<<"Initialized in "<<(boost::posix_time::microsec_clock::local_time() -start).total_milliseconds()<<"[ms]"<<endl; struct rusage usage; getrusage(RUSAGE_SELF , &usage); int baseMemory=usage.ru_maxrss/1024; string line; while(getline(cin,line)){ if(line==""){ continue; } if(line[0]!='P'){ cerr<<"Formatting problem"<<endl; return EXIT_FAILURE; } string name= line.substr(2,string::npos); string state; for(int i=0;i<7;i++){ getline(cin,line); state.append(line.substr(0,7)); } start =boost::posix_time::microsec_clock::local_time(); Puzzle puzzle(name,state); int moves=-1; puzzle.init(baseMemory); if(algorithmArg=="Astar"){ moves=puzzle.aStar(memoryArg); }else if(algorithmArg=="IDAstar"){ moves=puzzle.idaStar(ttSizeArg); cout<<"statistics "<<name<<" "<<puzzle.statistics()<<endl; }else if(algorithmArg=="fallback"){ moves=puzzle.aStar(memoryArg); if(moves<0&&!puzzle.isTimeOut()){ cout<<name<<" exceeded memory budget of "<<memoryArg<<"MB, reverting to IDA*"<<endl; moves=puzzle.idaStar(ttSizeArg); } } stringstream timeString; timeString<<(boost::posix_time::microsec_clock::local_time() -start).total_milliseconds()/1000.0; times.append(timeString.str()).append(" "); if(moves>=0){ cout<<"solution "<<name<<" "<<timeString.str()<< " "<<moves<<" "<<puzzle.getSolution()<<endl; }else{ if(puzzle.isTimeOut()){ cout<<"solution "<<name<<" "<<"timeout"<<endl; }else{ cout<<"solution "<<name<<" "<<"timeout -> exceeded memory budget"<<endl; } } stringstream ss; ss<<puzzle.getInitialH(); initialH.append(ss.str()).append(" "); // getrusage(RUSAGE_SELF , &usage); // // // cout<<"Usage: "<<usage.ru_maxrss/1024<<"MB"<<endl; } // // cout<<"P"<<algorithmArg<<heuristicArg<<"={"<<Problems.substr(0,Problems.size()-1)<<"};"<<endl; // cout<<"T"<<algorithmArg<<heuristicArg<<"=["<<times<<"];"<<endl; // // cout<<"N"<<algorithmArg<<heuristicArg<<"=["<<nodesSearched<<"];"<<endl; // cout<<"H"<<algorithmArg<<heuristicArg<<"=["<<initialH<<"];"<<endl; // cout<<"G"<<algorithmArg<<heuristicArg<<"=["<<actualSolution<<"];"<<endl; // cout<<"M"<<algorithmArg<<heuristicArg<<"=["<<memories<<"];"<<endl; }
int main(int argc,char** argv) { if (argc > 1 && strcmp(argv[1], "-stp") == 0) { bool showPath = false; int low = 1; int high = 100; if (argc > 3) { low = atoi(argv[2]); high = atoi(argv[3]); } if (argc > 4 && strcmp(argv[4], "-showpath") == 0) showPath = true; SlidingTilePuzzle puzzle(4, 4); SlidingTilePuzzleState start(4, 4); SlidingTilePuzzleState goal(4, 4); ManhattanDistanceHeuristic heur; heur.SetGoal(goal); MyIDAStar<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic> *ida; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; //int i = atoi(argv[1]); for (int i = low - 1; i < high; i++) { startTime = clock(); GetSildingTileInstance(i, start); ida = new MyIDAStar<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic>(heur, 500); std::cout << "********************************\n" << "Puzzle " << i + 1 << " of 100\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<SlidingTilePuzzleAction> acs; puzzle = SlidingTilePuzzle(4, 4); if (ida->GetPath(puzzle, start, goal)) { std::cout << "IDA* found a path!\n"; acs = ida->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "IDA* did not find a path.\n"; std::cout << "nodes expanded: " << ida->GetNodesExpanded() << "\n"; delete ida; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << "time spent: " << timeInSeconds << " seconds\n"; } } else if (argc > 1 && strcmp(argv[1], "-bf") == 0) { bool showPath = false; int low = 1; int high = 100; if (argc > 3) { low = atoi(argv[2]); high = atoi(argv[3]); } if (argc > 4 && strcmp(argv[4], "-showpath") == 0) showPath = true; SlidingTilePuzzle puzzle(4, 4); SlidingTilePuzzleState start(4, 4); SlidingTilePuzzleState goal(4, 4); ManhattanDistanceHeuristic heur; //heur.SetGoal(goal); MySFBDS<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic> *sfbds; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; //int i = atoi(argv[1]); for (int i = low - 1; i < high; i++) { startTime = clock(); GetSildingTileInstance(i, start); goal.Reset(); sfbds = new MySFBDS<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic>(heur,heur,BF, 500.0); std::cout << "********************************\n" << "Puzzle " << i + 1 << " of 100\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<SlidingTilePuzzleAction> acs; puzzle = SlidingTilePuzzle(4, 4); if (sfbds->GetPath(puzzle, start, goal)) { std::cout << "SFBDS with BF Jumping Policy found a path!\n"; acs = sfbds->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "SFBDS with BF Jumping Policy did not find a path.\n"; std::cout << "nodes expanded: " << sfbds->GetNodesExpanded() << "\n"; std::cout << "number of jumps: " << sfbds->GetNumOfJumps() << "\n"; delete sfbds; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << "time spent: " << timeInSeconds << " seconds\n"; } } else if (argc > 1 && strcmp(argv[1], "-jil1") == 0) { bool showPath = false; int low = 1; int high = 100; if (argc > 3) { low = atoi(argv[2]); high = atoi(argv[3]); } if (argc > 4 && strcmp(argv[4], "-showpath") == 0) showPath = true; SlidingTilePuzzle puzzle(4, 4); SlidingTilePuzzleState start(4, 4); SlidingTilePuzzleState goal(4, 4); ManhattanDistanceHeuristic heur; //heur.SetGoal(goal); MySFBDS<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic> *sfbds; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; //int i = atoi(argv[1]); for (int i = low - 1; i < high; i++) { startTime = clock(); GetSildingTileInstance(i, start); goal.Reset(); sfbds = new MySFBDS<SlidingTilePuzzleState, SlidingTilePuzzleAction, SlidingTilePuzzle, ManhattanDistanceHeuristic>(heur, heur, JIL1, 500.0); std::cout << "********************************\n" << "Puzzle " << i + 1 << " of 100\n" << "start: " << start << "goal: " << goal << "\n"; std::vector<SlidingTilePuzzleAction> acs; puzzle = SlidingTilePuzzle(4, 4); if (sfbds->GetPath(puzzle, start, goal)) { std::cout << "SFBDS with JIL1 Jumping Policy found a path!\n"; acs = sfbds->GetActionSequence(); std::cout << "Path length: " << acs.size() << "\n"; if (showPath) { std::cout << "Path: "; for (int i = 0; i < acs.size(); i++) std::cout << acs[i] << " "; std::cout << "\n"; } } else std::cout << "SFBDS with JIL1 Jumping Policy did not find a path.\n"; std::cout << "nodes expanded: " << sfbds->GetNodesExpanded() << "\n"; std::cout << "number of jumps: " << sfbds->GetNumOfJumps() << "\n"; delete sfbds; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << "time spent: " << timeInSeconds << " seconds\n"; } } else if (argc > 1 && strcmp(argv[1], "-oct") == 0) { Map2D map; map.LoadMap("../../resources/maps/lak303d.map"); OctileDistanceHeuristic odh; //load the benchmark std::vector<int> startx, starty, goalx, goaly; std::vector<double> expectedCost; if (!LoadBenchmark(startx, starty, goalx, goaly, expectedCost, "../../resources/benchmarks/lak303d.map.scen")) return 1; MyAStar<Map2DState, Map2DAction, Map2D, OctileDistanceHeuristic> *astar1; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; double solutionCost; for (int i = 0; i < startx.size(); i++) { Map2DState start(startx[i], starty[i]); Map2DState goal(goalx[i], goaly[i]); odh.SetGoal(goal); astar1 = new MyAStar<Map2DState, Map2DAction, Map2D, OctileDistanceHeuristic> (odh); std::cout << "********************************\n" << "test case" << i + 1 << "\n" << "start: " << start << "goal: " << goal << "\n"; startTime = clock(); if (astar1->GetPath(map, start, goal)) { std::cout << "A* w/ ODH found a path!\n"; std::cout << "nodes expanded:\tPath length:\ttime spent(s)\n"; std::cout << astar1->GetNodesExpanded() << "\t"; solutionCost = astar1->GetSolutionCost(); std::cout << solutionCost << "\t"; } delete astar1; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << timeInSeconds << "\n"; if (solutionCost - expectedCost[i] > 0.01 || solutionCost - expectedCost[i] < -0.01) { std::cout << "error solution cost:\t expected cost\n"; std::cout << solutionCost << "\t" << expectedCost[i] << "\n"; return 1; } else std::cout << "passed!\n"; } } else if (argc > 1 && strcmp(argv[1], "-far") == 0) { int k = 10; if (argc > 2) k = atoi(argv[2]); Map2D map; //initialize differential heuristic Map2DDifferentialHeuristic diffHeur(map); if (!diffHeur.LoadMap("../../resources/maps/lak303d.map")) { std::cout << "fail to load map: " << "../../resources/maps/lak303d.map" << "\n"; return 1; } //choose a random pivot and get its farthest point Map2DState s; std::vector<Map2DAction> actions; int x, y; srand(time(NULL)); map.LoadMap("../../resources/maps/lak303d.map"); while (actions.size() == 0) { x = rand() % map.width; y = rand() % map.height; s = Map2DState(x, y); map.GetActions(s, actions); } diffHeur.AddPivot(s); Map2DState next; next = diffHeur.GetFarthestPivot(); diffHeur.ChangePivot(next,0); //add k-1 more pivots that are farthest from current pivots for (int i = 0; i < k-1; i++) { next = diffHeur.GetFarthestPivot(); diffHeur.AddPivot(next); } //build the PDB heuristic and save the visualization images diffHeur.BuildPDBs(); for (int i = -1; i < k; i++) diffHeur.SaveAsBMP("../../resources/maps/lak303d_far/", i); OctileDistanceHeuristic odh; //load the benchmark std::vector<int> startx, starty, goalx, goaly; std::vector<double> expectedCost; if (!LoadBenchmark(startx, starty, goalx, goaly, expectedCost, "../../resources/benchmarks/lak303d.map.scen")) return 1; MyIDAStar<Map2DState, Map2DAction, Map2D, MaxHeuristic<Map2DState>> *ida; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; double solutionCost; for (int i = 0; i < startx.size(); i++) { Map2DState start(startx[i], starty[i]); Map2DState goal(goalx[i], goaly[i]); diffHeur.SetGoal(goal); odh.SetGoal(goal); MaxHeuristic<Map2DState> heur; heur.heurs.push_back(&diffHeur); heur.heurs.push_back(&odh); ida = new MyIDAStar<Map2DState, Map2DAction, Map2D, MaxHeuristic<Map2DState>> (heur,100000); std::cout << "********************************\n" << "test case" << i + 1 << "\n" << "start: " << start << "goal: " << goal << "\n"; startTime = clock(); if (ida->GetPath(map, start, goal)) { std::cout << "A* w/ Farthest Diff Heur & ODH found a path!\n"; std::cout << "nodes expanded:\tPath length:\ttime spent(s)\n"; std::cout << ida->GetNodesExpanded() << "\t"; solutionCost = ida->GetSolutionCost(); std::cout << solutionCost << "\t"; } delete ida; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << timeInSeconds << "\n"; if (solutionCost - expectedCost[i] > 0.01 || solutionCost - expectedCost[i] < -0.01) { std::cout << "error solution cost:\t expected cost\n"; std::cout << solutionCost << "\t" << expectedCost[i] << "\n"; return 1; } else std::cout << "passed!\n"; } } else if (argc > 1 && strcmp(argv[1], "-ran") == 0) { int k = 10; if (argc > 2) k = atoi(argv[2]); Map2D map; //initialize differential heuristic Map2DDifferentialHeuristic diffHeur(map); if (!diffHeur.LoadMap("../../resources/maps/lak303d.map")) { std::cout << "fail to load map: " << "../../resources/maps/lak303d.map" << "\n"; return 1; } //choose 10 random pivot Map2DState s; std::vector<Map2DAction> actions; int x, y; srand(time(NULL)); map.LoadMap("../../resources/maps/lak303d.map"); //add k pivots that are randomly chosen for (int i = 0; i < k; i++) { actions.resize(0); while (actions.size() == 0) { x = rand() % map.width; y = rand() % map.height; s = Map2DState(x, y); map.GetActions(s, actions); } diffHeur.AddPivot(s); } //build the PDB heuristic and save the visualization images diffHeur.BuildPDBs(); for (int i = -1; i < k; i++) diffHeur.SaveAsBMP("../../resources/maps/lak303d_ran/", i); OctileDistanceHeuristic odh; //load the benchmark std::vector<int> startx, starty, goalx, goaly; std::vector<double> expectedCost; if (!LoadBenchmark(startx, starty, goalx, goaly, expectedCost, "../../resources/benchmarks/lak303d.map.scen")) return 1; MyAStar<Map2DState, Map2DAction, Map2D, MaxHeuristic<Map2DState>> *astar1; clock_t startTime; clock_t endTime; clock_t clockTicksTaken; double timeInSeconds; double solutionCost; for (int i = 0; i < startx.size(); i++) { Map2DState start(startx[i], starty[i]); Map2DState goal(goalx[i], goaly[i]); diffHeur.SetGoal(goal); odh.SetGoal(goal); MaxHeuristic<Map2DState> heur; heur.heurs.push_back(&diffHeur); heur.heurs.push_back(&odh); astar1 = new MyAStar<Map2DState, Map2DAction, Map2D, MaxHeuristic<Map2DState>> (heur); std::cout << "********************************\n" << "test case" << i + 1 << "\n" << "start: " << start << "goal: " << goal << "\n"; startTime = clock(); if (astar1->GetPath(map, start, goal)) { std::cout << "A* w/ Random Diff Heur & ODH found a path!\n"; std::cout << "nodes expanded:\tPath length:\ttime spent(s)\n"; std::cout << astar1->GetNodesExpanded() << "\t"; solutionCost = astar1->GetSolutionCost(); std::cout << solutionCost << "\t"; } delete astar1; endTime = clock(); clockTicksTaken = endTime - startTime; timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC; std::cout << timeInSeconds << "\n"; if (solutionCost - expectedCost[i] > 0.01 || solutionCost - expectedCost[i] < -0.01) { std::cout << "error solution cost:\t expected cost\n"; std::cout << solutionCost << "\t" << expectedCost[i] << "\n"; return 1; } else std::cout << "passed!\n"; } } else { std::cout << "Usage: " << argv[0] << " -oct \n" << argv[0] << " -far [k] \n" << argv[0] << " -ran [k] \n" << argv[0] << " -stp \n" << argv[0] << " -bf \n" << argv[0] << " -jil1 \n"; } return 0; }