<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: The GA doesn't work</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>You are initialising all your genomes to "1,1,1,1,1,1" then just swapping them about with the SwapMutator. There is nothing happening to change these 1's into 2's, 3's etc.</FONT></P>
<P><FONT SIZE=2>-----------</FONT>
<BR><FONT SIZE=2> Allan</FONT>
</P>
<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: agna@brawijaya.ac.id [<A HREF="mailto:agna@brawijaya.ac.id">mailto:agna@brawijaya.ac.id</A>] </FONT>
<BR><FONT SIZE=2>Sent: 01 March 2003 06:30</FONT>
<BR><FONT SIZE=2>To: galib@mit.edu</FONT>
<BR><FONT SIZE=2>Subject: The GA doesn't work</FONT>
</P>
<BR>
<P><FONT SIZE=2>Dear Sir/Madar, </FONT>
</P>
<P><FONT SIZE=2>I am new user for GAlib.</FONT>
<BR><FONT SIZE=2>I've tried to instantiate a genome from GA1DArrayGenome<int>. Following this email is my own simple source. No problem with the program, but the GA does nothing. The final genome is the same as the initial genome. What's wrong ? Is there anyone ready to help me ? </FONT></P>
<P><FONT SIZE=2>With best wishes</FONT>
<BR><FONT SIZE=2>A Naba</FONT>
<BR><FONT SIZE=2> -----------------------------</FONT>
<BR><FONT SIZE=2>#include <stdio.h></FONT>
<BR><FONT SIZE=2>#include <iostream.h></FONT>
<BR><FONT SIZE=2>#include <ga/ga.h> </FONT>
</P>
<P><FONT SIZE=2>float Objective(GAGenome & c);</FONT>
<BR><FONT SIZE=2>void IntArrayInitializer(GAGenome & c); </FONT>
</P>
<P><FONT SIZE=2>int main(int argc, char *argv[]){ </FONT>
</P>
<P><FONT SIZE=2> int i,len;</FONT>
<BR><FONT SIZE=2> len=6;</FONT>
<BR><FONT SIZE=2> //Create Target</FONT>
<BR><FONT SIZE=2> int target[len];</FONT>
<BR><FONT SIZE=2> for(i=0;i<len;i++)</FONT>
<BR><FONT SIZE=2> target[i] = i+1;</FONT>
</P>
<P><FONT SIZE=2> // Create genome object</FONT>
<BR><FONT SIZE=2> GA1DArrayGenome<int> genome(len,Objective,(void *)target);</FONT>
</P>
<P><FONT SIZE=2> // to ensure that the target the right one</FONT>
<BR><FONT SIZE=2> cout<< "Check Target : \n";</FONT>
<BR><FONT SIZE=2> int * initialData = (int *) genome.userData();</FONT>
<BR><FONT SIZE=2> for(i=0;i<genome.length();i++)</FONT>
<BR><FONT SIZE=2> cout << initialData[i] << " ";</FONT>
<BR><FONT SIZE=2> cout << endl;</FONT>
</P>
<P><FONT SIZE=2> genome.initializer(IntArrayInitializer);</FONT>
<BR><FONT SIZE=2> genome.mutator(GA1DArrayGenome<int>::SwapMutator);</FONT>
<BR><FONT SIZE=2> genome.crossover(GA1DArrayGenome<int>::OnePointCrossover);</FONT>
</P>
<P><FONT SIZE=2> GAParameterList params;</FONT>
<BR><FONT SIZE=2> GASteadyStateGA::registerDefaultParameters(params);</FONT>
<BR><FONT SIZE=2> params.set(gaNpopulationSize, 100);</FONT>
<BR><FONT SIZE=2> params.set(gaNpCrossover, 0.8);</FONT>
<BR><FONT SIZE=2> params.set(gaNpMutation, 0.01); // probability of mutation</FONT>
<BR><FONT SIZE=2> params.set(gaNnGenerations, 100); // number of generations</FONT>
<BR><FONT SIZE=2> params.set(gaNscoreFrequency, 5); // how often to record scores</FONT>
<BR><FONT SIZE=2> params.set(gaNflushFrequency, 5); // HOW often to flush scores</FONT>
<BR><FONT SIZE=2> params.set(gaNscoreFilename, "bog.dat");</FONT>
<BR><FONT SIZE=2> params.parse(argc, argv, gaFalse);</FONT>
</P>
<P><FONT SIZE=2> GASteadyStateGA ga(genome);</FONT>
</P>
<P><FONT SIZE=2> ga.parameters(params);</FONT>
</P>
<P><FONT SIZE=2> GARankSelector selector;</FONT>
<BR><FONT SIZE=2> ga.selector(selector);</FONT>
</P>
<P><FONT SIZE=2> ga.initialize(); // If the genome has its own initializer, do this</FONT>
<BR><FONT SIZE=2> genome = ga.statistics().bestIndividual();</FONT>
<BR><FONT SIZE=2> // Initial Genome after initialized</FONT>
<BR><FONT SIZE=2> cout << "Initial Genome : \n";</FONT>
<BR><FONT SIZE=2> for(i=0;i<genome.length();i++)</FONT>
<BR><FONT SIZE=2> cout << genome[i] << " ";</FONT>
<BR><FONT SIZE=2> cout << endl;</FONT>
</P>
<P><FONT SIZE=2> while(!ga.done()) ga.step();</FONT>
<BR><FONT SIZE=2> ga.flushScores();</FONT>
</P>
<P><FONT SIZE=2> genome = ga.statistics().bestIndividual();</FONT>
</P>
<P><FONT SIZE=2> cout << "Best Genome : " << endl;</FONT>
<BR><FONT SIZE=2> for(i=0;i<genome.length();i++)</FONT>
<BR><FONT SIZE=2> cout << genome[i] << " ";</FONT>
<BR><FONT SIZE=2> cout << endl; </FONT>
</P>
<P><FONT SIZE=2> return 0;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>float Objective(GAGenome & c){</FONT>
<BR><FONT SIZE=2> GA1DArrayGenome<int> & genome = (GA1DArrayGenome<int> &) c;</FONT>
<BR><FONT SIZE=2> int * pattern = (int *)c.userData(); </FONT>
</P>
<P><FONT SIZE=2> float value = 0.0;</FONT>
<BR><FONT SIZE=2> for(int i=0;i<genome.length();i++)</FONT>
<BR><FONT SIZE=2> value += 1.0/(1.0 + fabs(genome[i]-pattern[i])); </FONT>
</P>
<P><FONT SIZE=2> return(value);</FONT>
<BR><FONT SIZE=2>} </FONT>
</P>
<P><FONT SIZE=2>void IntArrayInitializer(GAGenome & c){</FONT>
<BR><FONT SIZE=2> GA1DArrayGenome<int> & genome = (GA1DArrayGenome<int> &) c;</FONT>
</P>
<P><FONT SIZE=2> for(int i=0;i<genome.length();i++)</FONT>
<BR><FONT SIZE=2> genome[i] = 1;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>_______________________________________________</FONT>
<BR><FONT SIZE=2>galib mailing list</FONT>
<BR><FONT SIZE=2>galib@mit.edu</FONT>
<BR><FONT SIZE=2><A HREF="http://lancet.mit.edu/mailman/listinfo/galib" TARGET="_blank">http://lancet.mit.edu/mailman/listinfo/galib</A></FONT>
</P>
</BODY>
</HTML>