File size: 2,832 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// ------------------------------------------------------
/// 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
{
    /// <summary>
    /// Performs a number of optimization runs and returns the
    /// sum of the fitnesses. Ignores feasibility (constraint
    /// satisfaction.)
    /// This allows for Preemptive Fitness Evaluation.
    /// </summary>
    public class RepeatSum : Repeat
    {
        #region Constructors.
        /// <summary>
        /// Construct the object.
        /// </summary>
        /// <param name="optimizer">Optimizer to use.</param>
        /// <param name="numRuns">Number of optimization runs to perform.</param>
        public RepeatSum(Optimizer optimizer, int numRuns)

            : base(optimizer, numRuns)
        {
        }
        #endregion

        #region Base-class overrides.
        /// <summary>
        /// Return problem-name.
        /// </summary>
        public override string Name
        {
            get { return "RepeatSum(" + Optimizer.Name + ")"; }
        }

        /// <summary>
        /// Return minimum fitness possible. This is zero as
        /// the fitness summation is normalized.
        /// </summary>
        public override double MinFitness
        {
            get { return 0; }
        }

        /// <summary>
        /// Compute the fitness by repeating a number of optimization runs
        /// and sum the fitnesses achieved in the runs.
        /// </summary>
        /// <param name="parameters">Parameters to use for the Optimizer.</param>
        /// <param name="fitnessLimit">Preemptive Fitness Limit</param>
        /// <returns>Fitness value.</returns>
        public override double Fitness(double[] parameters, double fitnessLimit)
        {
            // Initialize the fitnses sum.
            double fitnessSum = 0;

            // Perform a number of optimization runs.
            for (int i = 0; i < NumRuns && fitnessSum < fitnessLimit; i++)
            {
                // Perform one optimization run.
                Result result = Optimizer.Optimize(parameters, fitnessLimit-fitnessSum);

                // Compute the normalized fitness.
                double fitnessNormalized = result.Fitness - Optimizer.MinFitness;

                Debug.Assert(fitnessNormalized >= 0);

                // Accumulate the fitness sum.
                fitnessSum += fitnessNormalized;
            }

            return fitnessSum;
        }
        #endregion
    }
}