/usr/include/libevocosm/scaler.h is in libevocosm-dev 3.1.0-3.1ubuntu1.
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | //---------------------------------------------------------------------
// Algorithmic Conjurings @ http://www.coyotegulch.com
// Evocosm -- An Object-Oriented Framework for Evolutionary Algorithms
//
// scaler.h
//---------------------------------------------------------------------
//
// Copyright 1996, 1999, 2002, 2003, 2004, 2005 Scott Robert Ladd
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.
// 59 Temple Place - Suite 330
// Boston, MA 02111-1307, USA.
//
//-----------------------------------------------------------------------
//
// For more information on this software package, please visit
// Scott's web site, Coyote Gulch Productions, at:
//
// http://www.coyotegulch.com
//
//-----------------------------------------------------------------------
#if !defined(LIBEVOCOSM_SCALER_H)
#define LIBEVOCOSM_SCALER_H
// Standard C Library
#include <cmath>
// Standard C++
#include <limits>
#include <algorithm>
// libevocosm
#include "organism.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;
//! Invert a population's fitness values
/*!
Scales a population's fitness values
*/
void invert(vector<OrganismType> & a_population)
{
double base = min_element(a_population.begin(), a_population.end())->fitness()
+ max_element(a_population.begin(), a_population.end())->fitness();
for (typename vector<OrganismType>::iterator organism = a_population.begin(); organism != a_population.end(); ++organism)
organism->fitness() = base - organism->fitness();
}
};
//! 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
double max_fitness = std::numeric_limits<double>::min();
double min_fitness = std::numeric_limits<double>::max();
double avg_fitness = 0.0;
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
{
// do we have a new maximum?
if (org->fitness() > max_fitness)
max_fitness = org->fitness();
// do we have a new minimum?
if (org->fitness() < min_fitness)
min_fitness = org->fitness();
// accumulate for average
avg_fitness += org->fitness();
}
avg_fitness /= double(a_population.size());
// calculate coefficients for fitness scaling
double slope;
double intercept;
double delta;
if (min_fitness > ((m_fitness_multiple * avg_fitness - max_fitness) / (m_fitness_multiple - 1.0)))
{
// normal scaling
delta = max_fitness - avg_fitness;
slope = (m_fitness_multiple - 1.0) * avg_fitness / delta;
intercept = avg_fitness * (max_fitness - m_fitness_multiple * avg_fitness) / delta;
}
else
{
// extreme scaling
delta = avg_fitness - min_fitness;
slope = avg_fitness / delta;
intercept = -min_fitness * avg_fitness / delta;
}
// adjust fitness values
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
org->fitness() = slope * org->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)
{
// Find minimum fitness
// Note that organisms sort in reverse order of fitness, such that
// the "maximum" value has the smallest fitness.
double min_fitness = min_element(a_population.begin(), a_population.end())->fitness();
// assign new fitness values
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
org->fitness() -= min_fitness;
}
};
//! 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 (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
org->fitness() = pow((m_a * org->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 (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
{
double f = org->fitness();
org->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)
{
// calculate the mean
double mean = 0.0;
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
mean += org->fitness();
mean /= static_cast<double>(a_population.size());
// calculate variance
double variance = 0.0;
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
{
double diff = org->fitness() - mean;
variance += (diff * diff);
}
variance /= static_cast<double>(a_population.size() - 1);
// calculate 2 times the std. deviation (sigma)
double sigma2 = 2.0 * sqrt(variance);
// now assign new fitness values
if (sigma2 == 0.0)
{
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
org->fitness() = 1.0;
}
else
{
for (typename vector<OrganismType>::iterator org = a_population.begin(); org != a_population.end(); ++org)
{
// change fitness
org->fitness() = (1.0 + org->fitness() / mean) / sigma2;
// avoid tiny or zero fitness value; everyone gets to reproduce
if (org->fitness() < 0.1)
org->fitness() = 0.1;
}
}
}
};
};
#endif
|