[galib] Re: Obtain "bestIndividual()" value in other genome types

Mert Tas mert_tas at yahoo.com
Thu Nov 11 04:25:37 EST 2004


Hi,
Well your solution seems to work fine in GARealGenome case. But I suppose you can not use the same approach on other genome types. I am using GABinaryStringGenome
and you cannot assign the bestIndividual() value that easily since it has no  gene() operator. What I did here is, as stated before, I used the file stream operators to get its value. Anyone has a better idea? 
 
Greetings,
Mert

galib-request at mit.edu wrote:
Send galib mailing list submissions to
galib at mit.edu

To subscribe or unsubscribe via the World Wide Web, visit
http://mailman.mit.edu/mailman/listinfo/galib
or, via email, send a message with subject or body 'help' to
galib-request at mit.edu

You can reach the person managing the list at
galib-owner at mit.edu

When replying, please edit your Subject line so it is more specific
than "Re: Contents of galib digest..."
Today's Topics:

1. Re: Obtain "bestIndividual()" value (Mert Tas)
2. Re: Re: Obtain "bestIndividual()" value (Luca Moscatelli)
3. Re: Obtain "bestIndividual()" value (gio sarto)
4. Re: Obtain "bestIndividual()" value (gio sarto)


> ATTACHMENT part 3.1 message/rfc822 
Date: Mon, 8 Nov 2004 23:37:41 -0800 (PST)
From: Mert Tas 
To: galib at mit.edu
Subject: [galib] Re: Obtain "bestIndividual()" value 


Unfortunately, it is not a quite simple problem. Since bestIndividual is of type GaGenome& you could not access its value directly. I solved this problem by first printing the bestIndividual value to a file, then reading its value from that file and assigning it to a new variable.

One way is to use file stream operators as follows:

ofstream fout;

fout.open("bestInd.txt", ios::in);

if (!fout.fail()){

fout << ga.statistics().bestIndividual() ;

fout.close();

}

ifstream fin;

fin.open("bestInd.txt", ios::out);

if (!fin.fail()){

float bestInd,

fin >> bestInd; // read the value from file and write it to bestInd

fin.close();

}

Hope this helps ...


Today's Topics:

1. Obtain "bestIndividual()" value (Luca Moscatelli)
2. Obtain "bestIndividual()" value (Luca Moscatelli)


> ATTACHMENT part 3.1 message/rfc822 
Date: Tue, 2 Nov 2004 18:36:29 +0100
From: Luca Moscatelli 
To: galib at mit.edu
Subject: [galib] Obtain "bestIndividual()" value

Hi to all.

I'm new with this library and I have a simple problem (I hope it's
simple... However, it is quite stupid :D).

I tried the first examples and they are all clear, but I saw that the
best individual is only printed at the end of each program, but what
have I to do if I want to use it?

I'll try to be more clear:
I created a GARealAlleleSet, filled with some values and inserted into
the GARealGenome, then I evolved it.
If I print the best individual like in the examples with "cout <<
ga.statistics().bestIndividual() I see the right value, but now I
want, for example, t! o add 1 to this value...

Now, I looked the documentation and I saw that the
ga.statistics().bestIndividual() returns a GaGenome&, how can the cout
print it?
And, how can I put this value(s) in a float variable(s)?

I hope that my problem is quite clear to you... :)

Thanks in advance to all of you!
Bye,
Luca




---------------------------------
Do you Yahoo!?
Check out the new Yahoo! Front Page. www.yahoo.com

> ATTACHMENT part 3.2 message/rfc822 
Date: Tue, 9 Nov 2004 19:27:18 +0100
From: Luca Moscatelli 
To: galib at mit.edu
Subject: Re: [galib] Re: Obtain "bestIndividual()" value

Thanks,
But, with some help, I used another (and better, I suppose) solution.

Here to you:
First of all, you put the best individual into an appropriate genome
(GARealGenome in my case):

GARealGenome best = (GARealGenome &)ga.statistics().bestIndividual();

Then
float f = best.gene(n);
where 'n' is the value into the gene you are interested in...

I tried this approach and it works!
Bye,
Luca

On Mon, 8 Nov 2004 23:37:41 -0800 (PST), Mert Tas wrote:
> 
> 
> 
> 
> Unfortunately, it is not a quite simple problem. Since bestIndividual is of
> type GaGenome& you could not access its value directly. I solved this
> problem by first printing the bestIndividual value to a file, then reading
> its value from that file and assigning it to a new variable. 
> 
> One way is to use file stream operators as follows: 
> 
> ofstream fout; 
> 
> fout.open("bestInd.txt", ios::in); 
> 
> if (!fout.fail()){ 
> 
> fout << ga.statistics().bestIndividual() ; 
> 
> fout.close(); 
> 
> } 
> 
> ifstream fin; 
> 
> fin.open("bestInd.txt", ios::out); 
> 
> if (!fin.fail()){ 
> 
> float bestInd, 
> 
> fin >> bestInd; // read the value from file and write it to bestInd 
> 
> fin.close(); 
> 
> } 
> 
> Hope this helps ...


> ATTACHMENT part 3.3 message/rfc822 
Date: Tue, 9 Nov 2004 20:15:34 +0000 (GMT)
From: gio sarto 
To: Luca Moscatelli , galib at mit.edu
Subject: Re: [galib] Obtain "bestIndividual()" value

When you use GAGenome's that are a derived of
GAArray (it is the case for GARealGenome),
all you need to do is to cast it to its actual type:

// before doing this you may want to check
// that the identity is correct, use
ga.statistics().bestIndividual().classID() ==
GAID::ArrayGenome;
// or the C++ dynamic_cast< T > operator
// then cast 
GARealGenome& gr = ga.statistics().bestIndividual();

// then you can use either the ::gene(int i) method
x = gr.gene(i); // 0<= i <= gr.size()
// or the indexing operator:
x = gr[i];

// or you can even cast the genome to a pointer to
// double, and then index this directly:
double* pgenome = (double*) gr; 
x = pgenome[i];
// this holds in general for any genome calass that
// is derived from GAArray, not only for
GARealGenome.

Have a look at the header files:
GAArray.h and GA1DArrayGenome.h


Cheers

Giovanni

--- Luca Moscatelli wrote: 
> Hi to all.
> I tried the first examples and they are all clear,
> and I saw that the
> best individual is only printed at the end of each
> program, but what
> have I to do if I want to use it?
> 
> I'll try to be more clear:
> I created a GARealAlleleSet, filled and inserted
> into the
> GARealGenome, then I evolved it.
> If I print the best individual like in the examples
> with "cout <<
> ga.statistics().bestIndividual()" I see the right
> value, but now I
> want, for example, to add 1 to this value...
> 
> Now, I looked the documentation and I saw that the
> ga.statistics().bestIndividual() returns a
> GaGenome&, how can I put
> this value(s) in a float variable(s)?
> 
> Thanks in advance to all of you!
> Bye,
> Luca
> _______________________________________________
> galib mailing list
> galib at mit.edu
> http://mailman.mit.edu/mailman/listinfo/galib
> 





___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com


> ATTACHMENT part 3.4 message/rfc822 
Date: Tue, 9 Nov 2004 20:28:51 +0000 (GMT)
From: gio sarto 
To: galib at mit.edu
Subject: Re: [galib] Obtain "bestIndividual()" value

My apologies there was an error in my previous reply,
the Id is GAID::FloatGenome and not GAID::ArrayGenome

When you use GAGenome's that are a derived of
GAArray (it is the case for GARealGenome),
all you need to do is to cast it to its actual type:

// before doing this you may want to check
// that the identity is correct, use
ga.statistics().bestIndividual().classID() ==
GAID::FloatGenome;
// or the C++ dynamic_cast< T > operator
// then cast 
GARealGenome& gr = ga.statistics().bestIndividual();

// then you can use either the ::gene(int i) method
x = gr.gene(i); // 0<= i <= gr.size()
// or the indexing operator:
x = gr[i];

// or you can even cast the genome to a pointer to
// double, and then index this directly:
double* pgenome = (double*) gr; 
x = pgenome[i];
// this holds in general for any genome calass that
// is derived from GAArray, not only for
GARealGenome.

Have a look at the header files:
GAArray.h and GA1DArrayGenome.h


Cheers

Giovanni

> --- Luca Moscatelli wrote: 
> > Hi to all.
> > I tried the first examples and they are all clear,
> > and I saw that the
> > best individual is only printed at the end of each
> > program, but what
> > have I to do if I want to use it?
> > 
> > I'll try to be more clear:
> > I created a GARealAlleleSet, filled and inserted
> > into the
> > GARealGenome, then I evolved it.
> > If I print the best individual like in the
> examples
> > with "cout <<
> > ga.statistics().bestIndividual()" I see the right
> > value, but now I
> > want, for example, to add 1 to this value...
> > 
> > Now, I looked the documentation and I saw that the
> > ga.statistics().bestIndividual() returns a
> > GaGenome&, how can I put
> > this value(s) in a float variable(s)?
> > 
> > Thanks in advance to all of you!
> > Bye,
> > Luca
> > _______________________________________________
> > galib mailing list
> > galib at mit.edu
> > http://mailman.mit.edu/mailman/listinfo/galib
> > 
> 
> 
> 
> 
> 
>
___________________________________________________________ALL-NEW
> Yahoo! Messenger - all new features - even more fun!
> http://uk.messenger.yahoo.com
> 





___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
_______________________________________________
galib mailing list
galib at mit.edu
http://mailman.mit.edu/mailman/listinfo/galib

			
---------------------------------
Do you Yahoo!?
 Check out the new Yahoo! Front Page. www.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20041111/022f6761/attachment.htm


More information about the galib mailing list