Writing code for Genetic Algorithm
Has anybody ever written java externals for implementing a genetic algorithm. I need these externals for an installation I am working on, and I am having some difficulty with regards to coding this algorithm in Java. Any help would be great.
If you're having difficulty with the algorithm, I recommend this book: http://www.amazon.com/Techniques-Programming-Premier-Press-Development/dp/193184108X/ref=pd_sim_b_3
If I remember correctly, it walks you through the an example of pathfinding using a GA. The impression I was left with is that GAs take a lot of tweaking to find the right blend of encoding, population size, crossover and fitness functions.
Hi,
if you're still looking: I implemented GA some years ago and had a lot of help from the itp nyu classes by daniel shiffman (it's in processing):
Hi,
this algorithm is pretty nice and I suppose that there are many problems that can be solved by it, for optimization problems where the starting space is huge this method is one to be choose, also for evaluation of a function where this as many parameters, as an example I attached what I just coded, my first application that use this algorithm, very important is to adjust the parameters: size of initial population, chromosome crossover, mutation probability:
#include <vector>
#include <iostream>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#define SIZE 10000
using namespace std;
double f(double x, double y) {
return 2 - x * x - y * y;
}
vector<pair<double, double>> POPULATION;
vector<pair<double, double>> PARENTS;
vector<double> FITNESS;
void initialization() {
srand(time(NULL));
for(int i = 0; i < SIZE; i++)
POPULATION.push_back(pair<double, double>(-1.0 + 2.0 * rand() / RAND_MAX, -1.0 + 2.0 * rand() / RAND_MAX));
}
void fitness() {
FITNESS.clear();
for(int i = 0; i < SIZE; i++)
FITNESS.push_back(f(POPULATION[i].first, POPULATION[i].second));
}
double fittest() {
double maximum = FITNESS[0];
for(int i = 1; i < SIZE; i++) {
if(FITNESS[i] > maximum)
maximum = FITNESS[i];
}
return maximum;
}
void print() {
#ifdef PRINT_POPULATION
for(int i = 0; i < SIZE - 1; i++)
cout << "(" << POPULATION[i].first << " ," << POPULATION[i].second << "), " << endl;
cout << "(" << POPULATION[SIZE - 1].first << " ," << POPULATION[SIZE - 1].second << ")";
cout << endl;
#endif
#ifdef PRINT_FITNESS
for(int i = 0; i < SIZE - 1; i++)
cout << FITNESS[i] << ", ";
cout << FITNESS[SIZE - 1];
cout << endl;
#endif
cout <<fittest() << endl;
}
void parents() {
double minimum, maximum;
minimum = maximum = FITNESS[0];
for(int i = 1; i < SIZE; i++) {
if(FITNESS[i] < minimum)
minimum = FITNESS[i];
if(FITNESS[i] > maximum)
maximum = FITNESS[i];
}
PARENTS.clear();
double threshold = minimum + 0.5 * (maximum - minimum);
for(int i = 0; i < SIZE - 1; i++)
if(FITNESS[i] >= threshold)
PARENTS.push_back(POPULATION[i]);
}
pair<double, double> crossover(pair<double, double> first, pair<double, double> second) {
pair<double, double> offspring;
offspring.first = first.first;
offspring.second = second.second;
return offspring;
}
void generation() {
POPULATION.clear();
for(int i = 0; i < SIZE; i++) {
int parents = PARENTS.size();
int first = rand() % parents;
int second = rand() % parents;
int random = rand();
pair<double, double> offspring = crossover(PARENTS[first], PARENTS[second]);
bool mutation = (random < 500);
if(mutation) {
if(mutation % 2) offspring.first = -1.0 + 2.0 * rand() / RAND_MAX;
else offspring.second = -1.0 + 2.0 * rand() / RAND_MAX;
}
POPULATION.push_back(offspring);
}
}
int main()
{
initialization();
fitness();
parents();
print();
double stop = fittest(), start;
do{
start = stop;
generation();
fitness();
parents();
print();
stop = fittest();
}
while (abs(start - stop) > 0.000001);
return 0;
}