Changeset 749 for cpp/frams/genetics


Ignore:
Timestamp:
02/28/18 19:42:24 (2 years ago)
Author:
Maciej Komosinski
Message:

Added a new function to linearly mix two vectors of real numbers

Location:
cpp/frams/genetics
Files:
2 edited

Legend:

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

    r743 r749  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    150150void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) //TODO
    151151{
    152         p.setInt(index, (paInt)value); //TODO value=2.5 will result in 2 but we want it to be 2 or 3 with equal probability. value=2.1 would be mostly 2, rarely 3. Careful with negative values (test it!)
     152        p.setInt(index, (paInt)(value + 0.5)); //TODO value=2.499 will result in 2 and 2.5 will result in 3, but we want these cases to be 2 or 3 with almost equal probability. value=2.1 should be mostly 2, rarely 3. Careful with negative values (test it!)
     153}
     154
     155void GenoOperators::linearMix(vector<double> &p1, vector<double> &p2, double proportion)
     156{
     157        if (p1.size() != p2.size())
     158        {
     159                logPrintf("GenoOperators", "linearMix", LOG_ERROR, "Cannot mix vectors of different length (%d and %d)", p1.size(), p2.size());
     160                return;
     161        }
     162        for (unsigned int i = 0; i < p1.size(); i++)
     163        {
     164                double v1 = p1[i];
     165                double v2 = p2[i];
     166                p1[i] = v1*proportion + v2*(1 - proportion);
     167                p2[i] = v2*proportion + v1*(1 - proportion);
     168        }
    153169}
    154170
  • cpp/frams/genetics/oper_fx.h

    r675 r749  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    125125        \code
    126126        {
    127                 int len=strlen(geno);
    128                 if (len==0 || random(2)==0) //add
    129                 {
    130                         method=0;
    131                         char* mutated=(char*)malloc(mutated,len+2); //allocate for mutated genotype
    132                         mutated[0]='A'+random(10); //first char random
    133                         strcpy(mutated+1,geno); //the rest is original
    134                         free(geno); //must take care of the original allocation
    135                         geno=mutated;
    136                 } else
    137                 {
    138                         method=1;
    139                         geno[len-1]=0; //simply shorten the string - remove last char
    140                 }
    141                 chg=1.0/max(len,1); //estimation of mutation strength, divby0-safe
     127        int len=strlen(geno);
     128        if (len==0 || random(2)==0) //add
     129        {
     130        method=0;
     131        char* mutated=(char*)malloc(mutated,len+2); //allocate for mutated genotype
     132        mutated[0]='A'+random(10); //first char random
     133        strcpy(mutated+1,geno); //the rest is original
     134        free(geno); //must take care of the original allocation
     135        geno=mutated;
     136        } else
     137        {
     138        method=1;
     139        geno[len-1]=0; //simply shorten the string - remove last char
     140        }
     141        chg=1.0/max(len,1); //estimation of mutation strength, divby0-safe
    142142        } \endcode
    143143        */
     
    190190        static double mutateCreep(char type, double current, double mn, double mx); ///<just as mutateCreepNoLimit(), but forces mutated value into the [mn,mx] range using the 'reflect' approach.
    191191        static void setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value); ///<sets a double value in an integer field; when a value is non-integer, applies random "dithering" so that both lower and higher integer value have some chance to be set.
    192         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.
     192        static void linearMix(vector<double> &p1, vector<double> &p2, double proportion); ///<mixes two real-valued vectors; inherited proportion should be within [0,1]; 1.0 does not change values (all inherited), 0.5 causes both vectors to become their average, 0.0 swaps values (none inherited).
     193        static void linearMix(ParamInterface &p1, int i1, ParamInterface &p2, int i2, double proportion); ///<mixes i1'th and i2'th properties of p1 and p2; inherited proportion should be within [0,1]; 1.0 does not change values (all inherited), 0.5 causes both properties to become their average, 0.0 swaps values (none inherited). For integer properties applies random "dithering" when necessary.
    193194        static NeuroClass* getRandomNeuroClass(); ///<returns random neuroclass or NULL when no active classes.
    194195        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)
     
    2152165,                 // distribution -999 _-^_^-_ +999
    216217-999, 999,         // each weight value may be useful, especially...
    217   -5, -0.3,        // ...little non-zero values
    218   -3, -0.6,
    219  0.6, 3,
    220  0.3, 5,
     218-5, -0.3,        // ...little non-zero values
     219-3, -0.6,
     2200.6, 3,
     2210.3, 5,
    221222};
    222223*/
Note: See TracChangeset for help on using the changeset viewer.