/// ------------------------------------------------------ /// 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.Collections.Generic; using System.Diagnostics; using System.Linq; namespace SwarmOps { /// /// Used for sorting optimization problems so that those hardest /// to optimize are tried first. This is used in the MetaFitness /// class where Pre-emptive Fitness Evaluation seeks to abort the /// meta-fitness evaluation as early as possible. /// public class ProblemIndex { #region Constructors. /// /// Construct the ProblemIndex-object. /// /// The problems to be indexed. public ProblemIndex(Problem[] problems) : base() { Debug.Assert(problems.Length > 0); int numProblems = problems.Count(); double weight = 1.0 / numProblems; Index = new List(numProblems); foreach (Problem problem in problems) { Index.Add(new ProblemFitness(problem, weight)); } } /// /// Construct the ProblemIndex-object. /// /// The problems to be indexed. public ProblemIndex(WeightedProblem[] weightedProblems) : base() { // Ensure array has elements. Debug.Assert(weightedProblems.Length > 0); Index = new List(weightedProblems.Length); double weightSum = weightedProblems.Sum(o => o.Weight); foreach (WeightedProblem weightedProblem in weightedProblems) { Problem problem = weightedProblem.Problem; double weight = weightedProblem.Weight; double weightNormalized = weight / weightSum; // Ensure weight is positive. Debug.Assert(weight > 0); Index.Add(new ProblemFitness(problem, weightNormalized)); } } #endregion #region Internal class. /// /// Associate an optimization problem with a fitness. /// class ProblemFitness { public ProblemFitness(Problem problem, double weight) { Problem = problem; Weight = weight; Fitness = problem.MaxFitness; } /// /// The optimization problem. /// public Problem Problem { get; private set; } /// /// Weight of the optimization problem. /// public double Weight { get; private set; } /// /// The fitness achieved when last optimizing that problem. /// public double Fitness { get; set; } public static int Compare(ProblemFitness x, ProblemFitness y) { Debug.Assert(x != null && y != null); return System.Math.Sign(y.Fitness - x.Fitness); } } #endregion #region Internal variables. /// /// The list of optimization problems sorted according to fitness. /// List Index; #endregion #region Public fields. /// /// Return the number of problems. /// public int Count { get { return Index.Count; } } #endregion #region Public methods. /// /// Return the i'th optimization problem, sorted so that the /// problems with the worst fitness have lowest indices. /// public Problem GetProblem(int i) { return Index[i].Problem; } /// /// Return the weight associated with the i'th problem. /// public double GetWeight(int i) { return Index[i].Weight; } /// /// Set the fitness associated with the i'th problem. /// public void SetFitness(int i, double fitness) { Index[i].Fitness = fitness; } /// /// Sort the optimization problems according to their associated fitness. /// public void Sort() { Index.Sort(ProblemFitness.Compare); } #endregion } }