how to create a genome with mixed discrete/continuous variables

Matthew Wall mbwall_NO_SPAM at
Wed Feb 12 11:20:44 EST 1997

Sunand Sandurkar of Clemson University asked how to create a single genome
that contains a mix of continuous and discrete variables.

> I have essentially modified the example (ex21.C) for my application.
> It fits well except that I am unable to incorporate all that I need
> in a single program. I have listed the details of my requirements below.
> 1. A set of real variables that are continuous in a given range.
> 2. A set of real variables that are continuous in a different range.
> 3. A set of integer varibles that are discretely generated
> from an enumerated list of alleles.
> I have a single objective function that must accept values
> for all the above variables and return the value of the objective.

Here's what to do.  First make your allele sets, then put those together
into an array of allele sets, then create the genome using the array.  Then
you'll end up with a genome that has mixed discrete and continuous
components to it.  Be sure to extract pieces properly in your objective
function, and you might want to try different crossovers and mutations for
the different parts of the genome.

To create the genome, do something like this:

  GARealAlleleSet set1(0,10);          // set of real numbers in [0,10]
  GARealAlleleSet set2(5,25.3,GAAllele::INCLUSIVE,GAAllele::EXCLUSIVE);
                                       // set of real numbers in [5,25.3)
  GARealAlleleSet set3(-5.2,-1.001);   // set of real numbers in [-5.2,-1.001]

  GARealAlleleSet set4(1,100,0.5);     // {1,1.5,2,2.5,3,...100}

  GARealAlleleSet set5;                // {1.0,6.0,22.0}

  GARealAlleleSetArray sets;  // put the sets into an array to make it easier
  sets.add(set1);             // to create the genome.

  GARealGenome genome(sets, objective);

This will create a genome with 5 elements.  Each element will be controlled
by the properties you defined in the 5 allele sets.

To use the genome in your objective function, do this:

objective(GAGenome &g) {
  GARealGenome genome = (GARealGenome&)g;
  float score = 0;

  float x = genome.gene(0);    // x is in [0,10]
  float y = genome.gene(1);    // y is in [5,25.3)
  float z = genome.gene(2);    // z is in set 3
  float r = genome.gene(3);    // r is in set 4
  float s = genome.gene(4);    // s is in set 5

  // rest of objective function goes here

  return score;

That should do it for you.  There is a bug in the allele set that allows
inclusive upper bounds even when you specify exclusive.  I'm working on the
patch and hope to release GAlib 2.4.3 with the fix soon.

Thank you Dr. Ralf Seliger for pointing out the allele set bug.


Matthew Wall
mbwall_NO_SPAM at

More information about the galib mailing list