File size: 6,366 Bytes
b1b3bae |
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 |
/// ------------------------------------------------------
/// SwarmOps - Numeric and heuristic optimization for C#
/// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// SwarmOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------
using System;
using System.Diagnostics;
namespace SwarmOps.Problems
{
/// <summary>
/// Search-space mangler, used to increase the difficulty of
/// optimizing benchmark problems and avoid correlation with
/// global optima such as zero. Note that this works with
/// parallel optimizers but not with parallel meta-optimization
/// because of the way MetaFitness is implemented.
/// </summary>
public class Mangler : ProblemWrapper
{
#region Constructors.
/// <summary>
/// Constructs a new object.
/// </summary>
/// <param name="problem">The problem being wrapped.</param>
/// <param name="diffusion">Diffusion factor, larger than 0, e.g. 0.01</param>
/// <param name="displacement">Displacement factor, larger than 0, e.g. 0.1</param>
/// <param name="fitnessNoise">FitnessNoise factor, lager than 0, e.g. 0.01</param>
/// <param name="spillover">Spillover factor, larger than 0, e.g. 0.05</param>
public Mangler(Problem problem,
double diffusion,
double displacement,
double spillover,
double fitnessNoise)
: base(problem)
{
// Assign to fields.
Diffusion = diffusion;
Displacement = displacement;
Spillover = spillover;
FitnessNoise = fitnessNoise;
// Allocate transformation matrix and array.
int n = problem.Dimensionality;
A = Tools.NewMatrix(n, n);
b = new double[n];
}
#endregion
#region Public fields.
/// <summary>
/// Diffusion factor.
/// </summary>
public double Diffusion
{
get;
protected set;
}
/// <summary>
/// Displacement factor.
/// </summary>
public double Displacement
{
get;
protected set;
}
/// <summary>
/// Spillover factor.
/// </summary>
public double Spillover
{
get;
protected set;
}
/// <summary>
/// FitnessNoise factor.
/// </summary>
public double FitnessNoise
{
get;
protected set;
}
#endregion
#region Private fields.
/// <summary>
/// Matrix used for transforming input position: y = A * x + b
/// </summary>
double[][] A;
/// <summary>
/// Array used for transforming input position: y = A * x + b
/// </summary>
double[] b;
#endregion
#region Base-class overrides.
/// <summary>
/// Compute fitness of wrapped problem and print the result.
/// </summary>
public override double Fitness(double[] parameters, double fitnessLimit, bool oldFeasible, bool newFeasible)
{
// Notational convenience.
int n = Problem.Dimensionality;
double[] x = parameters;
// Iterator variables.
int i, j;
// Allocate output array. This is thread-safe and fast enough.
double[] y = new double[n];
// Matrix transformation y = A * x + b
for (i = 0; i < n; i++)
{
y[i] = b[i];
for (j = 0; j < n; j++)
{
y[i] += A[i][j] * x[j];
}
}
// Diffusion.
for (i = 0; i < n; i++)
{
y[i] += Globals.Random.Gauss(0, Diffusion);
}
// Compute fitness.
double fitness = Problem.Fitness(y, fitnessLimit, oldFeasible, newFeasible);
// Fitness noise.
double noise = System.Math.Abs(Globals.Random.Gauss()) * FitnessNoise + 1;
fitness *= noise;
// Compute and return fitness of displaced parameters.
return fitness;
}
/// <summary>
/// Gradient is not defined for mangled search-spaces.
/// </summary>
public override bool HasGradient
{
get { return false; }
}
/// <summary>
/// Compute the gradient of the fitness-function.
/// This is not defined for mangled search-spaces.
/// </summary>
/// <param name="x">Candidate solution.</param>
/// <param name="v">Array for holding the gradient.</param>
public override int Gradient(double[] x, ref double[] v)
{
throw new NotImplementedException();
}
/// <summary>
/// At beginning of new optimization run create a random mangling.
/// </summary>
public override void BeginOptimizationRun()
{
// Notational convenience.
int n = Problem.Dimensionality;
// Iterator variables.
int i, j;
// Initialize transformation matrix.
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
// The diagonal of the transformation matrix is one, the remainder
// is chosen randomly.
A[i][j] = (i == j) ? (1) : (Globals.Random.Gauss(0, Spillover));
}
}
// Initialize displacement array.
for (i = 0; i < n; i++)
{
double range = Problem.UpperBound[i] - Problem.LowerBound[i];
double d = Displacement * Globals.Random.Uniform(-range, range);
b[i] = d;
}
// Call through problem-chain.
Problem.BeginOptimizationRun();
}
#endregion
}
}
|