Changeset 673 for cpp/frams/genetics


Ignore:
Timestamp:
08/19/17 02:43:11 (3 years ago)
Author:
Maciej Komosinski
Message:

Added three functions that may be useful in general

Location:
cpp/frams/genetics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/oper_fx.cpp

    r670 r673  
    176176}
    177177
    178 
    179178NeuroClass* GenoOperators::getRandomNeuroClass()
    180179{
    181         SListTempl<NeuroClass*> active;
     180        vector<NeuroClass*> active;
    182181        for (int i = 0; i < Neuro::getClassCount(); i++)
    183                 if (Neuro::getClass(i)->genactive) active += Neuro::getClass(i);
    184         if (active.size() == 0) return NULL; else return active(randomN(active.size()));
     182                if (Neuro::getClass(i)->genactive)
     183                        active.push_back(Neuro::getClass(i));
     184        if (active.size() == 0) return NULL; else return active[randomN(active.size())];
     185}
     186
     187int GenoOperators::getRandomNeuroClassWithOutput(const vector<NeuroClass*>& NClist)
     188{
     189        vector<int> allowed;
     190        for (size_t i = 0; i < NClist.size(); i++)
     191                if (NClist[i]->getPreferredOutput() != 0) //this NeuroClass provides output
     192                        allowed.push_back(i);
     193        if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
     194}
     195
     196int GenoOperators::getRandomNeuroClassWithInput(const vector<NeuroClass*>& NClist)
     197{
     198        vector<int> allowed;
     199        for (size_t i = 0; i < NClist.size(); i++)
     200                if (NClist[i]->getPreferredInputs() != 0) //this NeuroClass wants one input connection or more                 
     201                        allowed.push_back(i);
     202        if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
     203}
     204
     205int GenoOperators::getRandomChar(const char *choices, const char *excluded)
     206{
     207        int allowed_count = 0;
     208        for (size_t i = 0; i < strlen(choices); i++) if (!strchrn0(excluded, choices[i])) allowed_count++;
     209        if (allowed_count == 0) return -1; //no char is allowed
     210        int rnd_index = randomN(allowed_count) + 1;
     211        allowed_count = 0;
     212        for (size_t i = 0; i < strlen(choices); i++)
     213        {
     214                if (!strchrn0(excluded, choices[i])) allowed_count++;
     215                if (allowed_count == rnd_index) return i;
     216        }
     217        return -1; //never happens
    185218}
    186219
  • cpp/frams/genetics/oper_fx.h

    r670 r673  
    8787
    8888/**Used to perform initializations of Param parameters that are not handled by the Param itself
    89 (i.e. string parameters need to be initialized here)*/
     89(i.e. string parameters or fields that require some complex logic may be initialized here)*/
    9090   virtual void setDefaults() {}
    9191
     
    192192   static void linearMix(ParamInterface &p1, int i1, ParamInterface &p2, int i2, double proportion); ///<mixes i1'th and i2'th properties of p1 and p2; proportion should be within [0,1]; 0.5 causes both properties to become their average. For integer properties applies random "dithering" when necessary.
    193193   static NeuroClass* getRandomNeuroClass(); ///<returns random neuroclass or NULL when no active classes.
     194   static int getRandomNeuroClassWithOutput(const vector<NeuroClass*>& NClist); //returns index of random neuroclass from the NClist or -1 (no neurons on the list that provide output)
     195   static int getRandomNeuroClassWithInput(const vector<NeuroClass*>& NClist); //returns index of random neuroclass from the NClist or -1 (no neurons on the list that want input(s))
     196   static int getRandomChar(const char *choices, const char *excluded); ///<returns index of a random character from 'choices' excluding 'excluded', or -1 when everything is excluded or 'choices' is empty.
    194197   static NeuroClass* parseNeuroClass(char *&s); ///<returns longest matching neuroclass or NULL if the string does not begin with a valid neuroclass name. Advances \e s pointer.
    195198   static Neuro* findNeuro(const Model *m,const NeuroClass *nc); ///<returns pointer to first Neuro of class \e nc, or NULL if there is no such Neuro.
Note: See TracChangeset for help on using the changeset viewer.