[galib] Re: How to define a population based objective function?-Solution:)

RPrasad raghavendraprasad.nl at gmail.com
Wed Sep 28 23:26:47 EDT 2005


Hi Perone ,
 Thanks for your reply. But I think that is an genome based objective
function.
Anyhow I got the solution for population based objective function. Posting
my solution here.
Hope this will help others. Hope I am correct!!

#include <iostream>
#include<ga/GAPopulation.h>
#include <ga/GASStateGA.h>
#include <ga/GA2DBinStrGenome.h>

using namespace std;
void popEvaluator(GAPopulation & p);

int main()
{

int width=32;
int height=9;
GARandomBit();
GA2DBinaryStringGenome genome(width, height); //Note: No objective function
here
GAPopulation pop(genome,15); // Create a population object
pop.evaluator(popEvaluator); // Population evaluator
GASteadyStateGA ga(pop); // Attach population object to ga
ga.populationSize(10);
ga.initialize();
return 0;

}

void popEvaluator(GAPopulation & p)
{
int value=0;
for(int i=0; i<p.size(); i++)
{
GA2DBinaryStringGenome &genome=(GA2DBinaryStringGenome &)p.individual(i);
for(int j=0;j<genome.height();j++)
{
for(int k=0;k<genome.width();k++)
{
if(genome.gene(j,k)==0)
{
value--; // Evaluate objective score

}
else
{
value++;
}

}

}
p.individual(i).score(value); // Set objective score to the individual
cout<<"Fitness :"<<p.individual(i).score()<<endl;
cout<<p.individual(i)<<endl;
}
}

Thanks N Regards
Raghavendra Prasad NL
India
 On 9/29/05, galib-request at mit.edu <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. How to define a population based objective function? (RPrasad)
> 2. Re: How to define a population based objective function?
> (Christian Samuel Perone)
>
>
> ----------------------------------------------------------------------
>
> Date: Wed, 28 Sep 2005 11:05:49 +0530
> From: RPrasad <raghavendraprasad.nl at gmail.com>
> To: galib at mit.edu
> Subject: [galib] How to define a population based objective function?
> Message-ID: <602605e105092722356a3b15b6 at mail.gmail.com>
> Content-Type: multipart/alternative;
> boundary="----=_Part_1590_21226373.1127885749295"
> MIME-Version: 1.0
> Precedence: list
> Reply-To: RPrasad <raghavendraprasad.nl at gmail.com>
> Message: 1
>
> ------=_Part_1590_21226373.1127885749295
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: quoted-printable
> Content-Disposition: inline
>
> Hello friends,
> I am working on a research project.
> I have a genome type GA2DBinaryStringGenome genome(1, n); //Specific
> reaso=
> n
> to use this kind of genome
> For n=3D5, each genome will look like this
> 1
> 0
> 0
> 1
> 1
> GASteadyStateGA ga(genome); // creating genetic algorithm object
> ga.populationSize(5); // I want to invoke the population based objective
> function at this point
> Now the population will look something like this
> 10101
> 01101
> 01001
> 10110
> 00110
> Now I want to define a objective function for this population. How to
> create a population-based objective function?
> Please send me any code that might give a insight into this.
> Thanks N Regards
> Raghavendra Prasad NL
> RaghavendraPrasad.NL at gmail.com
>
> ------=_Part_1590_21226373.1127885749295
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: quoted-printable
> Content-Disposition: inline
>
> <br clear=3D"all">
> <div>Hello friends,</div>
> <div>I am working on a research project.</div>
> <div>&nbsp;</div>
> <div>I have a genome type&nbsp; GA2DBinaryStringGenome genome(1, n);&nbsp;
> =
> //Specific reason to use this kind of genome</div>
> <div>For n=3D5, each genome will look like this </div>
> <div>1</div>
> <div>0</div>
> <div>0</div>
> <div>1</div>
> <div>1</div>
> <div>GASteadyStateGA ga(genome);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // creating
> =
> genetic algorithm object</div>
> <div>ga.populationSize
> (5);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
>
> nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
> p; // I want to invoke the population based objective function at this
> poin=
> t</div>
> <div>&nbsp;</div>
> <div>Now the population will look something like this</div>
> <div>&nbsp;</div>
> <div>
> <div>10101</div>
> <div>01101</div>
> <div>01001</div>
> <div>10110</div>
> <div>00110</div>
> <div>&nbsp;</div>
> <div>Now&nbsp;I want to define a objective function for this
> population.&nb=
> sp;&nbsp;How to create a population-based objective function? </div>
> <div>Please send me any code that might give a insight into this.</div>
> <div>&nbsp;</div>
> <div>&nbsp;</div>
> <div>Thanks&nbsp;N Regards</div>
> <div>Raghavendra Prasad NL</div>
> <div><a onclick=3D"return top.js.OpenExtLink(window,event,this)"
> href=3D"ma=
> ilto:RaghavendraPrasad.NL at gmail.com" target=3D"_blank">
> RaghavendraPrasad.NL=
> @gmail.com <http://gmail.com></a></div></div>
>
> ------=_Part_1590_21226373.1127885749295--
> ------------------------------
>
> Date: Wed, 28 Sep 2005 10:49:40 -0300 (ART)
> From: Christian Samuel Perone <christian_perone at yahoo.com.br>
> To: galib at mit.edu
> Subject: Re: [galib] How to define a population based objective function?
> Message-ID: <20050928134940.95927.qmail at web31905.mail.mud.yahoo.com>
> In-Reply-To: <602605e105092722356a3b15b6 at mail.gmail.com>
> Content-Type: text/plain; charset=iso-8859-1
> MIME-Version: 1.0
> Content-Transfer-Encoding: 8bit
> Precedence: list
> Message: 2
>
> Hello,
> You can define the Objective function first
> declaring the function like this:
>
> float Objective(GAGenome& g) {
> GA2DBinaryStringGenome & genome =
> (GA2DBinaryStringGenome &)g;
> float score=0.0;
> int count=0;
> for(int i=0; i<genome.width(); i++){
> for(int j=0; j<genome.height(); j++){
> if(genome.gene(i,j) == 0 && count%2 == 0)
> score += 1.0;
> if(genome.gene(i,j) == 1 && count%2 != 0)
> score += 1.0;
> count++;
> }
> }
> return score;
> }
>
> then in the declaration of the genome you
> define like this:
>
> GA2DBinaryStringGenome genome(width, height,
> objective);
>
> Read the ex1.C in the examples directory of default
> galib package.
>
> I hope this help you.
>
> - Christian S. Perone
>
> --- RPrasad <raghavendraprasad.nl at gmail.com> escreveu:
>
> > Hello friends,
> > I am working on a research project.
> > I have a genome type GA2DBinaryStringGenome
> > genome(1, n); //Specific reason
> > to use this kind of genome
> > For n=5, each genome will look like this
> > 1
> > 0
> > 0
> > 1
> > 1
> > GASteadyStateGA ga(genome); // creating genetic
> > algorithm object
> > ga.populationSize(5); // I want to invoke the
> > population based objective
> > function at this point
> > Now the population will look something like this
> > 10101
> > 01101
> > 01001
> > 10110
> > 00110
> > Now I want to define a objective function for this
> > population. How to
> > create a population-based objective function?
> > Please send me any code that might give a insight
> > into this.
> > Thanks N Regards
> > Raghavendra Prasad NL
> > RaghavendraPrasad.NL at gmail.com
> > > _______________________________________________
> > galib mailing list
> > galib at mit.edu
> > http://mailman.mit.edu/mailman/listinfo/galib
> >
>
>
>
>
>
>
>
>
>
> _______________________________________________________
> Novo Yahoo! Messenger com voz: ligações, Yahoo! Avatars, novos emoticons e
> muito mais. Instale agora!
> www.yahoo.com.br/messenger/ <http://www.yahoo.com.br/messenger/>
> ------------------------------
>
> _______________________________________________
> galib mailing list
> galib at mit.edu
> http://mailman.mit.edu/mailman/listinfo/galib
>
>
> End of galib Digest, Vol 27, Issue 5
> ************************************
>



--
Regards
Raghavendra Prasad NL
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20050929/dc66c4e6/attachment.htm


More information about the galib mailing list