/// ------------------------------------------------------ /// 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/ /// ------------------------------------------------------ namespace SwarmOps { /// /// Performs a number of optimization runs and returns the /// minimum fitness found. Respects feasibility (constraint /// satisfaction.) /// This does NOT allow for Preemptive Fitness Evaluation! /// public class RepeatMin : Repeat { #region Constructors. /// /// Construct the object. /// /// Optimizer to use. /// Number of optimization runs to perform. public RepeatMin(Optimizer optimizer, int numRuns) : base(optimizer, numRuns) { } #endregion #region Base-class overrides. /// /// Return problem-name. /// public override string Name { get { return "RepeatMin(" + Optimizer.Name + ")"; } } /// /// Return minimum fitness possible. This is the same as /// the minimum fitness of the Optimizer in question. /// public override double MinFitness { get { return Optimizer.MinFitness; } } /// /// Compute the fitness by repeating a number of optimization runs /// and taking the best fitness achieved in any one of these runs. /// /// Parameters to use for the Optimizer. /// Preemptive Fitness Limit /// Fitness value. public override double Fitness(double[] parameters, double fitnessLimit) { // Best fitness found so far is initialized to the worst possible fitness. double fitness = Optimizer.MaxFitness; // Best feasibility found so far is initialized to the worst possible (infeasible). bool feasible = false; // Perform a number of optimization runs. for (int i = 0; i < NumRuns; i++) { // Perform one optimization run. Result result = Optimizer.Optimize(parameters, fitnessLimit); // Update the best fitness found so far, if improvement. if (Tools.BetterFeasibleFitness(feasible, result.Feasible, fitness, result.Fitness)) { fitness = result.Fitness; feasible = result.Feasible; } } return fitness; } #endregion } }