Changeset 957


Ignore:
Timestamp:
06/25/20 16:31:25 (4 years ago)
Author:
Maciej Komosinski
Message:

Replace magic "100" with GenoOperators::NEUROCLASS_PROP_OFFSET

Location:
cpp/frams/genetics
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f4/f4_oper.cpp

    r935 r957  
    451451        }
    452452        // weight
    453         nn->f1 = GenoOperators::mutateNeuProperty(nn->f1, NULL, -1);
     453        nn->f1 = GenoOperators::mutateNeuronProperty(nn->f1, NULL, -1);
    454454        //nn->f1 = 10.0f * (rndDouble(1) - 0.5f);
    455455}
     
    488488                break;
    489489        case 2: // change weight
    490                 nn->f1 = GenoOperators::mutateNeuProperty(nn->f1, NULL, -1);
     490                nn->f1 = GenoOperators::mutateNeuronProperty(nn->f1, NULL, -1);
    491491                //nn->f1 += 1.0f * (rndDouble(1) - 0.5f);
    492492                break;
  • cpp/frams/genetics/fH/fH_oper.cpp

    r935 r957  
    390390                        // if it is weight, then method needs to use mutateNeuProperty
    391391                        double current = par.getDouble(id);
    392                         par.setDouble(id, mutateNeuProperty(current, NULL, -1));
     392                        par.setDouble(id, mutateNeuronProperty(current, NULL, -1));
    393393                }
    394394        }
     
    439439                if (*par.type(i) == 'f')
    440440                {
    441                         double change = mutateNeuProperty(par.getDouble(i), &neu, 100 + i);
     441                        double change = mutateNeuronProperty(par.getDouble(i), &neu, GenoOperators::NEUROCLASS_PROP_OFFSET + i);
    442442                        par.setDouble(i, change);
    443443                }
     
    467467        {
    468468                nc = getRandomNeuroClass(Model::SHAPE_BALL_AND_STICK);
    469                 // checking of neuron class availability should be checked before
     469                // checking of neuron class availability should be done before
    470470        }
    471471
  • cpp/frams/genetics/genooperators.cpp

    r955 r957  
    7676}
    7777
    78 int GenoOperators::selectRandomNeuProperty(Neuro *n)
     78int GenoOperators::selectRandomNeuronProperty(Neuro *n)
    7979{
    8080        int neuext = n->extraProperties().getPropCount(),
     
    8282        if (neuext + neucls == 0) return -1; //no properties in this neuron
    8383        int index = rndUint(neuext + neucls);
    84         if (index >= neuext) index = index - neuext + 100;
     84        if (index >= neuext) index = index - neuext + NEUROCLASS_PROP_OFFSET;
    8585        return index;
    8686}
    8787
    88 double GenoOperators::mutateNeuProperty(double current, Neuro *n, int i)
     88double GenoOperators::mutateNeuronProperty(double current, Neuro *n, int i)
    8989{
    9090        if (i == -1) return mutateCreepNoLimit('f', current, 2, true); //i==-1: mutating weight of neural connection
    9191        Param p;
    92         if (i >= 100) { i -= 100; p = n->getClass()->getProperties(); }
     92        if (i >= NEUROCLASS_PROP_OFFSET) { i -= NEUROCLASS_PROP_OFFSET; p = n->getClass()->getProperties(); }
    9393        else p = n->extraProperties();
    9494        double newval = current;
     
    354354                        const char *n = p.id(i);
    355355                        int l = (int)strlen(n);
    356                         if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; }
     356                        if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = NEUROCLASS_PROP_OFFSET + i; Len = l; }
    357357                        if (also_v1_N_props) //recognize old properties symbols /=!
    358358                        {
     
    361361                                                if (strcmp(n, "fo") == 0) n = "!";
    362362                                l = (int)strlen(n);
    363                                 if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; }
     363                                if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = NEUROCLASS_PROP_OFFSET + i; Len = l; }
    364364                        }
    365365                }
  • cpp/frams/genetics/genooperators.h

    r955 r957  
    180180        /** \name Some helpful methods for you */
    181181        //@{
     182
     183        static const int NEUROCLASS_PROP_OFFSET = 100; //a property is identified by some functions below as a single-value integer index, yet a property concerns either a Neuron or a NeuroClass, hence this offset to tell one case from the other.
     184
    182185        static int roulette(const double *probtab, const int count);    ///<returns random index according to probabilities in the \e probtab table or -1 if all probs are zero. \e count is the number of elements in \e probtab.
    183186        static bool getMinMaxDef(ParamInterface *p, int propindex, double &mn, double &mx, double &def); ///<perhaps a more useful (higher-level) way to obtain min/max/def info for integer and double properties. Returns true if min/max/def was really available (otherwise it is just invented).
    184         static int selectRandomNeuProperty(Neuro* n); ///<selects random property (either 0-based extraproperty of Neuro or 100-based property of its NeuroClass). -1 if Neuro has no properties.
    185         static double mutateNeuProperty(double current, Neuro *n, int propindex); ///<returns value \e current mutated for the property \e propindex of NeuroClass \e nc or for extraproperty (\e propindex - 100) of Neuro. Neuro is used as read-only. Give \e propindex == -1 to mutate connection weight (\e nc is then ignored).
     187        static int selectRandomNeuronProperty(Neuro* n); ///<selects random property (either 0-based extraproperty of Neuro or NEUROCLASS_PROP_OFFSET-based property of its NeuroClass). -1 if Neuro has no properties.
     188        static double mutateNeuronProperty(double current, Neuro *n, int propindex); ///<returns value \e current mutated for the property \e propindex of NeuroClass \e nc or for extraproperty (\e propindex - NEUROCLASS_PROP_OFFSET) of Neuro. Neuro is used as read-only. Give \e propindex == -1 to mutate connection weight (\e nc is then ignored).
    186189        static bool mutatePropertyNaive(ParamInterface &p, int propindex); ///<creep-mutate selected property. Returns true when success. mutateProperty() should be used instead of this function.
    187190        static bool mutateProperty(ParamInterface &p, int propindex); ///<like mutatePropertyNaive(), but uses special probability distributions for some neuron properties.
     
    203206        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.
    204207        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.
    205         static int neuroClassProp(char *&s, NeuroClass *nc, bool also_v1_N_props = false); ///<returns 0-based property number for \e neuroclass, 100-based extraproperty number for Neuro, or -1 if the string does not begin with a valid property name. Advance \e s pointer if success.
     208        static int neuroClassProp(char *&s, NeuroClass *nc, bool also_v1_N_props = false); ///<returns 0-based property number for \e neuroclass, NEUROCLASS_PROP_OFFSET-based extraproperty number for Neuro, or -1 if the string does not begin with a valid property name. Advance \e s pointer if success.
    206209        static bool isWS(const char c); ///<is \e c a whitespace char?
    207210        static void skipWS(char *&s); ///<advances pointer \e s skipping whitespaces.
Note: See TracChangeset for help on using the changeset viewer.