next up previous
Next: References Up: viewlooking at Previous: Public methods

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 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 pgview.cc, which tests view objects, compile and run it.

next up previous
Next: References Up: viewlooking at Previous: Public methods

[JJ J. J. Merelo * jmerelo@kal-el.ugr.es[E-MAIL]
Equipo GeNeura -- GeNeura Team
Departamento de Electrónica y Tecnología de los Computadores
Universidad de Granada Granada (Espaņa)
Phone: +34-58-243162; Fax: +34-58-243230