• Main Page
  • Classes
  • Files
  • File List

D:/Radagast_D/Projekt/ECF_trunk/ECF/primjeri.cpp

00001 //
00002 // PRIMJERI
00003 //
00004 
00005 
00006 
00007 
00008 // primjer tipa podataka
00009 struct my_type
00010 {
00011     double v;
00012     bool b;
00013 };
00014 
00015 // terminal za doticni tip
00016 class MyTerminal : public Primitives::Primitive
00017 {
00018 public:
00019     my_type value_;
00020 
00021     MyTerminal()
00022     {
00023         nArguments_ = 0;
00024     }
00025     void execute(void* result, Tree& tree)
00026     {
00027         my_type& res = *(my_type*)result;
00028         res = value_;
00029     }
00030     void setValue(void* value)
00031     {
00032         value_ = *(my_type*)value;
00033     }
00034     ~MyTerminal()
00035     {   }
00036 };
00037 
00038 // primjer funkcije za korisnicki tip podataka
00039 class MyFunc : public Primitives::Primitive
00040 {
00041 public:
00042     MyFunc()
00043     {
00044         nArguments_ = 2;
00045         name_ = "func";
00046     }
00047     void execute(void* result, Tree& tree)
00048     {
00049         my_type first, second;
00050         my_type& func = *(my_type*)result;
00051 
00052         getNextArgument(&first, tree);
00053         getNextArgument(&second, tree);
00054         
00055         func.b = first.b && second.b;
00056         func.v = first.v + second.v;
00057     }
00058     ~MyFunc()
00059     {   }
00060 };
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 // primjer korisnickog genotipa (obrisati)
00069 // empty crx op
00070 class MyGenotypeCrxOp : public CrossoverOp
00071 {
00072 public:
00073     bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00074     {
00075         // does nothing! (see existing operators)
00076         return true;
00077     }
00078 };
00079 typedef boost::shared_ptr<MyGenotypeCrxOp> MyGenotypeCrxOpP;
00080 
00081 // empty mut op
00082 class MyGenotypeMutOp : public MutationOp
00083 {
00084 public:
00085     bool mutate(GenotypeP gene)
00086     {
00087         // does nothing! (see existing operators)
00088         return true;
00089     }
00090 };
00091 typedef boost::shared_ptr<MyGenotypeMutOp> MyGenotypeMutOpP;
00092 
00093 
00094 class MyGenotype : public Genotype
00095 {
00096 public:
00097     std::vector<double> realValues_;
00098 
00099     // mandatory: define name, other things as needed
00100     MyGenotype()
00101     {
00102         name_ = "MyGenotype";
00103     }
00104 
00105     // mandatory: must provide copy method
00106     MyGenotype* copy()
00107     {
00108         MyGenotype *newObject = new MyGenotype(*this);
00109         return newObject;
00110     }
00111 
00113     //std::vector<CrossoverOpP> getCrossoverOp()
00114     //{
00115     //  std::vector<CrossoverOpP> crx;
00116     //  crx.push_back(static_cast<CrossoverOpP> (new MyGenotypeCrxOp));
00117     //  return crx;
00118     //}
00119 
00121     //std::vector<MutationOpP> getMutationOp()
00122     //{
00123     //  std::vector<MutationOpP> mut;
00124     //  mut.push_back(static_cast<MutationOpP> (new MyGenotypeMutOp));
00125     //  return mut;
00126     //}
00127 
00128     // optional: register any parameters
00129     void registerParameters(StateP state)
00130     {
00131         registerParameter(state, "size", (voidP) (new uint(1)), UINT);
00132     }
00133 
00134     // mandatory: build initial genotype structure
00135     bool initialize(StateP state)
00136     {
00137         if(!isParameterDefined(state, "size")) {
00138             state->getLogger()->log(1, "Error: MyGenotype size not defined");
00139             throw("");
00140         }
00141 
00142         voidP sizep = getParameterValue(state, "size");
00143         uint size = *((uint*) sizep.get());
00144 
00145         realValues_.resize(size);
00146         // generate random doubles in [0, 1]
00147         for(uint i = 0; i < size; i++)
00148             realValues_[i] = state->getRandomizer()->getRandomDouble();
00149 
00150         return true;
00151     }
00152 
00153     // mandatory: write to XMLNode
00154     void write(XMLNode &xMyGenotype)
00155     {
00156         xMyGenotype = XMLNode::createXMLTopNode("MyGenotype");
00157         std::stringstream sValue;
00158         sValue << realValues_.size();
00159         xMyGenotype.addAttribute("size", sValue.str().c_str());
00160 
00161         sValue.str("");
00162         for(uint iVar = 0; iVar < realValues_.size(); iVar++)
00163             sValue << "\t" << realValues_[iVar];
00164         xMyGenotype.addText(sValue.str().c_str());
00165     }
00166 
00167     // mandatory: read from XMLNode
00168     void read(XMLNode& xMyGenotype)
00169     {
00170         XMLCSTR values = xMyGenotype.getText();
00171         std::stringstream sValues;
00172         sValues << values;
00173 
00174         for(uint iVar = 0; iVar < realValues_.size(); iVar++)
00175             sValues >> realValues_[iVar];
00176     }
00177 };
00178 typedef boost::shared_ptr<MyGenotype> MyGenotypeP;
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 // primjer kornisnickog operatora (obrisati)
00187 class MyOp : public Operator
00188 {
00189 public:
00190     uint myStopGen_;
00191 
00192     // optional: register any parameters
00193     void registerParameters(StateP state)
00194     {
00195         state->getRegistry()->registerEntry("my.stopgen", (voidP) (new uint(0)), UINT);
00196     }
00197 
00198     // optional: initialize (with parameter check)
00199     bool initialize(StateP state)
00200     {
00201         // if parameter not defined in config, return false (inactive op)
00202         if(!state->getRegistry()->isModified("my.stopgen"))
00203             return false;
00204         // otherwise, read param. value and return true (active op)
00205         voidP sptr = state->getRegistry()->getEntry("my.stopgen");
00206         myStopGen_ = *((uint*) sptr.get());
00207         return true;
00208     }
00209 
00210     // mandatory: actual operation
00211     bool operate(StateP state)
00212     {
00213         // read generation no, stop if needed
00214         if(state->getGenerationNo() == myStopGen_)
00215             state->setTerminateCond();
00216         return true;
00217     }
00218 };
00219 typedef boost::shared_ptr<MyOp> MyOpP;
00220 
00221 
00222 
00223 
00224 
00225 
00226 // primjer korisnickog algoritma (obrisati)
00227 class MyAlg : public Algorithm
00228 {
00229 protected:
00230     // declare all available selection operators (not all get used)
00231     SelFitnessProportionalOpP selFitOp_;
00232     SelRandomOpP selRandomOp_;
00233     SelBestOpP selBestOp_;
00234     SelWorstOpP selWorstOp_;
00235     // what individual to replace (worst or random)
00236     bool replaceWorst_;
00237 public:
00238     // mandatory: define name, construct selection operators
00239     MyAlg()
00240     {
00241         name_ = "MyAlg";
00242         selFitOp_ = (SelFitnessProportionalOpP) (new SelFitnessProportionalOp);
00243         selRandomOp_ = (SelRandomOpP) (new SelRandomOp);
00244         selBestOp_ = (SelBestOpP) (new SelBestOp);
00245         selWorstOp_ = (SelWorstOpP) (new SelWorstOp);
00246     }
00247 
00248     // optional: register any parameters
00249     void registerParameters(StateP state)
00250     {
00251         // string parameter, options: random, worst
00252         registerParameter(state, "replace", (voidP) (new std::string("random")), STRING);
00253     }
00254 
00255     // optional: initialize components, read parameters
00256     bool initialize(StateP state)
00257     {
00258         // selection operators must be initialized if used!
00259         selFitOp_->initialize(state);
00260         // optional: set ratio between the best and the worst individual's selection probability
00261         selFitOp_->setSelPressure(10);
00262         // the following would cause the selection to be inverted fitness (badness) proportional
00263         //selFitOp_->setInverseProportional();
00264         selRandomOp_->initialize(state);
00265         selBestOp_->initialize(state);
00266         selWorstOp_->initialize(state);
00267 
00268         // get parameter, decide what to replace
00269         voidP sptr = getParameterValue(state, "replace");
00270         std::string replace = *((std::string*) sptr.get());
00271         replaceWorst_ = false;
00272         if(replace == "worst")
00273             replaceWorst_ = true;
00274 
00275         return true;
00276     }
00277 
00278     // mandatory: perform single 'generation' (however the algorithm defines it)
00279     bool advanceGeneration(StateP state, DemeP deme)
00280     {
00281         // select parents
00282         IndividualP first = selFitOp_->select(*deme);
00283         IndividualP second = selBestOp_->select(*deme);
00284         // select child (random or worst)
00285         IndividualP child;
00286         if(replaceWorst_)
00287             child = selWorstOp_->select(*deme);
00288         else
00289             child = selRandomOp_->select(*deme);
00290 
00291         mate(first, second, child); // crossover
00292         mutate(child);              // mutation (probability defined in Registry!)
00293         // to explicitly mutate an individual:
00294         //mutation_->mutate(child);
00295         evaluate(child);            // evaluation
00296 
00297         // some other helper functions (see existing algorithms):
00298         // copy, replaceWith, removeFrom, isMember
00299 
00300         return true;
00301     }
00302 };
00303 typedef boost::shared_ptr<MyAlg> MyAlgP;

Generated on Wed Sep 1 2010 14:25:54 for ECF by  doxygen 1.7.1