/* * Input: Number of genes to generate * Output: Vector of floats * Function: Generates a random vector of floats. Length is based on input * Notes: None */ std::vector<float> GAUtils::generateGenes(int &genesNum){ std::vector<float> genes; for (int i = 0; i < genesNum; i++){ float r = randFloatGen(); genes.push_back(r); } return genes; }
int hillClimbingProb(int sol[], int **matrix, int M, int G, int iters, int swap, float prob) { int *newSol, *bestSol; int i; double rnd; int totalSol, totalNewSol, totalBestSol; newSol = (int *)malloc(M * sizeof(int)); bestSol = (int *)malloc(M * sizeof(int)); if(!newSol || !bestSol) exit(1); copySol(sol, bestSol, M); totalSol = calFitness(sol, matrix, M, G); totalBestSol = totalSol; for(i=0; i<iters; i++) { neighbouringGen(sol, newSol, M, G, swap); totalNewSol = calFitness(newSol, matrix, M, G); if(totalNewSol >= totalSol) { copySol(newSol, sol, M); totalSol = totalNewSol; } else { rnd = randFloatGen(); if (rnd < prob) { copySol(newSol, sol, M); totalSol = totalNewSol; } } if(totalBestSol < totalSol) { copySol(sol, bestSol, M); totalBestSol = totalSol; } } copySol(bestSol, sol, M); totalSol = totalBestSol; free(newSol); free(bestSol); return totalSol; }