void FirstChoiceHillClimbing::hillClimbing(Board b) {
    meetResult = false;
    Board successor = Board::generateProperBoard(b);
    steps++;
    if (b.getQueensState() == successor.getQueensState()) {
        if (successor.getHeuristic() == 0) {
            meetResult = true;
        } else {
            meetResult = false;
        }
    } else {
        hillClimbing(successor);
    }
}
int main(int argc, char *argv[]) {
    int algorithm, times_to_execute=1;

    do {
        printf ("Number of queens (1<=nqueens<%d): ", MAXQ);
        scanf ("%d", &nqueens);
    } while ((nqueens < 1) || (nqueens > MAXQ));

    do {
        printf ("Algorithm: (1) Random search  (2) Hill climbing  ");
        printf ("(3) Simulated Annealing  (4) Genetic Algorithm: ");
        scanf ("%d", &algorithm);
    } while ((algorithm < 1) || (algorithm > 4));
    
    if (algorithm != 4) {
        do {
            printf("How many times to execute? (1<n<10000):");
            scanf("%d", &times_to_execute);
        } while ((times_to_execute < 1 || times_to_execute > 10000));
    }
  
    initializeRandomGenerator();
    
    int i=0;
    switch (algorithm) {
        case 1: randomSearch();       break;
        case 2:
            solutions_found = 0;
            for (i=0; i<times_to_execute; i++) {
                initiateQueens(1);
                hillClimbing();
            }
            printf("Algorithm finished. Found a solution in %d out of %d executions.\n", solutions_found, i);
            break;
        case 3:
            solutions_found = 0;
            for(i = 0; i < times_to_execute; i++)
            {
                initiateQueens(1);
                simulatedAnnealing();
            }
            printf("Algorithm finished. Found a solution in %d out of %d executions.\n", solutions_found, i);
            break;
        case 4:
            initiateQueens(1);
            geneticAlgorithm();
    }

    return 0;  
}
Exemplo n.º 3
0
   void operator()()
   {
      // Memory leak at the end...
      inst::Instance* instance = createInstance(_param);

      sol::Solution initialSolution(instance);
      sol::ObjValue initObjValue
         = initialSolution.computeObjValue();
      initialSolution.applyDelta(initObjValue);

      _pool.addSolution(initialSolution);

      RandomMoves randomMoves(
         _dist(_gen),
         *instance,
         instance->numProcesses() * _param["a"].as<double>());
      
      HillClimbing hillClimbing(_dist(_gen), *instance, &_pool, 
                                _param["b"].as<int>(),
                                _param["e"].as<int>(),
                                _param["f"].as<int>());
      
      IteratedLocalSearch<HillClimbing, RandomMoves>
         ils(_param["c"].as<int>(),
             &hillClimbing,
             &randomMoves,
             &_pool);

      do
      {
         sol::Solution solution(bestSolution());
         ils.apply(solution);
         boost::this_thread::interruption_point();
      }
      while(true);
   }
Exemplo n.º 4
0
int main(){
  double result;
  double bestsofar = -999;
  double resultA[10];
  double bestA[10];
  double randA[10];
  int i;
  while(result < 8 && numeval <= NUMEVAL){
    srand(time(NULL));
    for(i = 0; i < 10; i++){
      randA[i] = (rand()%10 < 5 ? -1 : 1)*(double)rand() / ((double)(RAND_MAX)+(double)(1));
    }
    result = hillClimbing(randA,resultA);
    if(result > bestsofar){
      bestsofar = result;
      memcpy(bestA,resultA,sizeof(double)*10);
      for(i = 0; i < 10; i++){
	printf("\nBSF[%d]: %lf",i,bestA[i]);      
      }
      printf("\nBestSoFarGeneral: %lf No. Eval: %d\n", bestsofar,numeval);
    }
  }
  printf("No. Eval FINAL: %d\n", numeval);
}
Exemplo n.º 5
0
void runSample() {
    // Getting random numbers
    int n = 100;
    uint64_t* nums = malloc(sizeof(uint64_t) * n);
    for (int i = 0; i < n; i++) {
        nums[i] = getNum();
    }

    // Generate sequence and partition (same result)
    int* s = sequenceGenerate(n);
    int* p = partitionGenerate(n);

    // Results and times
    uint64_t results[9];
    struct timeval times[10];
    gettimeofday(&times[0], NULL);
    results[0] = kk(nums, n);
    gettimeofday(&times[1], NULL);
    results[1] = sequenceResidue(nums, s, n);
    gettimeofday(&times[2], NULL);
    results[2] = partitionResidue(nums, p, n);
    gettimeofday(&times[3], NULL);
    results[3] = repeatedRandom(nums, s, n, 1);
    gettimeofday(&times[4], NULL);
    results[4] = repeatedRandom(nums, p, n, 0);
    gettimeofday(&times[5], NULL);
    results[5] = hillClimbing(nums, s, n, 1);
    gettimeofday(&times[6], NULL);
    results[6] = hillClimbing(nums, p, n, 0);
    gettimeofday(&times[7], NULL);
    results[7] = simulatedAnnealing(nums, s, n, 1);
    gettimeofday(&times[8], NULL);
    results[8] = simulatedAnnealing(nums, p, n, 0);
    gettimeofday(&times[9], NULL);

    // Difference between times
    int diff[9];
    for (int i = 0; i < 9; i++) {
        diff[i] = (int) ((1000000 * (times[i + 1].tv_sec - times[i].tv_sec) + (times[i + 1].tv_usec - times[i].tv_usec)) / 1000);
    }

    // Writing data and times to file
    FILE* f = fopen("data/data.csv", "a");
    if (f == NULL) {
        printf("Error opening file\n");
        return;
    }
    fprintf(f, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld\n", results[0], results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8]);
    fclose(f);

    f = fopen("data/times.csv", "a");
    if (f == NULL) {
        printf("Error opening file\n");
        return;
    }
    fprintf(f, "%i,%i,%i,%i,%i,%i,%i,%i,%i\n", diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7], diff[8]);
    fclose(f);

    // Free all allocated pointers
    free(nums);
    free(s);
    free(p);
}