/// ------------------------------------------------------ /// 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 { /// /// Optimizer that iterates over all possible combinations /// of parameters fitting a mesh of a certain size. This is /// particularly useful for displaying performance /// landscapes from meta-optimization, relating choices of /// control parameters to the performance of the optimizer. /// public class MESH : Optimizer { #region Constructors. /// /// Construct the object. /// public MESH() : base() { } /// /// Construct the object. /// /// Fix certain parameters or leave null. /// Problem to optimize. public MESH(double?[] mask, Problem problem) : base(problem) { Debug.Assert(mask == null || mask.Length == problem.Dimensionality); Mask = mask; } #endregion #region Get control parameters. /// /// Get parameter, Number of iterations per dimension. /// /// Optimizer parameters. public int GetNumIterationsPerDim(double[] parameters) { return (int)System.Math.Round(parameters[0], System.MidpointRounding.AwayFromZero); } #endregion #region Base-class overrides, Problem. /// /// Name of the optimizer. /// public override string Name { get { return "MESH"; } } /// /// Number of control parameters for optimizer. /// public override int Dimensionality { get { return 1; } } string[] _parameterName = { "NumIterationsPerDim" }; /// /// Control parameter names. /// public override string[] ParameterName { get { return _parameterName; } } static readonly double[] _defaultParameters = { 8.0 }; /// /// Default control parameters. /// public override double[] DefaultParameters { get { return _defaultParameters; } } static readonly double[] _lowerBound = { 1 }; /// /// Lower search-space boundary for control parameters. /// public override double[] LowerBound { get { return _lowerBound; } } static readonly double[] _upperBound = { 1000.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 this optimizer. int numIterationsPerDim = GetNumIterationsPerDim(parameters); Debug.Assert(numIterationsPerDim >= 1); // Get problem-context. double[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; int n = Problem.Dimensionality; // Allocate mesh position and mesh-incremental values. double[] x = new double[n]; // Mesh position. double[] delta = new double[n]; // Mesh incremental values. double[] g = new double[n]; // Best found position for this run. double gFitness = Problem.MaxFitness; // Fitness for best found position. bool gFeasible = false; // Feasibility for best found position. // Initialize mesh position to the lower boundary. LowerBound.CopyTo(x, 0); // Compute mesh incremental values for all dimensions. for (int i = 0; i < n; i++) { delta[i] = (upperBound[i] - lowerBound[i]) / (numIterationsPerDim - 1); } // Start recursive traversal of mesh. Recursive(0, numIterationsPerDim, delta, ref x, ref g, ref gFitness, ref gFeasible); // Signal end of optimization run. Problem.EndOptimizationRun(); // Return best-found solution and fitness. return new Result(g, gFitness, gFeasible, (int)System.Math.Pow(numIterationsPerDim, n)); } #endregion #region Protected methods. /// /// Fix certain parameters or leave null. /// double?[] Mask; /// /// Helper function for recursive traversal of the mesh in a depth-first order. /// /// Current dimension being processed. /// Number of mesh iterations per dimension. /// Distance between points in mesh. /// Current mesh point. /// Best found point in mesh. /// Fitness of best found point. /// Feasibility of best found point. void Recursive( int curDim, int numIterationsPerDim, double[] delta, ref double[] x, ref double[] g, ref double gFitness, ref bool gFeasible) { // Get problem-context. double[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; int n = Problem.Dimensionality; Debug.Assert(curDim >= 0 && curDim < n); // Should parameters be mesh-computed? if (Mask == null || Mask[curDim] == null) { // Iterate over all mesh-entries for current dimension. int i; for (i = 0; i < numIterationsPerDim; i++) { // Update mesh position for current dimension. x[curDim] = lowerBound[curDim] + delta[curDim] * i; // Bound mesh position for current dimension. x[curDim] = Tools.Bound(x[curDim], lowerBound[curDim], upperBound[curDim]); // Do actual recursion or fitness evaluation. RecursiveInner(curDim, numIterationsPerDim, delta, ref x, ref g, ref gFitness, ref gFeasible); } } else { // Use fixed parameter instead of mesh-computed. x[curDim] = Mask[curDim].Value; // Do actual recursion or fitness evaluation. RecursiveInner(curDim, numIterationsPerDim, delta, ref x, ref g, ref gFitness, ref gFeasible); } } /// /// Helper function for recursive traversal of the mesh in a depth-first order. /// /// Current dimension being processed. /// Number of mesh iterations per dimension. /// Distance between points in mesh. /// Current mesh point. /// Best found point in mesh. /// Fitness of best found point. /// Feasibility of best found point. void RecursiveInner( int curDim, int numIterationsPerDim, double[] delta, ref double[] x, ref double[] g, ref double gFitness, ref bool gFeasible) { int n = Problem.Dimensionality; Debug.Assert(curDim >= 0 && curDim < n); // Either recurse or compute fitness for mesh position. if (curDim < n - 1) { // Recurse for next dimension. Recursive(curDim + 1, numIterationsPerDim, delta, ref x, ref g, ref gFitness, ref gFeasible); } else { // Compute feasibility (constraint satisfaction). bool feasible = Problem.Feasible(x); // Compute fitness for current mesh position. // This is done regardless of feasibility. double fitness = Problem.Fitness(x, feasible); // Update best position and fitness found in this run. if (Tools.BetterFeasibleFitness(gFeasible, feasible, gFitness, fitness)) { // Update this run's best known position. x.CopyTo(g, 0); // Update this run's best know fitness. gFitness = fitness; // Update best known feasibility. gFeasible = feasible; } } } #endregion } }