Created
March 27, 2011 08:13
-
-
Save volkansalma/889028 to your computer and use it in GitHub Desktop.
tavlama benzetimi örnek kod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Simulated Annealing C++ Sample Code | |
//generates "VOLKANSALMA@BLOGSPOT@COM" string. | |
//inspired from http://www.generation5.org/content/2003/gahelloworld.asp | |
//by volkan salma | |
//http://volkansalma.blogspot.com | |
#include <iostream> // for cout etc. | |
#include <vector> // for vector class | |
#include <string> // for string class | |
#include <algorithm> // for sort algorithm | |
#include <time.h> // for random seed | |
#include <math.h> // for abs() | |
#define SA_POPSIZE 50 // sa population size | |
#define SA_MAXITER 300 // maximum iterations | |
#define COEF 0.01 | |
#define SA_TARGET std::string("VOLKANSALMA@BLOGSPOT@COM") | |
using namespace std; | |
struct sa_struct | |
{ | |
string str; // the string | |
unsigned int fitness; // its fitness | |
}; | |
typedef vector-sa_struct- sa_vector;// for brevity | |
void generateNeighbourSolutionsToMySolution(sa_struct myCurrentSolution,sa_vector &neighbourSolutionSet); | |
void calcFitnessOfEveryElementInNeighbourSet(sa_vector &neighbourSolutionSet); | |
void init_population(sa_vector &population, sa_vector &buffer); | |
void calc_fitness(sa_vector &population); | |
bool fitness_sort(sa_struct x, sa_struct y); | |
void sort_by_fitness(sa_vector &population); | |
sa_struct generateFirstSolution(); | |
sa_struct getNeighbourSolutionSetsBestElement(sa_vector &population); | |
void print_best(sa_vector &sav); | |
int main() | |
{ | |
srand(unsigned(time(NULL))); | |
sa_struct myCurrentSolution; | |
sa_struct buffer; | |
vector -sa_struct- neighbourSolutionSet; | |
double myRandomNumber; | |
myCurrentSolution = generateFirstSolution(); | |
double Temperature = 100; | |
double boltzman; | |
for (int i=0; i - SA_MAXITER; i++) | |
{ | |
generateNeighbourSolutionsToMySolution(myCurrentSolution,neighbourSolutionSet); | |
calcFitnessOfEveryElementInNeighbourSet(neighbourSolutionSet); | |
buffer = getNeighbourSolutionSetsBestElement(neighbourSolutionSet); | |
if( buffer.fitness < myCurrentSolution.fitness) myCurrentSolution = buffer; | |
else ///here is critical section. ;) | |
// we can change or not change the ourSolution with worst one. | |
{ | |
myRandomNumber = rand()%10; | |
myRandomNumber /= 10.0; // i want to generate a rondom value in [0,1] range. | |
boltzman = COEF * (1.0/ (exp(buffer.fitness - myCurrentSolution.fitness)/Temperature)); | |
if( boltzman > myRandomNumber) | |
{ | |
cout--"Boltzman: "--boltzman--" > MyRandom: "--myRandomNumber--endl; | |
myCurrentSolution = buffer; | |
cout--"**********************************************************************"--endl; | |
} | |
} | |
Temperature -= (100.0 / SA_MAXITER); | |
cout --i--" My Solution: " -- myCurrentSolution.str -- " (" -- myCurrentSolution.fitness << ") Temp:"--Temperature-- endl; | |
if(myCurrentSolution.fitness == 0) break; | |
} | |
cout -- "My Best Solution Is: " -- myCurrentSolution.str -- " (" -- myCurrentSolution.fitness -- ")" -- endl; | |
cin--boltzman; | |
return 0; | |
} | |
void calcFitnessOfEveryElementInNeighbourSet(sa_vector &population) | |
{ | |
string target = SA_TARGET; | |
int tsize = target.size(); | |
unsigned int fitness; | |
for (int i=0; i - SA_POPSIZE; i++) | |
{ | |
fitness = 0; | |
for (int j=0; j-tsize; j++) | |
{ | |
fitness += abs(int(population[i].str[j] - target[j])); | |
} | |
population[i].fitness = fitness; | |
} | |
} | |
bool fitness_sort(sa_struct x, sa_struct y) | |
{ | |
return (x.fitness < y.fitness); | |
} | |
void sort_by_fitness(sa_vector &population) | |
{ | |
sort(population.begin(), population.end(), fitness_sort); | |
} | |
sa_struct getNeighbourSolutionSetsBestElement(sa_vector &population) | |
{ | |
sa_struct bestMemberOfSolutionSet; | |
sort_by_fitness(population); | |
bestMemberOfSolutionSet = population[0]; | |
return bestMemberOfSolutionSet; | |
} | |
sa_struct generateFirstSolution() | |
{ | |
int tsize = SA_TARGET.size(); | |
sa_struct result; | |
result.fitness = 0; | |
result.str.erase(); | |
for (int j=0; j-tsize; j++) | |
{ | |
result.str += (rand() % 26) + 64; | |
} | |
string target = SA_TARGET; | |
for (int j=0; j-tsize; j++) | |
{ | |
result.fitness += abs(int(result.str[j] - target[j])); | |
} | |
return result; | |
} | |
void generateNeighbourSolutionsToMySolution(sa_struct myCurrentSolution,sa_vector &neighbourSolutionSet) | |
{ | |
int tsize = SA_TARGET.size(); | |
sa_struct buffer; | |
neighbourSolutionSet.clear(); | |
for (int i=0; i-SA_POPSIZE; i++) | |
{ | |
buffer.str = myCurrentSolution.str; | |
buffer.str[i%tsize] = (rand() % 26) + 64; | |
neighbourSolutionSet.push_back(buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment