BetterFeasibleFitness() public static method

Return whether the new candidate solution is better than the old, depending on their feasibility (constraint satisfaction) and fitness.
public static BetterFeasibleFitness ( bool oldFeasible, bool newFeasible, double oldFitness, double newFitness ) : bool
oldFeasible bool Feasibility of old candidate solution.
newFeasible bool Feasibility of new candidate solution.
oldFitness double Fitness of old candidate solution.
newFitness double Fitness of new candidate solution.
return bool
Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Print parameters, fitness and feasibility to Console, and print a marking if
        /// fitness was an improvement to fitnessLimit.
        /// </summary>
        public static void PrintSolution(double[] parameters, double fitness, double fitnessLimit, bool oldFeasible, bool newFeasible, bool formatAsArray)
        {
            // Convert parameters to a string.
            string parametersStr = (formatAsArray) ? (Tools.ArrayToString(parameters)) : (Tools.ArrayToStringRaw(parameters));

            Console.WriteLine("{0} \t{1} \t{2} {3}",
                              parametersStr,
                              Tools.FormatNumber(fitness),
                              (newFeasible) ? (1) : (0),
                              Tools.BetterFeasibleFitness(oldFeasible, newFeasible, fitnessLimit, fitness) ? ("***") : (""));

            // Flush stdout, this is useful if piping the output and you wish
            // to study the the output before the entire optimization run is complete.
            Console.Out.Flush();
        }
Exemplo n.º 3
0
            public int Compare(Solution x, Solution y)
            {
                int retVal;

                if (x.Fitness == y.Fitness && x.Feasible == y.Feasible)
                {
                    retVal = 0;
                }
                else if (Tools.BetterFeasibleFitness(x.Feasible, y.Feasible, x.Fitness, y.Fitness))
                {
                    retVal = 1;
                }
                else
                {
                    retVal = -1;
                }

                return(retVal);
            }