We are about to switch to a new forum software. Until then we have removed the registration on this forum.
You can try out this simple optimization algorithm if you like:
// Continuous Gray code optimization
//http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=14BEBFED079E983F49AD75C3CA4AFF82?doi=10.1.1.100.6090&rep=rep1&type=pdf
// The most simple way to evolve solutions to engineering or other problems.
// And effective too.
void setup() {
int iterations=100000;
int precision=1000;
int vecLen=50;
float range=5.12f;
float[] parent=new float[vecLen];
float[] child=new float[vecLen];
for (int i=0; i<parent.length; i++) {
parent[i]=random(-range, range); // random from -5.12 to 5.12
}
float parentCost=deYong(parent);
for (int i=0; i<iterations; i++) {
for (int j=0; j<parent.length; j++) {
float mutation=exp(-precision*random(0f, 1f));
if (random(-1f, 1f)<0) {
mutation=-mutation;
}
mutation*=2*range;
float c=parent[j]+mutation;
if (c>range) c=parent[j];
if (c<-range) c=parent[j];
child[j]=c;
}
float childCost=deYong(child);
if (childCost<parentCost) {
parentCost=childCost;
for (int j=0; j<parent.length; j++) {
parent[j]=child[j];
}
}
}
println("Parent Cost:"+parentCost);
for(int i=0;i<parent.length;i++){
println("Vector: "+parent[i]);
}
}
float deYong(float[] x){
float result=0f;
for(int i=0;i<x.length;i++){
result+=x[i]*x[i];
}
return result;
}
Comments
More of the same:
Thanks!
Is it a genetic algorithm?
It is related to genetic algorithms: It's called an evolutionary strategy. However it doesn't use the Gaussian distribution for mutation as most of them do: https://blog.openai.com/evolution-strategies/ To use the mutation operations you choose to have all the parameters to the function you want to optimized to be either between 0 and 1 and use the mutateX function, or to be between -1 and 1 and use the mutateXSym function. The only control parameter to choose is the precision. Maybe pick 30 for simple problems, 100 for more complex problems etc.
For more complex problems you can add a grandparent. Basically you are adding local (or not so local) restarts to the algorithm. Helping move it out of local minimum it could get trapped in.