Advertisement
ExtremeDude2

GeneticAlgorithm

Oct 4th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. int GeneticFitnessFunction(node state) {
  2.     return MAX_COST - CalculateAttackingQueens(state);
  3. }
  4.  
  5. node GeneticFindBestState(vector<node>& population) {
  6.     node best = population[0];
  7.     int bestScore = GeneticFitnessFunction(best);
  8.  
  9.     for (unsigned i = 1; i < population.size(); i++)
  10.     {
  11.         int score = GeneticFitnessFunction(population[i]);
  12.         if (score > bestScore) {
  13.             best = population[i];
  14.             bestScore = score;
  15.         }
  16.     }
  17.  
  18.     return best;
  19. }
  20.  
  21. void GeneticCreatePopulation(vector<node>& population) {
  22.     // TODO Implement this (InClass #7)
  23. }
  24.  
  25. node GeneticSelect(vector<node>& population) {
  26.     float probability = 1.f;
  27.     int score = 0;
  28.  
  29.     int i = rand() % sizeof(population);
  30.  
  31.     for (; true; i = rand() % sizeof(population)) {
  32.         score = GeneticFitnessFunction(population[i]);
  33.         probability = (float)(score / GENETIC_MAX);
  34.         if (probability >= (float)((rand() % 101) / 100.f)) {
  35.         }
  36.     }
  37.  
  38.     return population[i];
  39. }
  40.  
  41. node GeneticReproduce(node x, node y) {
  42.     node child = x;
  43.  
  44.     for (int i = rand() % (sizeof(x) / sizeof(node)); i < (sizeof(x) / sizeof(node)); i++) {
  45.         child.queens[i] = y.queens[i];
  46.     }
  47.  
  48.     return child;
  49. }
  50.  
  51. node GeneticMutate(node child) {
  52.     // TODO Implement this (InClass #7)
  53.     return child;
  54. }
  55.  
  56. // TODO (InClass #7 - Implement genetic algorithm)
  57. node GeneticAlgorithm() {
  58.     node test = {0, 1, 2, 3, 4, 5, 6, 7};
  59.     return test;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement