File size: 7,769 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | /// ------------------------------------------------------
/// 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.Diagnostics;
namespace SwarmOps.Optimizers
{
/// <summary>
/// Local Unimodal Sampling (LUS) optimizer originally
/// due to Pedersen (1). Does local sampling with an
/// exponential decrease of the sampling-range. Works
/// well for many optimization problems, especially
/// when only short runs are allowed. Is particularly
/// well suited as the overlaying meta-optimizer when
/// tuning parameters for another optimizer.
/// </summary>
/// <remarks>
/// References:
/// (1) M.E.H. Pedersen. Tuning & Simplifying Heuristical
/// Optimization. PhD Thesis, University of Southampton,
/// 2010.
/// </remarks>
public class LUS : Optimizer
{
#region Constructors.
/// <summary>
/// Construct the object.
/// </summary>
public LUS()
: base()
{
}
/// <summary>
/// Construct the object.
/// </summary>
/// <param name="problem">Problem to optimize.</param>
public LUS(Problem problem)
: base(problem)
{
}
#endregion
#region Get control parameters.
/// <summary>
/// Get parameter, Gamma.
/// </summary>
/// <param name="parameters">Optimizer parameters.</param>
public double GetGamma(double[] parameters)
{
return parameters[0];
}
#endregion
#region Base-class overrides, Problem.
/// <summary>
/// Name of the optimizer.
/// </summary>
public override string Name
{
get { return "LUS"; }
}
/// <summary>
/// Number of control parameters for optimizer.
/// </summary>
public override int Dimensionality
{
get { return 1; }
}
string[] _parameterName = { "Gamma" };
/// <summary>
/// Control parameter names.
/// </summary>
public override string[] ParameterName
{
get { return _parameterName; }
}
static readonly double[] _defaultParameters = { 3.0 };
/// <summary>
/// Default control parameters.
/// </summary>
public override double[] DefaultParameters
{
get { return _defaultParameters; }
}
static readonly double[] _lowerBound = { 0.5 };
/// <summary>
/// Lower search-space boundary for control parameters.
/// </summary>
public override double[] LowerBound
{
get { return _lowerBound; }
}
static readonly double[] _upperBound = { 100.0 };
/// <summary>
/// Upper search-space boundary for control parameters.
/// </summary>
public override double[] UpperBound
{
get { return _upperBound; }
}
#endregion
#region Base-class overrides, Optimizer.
/// <summary>
/// Perform one optimization run and return the best found solution.
/// </summary>
/// <param name="parameters">Control parameters for the optimizer.</param>
public override Result Optimize(double[] parameters)
{
Debug.Assert(parameters != null && parameters.Length == Dimensionality);
// Signal beginning of optimization run.
Problem.BeginOptimizationRun();
// Retrieve parameter specific to LUS method.
double gamma = GetGamma(parameters);
// Get problem-context.
double[] lowerBound = Problem.LowerBound;
double[] upperBound = Problem.UpperBound;
double[] lowerInit = Problem.LowerInit;
double[] upperInit = Problem.UpperInit;
int n = Problem.Dimensionality;
// Allocate agent position and search-range vectors.
double[] x = new double[n]; // Current position.
double[] y = new double[n]; // Potentially new position.
double[] d = new double[n]; // Search-range.
// Initialize search-range and decrease-factor.
double r = 1; // Search-range.
double q = System.Math.Pow(2.0, -1.0 / (n * gamma)); // Decrease-factor (using gamma = 1.0/alpha).
// Initialize agent-position in search-space.
Tools.InitializeUniform(ref x, lowerInit, upperInit);
// Initialize search-range to full search-space.
Tools.InitializeRange(ref d, lowerBound, upperBound);
// Enforce constraints and evaluate feasibility.
bool feasible = Problem.EnforceConstraints(ref x);
// Compute fitness of initial position.
// This counts as an iteration below.
double fitness = Problem.Fitness(x, feasible);
// Trace fitness of best found solution.
Trace(0, fitness, feasible);
int i;
for (i = 1; Problem.Continue(i, fitness, feasible); i++)
{
// Compute potentially new position.
for (int j = 0; j < n; j++)
{
// Pick a sample from the neighbourhood of the current
// position and within the given range.
y[j] = Tools.SampleBounded(x[j], r * d[j], lowerBound[j], upperBound[j]);
}
// Enforce constraints and evaluate feasibility.
bool newFeasible = Problem.EnforceConstraints(ref y);
// Compute fitness if feasibility (constraint satisfaction) is same or better.
if (Tools.BetterFeasible(feasible, newFeasible))
{
// Compute fitness of new position.
double newFitness = Problem.Fitness(y, fitness, feasible, newFeasible);
// Update best known position, if improvement.
if (Tools.BetterFeasibleFitness(feasible, newFeasible, fitness, newFitness))
{
// Update fitness.
fitness = newFitness;
// Update feasibility.
feasible = newFeasible;
// Update position by swapping array x and y.
double[] temp = x;
x = y;
y = temp;
}
else // Worse fitness.
{
// Decrease the search-range.
r *= q;
}
}
else // Worse feasibility.
{
// Decrease the search-range.
r *= q;
}
// Trace fitness of best found solution.
Trace(i, fitness, feasible);
}
// Signal end of optimization run.
Problem.EndOptimizationRun();
// Return best-found solution and fitness.
return new Result(x, fitness, feasible, i);
}
#endregion
}
} |