[galib] CONSTRAINS DEFINITION
Markku Laukkanen
markku at ailabsolutions.com
Mon Nov 10 02:24:44 EST 2003
On Sun, 9 Nov 2003, Carlos Andres wrote:
> Hi everyone, I saw the example 9 that comes witth galib, and I need to do something similar, my problem is that in this example they uses constrains that have only one variable and I need to represent constrains that uses two or more variables, like this:
>
> X1+6*X2-13*X3 = 20
>
> How can I do that?
>
> using the GABin2DecPhenotype?
>
> Please be the clearest as you can, Im new. Thanks
>
> Carlos
>
>
>
Not really a nice solution, but works maybe like you want
PKY
-------------- next part --------------
/* ----------------------------------------------------------------------------
ex9.C
mbwall 10apr95
Copyright 1995-1996 Massachusetts Institute of Technology
DESCRIPTION:
Sample program that illustrates how to use a GA to find the maximum value
of a continuous function in two variables. This program uses a binary-to-
decimal genome.
---------------------------------------------------------------------------- */
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <ga/ga.h>
float objective(GAGenome &);
int
main(int argc, char **argv)
{
/*
This program tries to calculate
X1+6*X2-13*X3 - 20 = 0;
*/
// See if we've been given a seed to use (for testing purposes). When you
// specify a random seed, the evolution will be exactly the same each time
// you use that seed number.
unsigned int seed = 0;
for(int i=1; i<argc; i++) {
if(strcmp(argv[i++],"seed") == 0) {
seed = atoi(argv[i]);
}
}
// Declare variables for the GA parameters and set them to some default values.
int popsize = 1000;
int ngen = 100;
float pmut = 0.1;
float pcross = 0.6;
// Create a phenotype for two variables. The number of bits you can use to
// represent any number is limited by the type of computer you are using. In
// this case, we use 16 bits to represent a floating point number whose value
// can range from -5 to 5, inclusive. The bounds on x1 and x2 can be applied
// here and/or in the objective function.
GABin2DecPhenotype map;
map.add(16, -5, 5);
map.add(16, -5, 5);
map.add(16, -5, 5);
// Create the template genome using the phenotype map we just made.
GABin2DecGenome genome(map, objective);
// Now create the GA using the genome and run it. We'll use sigma truncation
// scaling so that we can handle negative objective scores.
GASimpleGA ga(genome);
GASigmaTruncationScaling scaling;
ga.populationSize(popsize);
ga.nGenerations(ngen);
ga.pMutation(pmut);
ga.pCrossover(pcross);
ga.scaling(scaling);
ga.scoreFilename("bog.dat");
ga.scoreFrequency(10);
ga.flushFrequency(50);
ga.evolve(seed);
// Dump the results of the GA to the screen.
genome = ga.statistics().bestIndividual();
cout << "the ga found an optimum at the point (";
cout << genome.phenotype(0) << ", " << genome.phenotype(1) << ", " << genome.phenotype(2) << ")\n\n";
cout << "the value is " << genome.phenotype(0) + 6 * genome.phenotype(1) - 13 * genome.phenotype(2) - 20 << endl;
cout << "best of generation data are in '" << ga.scoreFilename() << "'\n";
return 0;
}
// This objective function tries to maximize the value of the function
//
// y = -(x1*x1 + x2*x2)
//
float
objective(GAGenome & c)
{
GABin2DecGenome & genome = (GABin2DecGenome &)c;
/* So the function was X1+6*X2-13*X3 - 20 = 0;*/
float y = -20;
y += genome.phenotype(0);
y += 6 * genome.phenotype(1);
y -= 13 * genome.phenotype(2);
if (y == 0) {
cout << "Exact match = " << genome.phenotype(0) << ", " << genome.phenotype(1) << ", " << genome.phenotype(2) << ")\n\n";
cout << "the value is " << genome.phenotype(0) + 6 * genome.phenotype(1) - 13 * genome.phenotype(2) - 20 << endl;
exit(0);
} else
return 1 / y;
}
More information about the galib
mailing list