Changeset 968


Ignore:
Timestamp:
06/30/20 00:30:39 (4 years ago)
Author:
Maciej Komosinski
Message:

Introduced a separate function to mutate neuron connection weight

Location:
cpp/frams/genetics
Files:
4 edited

Legend:

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

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

    r967 r968  
    390390                        // if it is weight, then method needs to use mutateNeuProperty
    391391                        double current = par.getDouble(id);
    392                         par.setDouble(id, getMutatedNeuroClassProperty(current, NULL, -1));
     392                        par.setDouble(id, getMutatedNeuronConnectionWeight(current));
    393393                }
    394394        }
  • cpp/frams/genetics/genooperators.cpp

    r967 r968  
    99#include <frams/util/rndutil.h>
    1010
     11//
     12// custom distributions for mutations of various parameters
     13//
    1114static double distrib_force[] =   // for '!'
    1215{
     
    3134        -1, 1,         // ~linear
    3235};
    33 
     36/*
     37static double distrib_weight[] =
     38{
     395,                 // distribution -999 _-^_^-_ +999
     40-999, 999,         // each weight value may be useful, especially...
     41-5, -0.3,          // ...little non-zero values
     42-3, -0.6,
     430.6, 3,
     440.3, 5,
     45};
     46*/
    3447
    3548int GenoOperators::roulette(const double *probtab, const int count)
     
    108121double GenoOperators::getMutatedNeuroClassProperty(double current, Neuro *n, int i)
    109122{
    110         if (i == -1) return mutateCreepNoLimit('f', current, 2, true); //i==-1: mutating weight of neural connection
     123        if (i == -1)
     124        {
     125                logPrintf("GenoOperators", "getMutatedNeuroClassProperty", LOG_WARN, "Deprecated usage in C++ source: to mutate connection weight, use getMutatedNeuronConnectionWeight().");
     126                return getMutatedNeuronConnectionWeight(current);
     127        }
    111128        Param p;
    112129        if (i >= NEUROCLASS_PROP_OFFSET) { i -= NEUROCLASS_PROP_OFFSET; p = n->getClass()->getProperties(); }
     
    117134}
    118135
     136double GenoOperators::getMutatedNeuronConnectionWeight(double current)
     137{
     138        return mutateCreepNoLimit('f', current, 2, true);
     139}
     140
    119141bool GenoOperators::mutatePropertyNaive(ParamInterface &p, int i)
    120142{
     
    145167        if (p.type(i)[0] != 'f' && p.type(i)[0] != 'd') return false; //don't know how to mutate
    146168        const char *n = p.id(i), *na = p.name(i);
    147         if (strcmp(n, "si") == 0 && strcmp(na, "Sigmoid") == 0) newval = CustomRnd(distrib_sigmo); else
    148                 if (strcmp(n, "in") == 0 && strcmp(na, "Inertia") == 0) newval = CustomRnd(distrib_inertia); else
    149                         if (strcmp(n, "fo") == 0 && strcmp(na, "Force") == 0) newval = CustomRnd(distrib_force); else
     169        if (strcmp(n, "si") == 0 && strcmp(na, "Sigmoid") == 0) newval = round(CustomRnd(distrib_sigmo), 3); else
     170                if (strcmp(n, "in") == 0 && strcmp(na, "Inertia") == 0) newval = round(CustomRnd(distrib_inertia), 3); else
     171                        if (strcmp(n, "fo") == 0 && strcmp(na, "Force") == 0) newval = round(CustomRnd(distrib_force), 3); else
    150172                        {
    151173                                double mn, mx, df;
     
    167189        {
    168190                if (limit_precision_3digits)
    169                         result = floor(result * 1000 + 0.5) / 1000.0; //round
     191                        result = round(result, 3);
    170192        }
    171193        return result;
     
    186208                {
    187209                        //reflect and wrap above may have changed the (limited) precision, so try to round again (maybe unnecessarily, because we don't know if reflect+wrap above were triggered)
    188                         double result_try = floor(result * 1000 + 0.5) / 1000.0; //round
     210                        double result_try = round(result, 3);
    189211                        if (mn <= result_try && result_try <= mx) result = result_try; //after rounding still witin allowed range, so keep rounded value
    190212                }
     
    375397                        int l = (int)strlen(n);
    376398                        if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = NEUROCLASS_PROP_OFFSET + i; Len = l; }
    377                         if (also_v1_N_props) //recognize old properties symbols /=!
     399                        if (also_v1_N_props) //recognize old symbols of properties: /=!
    378400                        {
    379401                                if (strcmp(n, "si") == 0) n = "/"; else
  • cpp/frams/genetics/genooperators.h

    r967 r968  
    187187        static bool mutateRandomNeuroClassProperty(Neuro* n); ///<high-level neuron mutation function, will select and mutate a random property of Neuron's NeuroClass. Returns true if successful and some property was actually mutated. Could return false when the NeuroClass of the Neuron have no properties, or when a randomly selected property was not suitable for mutation (for example a string or another non-number type).
    188188        static int selectRandomNeuroClassProperty(Neuro* n); ///<selects random property (either 0-based extraproperty of NeuroClass or NEUROCLASS_PROP_OFFSET-based standard property of NeuroClass). -1 if Neuroclass has no properties.
    189         static double getMutatedNeuroClassProperty(double current, Neuro *n, int propindex); ///<returns value \e current mutated for the property \e propindex of Neuron's NeuroClass or for extraproperty (\e propindex - NEUROCLASS_PROP_OFFSET) of Neuron's NeuroClass. Neuro \e n is used as read-only. Use \e propindex == -1 to mutate connection weight (\e n and \e i are then ignored).
     189        static double getMutatedNeuroClassProperty(double current, Neuro *n, int propindex); ///<returns value \e current mutated for the property \e propindex of Neuron's NeuroClass or for extraproperty (\e propindex - NEUROCLASS_PROP_OFFSET) of Neuron's NeuroClass. Neuro \e n is used as read-only.
     190        static double getMutatedNeuronConnectionWeight(double current); ///<returns mutated value of \e current.
    190191        static bool mutatePropertyNaive(ParamInterface &p, int propindex); ///<creep-mutate selected property. Returns true when success. mutateProperty() should be used instead of this function.
    191192        static bool mutateProperty(ParamInterface &p, int propindex); ///<like mutatePropertyNaive(), but uses special probability distributions for some neuron properties.
     
    216217};
    217218
    218 
    219 //
    220 // custom distributions for mutations of various parameters
    221 //
    222 /*
    223 static double distrib_weight[]=
    224 {
    225 5,                 // distribution -999 _-^_^-_ +999
    226 -999, 999,         // each weight value may be useful, especially...
    227 -5, -0.3,        // ...little non-zero values
    228 -3, -0.6,
    229 0.6, 3,
    230 0.3, 5,
    231 };
    232 */
    233 
    234219#endif
Note: See TracChangeset for help on using the changeset viewer.