void dfs(){ int city, length; tipeTour* tour; tipeStack* stack; tour = malloc(sizeof(tipeTour)); initTour(tour); stack->tour = tour; stack->city = HOME; stack->length = 0; stack->nextTour = NULL; while(!empty(stack)){ pop(&tour, &city, &length, &stack); tour->cities[tour->countCities] = city; tour->length += length; tour->countCities += 1; if(tour->countCities == N) isABestTour(city, &tour); else for(int neighbor = N-1; neighbor > 0; neighbor--) if(feasible(city, neighbor, tour)) push(tour, neighbor, graphCity[city][neighbor], &stack); } free(tour); }
void* TSP(void *argument){ Stack *s = (Stack *)argument; while (0<=s->top){ tour city=pop(s); if (city.n==N){ omp_set_lock(&best_tour_lock); if (city.cost<best_tour->cost){ /**imprime los mejores costos for (int i = 0; i < thread_count; i++){ if (s==stacks[i]) printf("id: %d\n",i); } printf("costo: %f\n",best_tour->cost);**/ copy(best_tour, &city); display(); reshape(WIDTH, HEIGHT); } omp_unset_lock(&best_tour_lock); } else{ for (int i=N-1; 1<=i; i--){ if (feasible(city,i)){ add_city(&city, i); push(s, city); remove_last_city(&city); } } } } return NULL; }
void TSP(tour t){ for (int i=N-1; 1<=i; i--) push(stack, i); while (0<=stack->top){ int city=pop(stack); if (city==NO_CITY) remove_last_city(&t); else{ add_city(&t, city); if (t.n==N){ if (t.cost<best_tour->cost){ copy(best_tour, &t); display(); reshape(WIDTH, HEIGHT); } remove_last_city(&t); } else{ push(stack, NO_CITY); for (int i=N-1; 1<=i; i--){ if (feasible(t,i)){ push(stack, i); } } } } } }
// 某一位为1 表示可以下棋,否则不能下 void board::cal_all_feasible() { bool current_feasible[array_size][array_size]; memset(current_feasible, 0, sizeof(current_feasible)); int temp_x, temp_y; for (int i = 1; i <= m_chess_num; i++) {// O(m_chess_num )=O(20) data_itoxy(m_chess[i], temp_x, temp_y); // current_feasible[temp_x][temp_y] = 1;// 已经有棋子的地方不能下 if (i % 2 == selected_color) { if (m_limit[selected_color][temp_x][temp_y] >= m_inf2 + 1) {//这个棋子附近有一个同色棋子,附近不可以再下同色棋子 for (int j = 0; j < dir_count; j++) { current_feasible[temp_x + m_surround[j][0]][temp_y + m_surround[j][1]] = 1; } } } } // m_feasible.assign(board_size*board_size, 0); int x, y; //bool no = 0; for (int i = 0; i <board_size*board_size; i++) { m_feasible[i] = 0; real_itoxy(i, x, y); real2data(x, y); if (m_curr_color[x][y]) { } else if (m_limit[selected_color][x][y] >= m_inf) { // no = 0; } else if (m_limit[selected_color][x][y] >= 2) { } else if (!current_feasible[x][y] /*&& m_limit[selected_color][x][y]<m_inf&& m_curr_color[x][y]==0*/) { //ans |= 1; //cout << "(" << x << "," << y << ")"; feasible(selected_color, x, y); // no = 1; m_feasible[i] = 1; } else m_feasible[i] = 0; } //cout << endl; int index; }
void partition2(Stack **s,tour t){ s = malloc((thread_count)*sizeof(Stack*)); for (int i = 0; i < thread_count; i++){ s[i] = malloc(sizeof(Stack)); s[i]->max = N*((N-1)*0.5); s[i]->top = -1; s[i]->content = malloc((s[i]->max)*sizeof(tour)); } for (int i=N-1,k=0; 1<=i; i--){ if (feasible(t,i)){ add_city(&t, i); for (int j=N-1; 1<=j; j--){ if (feasible(t,j)){ add_city(&t, j); push(s[k%thread_count], t); remove_last_city(&t); k++; } } remove_last_city(&t); } } stacks=s; }
void nqueen(int row) { int i, j; if (row < n) { for (i = 0; i < n; i++) { if (feasible(row, i)) { a[row][i] = 'Q'; nqueen(row + 1); a[row][i] = '.'; } } } else { printf("\nThe solution is:- "); printmatrix(); } }
template <int N, int N_obj> void GA<N, N_obj>::crossover_scattered (const Individual &parent1, const Individual &parent2, Individual &child) { for(int i = 0; i < N; ++i) { if(dist01(rnd_generator) >= 0.5) { child[i] = parent1[i]; } else { child[i] = parent2[i]; } } if( !feasible(child) ) crossover_arithmetic(parent1, parent2, child); }
//放下棋子 bool board::lay(int color, int x, int y) { if (feasible(color, x, y)) { limit_lay(color, x, y); // 插入棋局链表中 node * temp; for (int i(0); i < 4; ++i) { temp = m_head[x][y][i]; while (!(temp->m_next == NULL) && (temp->m_next)->m_index < m_network[x][y][i].m_index) temp = temp->m_next; m_network[x][y][i].m_pioneer = temp; m_network[x][y][i].m_next = temp->m_next; if (temp->m_next != NULL) { temp->m_next->m_pioneer = &m_network[x][y][i]; } temp->m_next = &m_network[x][y][i]; } return true; } return false; }
std::pair<size_t, Double> KMInput::getBest(size_t i) const { size_t const l(label(i)); std::pair<size_t, Double> min(l, 0); if (sizeOfLabel(l) != 1) { Double const cst(-getDistance(i) * getCoeff<false>(i, l)); for (size_t j(0); j < getK(); ++j) { if (j != l && feasible(i, j)) { Double delta(cst); delta += getDistance(i, j) * getCoeff<true>(i, j); delta *= obsWeight(i); if (delta < min.second) { min.first = j; min.second = delta; } } } } return min; }
void solve_sudoku(int S[][9], int x, int y){ if(y == 9){ if(x == 8){ printSolution(S); exit(0); } else { solve_sudoku(S, x+1,0); } } else if(S[x][y] == 0){ int k = 0; for (k = 1; k <=9; k++){ if(feasible(S,x,y,k)){ S[x][y] = k; solve_sudoku(S, x, y+1); S[x][y] = 0; } } } else { solve_sudoku(S,x,y+1); } }
bool pms::app::packer::run ( const std::string& source_file_path, layout::atlas atlas ) const { claw::logger << claw::log_verbose << "Generating sprite sheet:\n" << atlas.to_string() << " from file '" << source_file_path << "'\n"; if ( !feasible( atlas ) ) return false; const bool allow_rotation ( m_options.get_rotation_direction() != generators::rotation_direction::none ); if ( !layout::build( allow_rotation, atlas ) ) return false; generate( source_file_path, atlas ); return true; }
int TreeSearch(struct Graph *graph,int src,int V,int * costMatrix) { struct tour temp3; Stack *s=createStack(1000); struct tour bestTour; //printf("asdsad\n"); struct tour t={0,NULL,0}; struct pathNode *temp11=NULL; struct tour temp={0,NULL,0}; //printf("asdsad\n"); addCity(&t,src,V,costMatrix); //printf("asdsad\n"); push(s,t); //printf("asdsad\n"); while(!isEmpty(s)) { t=pop(s); if(t.cityCount==V) { int lastCost=*(costMatrix+V*t.thePath->lastNode+t.thePath->head->nodeNumber); if(t.totalCost+lastCost<minCost) { minCost=t.totalCost+lastCost; //printTour(&t); } struct pathNode *temp44=t.thePath->head; struct pathNode *temp55=temp44; while(temp44!=NULL) { temp44=temp44->next; free(temp55); temp55=temp44; } free(t.thePath); } else { int i=0; for(i=V-1;i>0;i--) { if(feasible(&t,i)) { addCity(&t,i,V,costMatrix); temp3.thePath=NULL; temp3.totalCost=t.totalCost; temp11=t.thePath->head; while(temp11->next!=NULL) { addCity(&temp3,temp11->nodeNumber,V,costMatrix); temp11=temp11->next; } //printf("asdasdsada\n"); addCity(&temp3,temp11->nodeNumber,V,costMatrix); temp3.cityCount=t.cityCount; push(s,temp3); //printf("Asdsadasdsad\n"); removeLastCity(&t,costMatrix,V); //printf("%lu\n",sizeof(struct tour)); } } struct pathNode *temp44=t.thePath->head; struct pathNode *temp55=temp44; while(temp44!=NULL) { temp44=temp44->next; free(temp55); temp55=temp44; } free(t.thePath); } } return minCost; }
void MOGA::print() const { if (pipe_fp_) { std::vector< std::vector<int> > sol_of_rank; std::vector<bool> feasible( population_.size() ); int rank_max( 0 ); bool plotted_before( false ); // Compute feasibility and rank max for ( unsigned int i = 0; i < population_.size(); ++i ) { feasible[i] = population_[i].is_feasible(); rank_max = std::max( population_[i].rank, rank_max ); } // Sort population into subpopulations of same rank sol_of_rank.resize( rank_max+1 ); for ( unsigned int i = 0; i < population_.size(); ++i ) { if ( feasible[i] && population_[i].rank > 0 ) { sol_of_rank[population_[i].rank-1].push_back( i ); } else { sol_of_rank[rank_max].push_back( i ); } } // Begin plot std::fputs( "plot ", pipe_fp_ ); // Show the infeasible solutions in background if ( sol_of_rank[rank_max].size() > 0 ) { std::fputs( "'-' title 'infeasible' linecolor rgb 'turquoise' pt 9", pipe_fp_ ); plotted_before = true; } // Show the feasible solutions of rank > 1 for ( unsigned int r = rank_max-1; r > 0; --r ) { if ( sol_of_rank[r].size() > 0 ) { if ( plotted_before == true ) std::fputs( ", ", pipe_fp_ ); std::fprintf( pipe_fp_, "'-' title '%d'", r ); plotted_before = true; } } // Show the feasible solutions of rank 1 in foreground if ( sol_of_rank[0].size() > 0 ) { if ( plotted_before == true ) std::fputs( ", ", pipe_fp_ ); std::fputs( "'-' title '1' linecolor rgb 'blue' pt 9", pipe_fp_ ); } std::fputs( "\n", pipe_fp_ ); // Transfer points for ( int r = rank_max; r >= 0; --r ) { if ( sol_of_rank[r].size() > 0 ) { for ( unsigned int i = 0; i < sol_of_rank[r].size(); ++i ) { for ( int k = 0; k < getNbObjective(); ++k ) { std::fprintf( pipe_fp_, "%f ", population_[sol_of_rank[r][i]].obj[k] ); } std::fputs( "\n", pipe_fp_ ); } std::fputs( "e\n", pipe_fp_ ); } } std::fputs( "\n", pipe_fp_ ); } }
template <int N, int N_obj> void GA<N, N_obj>::mutation_adaptive (const Individual &parent, Individual &child) { double tol = 1.e-8; // tolerance, pure magic // set step size if(generation <= 1) { ma_step_size = 1.; } else { if(!ma_step_changed) { ma_step_changed = true; if(N_obj == 1) { if(best_score[generation] < best_score[generation - 1]) ma_step_size = std::min(1., ma_step_size * 4.); else ma_step_size = std::max(tol, ma_step_size / 4.); } else { if(spread[generation] > spread[generation - 1]) ma_step_size = std::min(1., ma_step_size * 4.); else ma_step_size = std::max(tol, ma_step_size / 4.); } } } // set logarithmic scale Individual scale; for(int i = 0; i < N; i++) { double exponent = 0.; if( fabs(lower_boundary[i]) > tol ) exponent += 0.5 * log(fabs(lower_boundary[i])) / log(2.); if( fabs(upper_boundary[i]) > tol ) exponent += 0.5 * log(fabs(upper_boundary[i])) / log(2.); scale[i] = pow(2., exponent); } Individual raw_basis[N], basis[N], tangent_cone[N], dir[2 * N]; double dir_sign[4 * N]; int n_tangent = 0, n_basis = N, index_vector[4 * N], order_vector[4 * N]; // calculate mutation direction set // tangent components for(int i = 0; i < N; i++) { if( fabs(parent[i] - lower_boundary[i]) < tol || fabs(parent[i] - upper_boundary[i]) < tol ) { tangent_cone[n_tangent] = 0.; tangent_cone[n_tangent][i] = 1.; n_tangent++; } } // raw basis vectors double poll_param = 1. / sqrt(ma_step_size); for(int i = 0; i < n_basis; i++) { raw_basis[i] = 0.; raw_basis[i][i] = poll_param * (dist01(rnd_generator) >= 0.5 ? 1. : -1.); for(int j = i + 1; j < n_basis; j++) { raw_basis[i][j] = round( (poll_param + 1.) * dist01(rnd_generator) - 0.5); } } // basis as random permutation of raw basis for(int j = 0; j < n_basis; j++) order_vector[j] = j; std::random_shuffle(order_vector, order_vector + n_basis); for(int i = 0; i < n_basis; i++) for(int j = 0; j < n_basis; j++) basis[i][j] = raw_basis[order_vector[i]][order_vector[j]]; // prerare random direction mutation int n_dir = n_tangent + n_basis; for(int i = 0; i < n_basis; i++) dir[i] = basis[i]; for(int i = 0; i < n_tangent; i++) dir[n_basis + i] = tangent_cone[i]; for(int i = 0; i < n_basis; i++) { index_vector[i] = i; dir_sign[i] = 1.; } int i_base = n_basis; for(int i = i_base; i < i_base + n_basis; i++) { index_vector[i] = i - i_base; dir_sign[i] = -1.; } i_base += n_basis; for(int i = i_base; i < i_base + n_tangent; i++) { index_vector[i] = i - i_base; dir_sign[i] = 1.; } i_base += n_tangent; for(int i = i_base; i < i_base + n_tangent; i++) { index_vector[i] = i - i_base; dir_sign[i] = -1.; } int n_dir_total = 2 * n_dir; for(int i = 0; i < n_dir_total; i++) order_vector[i] = i; std::random_shuffle(order_vector, order_vector + n_dir_total); // finally, mutate double success = false; for(int i = 0; i < n_dir_total; i++) { int k = index_vector[order_vector[i]]; Individual direction = dir_sign[k] * dir[k]; child = parent + ma_step_size * scale * direction; if(feasible(child)) { success = true; break; } } if( !success ) { child = parent; if(options.verbose && N < 5) std::cout << "mutation failed at x = " << parent << "\n"; } }