Wrapper for an optimizer providing statistics such as mean fitness achieved over a number of optimization runs, best results achieved, etc. Transparently supports the same methods as the the optimizer itself, but stores the optimization results so as to compute the statistics.
Inheritance: OptimizerWrapper
コード例 #1
0
ファイル: Program.cs プロジェクト: firestrand/SwarmOps
        static void Main(string[] args)
        {
            // Create and initialize PRNG.
            Globals.Random = new RandomOps.MersenneTwister();

            // Assign run-condition to problem.
            Problem.RunCondition = RunCondition;

            // Wrap the optimizer in a logger of result-statistics.
            Statistics Statistics = new Statistics(Optimizer);

            // Wrap it again in a repeater.
            Repeat repeat = new RepeatSum(Statistics, NumRuns);

            // Output optimization settings.
            Console.WriteLine("Problem: {0}", Problem.Name);
            Console.WriteLine("Optimizer: {0}", Optimizer.Name);
            Console.WriteLine("Using following parameters:");
            Tools.PrintParameters(Optimizer, Optimizer.DefaultParameters);
            Console.WriteLine("Number of runs: {0}", NumRuns);
            Console.WriteLine("Dimensionality: {0}", Dim);
            Console.WriteLine("Dim-factor: {0}", DimFactor);
            Console.WriteLine();

            // Start-time.
            DateTime t1 = DateTime.Now;

            // Perform optimizations.
            double fitness = repeat.Fitness();

            // End-time.
            DateTime t2 = DateTime.Now;

            // Compute result-statistics.
            Statistics.Compute();

            // Output best results, as well as result-statistics.
            Console.WriteLine("Best parameters found:");
            Tools.PrintParameters(Problem, Statistics.BestParameters);
            Console.WriteLine("With fitness: {0}", Tools.FormatNumber(Statistics.FitnessMin));
            Console.WriteLine("Time usage: {0}", t2 - t1);
            Console.WriteLine("Mean number of iterations: {0}", Statistics.IterationsMean);
            Console.WriteLine();
            Console.WriteLine("x\ty\tComputedY");
            Console.WriteLine("--------------------");

            // Output fitted curve.
            for (int i=0; i<X.Length; i++)
            {
                double x = X[i];
                double y = Y[i];
                double computedY = Problem.ComputeY(Statistics.BestParameters, x);

                Console.WriteLine("{0}\t{1}\t{2}", x, y, computedY);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: firestrand/SwarmOps
        /// <summary>
        /// Optimize the given problem and output result-statistics.
        /// </summary>
        static void Optimize(Problem problem)
        {
            // Create a fitness trace for tracing the progress of optimization, mean.
            int NumMeanIntervals = 3000;
            FitnessTrace fitnessTraceMean = new FitnessTraceMean(NumIterations, NumMeanIntervals);

            // Create a fitness trace for tracing the progress of optimization, quartiles.
            // Note that fitnessTraceMean is chained to this object by passing it to the
            // constructor, this causes both fitness traces to be used.
            int NumQuartileIntervals = 10;
            FitnessTrace fitnessTraceQuartiles = new FitnessTraceQuartiles(NumRuns, NumIterations, NumQuartileIntervals, fitnessTraceMean);

            // Assign the problem etc. to the optimizer.
            Optimizer.Problem = problem;
            Optimizer.RunCondition = RunCondition;
            Optimizer.FitnessTrace = fitnessTraceQuartiles;

            // Wrap the optimizer in a logger of result-statistics.
            Statistics Statistics = new Statistics(Optimizer);

            // Wrap it again in a repeater.
            Repeat Repeat = new RepeatSum(Statistics, NumRuns);

            // Perform the optimization runs.
            double fitness = Repeat.Fitness(Parameters);

            // Compute result-statistics.
            Statistics.Compute();

            // Output result-statistics.
            Console.WriteLine("{0} & {1} & {2} & {3} & {4} & {5} & {6} & {7} \\\\",
                problem.Name,
                Tools.FormatNumber(Statistics.FitnessMean),
                Tools.FormatNumber(Statistics.FitnessStdDev),
                Tools.FormatNumber(Statistics.FitnessQuartiles.Min),
                Tools.FormatNumber(Statistics.FitnessQuartiles.Q1),
                Tools.FormatNumber(Statistics.FitnessQuartiles.Median),
                Tools.FormatNumber(Statistics.FitnessQuartiles.Q3),
                Tools.FormatNumber(Statistics.FitnessQuartiles.Max));

            // Output fitness trace, mean.
            string traceFilenameMean = Optimizer.Name + "-FitnessTraceMean-" + problem.Name + ".txt";
            fitnessTraceMean.WriteToFile(traceFilenameMean);

            // Output fitness trace, quartiles.
            string traceFilenameQuartiles = Optimizer.Name + "-FitnessTraceQuartiles-" + problem.Name + ".txt";
            fitnessTraceQuartiles.WriteToFile(traceFilenameQuartiles);
        }