示例#1
0
int main() {
  double result;
  double bestsofar = -999;
  double bestA[10];
  double ss[10];
  int i;
  while(result < 9 && numeval <= NUMEVAL) {
    srand(time(NULL));
    result = geneticAlgorithm(ss);
    if(result > bestsofar) {
      bestsofar = result;
      memcpy(bestA,ss,sizeof(double)*10);
    }
    printf("\nBestSoFarGeneral: %lf No. Eval: %d \n", bestsofar,numeval);
  }

  while(result < 10 && numeval <= NUMEVAL) {
    srand(time(NULL));
    result = simulatedAnnealing(300000,ss);
    if(result > bestsofar) {
      bestsofar = result;
      memcpy(bestA,ss,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);
  }  
}
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;  
}
示例#3
0
int main(){
  double result;
  double bestsofar = -999;
  double bestA[10];
  double ss[10];
  int i;
  int k;
  while(result < 9 && numeval <= NUMEVAL){
    result = geneticAlgorithm(ss);
    if(result > bestsofar) {
      bestsofar = result;
      memcpy(bestA,ss,sizeof(double)*10);
    }
    printf("\nBestSoFarGeneral: %lf No. Eval: %d\n", bestsofar, numeval);
    for(k = 0; k<10;k++){
      printf("\nBSF[%d]: %lf",k,bestA[k]);
    }
  }
}