void test_solution_generator_col_begin(void) { int permutation[4] = { 1, 2, 3, 4 }; int size = 4; bool isRow = false; int rowColNum = 1; int solution[4][4] = { { 1, 3, 2, 4 }, { 2, 4, 1, 3 }, { 3, 1, 4, 2 }, { 4, 2, 3, 1 } }; int *puzzle = generate_solution(permutation, size, isRow, rowColNum); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { CU_ASSERT_EQUAL_FATAL(solution[j][i], (puzzle + (j * size))[i]); } } }
void test_solution_generator_row_end(void) { int permutation[4] = { 1, 2, 3, 4 }; int size = 4; bool isRow = true; int rowColNum = 4; int solution[4][4] = { { 2, 1, 4, 3 }, { 3, 4, 1, 2 }, { 4, 3, 2, 1 }, { 1, 2, 3, 4 } }; int *puzzle = generate_solution(permutation, size, isRow, rowColNum); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { CU_ASSERT_EQUAL_FATAL(solution[3][j], (puzzle + (3 * size))[j]); } } }
int main( int argc, char **argv) { // Declare the supported options. boost::program_options::options_description desc("Allowed options"); boost::program_options::variables_map vm; std::string input_filename; std::string output_filename; int num_partials= 1; // determines the stepsize int random_instances= 5; int num_random_changes= 4; desc.add_options() ("num-partials", boost::program_options::value<int>(&num_partials), "we can caluclate partial solutions (default 1)") ("random-instances", boost::program_options::value<int>(&random_instances), "number of random tries for each partial (default 5)") ("random-changes", boost::program_options::value<int>(&num_random_changes), "max difference in spine changes of the instances (default 4)") ; if( common_cmdline( argc, argv, desc, vm, input_filename, output_filename ) ) return 1; // // instance input // std::auto_ptr<KPMPInstance> instance( KPMPInstance::readInstance( input_filename ) ); ofstream outfile(output_filename, ios::out); if( !outfile ) { std::cerr << "error opening output file for writing" << std::endl; return 1; } // moegliche variante: // // nodes kommen in uckets dazu, // es werden nur edges reingeworfen die reinreichen // // * wie schnell findet man nur edges die auf das set passen? // mit eineme vector der die position im ergebnis angibt, // mit dem auslesen der // // * wird die page allocation jedesmal vollkommen neu gemacht? // ja // int num_vertices= instance->getNumVertices(); int stepsize; if( num_partials > num_vertices ) { num_partials= 1; stepsize= num_vertices; } else { stepsize= num_vertices / num_partials; if( (stepsize * num_partials) < num_vertices ) stepsize++; } #ifdef DEBUG std::cout << "num_vertices: " << num_vertices << " stepsize: " << stepsize << " num_partials: " << num_partials << std::endl; #endif // starting with empty sets //random_spine_generator* current_spine_generator= new random_spine_generator( num_vertices ); random_spine_generator* current_spine_generator= new random_spine_generator( spine_order_num_edges(instance->getAdjacencyList()) ); solution * current_solution= new solution(instance->getK(), num_vertices ); vector<vector<unsigned int> > adjacencyList= instance->getAdjacencyList(); for( int sol_length= 0; sol_length<num_vertices; sol_length+=stepsize ) { //initial partial auto best_partial = generate_solution( current_spine_generator, current_solution, stepsize, 0, adjacencyList ); // now we try to top for( int i=0; i<random_instances; ++i ) { auto random_partial = generate_solution( current_spine_generator, current_solution, stepsize, num_random_changes, adjacencyList ); #ifdef DEBUG std::cout << "sbest: " << *(best_partial.second) << std::endl; std::cout << "random_partial: " << *(random_partial.second) << std::endl; #endif if( random_partial.second->get_crossings() < best_partial.second->get_crossings() ) { std::swap( best_partial, random_partial ); } delete random_partial.first; delete random_partial.second; } // best partial is our next solution base delete current_spine_generator; delete current_solution; current_spine_generator= best_partial.first; current_solution= best_partial.second; } // // output // write_solution( outfile, *current_solution ); outfile.close(); write_solution_statistics( std::cout, *current_solution ); return 0; }
//this function is going to solve the puzzle void frog_jumping_game_model::solve_puzzle() { game_solutions = (Node **) malloc(sizeof(Node *) * MAX_SOLUTION_NUMBER); number_of_solutions = 0; Root = create_node(); int currnt [FROG_NUMBER] = {GF,GF,GF,ES,RF,RF,RF}; for(int i = 0; i < FROG_NUMBER; i++) { Root->current_state[i] = currnt[i]; } //generate solution tree generate_all_states(); //find the solution generate_solution(); // for(int i = 0; i < number_of_solutions; i++) { cout<<"***************************"<<endl; Node *ptr; ptr = game_solutions[0]; while(ptr != NULL) { //stroe the current state in ptr pointer to the game state node game_state_node current_state; for(int i = 0; i < FROG_NUMBER; i++) { current_state.state[i] = ptr->current_state[i]; } //add current state node to the vector this->solution_states.push_back(current_state); //print the node print_node(ptr); //go to the parent ptr = ptr->parent; } cout<<endl; } //now generate indexes from the inexes array solution vector for(int i = 0; i < this->solution_states.size() - 1; i++) { int next_index = NULL_PTR; //game_state_node current_state = this->solution_states[i]; game_state_node next_state = this->solution_states[i + 1]; for(int i = 0 ; i < FROG_NUMBER; i++) { if(next_state.state[i] == ES) { next_index = i; break; } } if(next_index != NULL_PTR) { //add it to the vector this->solution_sequance.push_back(next_index); } } //print the solutuoin for(int i = 0; i < solution_sequance.size(); i++) { cout<<solution_sequance[i]<<endl; } this->count_all_nodes(this->Root); cout<<"Number of nodes in the tree is "<<this->node_counter<<endl; }