Example #1
0
        /// <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;
        }
Example #2
0
        /// <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>
        /// <returns>Fitness value.</returns>
        public override double Fitness(double[] parameters, double fitnessLimit)
        {
            double fitness = Problem.Fitness(parameters, fitnessLimit);

            if (fitness < fitnessLimit)
            {
                Solution candidateSolution = new Solution(parameters, fitness);

                // Add new solution to the log.
                _log.Add(fitness, candidateSolution);

                if (Log.Count > Capacity)
                {
                    // Remove worst from the log.
                    _log.RemoveAt(Log.Count - 1);
                }
            }

            return fitness;
        }