//Pseudo-code foundation from lecture 11/20 void tour(int* arr, int size, int startingPoint){ if(size - startingPoint == 1){ EdgeWeight currDist = tourDistance(arr, size); if(currDist < bestTourLength_){ for(int i = 0; i < size; i++){ bestTour_[i] = arr[i]; //Makes 'bestTour_' the new tour which is shorter than it was } bestTourLength_ = currDist; } } else { for(int i = startingPoint; i < size; i++){ //Branch and bound conditional, we can use the same method and just replace num_nodes with startingPoint if(startingPoint != 0){ if(tourDistance(arr, startingPoint) > bestTourLength_) return; } swap(arr, startingPoint, i); tour(arr, size, startingPoint + 1); swap(arr, startingPoint, i); } } }
void tour(int* nodeID, int start, Graph* G, std::pair<std::vector<NodeID>, EdgeWeight>* finalPair){ if(G->size()-start==1){ std::pair<std::vector<NodeID>, EdgeWeight> currentPair; std::vector<NodeID> currentVector; double edgeWeight = 0.0; currentPair.first = currentVector; currentPair.second = edgeWeight; for(int i=0; i<G->size(); i++){ if(i!=G->size()-1){ (currentPair).second += G->weight(nodeID[i],nodeID[i+1]); (currentPair).first.push_back(nodeID[i]); if((currentPair).second > (*finalPair).second) return; } else{ (currentPair).second += G->weight(nodeID[i],nodeID[0]); (currentPair).first.push_back(nodeID[i]); if((currentPair).second < (*finalPair).second) *finalPair = currentPair; } } } else{ for(int i=start; i<G->size(); i++){ swap(nodeID,start,i); tour(nodeID,start+1,G,finalPair); swap(nodeID,start,i); } } }
/* Gets the tour of nodes according to the nearest neighbor heuristic. */ vector<unsigned> nearest_neighbor(const vector<vector<unsigned>>& nearest) { if (nearest.empty()) throw invalid_argument("nearest"); const auto len = nearest.front().size() + 1; vector<unsigned> tour(len); // list of nodes still available vector<bool> available(len, true); // the first city has index equal to 0 available[0] = false; for (size_t i = 1; i < len; i++) { // index of the final node auto idx = 0; // index of the starting node const auto j = tour[i - 1]; // select the index of the closest node still available while (!available[nearest[j][idx]]) idx++; tour[i] = nearest[j][idx]; available[tour[i]] = false; } return tour; }
std::pair<std::vector<NodeID>, EdgeWeight> TSP(Graph* G){ int* nodeID = new int[G->size()]; for(int i=0; i<G->size(); i++){ nodeID[i] = i; } double shortest = 0.0; std::vector<NodeID> result; for(int i=0; i<G->size()-1; i++){ shortest += G->weight(nodeID[i],nodeID[i+1]); result.push_back(nodeID[i]); } shortest += G->weight(nodeID[G->size()-1], nodeID[0]); result.push_back(nodeID[G->size()-1]); /* for(int i=0; i<G->size(); i++){ if(i!=G->size()-1) shortest += G->weight(nodeID[i],nodeID[i+1]); else shortest += G->weight(nodeID[i],nodeID[0]); result.push_back(nodeID[i]); } */ std::pair<std::vector<NodeID>, EdgeWeight> finalPair; (finalPair).first = result; (finalPair).second = shortest; tour(nodeID, 0, G, &finalPair); return finalPair; }
/* annonce de début de session avec envoi du tableau */ void session(){ /* Selection d'un plateau aléatoire */ srand(time(NULL)); int alea = rand()%3; pthread_mutex_lock(&mutex_num_plateau); num_plateau = alea; pthread_mutex_unlock(&mutex_num_plateau); /* Envoi du plateau à tous les joueurs */ pthread_mutex_lock(&mutex_clients); client_list *l = clients; char buffer[MAX_SIZE]; sprintf(buffer,"SESSION/%s/\n",plateaux[alea]); while(l != NULL){ l->score = 0; /*remise à 0 des scores (nouvelle session)*/ write(l->socket, buffer, strlen(buffer)); l = l->next; } pthread_mutex_unlock(&mutex_clients); /* Reinitialisation du numero de tour */ pthread_mutex_lock(&mutex_tour); num_tour = 0; pthread_mutex_unlock(&mutex_tour); /* Lancement du premier tour */ tour(); }
/* execute la simulation, avec n tours */ static void simulation(unsigned long n) { unsigned long i; int a, b; printf("Simulation de %lu tours...\n", n); for (i=0; i<n || !n; i++) { putchar('.'); fflush(stdout); rendu(i); /* n est nul -> on regarde si il reste du surcre * sur la grille */ if (!n) { for (a=0; a<=19; a++) { for (b=0; b<=19; b++) { if (kor(a,b)->sucre || kor(a,b)->quoi == 'F' || kor(a,b)->ph_sucre) { goto encore; } } } /* plus de sucre -> on s'arrete */ break; } encore: tour(); } printf("\n"); printf("Fin de la simulation : %lu tours.\n", i); }
int main() { Joueur joueur1, joueur2; strcpy(joueur1.name, "elouan"); strcpy(joueur2.name, "allan"); joueur1.cape = PAS_CAPE; joueur2.cape = PAS_CAPE; joueur1.nbFiole = 0; joueur1.nbFiole = 0; joueur2.nbFiole = 0; joueur2.nbFiole = 0; joueur1.nbCape = 0; joueur1.nbCape = 0; joueur2.nbCape = 0; joueur2.nbCape = 0; joueur1.e = 0, joueur2.e = 0; joueur1.potion = PAS_POTION; joueur2.potion = PAS_POTION; printf("Joueur 1 : %s, Joueur 2 : %s\n", joueur1.name, joueur2.name); system("PAUSE"); tour(joueur1, joueur2); return EXIT_SUCCESS; }
int main(int argc, char*argv[]) { int h = (int)(log(NUM_THREADS)/log(2)); int numtasks, rank, rc; int ecart[h]; int sender[h]; int numb_msg[h]; int i; tree_init(h,ecart,sender,numb_msg); rc = MPI_Init(&argc,&argv); if(rc != MPI_SUCCESS){ printf("Error starting MPI Program. Terminating.\n"); MPI_Abort(MPI_COMM_WORLD,rc); } MPI_Comm_size(MPI_COMM_WORLD,&numtasks); MPI_Comm_rank(MPI_COMM_WORLD,&rank); /*******TEST : global_achievement_time *******/ double global_achievement_time = 0; double global_arrival_time = 0; double global_wake_up_time = 0; double local_achievement_time = 0; double local_arrival_time = 0; double local_wake_up_time = 0; double t_start,t_end; int j=0; t_start = MPI_Wtime(); //printf("Hello world from thread %d BEFORE barrier\n",rank); for(i=0;i<NUM_BARRIERS;i++) { tour(rank,h,ecart,sender,numb_msg,&local_arrival_time, &local_wake_up_time); //printf("Hello world from thread %d AFTER barrier\n",rank); } t_end = MPI_Wtime(); local_achievement_time = (local_achievement_time+ t_end-t_start)/NUM_BARRIERS; local_arrival_time = local_arrival_time/NUM_BARRIERS; local_wake_up_time = local_wake_up_time/NUM_BARRIERS; rc = MPI_Reduce(&local_achievement_time, &global_achievement_time, 1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD); rc = MPI_Reduce(&local_arrival_time, &global_arrival_time, 1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD); rc = MPI_Reduce(&local_wake_up_time, &global_wake_up_time, 1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD); /* if (rank ==0) { FILE *fp; fp = fopen("tour_test.csv","a"); fprintf(fp,"Num_threads,%d,",NUM_THREADS); fprintf(fp,"%f,",global_achievement_time); fprintf(fp,"%f,",global_arrival_time); fprintf(fp,"%f\n",global_wake_up_time); fclose(fp); } */ MPI_Finalize(); return 0; }
/** * \fn void partie() * \brief Permet le bon déroulement de la partie. * */ void partie(){ perso_vivant(); while(!victoire() && action != 5){ tour(); i_perso_actuel = 0; } if(action == 5); else printf("Le joueur %i a gagné !\n", victoire()); }
std::pair<std::vector<NodeID>, EdgeWeight> TSP(Graph* G){ int* arr = new int[G->size()]; double min; for(int i = 0; i<G->size(); i++){ arr[i] = i; } min = tour(arr, G->size(), 0, G, -1); std::vector<NodeID> vect = findVector(arr, G->size(), 0, G, min); return std::pair<std::vector<NodeID>, EdgeWeight> (vect, min); }
void GSTM::mutation(GAIndividual<CodeVInt> &indivl) { int starts, ends; vector<int> arr(m_numDim), tour(m_numDim); int i, j, z; int len = 0; for (i = 0; i<m_numDim; i++) { arr[i] = i; tour[i] = indivl.data().m_x[i]; } starts = Global::msp_global->getRandInt(0, m_numDim - 1); arr[starts] = arr[m_numDim - 1]; i = 0; for (z = 0; z<m_numDim; z++) { if (tour[z] == starts) { j = z; break; } } do { int temp = Global::msp_global->getRandInt(0, m_numDim - 2 - i); ends = arr[temp]; arr[temp] = arr[m_numDim - 2 - i]; i++; for (int t = 0; t<m_numDim; t++) { if (tour[t] == ends) { z = t; break; } } len = (z - j + m_numDim) % m_numDim; } while (len<m_LMIN || len>m_LMAX); double rnd = Global::msp_global->mp_uniformAlg->Next(); if (rnd <= m_PRC) Reconnection(indivl, starts, ends); else { double rnd1 = Global::msp_global->mp_uniformAlg->Next(); if (rnd1 <= m_PCP) DistortionMethod(indivl, starts, ends, m_PL); else Rotation(indivl, starts, ends, m_NLMax); } }
std::pair<std::vector<NodeID>, EdgeWeight> TSP(Graph* G){ current_graph_ = G; int num_nodes = G -> size(); std::vector<NodeID> bestTourNodes (num_nodes); bestTour_ = new int[num_nodes]; for(int i = 0; i < num_nodes; i++){ bestTour_[i] = i; //Initalize as a starting point to find best tour } bestTourLength_ = tourDistance(bestTour_, num_nodes); //Initalizes the distance for the first tour tour(bestTour_, num_nodes, 0); for(int i = 0; i < num_nodes; i++){ bestTourNodes[i] = bestTour_[i]; } std::pair<std::vector<NodeID>, EdgeWeight> finalTour(bestTourNodes, tourDistance(bestTour_,num_nodes)); return finalTour; }
int main() { morpion_t partie = creer_partie(); int vainqueur = VIDE , i = 0; while(vainqueur == VIDE && i < 9) { vainqueur = tour(&partie); afficher_plateau(partie); changer_joueur(&partie); i++; } if(vainqueur == ROND) printf("LE JOUEUR ROND A GAGNÉ\n"); else if(vainqueur == CROIX) printf("LE JOUEUR CROIX A GAGNÉ\n"); else printf("PERSONNE N'A GAGNE\n"); return EXIT_SUCCESS; }
double tour(int* arr, int n, int startingPlace, Graph* G, double min){ double myMin = 0; if(n-startingPlace == 1){ for(int i = 0; i<n-1; i++){ myMin += G->weight(arr[i], arr[i+1]); } if(min == -1){ min = myMin; } else{ if(myMin < min){ min = myMin; } } } else{ for(int i=startingPlace; i<n; i++){ swap(arr, startingPlace, i); min = tour(arr, n, startingPlace, G, min); swap(arr, startingPlace, i); } } return min; }
bool BKZReduction<FT>::svp_preprocessing(int kappa, int block_size, const BKZParam ¶m) { bool clean = true; FPLLL_DEBUG_CHECK(param.strategies.size() > block_size); int lll_start = (param.flags & BKZ_BOUNDED_LLL) ? kappa : 0; if (!lll_obj.lll(lll_start, lll_start, kappa + block_size)) { throw std::runtime_error(RED_STATUS_STR[lll_obj.status]); } if (lll_obj.n_swaps > 0) clean = false; auto &preproc = param.strategies[block_size].preprocessing_block_sizes; for (auto it = preproc.begin(); it != preproc.end(); ++it) { int dummy_kappa_max = num_rows; BKZParam prepar = BKZParam(*it, param.strategies, LLL_DEF_DELTA, BKZ_GH_BND); clean &= tour(0, dummy_kappa_max, prepar, kappa, kappa + block_size); } return clean; }
template <class FT> bool BKZReduction<FT>::bkz() { int flags = param.flags; int final_status = RED_SUCCESS; nodes = 0; bool sd = (flags & BKZ_SD_VARIANT); bool sld = (flags & BKZ_SLD_RED); algorithm = sd ? "SD-BKZ" : sld ? "SLD" : "BKZ"; if (sd && sld) { throw std::runtime_error("Invalid flags: SD-BKZ and Slide reduction are mutually exclusive!"); } if (flags & BKZ_DUMP_GSO) { std::ostringstream prefix; prefix << "Input"; dump_gso(param.dump_gso_filename, prefix.str(), false); } if (param.block_size < 2) return set_status(RED_SUCCESS); int i = 0; BKZAutoAbort<FT> auto_abort(m, num_rows); if (sd && !(flags & (BKZ_MAX_LOOPS | BKZ_MAX_TIME | BKZ_AUTO_ABORT))) { cerr << "Warning: SD Variant of BKZ requires explicit termination condition. Turning auto " "abort on!" << endl; flags |= BKZ_AUTO_ABORT; } if (flags & BKZ_VERBOSE) { cerr << "Entering " << algorithm << ":" << endl; print_params(param, cerr); cerr << endl; } cputime_start = cputime(); m.discover_all_rows(); if (sld) { m.update_gso(); sld_potential = m.get_slide_potential(0, num_rows, param.block_size); } // the following is necessary, since sd-bkz starts with a dual tour and // svp_reduction calls size_reduction, which needs to be preceeded by a // call to lll lower blocks to avoid seg faults if (sd) lll_obj.lll(0, 0, num_rows); int kappa_max = -1; bool clean = true; for (i = 0;; ++i) { if ((flags & BKZ_MAX_LOOPS) && i >= param.max_loops) { final_status = RED_BKZ_LOOPS_LIMIT; break; } if ((flags & BKZ_MAX_TIME) && (cputime() - cputime_start) * 0.001 >= param.max_time) { final_status = RED_BKZ_TIME_LIMIT; break; } if ((flags & BKZ_AUTO_ABORT) && auto_abort.test_abort(param.auto_abort_scale, param.auto_abort_max_no_dec)) { break; } try { if (sd) { clean = sd_tour(i, param, 0, num_rows); } else if (sld) { clean = slide_tour(i, param, 0, num_rows); } else { clean = tour(i, kappa_max, param, 0, num_rows); } } catch (RedStatus &e) { return set_status(e); } if (clean || param.block_size >= num_rows) break; } int dummy_kappa_max = num_rows; if (sd) { try { hkz(dummy_kappa_max, param, num_rows - param.block_size, num_rows); print_tour(i, 0, num_rows); } catch (RedStatus &e) { return set_status(e); } } if (sld) { try { int p = num_rows / param.block_size; if (num_rows % param.block_size) ++p; for (int j = 0; j < p; ++j) { int kappa = j * param.block_size + 1; int end = min(num_rows, kappa + param.block_size - 1); hkz(dummy_kappa_max, param, kappa, end); } print_tour(i, 0, num_rows); } catch (RedStatus &e) { return set_status(e); } } if (flags & BKZ_DUMP_GSO) { std::ostringstream prefix; prefix << "Output "; prefix << " (" << std::fixed << std::setw(9) << std::setprecision(3) << (cputime() - cputime_start) * 0.001 << "s)"; dump_gso(param.dump_gso_filename, prefix.str()); } return set_status(final_status); }