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

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.