next up previous
Next: Extending genetic operators Up: Extending GAGS Previous: Extending GAGS

Extending views

If you want to use views properly, the same interface should be kept, which means that the envelope class will stay more or less the same, except for new constructors, and only a new ``letter'' will be created. To do so, several steps have to be taken. We will build a Travelling Salesman view, in which there are tex2html_wrap_inline272 size genes, for n cities; and they are represented as letters, being a the first city, and so on.

  1. Add a new constructor to the View class. This constructors should have different calling structure than the others; if it does not, an enum will have to be created to differentiate it from the constructors for other objects with the same calling convention. The declaration will be included in gagsview.hpp, and the definition in gagsview.icc, which includes all the template definitions. Once the name of the view is decided, the constructor includes a forward to the constructor of the derived class, and a check.
    template<class Type>
    view<Type>::view( unsigned \_numCities ) { 
      inView = new TSPView(  \_numCities );
      check( inView );
    In this case, TSPView is not itself a template, since it will always return characters; usually, Type will be used for the construction of the letter object.
  2. Declaration and definition of TSPView itself. The public virtual functions operator () and size have to be redefined; the first one will return the ith gene, and the other the total number of genes. Take into account that only methods in the base class can access private parts of the chromosome, so that usually these functions will forward request to the base class. In this case, it access the getValue protected method, that in turn accesses the internal representation of the chromosome. Since the base and envelope class has only a pointer to the letter class, its default constructor is called.
  3. Testing the new view. Insert an object of that new class in the program, which tests view objects, compile and run it.
#include "gagsview.hpp"
main () {
    const unsigned NUMGENES = 5;
    const bitLength_t SIZEGENES = 8;
    chrom aChrom( NUMGENES*SIZEGENES ); 
    view<unsigned long> vista( SIZEGENES, -1, 2 );
    for ( unsigned i = 0; i < vista.size(&aChrom); i ++ ) {
        cout << vista( &aChrom, i) << "_";
    cout << endl;

    const unsigned numCities = 8;
    const unsigned numBits = 3;
    chrom anotherChrom( numCities * numBits );

    view<char> TSPvista( numCities );
    for ( unsigned i = 0; i < TSPvista.size(&anotherChrom); i ++ ) {
        cout << TSPvista( &anotherChrom, i) << "_";
    cout << endl;

next up previous
Next: Extending genetic operators Up: Extending GAGS Previous: Extending GAGS

J.J. Merelo Guervos
Fri Aug 22 12:58:28 MDT 1997