<div>i am having problem with code how to generate a specific tree.This code generate a tree using sedd value which is random.But i want to generate a specific tree .... Please Anyone can help me .The code is as below</div> <div> </div> <div> </div> <div>#include <stdio.h><BR>#include <stdlib.h><BR>#include <iostream.h><BR>#include <fstream.h><BR>#include <ga/ga.h></div> <div> </div> <div>// The objective function is declared here and defined below.<BR>float objective(GAGenome &);</div> <div>// This is the declaration for the initialization operator for our trees.<BR>void TreeInitializer(GAGenome &);</div> <div>// This is a recursive function that will be used in the 'write' method for <BR>// our tree genomes.<BR>void WriteNode(ostream & os, GANode<int> * n);</div> <div><BR>int<BR>main(int argc, char *argv[])<BR>{<BR> /*cout << "Example 6\n\n";<BR> cout << "This example uses a
SteadyState GA and Tree<int> genome. It\n";<BR> cout << "tries to maximize the size of the tree genomes that it\n";<BR> cout << "contains. The genomes contain ints in its nodes.\n\n";*/<BR> //cout.flush();</div> <div>// See if we've been given a seed to use (for testing purposes). When you<BR>// specify a random seed, the evolution will be exactly the same each time<BR>// you use that seed number.</div> <div> unsigned int seed = 0;<BR> /*for(int i=1; i<argc; i++) {<BR> if(strcmp(argv[i++],"seed") == 0) {<BR> seed = atoi(argv[i]);<BR> }<BR> }*/</div> <div>// Set the default values of the parameters.</div> <div> GAParameterList params;<BR> GASteadyStateGA::registerDefaultParameters(params);<BR> params.set(gaNpopulationSize, 5);<BR> params.set(gaNpCrossover, 0.7);<BR> params.set(gaNpMutation,
0.01);<BR> params.set(gaNnGenerations, 10);<BR> params.set(gaNscoreFilename, "bog.dat");<BR> params.set(gaNscoreFrequency, 5); // record score every 10th generation<BR> params.set(gaNflushFrequency, 5); // dump scores every 10th recorded score<BR> params.parse(argc, argv, gaFalse); // Parse the command line for GAlib args.</div> <div>// Now create the GA and run it. We first create a chromsome with the<BR>// operators we want. Once we have the genome set up, create the genetic <BR>// algorithm, set the parameters, and let it go.</div> <div> GATreeGenome<int> genome(objective);<BR> genome.initializer(TreeInitializer);<BR> genome.mutator(GATreeGenome<int>::SwapSubtreeMutator);</div> <div> GASteadyStateGA ga(genome);<BR> ga.parameters(params);<BR> ga.evolve(seed);</div> <div> genome = ga.statistics().bestIndividual();<BR> cout << "the ga generated this tree:\n"
<< genome << "\n";<BR> cout << genome.size() << " nodes, " << genome.depth() << " levels deep.\n";<BR> cout << "best of generation data are in '" << ga.scoreFilename() << "'\n";</div> <div> return 0;<BR>}<BR> </div> <div><BR>/* ----------------------------------------------------------------------------<BR>Objective function<BR> All we do in this objective function is try to maximize the size of the tree.<BR>Just return the tree size. This means that if you run this objective function<BR>for many generations you'll run out of memory! There is no limit to tree or<BR>list sizes built-in to the GA library.<BR>---------------------------------------------------------------------------- */<BR>float<BR>objective(GAGenome & c)<BR>{<BR> GATreeGenome<int> & genome = (GATreeGenome<int> &)c;<BR> return genome.size();<BR>}</div> <div> </div>
<div>/* ----------------------------------------------------------------------------<BR>Here is the initializer for our genomes. It builds a tree of n items of type<BR>int. Notice that we first destroy any tree that is already in the genome <BR>before we do our initialization. This is so that the genomes can be re-used.<BR>When you re-run a GA, it does not destroy the individuals in the population - <BR>it reuses them. Thus, the initializer must make sure that the genome is <BR>cleaned up before it tries to initialize it.<BR>---------------------------------------------------------------------------- */<BR>void<BR>TreeInitializer(GAGenome & c)<BR>{<BR> GATreeGenome<int> &child=(GATreeGenome<int> &)c;</div> <div>// destroy any pre-existing tree<BR> child.root();<BR> child.destroy();</div> <div>// Create a new tree with depth of 'depth' and each eldest node containing<BR>// 'n' children (the other siblings
have none).<BR> int depth=2, n=3, count=0;<BR> child.insert(count++,GATreeBASE::ROOT);</div> <div> for(int i=0; i<depth; i++){<BR> child.eldest();<BR> child.insert(count++);<BR> for(int j=0; j<n; j++)<BR> child.insert(count++,GATreeBASE::AFTER);<BR> }<BR>}</div> <div> </div> <div> </div> <div><BR>/* ----------------------------------------------------------------------------<BR>Tree genome method overrides<BR>-------------------------------------------------------------------------------<BR> Here we override the built-in methods for the tree class. We can do this<BR>because the tree class is template-ized - when the compiler looks for an<BR>instance of the tree class methods, it finds these so it won't generate an<BR>instance from the templates. You can do this with ANY method of a template<BR>class. Here we do it only for
the write method.<BR> The default write operator prints out pointers to the contents of each node.<BR>Here we print out the actual contents of each node. This assumes that the <BR>object in our node has the operator<< defined for it.<BR>---------------------------------------------------------------------------- */<BR>void <BR>WriteNode(ostream & os, GANode<int> * n)<BR>{<BR> if(!n) return;<BR> GANodeBASE * node = (GANodeBASE *)n;</div> <div> os.width(10);<BR> os << ((GANode<int> *)node)->contents << " ";<BR> os.width(10); <BR> if(node->parent) os << ((GANode<int> *)node->parent)->contents << " ";<BR> else os << "." << " ";<BR> os.width(10); <BR> if(node->child) os << ((GANode<int> *)node->child)->contents << " ";<BR> else os << "." << " ";<BR> os.width(10);<BR>
if(node->next) os << ((GANode<int> *)node->next)->contents << " ";<BR> else os << "." << " ";<BR> os.width(10);<BR> if(node->prev) os << ((GANode<int> *)node->prev)->contents << "\n";<BR> else os << ".\n";<BR> WriteNode(os, (GANode<int> *)node->child);</div> <div> for(GANodeBASE * tmp=node->next; tmp && tmp != node; tmp=tmp->next){<BR> os.width(10);<BR> os << ((GANode<int> *)tmp)->contents << " ";<BR> os.width(10);<BR> if(tmp->parent) os << ((GANode<int> *)tmp->parent)->contents << " ";<BR> else os << "." << " ";<BR> os.width(10); <BR> if(tmp->child) os << ((GANode<int> *)tmp->child)->contents << " ";<BR> else os
<< "." << " ";<BR> os.width(10); <BR> if(tmp->next) os << ((GANode<int> *)tmp->next)->contents << " ";<BR> else os << "." << " ";<BR> os.width(10); <BR> if(tmp->prev) os << ((GANode<int> *)tmp->prev)->contents << "\n";<BR> else os << ".\n";<BR> WriteNode(os, (GANode<int> *)tmp->child);<BR> }<BR>}</div> <div>int<BR>GATreeGenome<int>::write(ostream & os) const<BR>{<BR> os << " node parent child next prev\n";<BR> WriteNode(os, (GANode<int> *)rt);<BR> return os.fail() ? 1 : 0;<BR>}</div> <div> </div> <div><BR>// If your compiler does not do automatic
instantiation (e.g. g++ 2.6.8),<BR>// then define the NO_AUTO_INST directive. The following includes force an<BR>// instantiation of the template class that we're going to use in the example.<BR>#ifdef NO_AUTO_INST<BR>#include <ga/GATree.C><BR>#include <ga/GATreeGenome.C><BR>#if defined(__GNUG__)<BR>template class GATree<int>;<BR>template class GATreeGenome<int>;<BR>#else<BR>GATree<int>;<BR>GATreeGenome<int>;<BR>#endif<BR>#endif<BR></div><p>
        
        
                <hr size=1>
Jiyo cricket on <a href="http://us.rd.yahoo.com/mail/in/mailcricket/*http://in.sports.yahoo.com/cricket/">Yahoo! India cricket</a><br>
<a href="http://us.rd.yahoo.com/mail/in/mailmobilemessenger/*http://in.mobile.yahoo.com/new/messenger/">Yahoo! Messenger Mobile</a> Stay in touch with your buddies all the time.