/// ------------------------------------------------------ /// 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; namespace SwarmOps { /// /// Base-class for an optimization problem. /// public abstract class Problem { #region Constructors. /// /// Create the object. /// public Problem() : this(0, true) { } /// /// Create the object. /// /// Max optimization iterations to perform. public Problem(int maxIterations) : this(maxIterations, true) { } /// /// Create the object. /// /// Max optimization iterations to perform. /// Require solution to be feasible (satisfy constraints.) public Problem(int maxIterations, bool requireFeasible) { MaxIterations = maxIterations; RequireFeasible = requireFeasible; } #endregion #region Public fields. /// /// Maximum number of optimization iterations to perform. /// public int MaxIterations { get; set; } /// /// Require solution is feasible (satisfies constraints). /// public bool RequireFeasible { get; set; } #endregion #region Public fields, override these. /// /// Return name of the optimization problem. /// public abstract string Name { get; } /// /// Array with names of parameters. /// public virtual string[] ParameterName { get { return null; } } /// /// Lower search-space boundary. /// public abstract double[] LowerBound { get; } /// /// Upper search-space boundary. /// public abstract double[] UpperBound { get; } /// /// Lower initialization boundary, /// if different from search-space boundary. /// public virtual double[] LowerInit { get { return LowerBound; } } /// /// Upper initialization boundary, /// if different from search-space boundary. /// public virtual double[] UpperInit { get { return UpperBound; } } /// /// Maximum (i.e. worst) fitness possible. /// public virtual double MaxFitness { get { return double.MaxValue; } } /// /// Minimum (i.e. best) fitness possible. This is /// especially important if using meta-optimization /// where the Fitness is assumed to be non-negative, /// and should be roughly equivalent amongst all the /// problems meta-optimized for. /// public abstract double MinFitness { get; } /// /// Threshold for an acceptable fitness value. /// public virtual double AcceptableFitness { get { return MinFitness; } } /// /// Threshold for an acceptable tolerance. /// public double Tolerance { get; set; } /// /// Minimum number of iterations. /// public double MinIterations { get; set; } /// /// Previous fitness value. /// public virtual double PreviousFitnessValue { get; set; } /// /// Return dimensionality of the problem, that is, the number /// of parameters in a candidate solution. /// public abstract int Dimensionality { get; } /// /// Has the gradient has been implemented? /// public virtual bool HasGradient { get { return false; } } #endregion #region Public methods, override these. /// /// Compute and return fitness for the given parameters. /// /// Candidate solution. public virtual double Fitness(double[] parameters) { return Fitness(parameters, true); } /// /// Compute and return fitness for the given parameters. /// The fitness evaluation is aborted preemptively, if the /// fitness becomes higher (i.e. worse) than fitnessLimit, and /// if it is not possible for the fitness to improve. /// /// Candidate solution. /// Preemptive Fitness Limit. public virtual double Fitness(double[] parameters, double fitnessLimit) { return Fitness(parameters); } /// /// Compute and return fitness for the given parameters. /// The fitness evaluation is aborted preemptively if /// feasibility of the new candidate solution is same or better /// as that of the old candidate solution, and if the /// fitness becomes higher (i.e. worse) than fitnessLimit and /// if it is not possible for the fitness to improve. /// /// Candidate solution. /// Preemptive Fitness Limit. /// Feasibility of old candidate solution. /// Feasibility of new candidate solution. public virtual double Fitness(double[] parameters, double fitnessLimit, bool oldFeasible, bool newFeasible) { double fitness; if (Tools.BetterFeasible(oldFeasible, newFeasible)) { fitness = Fitness(parameters, fitnessLimit); } else { fitness = Fitness(parameters); } return fitness; } /// /// Compute and return fitness for the given parameters. /// /// Candidate solution. /// Feasibility of candidate solution. public virtual double Fitness(double[] parameters, bool feasible) { return Fitness(parameters, MaxFitness, feasible, feasible); } /// /// Compute the gradient of the fitness-function. /// /// Candidate solution. /// Array for holding the gradient. /// /// Computation time-complexity factor. E.g. if fitness takes /// time O(n) to compute and gradient takes time O(n*n) to compute, /// then return n. /// public virtual int Gradient(double[] x, ref double[] v) { throw new NotImplementedException(); } /// /// Enforce constraints and evaluate feasiblity. /// /// /// If you do not wish to enforce constraints you should make /// this call Feasible(). /// /// Candidate solution. public virtual bool EnforceConstraints(ref double[] parameters) { // By default we bound the candidate solution to the search-space boundaries. Tools.Bound(ref parameters, LowerBound, UpperBound); // Since we know that candidate solution is now within bounds and this is // all that is required for feasibility, we could just return true here. // Feasible() is called for educational purposes and because most optimizers // do not call Feasible() but call EnforceConstraints() so if the user were // to only override Feasible() constraint handling would not work as expected. return Feasible(parameters); } /// /// Evaluate feasibility (constraint satisfaction). /// /// Candidate solution. public virtual bool Feasible(double[] parameters) { return Tools.BetweenBounds(parameters, LowerBound, UpperBound); } /// /// Called at the beginning of an optimization run. /// public virtual void BeginOptimizationRun() { // Do nothing by default. } /// /// Called at the end of an optimization run. /// public virtual void EndOptimizationRun() { // Do nothing by default. } /// /// Return whether optimization is allowed to continue. /// /// Number of iterations performed in optimization run. /// Best fitness found in optimization run. /// Feasibility of best found candidate solution. public virtual bool Continue(int iterations, double fitness, bool feasible) { if (iterations <= MinIterations) return true; var comparison = (iterations < MaxIterations && Math.Abs((fitness - PreviousFitnessValue) / fitness) > Tolerance && !(fitness <= AcceptableFitness && (!RequireFeasible || feasible))); PreviousFitnessValue = fitness; return comparison; } #endregion } }