Language Reference

GASETUP Function    -    Experimental

sets up the problem encoding for a genetic algorithm optimization problem

GASETUP( encoding, size <, seed > )

The GASETUP function returns a scalar number identifying the genetic algorithm optimization problem, to be used in subsequent calls in setting up and executing the optimization.

The inputs 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 is used to indicate a numeric matrix of arbitrary dimensions, 1 to indicate a fixed-length floating-point row vector, 2 to indicate a fixed-length integer row vector, and 3 to indicate 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 to use to initialize 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 call to GAEND 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) ); 
  
    /* calculate 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);
 

Previous Page | Next Page | Top of Page