/usr/include/libevocosm/scaler.h is in libevocosm-dev 4.0.2-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | /*
Evocosm is a C++ framework for implementing evolutionary algorithms.
Copyright 2011 Scott Robert Ladd. All rights reserved.
Evocosm is user-supported open source software. Its continued development is dependent
on financial support from the community. You can provide funding by visiting the Evocosm
website at:
http://www.coyotegulch.com
You may license Evocosm in one of two fashions:
1) Simplified BSD License (FreeBSD License)
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Scott Robert Ladd.
2) Closed-Source Proprietary License
If your project is a closed-source or proprietary project, the Simplified BSD License may
not be appropriate or desirable. In such cases, contact the Evocosm copyright holder to
arrange your purchase of an appropriate license.
The author can be contacted at:
scott.ladd@coyotegulch.com
scott.ladd@gmail.com
http:www.coyotegulch.com
*/
#if !defined(LIBEVOCOSM_SCALER_H)
#define LIBEVOCOSM_SCALER_H
// libevocosm
#include "organism.h"
#include "stats.h"
namespace libevocosm
{
//! Fitness scaling for a population
/*!
As a population converges on a definitive solution, the difference
between fitness values may become very small. That prevents the
best solutions from having a significant advantage in reproduction.
Fitness scaling solves this problem by adjusting the fitness values
to the advantage of the most-fit chromosomes.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class scaler : protected globals
{
public:
//! Virtual destructor
/*!
A virtual destructor. By default, it does nothing; this is
a placeholder that identifies this class as a potential base,
ensuring that objects of a derived class will have their
destructors called if they are destroyed through a base-class
pointer.
*/
virtual ~scaler()
{
// nada
}
//! Scale a population's fitness values
/*!
The scale_fitness method can adjust the fitness of a population
to make it more likely that the "best" (whatever that menas)
organisms have the best chance of reproduction.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population) = 0;
};
//! A do-nothing scaler
/*!
The null_scaler doesn't scale anything; it's just a placeholder used
in evolutionary algorithms that do not use fitness scaling.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class null_scaler : public scaler<OrganismType>
{
public:
//! Do-nothing scaling function
/*!
Has no effect on the target population.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
// nada
}
};
//! A linear normalization scaler
/*!
A simple scaler implementing a configurable linear normalization scaler, as
per Goldberg 1979.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class linear_norm_scaler : public scaler<OrganismType>
{
public:
//! Constructor
/*!
Creates a new scaler for linear normalization.
*/
linear_norm_scaler(double a_fitness_multiple = 2.0)
: m_fitness_multiple(a_fitness_multiple)
{
// nada
}
//! Scaling function
/*!
Performs linear normalization on the fitness of the target population.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
// calculate max, average, and minimum fitness for the population
fitness_stats<OrganismType> stats(a_population);
// calculate coefficients for fitness scaling
double slope;
double intercept;
double delta;
if (stats.getMin() > ((m_fitness_multiple * stats.getMean() - stats.getMax()) / (m_fitness_multiple - 1.0)))
{
// normal scaling
delta = stats.getMax() - stats.getMean();
slope = (m_fitness_multiple - 1.0) * stats.getMean() / delta;
intercept = stats.getMean() * (stats.getMax() - m_fitness_multiple * stats.getMean()) / delta;
}
else
{
// extreme scaling
delta = stats.getMean() - stats.getMin();
slope = stats.getMean() / delta;
intercept = -stats.getMin() * stats.getMean() / delta;
}
// adjust fitness values
for (int n = 0; n < a_population.size(); ++n)
a_population[n].fitness = slope * a_population[n].fitness + intercept;
}
private:
double m_fitness_multiple;
};
//! A windowed fitness scaler
/*!
Implements windowed fitness scaling, whereby all fitness values are modified
by subtracting the minimum fitness in the population.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class windowed_scaler : public scaler<OrganismType>
{
public:
//! Constructor
/*!
Creates a new windowed scaler with a given set of parameters.
*/
windowed_scaler()
{
// nada
}
//! Scaling function
/*!
Performs windowed scaling on the fitness of the target population.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
fitness_stats<OrganismType> stats(a_population);
// assign new fitness values
for (int n = 0; n < a_population.size(); ++n)
a_population[n].fitness = stats.getMin();
}
};
//! An exponential fitness scaler
/*!
Implements an exponential fitness scaling, whereby all fitness values are modified
such that new fitness = (a * fitness + b) ^ n.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class exponential_scaler : public scaler<OrganismType>
{
public:
//! Constructor
/*!
Creates a new exponential scaler with a given set of parameters. The
formula used is new_fitness = (a * fitness + b) ^ power.
\param a_a - A multplier against the fitness
\param a_b - Added to fitness before exponentiation
\param a_power - Power applied to the value
*/
exponential_scaler(double a_a = 1.0, double a_b = 1.0, double a_power = 2.0)
: m_a(a_a),
m_b(a_b),
m_power(a_power)
{
// nada
}
//! Scaling function
/*!
Performs exponential scaling on the fitness of the target population.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
// assign new fitness values
for (int n = 0; n < a_population.size(); ++n)
a_population[n].fitness = pow((m_a * a_population[n].fitness + m_b),m_power);
}
private:
double m_a;
double m_b;
double m_power;
};
//! A quadratic scaler
/*!
Uses a quadratic equation to scale the fitness of organisms.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class quadratic_scaler : public scaler<OrganismType>
{
public:
//! Constructor
/*!
Creates a new scaler for quadratic scaling.
*/
quadratic_scaler(double a_a, double a_b, double a_c)
: m_a(a_a), m_b(a_b), m_c(a_c)
{
// nada
}
//! Scaling function
/*!
Performs quadratic scling on the fitness of the target population.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
// adjust fitness values
for (int n = 0; n < a_population.size(); ++n)
{
double f = a_population[n].fitness;
a_population[n].fitness = m_a * pow(f,2.0) + m_b * f + m_c;
}
}
private:
double m_a;
double m_b;
double m_c;
};
//! A sigma scaler
/*!
A sigma scaler, as per Forrest and Tanese.
\param OrganismType - The type of organism
*/
template <class OrganismType>
class sigma_scaler : public scaler<OrganismType>
{
public:
//! Constructor
/*!
Creates a new sigma scaler
*/
sigma_scaler()
{
}
//! Scaling function
/*!
Performs sigma scaling, which maintains selection pressure over the
length of a run, thus minimizing the affects of convergence on
reproductive selection. The function adjusts an organism's fitness
in relation to the standard deviation of the population's fitness.
\param a_population - A population of organisms
*/
virtual void scale_fitness(vector<OrganismType> & a_population)
{
fitness_stats<OrganismType> stats(a_population);
// calculate 2 times the std. deviation (sigma)
double sigma2 = 2.0 * stats.getSigma();
// now assign new fitness values
if (sigma2 == 0.0)
{
for (int n = 0; n < a_population.size(); ++n)
a_population[n].fitness = 1.0;
}
else
{
for (int n = 0; n < a_population.size(); ++n)
{
// change fitness
a_population[n].fitness = (1.0 + a_population[n].fitness / stats.mean) / sigma2;
// avoid tiny or zero fitness value; everyone gets to reproduce
if (a_population[n].fitness < 0.1)
a_population[n].fitness = 0.1;
}
}
}
};
};
#endif
|