Language Reference


GASETUP Function

GASETUP (encoding, size <, seed> );

The GASETUP function sets up the problem encoding for a genetic algorithm optimization problem. The GASETUP function returns a scalar number that identifies the genetic algorithm optimization problem. This number is used in subsequent calls to carry out the optimization.

The arguments to the GASETUP function are as follows:

encoding

is a scalar number used to specify the form or structure of the problem solutions to be optimized. A value of 0 indicates a numeric matrix of arbitrary dimensions, 1 indicates a fixed-length floating-point row vector, 2 indicates a fixed-length integer row vector, and 3 indicates a fixed-length sequence of integers, with alternate solutions distinguished by different sequence ordering.

size

is a numeric scalar, whose value is the vector or sequence length, if a fixed-length encoding is specified. For arbitrary matrix encoding (encoding value of 0), size is not used.

seed

is an optional initial random number seed to be used for the initialization and the selection process. If seed is not specified or its value is 0, an initial seed is derived from the current system time.

GASETUP is the first call that must be made to set up a genetic algorithm optimization problem. It specifies the problem encoding, the size of a population member, and an optional seed that initializes the random number generator used in the selection process. GASETUP returns an identifying number that must be passed to the other modules that specify genetic operators and control the execution of the genetic algorithm. More than one optimization can be active concurrently, and optimization problems with different problem identifiers are completely independent. When a satisfactory solution has been determined, the optimization problem should be terminated with a GAEND call to free up resources associated with the genetic algorithm.

The following example demonstrates the use of several genetic algorithm subroutines:

/* Use a genetic algorithm to explore the solution space for the
   "traveling salesman" problem. First, define the objective
   function to minimize:
   Compute the sum of distances between sequence of cities */
start EvalFitness( pop ) global ( dist );
   fitness = j( nrow(pop),1 );
   do i = 1 to nrow(pop);
      city1 = pop[i,1];
      city2 = pop[i,ncol(pop)];
      fitness[i] = dist[ city1, city2 ];
      do j = 1 to ncol(pop)-1;
         city1 = pop[i,j];
         city2 = pop[i,j+1];
         fitness[i] = fitness[i] + dist[city1,city2];
      end;
   end;
   return ( fitness );
finish;

/* Set up parameters for the genetic algorithm */

mutationProb = 0.15;   /* prob that a child will be mutated */
numElite = 2;          /* copy this many to next generation */
numCities = 15;        /* number of cities to visit */
numGenerations = 100;  /* number of generations to evolve */
seed = 54321;          /* random number seed */

/* fix population size; generate random locations for cities */
popSize = max(30,2*numCities);
locations = uniform( j(numCities,2,seed) );

/* compute distances between cities one time */
dist = j( numCities, numCities, 0 );
do i = 1 to numCities;
   do j = 1 to i-1;
      v = locations[i,]-locations[j,];
      dist[i,j] = sqrt( v[##] );
      dist[j,i] = dist[i,j];
   end;
end;

/* run the genetic algorithm */
id = gasetup( 3, numCities, seed);
call gasetobj(id, 0, "EvalFitness" );
call gasetcro(id, 1.0, 6);
call gasetmut(id, mutationProb, 3);
call gasetsel(id, numElite, 1, 0.95);
call gainit(id, popSize );

do i = 1 to numGenerations;
   if mod(i,20)=0 then do;
      call gagetval( value, id, 1 );
      print "Iteration:" i "Top value:" value;
   end;
   call garegen(id);
end;

/* report final sequence for cities */
call gagetmem(mem, value, id, 1);
print mem, value;
call gaend(id);

Figure 24.143: Result of a Genetic Algorithm Optimization

  i   value
Iteration: 20 Top value: 3.6836569

  i   value
Iteration: 40 Top value: 3.5567152

  i   value
Iteration: 60 Top value: 3.4562136

  i   value
Iteration: 80 Top value: 3.4562136

  i   value
Iteration: 100 Top value: 3.437183

mem
  COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 COL12 COL13 COL14 COL15
ROW1 6 4 12 7 13 15 8 9 11 5 2 14 10 3 1

value
3.437183