File size: 4,538 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// ------------------------------------------------------
/// 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;

namespace SwarmOps
{
    /// <summary>
    /// Log best solutions found during optimization, that is, log
    /// parameters and their associated fitness. Transparently wraps
    /// around a problem-object.
    /// </summary>
    public class LogSolutions : ProblemWrapper
    {
        #region Constructors.
        /// <summary>
        /// Create the object.
        /// </summary>
        /// <param name="problem">Problem-object to be wrapped.</param>
        /// <param name="capacity">Log capacity.</param>
        /// <param name="onlyFeasible">Only log feasible solutions.</param>
        public LogSolutions(Problem problem, int capacity, bool onlyFeasible)

            : base(problem)
        {
            Capacity = capacity;
            OnlyFeasible = onlyFeasible;

            Log = new List<Solution>(capacity);
        }
        #endregion

        #region Public fields.
        /// <summary>
        /// Maximum capacity of log.
        /// </summary>
        public int Capacity
        {
            get;
            private set;
        }

        /// <summary>
        /// Only log feasible solutions.
        /// </summary>
        public bool OnlyFeasible
        {
            get;
            private set;
        }

        /// <summary>
        /// Log of candidate solutions.
        /// </summary>
        public List<Solution> Log
        {
            get;
            private set;
        }
        #endregion

        #region Public methods.
        /// <summary>
        /// Clear the log.
        /// </summary>
        public void Clear()
        {
            Log.Clear();
        }
        #endregion

        #region Problem base-class overrides.
        /// <summary>
        /// Return the name of the problem.
        /// </summary>
        public override string Name
        {
            get { return "LogSolutions (" + Problem.Name + ")"; }
        }

        /// <summary>
        /// Compute the fitness measure by passing the
        /// given parameters to the wrapped problem, and if
        /// candidate solution is an improvement then log
        /// the results.
        /// </summary>
        /// <param name="parameters">Candidate solution.</param>
        /// <param name="fitnessLimit">Preemptive Fitness Limit</param>
        /// <param name="newFeasible">Feasibility of old candidate solution.</param>
        /// <param name="oldFeasible">Feasibility of new candidate solution.</param>
        /// <returns>Fitness value.</returns>
        public override double Fitness(double[] parameters, double fitnessLimit, bool oldFeasible, bool newFeasible)
        {
            double fitness = Problem.Fitness(parameters, fitnessLimit, oldFeasible, newFeasible);

            // Log solutions. If feasibiilty is required then only log feasible solutions.
            if (!OnlyFeasible || newFeasible)
            {
                // Log solutions with better fitness and feasibility.
                if (Tools.BetterFeasibleFitness(oldFeasible, newFeasible, fitnessLimit, fitness))
                {
                    // Ensure log does not exceed desired capacity.
                    if (Log.Count >= Capacity)
                    {
                        // Assume log is sorted in ascending (worsening) order.
                        // Remove worst from the log.
                        Log.RemoveAt(Log.Count - 1);
                    }

                    Solution candidateSolution = new Solution(parameters, fitness, newFeasible);

                    // Add new solution to the log.
                    Log.Add(candidateSolution);

                    // Sort log according to fitness.
                    // This could be implemented more efficiently
                    // but it is not crucial for runtime performance
                    // so this simple implementation is sufficient.
                    Log.Sort(new Solution.FitnessComparer());
                }
            }

            return fitness;
        }
        #endregion
    }
}