/// ------------------------------------------------------ /// 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 { /// /// 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. /// /// /// References: /// (1) M.E.H. Pedersen. Tuning & Simplifying Heuristical /// Optimization. PhD Thesis, University of Southampton, /// 2010. /// public class LUS : Optimizer { #region Constructors. /// /// Construct the object. /// public LUS() : base() { } /// /// Construct the object. /// /// Problem to optimize. public LUS(Problem problem) : base(problem) { } #endregion #region Get control parameters. /// /// Get parameter, Gamma. /// /// Optimizer parameters. public double GetGamma(double[] parameters) { return parameters[0]; } #endregion #region Base-class overrides, Problem. /// /// Name of the optimizer. /// public override string Name { get { return "LUS"; } } /// /// Number of control parameters for optimizer. /// public override int Dimensionality { get { return 1; } } string[] _parameterName = { "Gamma" }; /// /// Control parameter names. /// public override string[] ParameterName { get { return _parameterName; } } static readonly double[] _defaultParameters = { 3.0 }; /// /// Default control parameters. /// public override double[] DefaultParameters { get { return _defaultParameters; } } static readonly double[] _lowerBound = { 0.5 }; /// /// Lower search-space boundary for control parameters. /// public override double[] LowerBound { get { return _lowerBound; } } static readonly double[] _upperBound = { 100.0 }; /// /// Upper search-space boundary for control parameters. /// public override double[] UpperBound { get { return _upperBound; } } #endregion #region Base-class overrides, Optimizer. /// /// Perform one optimization run and return the best found solution. /// /// Control parameters for the optimizer. 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 } }