[galib] THE OBJECTIVE FUNCTION TYPE

Adam Nohejl adam at nohejl.name
Fri Jan 29 04:44:44 EST 2010


2010/01/28 v 11:27, Oly Mo:

> Hello,
> 
> Please I have sent email previously concerning changing the type of the objective function from float to double  but i didn't get reply , I am not familiar with Galib and I hope , what I have done I have tried to change the type of the variables  "score" and "fitnesse" and the type of the functions "evaluate" and " evaluator" in the Genome.h and Genome.cpp
> put this didn't solve the problem so to whome which are familiar with galib and c++  and want to encourage and give push to beginners ,please which variables and in which files i have to make this change .

Hello Oly,

it looks like that what you did was in the right direction, but there is still something you have left out. The problem is, that fitness and the related types should have been defined as a typedef (or perhaps even a class) from the beginning. Because it was not, there is no simple solution.

Before I get further, I wonder what do you need the increased precision for? As far as I can tell float is a good fitness type for almost any application.

What might help you discover the problem is in fact designing your own class with overloaded arithmetic operators and changing the fitness floats to that class. Just make sure that it has a constructor with the "explicit" keyword and no overloaded typecast to force explicit assignments to and from numeric values. If you do not know how to write such a class, you'll almost certainly find it as an example of overloading in a C++ textbook. It will look similar to this:

class MyFitness{
 double  x;
public:
 MyFitness(double x1) : x(x1){} // you will want ">explicit< MyFitness(x1) : ..." instead
 operator double() const{ return x; } // this a type cast operator, you'll want an explicit method like double "getValue() const{ return x; }" instead
 friend MyFitness operator+(const MyFitness& m, const MyFitness& n);
 /* other overloaded operators ... */
};

inline MyFitness operator+(const MyFitness& m, const MyFitness& n){ return MyFitness(m.x + n.x); }
/* other overloaded operators ... */ 

Now, when you do that and use MyFitness instead of the floats (where you know floats are used for fitness values), you will get an "incompatible types in assignment" or some such error when the new class gets assigned to a float or double or vice versa somewhere. Once you eliminate these errors by

(1) changing the floats in other parts of source code to MyFitness, when the floats are used for fitness,
(2) using the MyFitness::getValue() method to convert to double when appropriate (for use in math functions or output),
(3) using the MyFitness() constructor to convert from double when appropriate.

Once you are finished, you can replace the MyFitness class with a simple typedef to double, and remove the getValue() method calls (or replace them with a macro/inline function that just returns its argument, if you want to keep it general).

I hope this helps,

-- 
Adam




More information about the galib mailing list