Ejemplo n.º 1
0
        public IOptimizationResults <T> Evolve( )
        {
            if (this.AlgebraProvider == null)
            {
                throw new ArgumentNullException("The Rosenbrock optimizer must have its AlgebraProvider property not set to 'null'");
            }
            currentPoint = startingPoint;
            IBase   b           = createNewBase(currentPoint);
            IVector stepsLength = b.CreateVector( );

            for (int i = 0; i < stepsLength.Length; i++)
            {
                stepsLength[i] = initialStep;
            }
            List <IObjectiveScores <T> > scores = new List <IObjectiveScores <T> >();

            while (!terminationCondition.IsFinished() && !isCancelled)
            {
                var endOfStage = performStage(b, currentPoint, stepsLength);
                b            = endOfStage.Item1;
                currentPoint = endOfStage.Item2;
                scores.AddRange(endOfStage.Item3);
            }
            //return new BasicOptimizationResults<T>( new IObjectiveScores<T>[] { currentPoint } );
            return(new BasicOptimizationResults <T>(scores.ToArray()));
        }
Ejemplo n.º 2
0
        public BestOfSampling(ICandidateFactory <TSysConfig> innerSamplingFactory, int poolSize, int bestPoints,
                              IObjectiveEvaluator <TSysConfig> evaluator, IFitnessAssignment <double> fitAssignment = null)
        {
            if (fitAssignment == null)
            {
                fitAssignment = new DefaultFitnessAssignment();
            }
            if (poolSize < bestPoints)
            {
                throw new ArgumentOutOfRangeException("poolSize", poolSize, String.Format("poolSize must be >= bestPoints({0})", bestPoints));
            }
            this.HcFactory            = (IHyperCubeOperationsFactory)innerSamplingFactory;
            this.innerSamplingFactory = innerSamplingFactory;
            var tmp = new IObjectiveScores[poolSize];

            for (int i = 0; i < poolSize; i++)
            {
                tmp[i] = evaluator.EvaluateScore(innerSamplingFactory.CreateRandomCandidate());
            }
            var points = fitAssignment.AssignFitness(tmp);

            Array.Sort(points);
            candidates = new FitnessAssignedScores <double> [bestPoints];
            for (int i = 0; i < bestPoints; i++)
            {
                candidates[i] = points[i];
            }
        }
Ejemplo n.º 3
0
            public IOptimizationResults <StringConfiguration> Evolve()
            {
                IObjectiveEvaluator <StringSystemConfiguration> objectiveEvaluator = new StringObjEvaluator(this.target);
                StringSystemConfiguration point = initializationFactory.CreateRandomCandidate();
                IObjectiveScores          score = objectiveEvaluator.EvaluateScore(point);

                // TODO: Expand on the details of the algorithm. For now, the high-level interfaces is what we are teasing out.
                return(null);
            }
        public IObjectiveScores <MpiSysConfig>[] EvaluateScore(MpiSysConfig systemConfiguration)
        {
            this.Execute(systemConfiguration);
            var c      = this.CatchmentScores;
            var result = new IObjectiveScores <MpiSysConfig> [c.Length];

            c.CopyTo(result, 0);
            return(result);
        }
        public void CorrectObjectiveCount()
        {
            const int        objectivesPerScore = 12;
            const int        numScores          = 305;
            var              randomScores       = CreateRandomScores(numScores, objectivesPerScore);
            IObjectiveScores result             = ObjectiveScoresHelper.Mean(randomScores, config);

            Assert.That(result.ObjectiveCount, Is.EqualTo(objectivesPerScore));
        }
Ejemplo n.º 6
0
 private void assertPoint(IObjectiveScores iObjectiveScores, double x, double y, double z)
 {
     var s = (IObjectiveScores<TestHyperCube>)iObjectiveScores;
     Assert.AreEqual(x, (double)s.GetObjective(0).ValueComparable, 1e-9);
     Assert.AreEqual(y, (double)s.GetObjective(1).ValueComparable, 1e-9);
     Assert.AreEqual(z, (double)s.GetObjective(2).ValueComparable, 1e-9);
     Assert.AreEqual(x, s.SystemConfiguration.GetValue("0"), 1e-9);
     Assert.AreEqual(y, s.SystemConfiguration.GetValue("1"), 1e-9);
     Assert.AreEqual(z, s.SystemConfiguration.GetValue("2"), 1e-9);
 }
        public static string GetHumanReadable(IObjectiveScores scores)
        {
            var sb = new StringBuilder( );

            for (int i = 0; i < scores.ObjectiveCount; i++)
            {
                sb.AppendLine(scores.GetObjective(i).GetText( ));
            }
            sb.AppendLine(scores.GetSystemConfiguration( ).GetConfigurationDescription( ));
            return(sb.ToString( ));
        }
Ejemplo n.º 8
0
        private void assertPoint(IObjectiveScores iObjectiveScores, double x, double y, double z)
        {
            var s = (IObjectiveScores <TestHyperCube>)iObjectiveScores;

            Assert.AreEqual(x, (double)s.GetObjective(0).ValueComparable, 1e-9);
            Assert.AreEqual(y, (double)s.GetObjective(1).ValueComparable, 1e-9);
            Assert.AreEqual(z, (double)s.GetObjective(2).ValueComparable, 1e-9);
            Assert.AreEqual(x, s.SystemConfiguration.GetValue("0"), 1e-9);
            Assert.AreEqual(y, s.SystemConfiguration.GetValue("1"), 1e-9);
            Assert.AreEqual(z, s.SystemConfiguration.GetValue("2"), 1e-9);
        }
        public void CorrectObjectiveNames()
        {
            const int        objectivesPerScore = 3;
            const int        numScores          = 3;
            var              randomScores       = CreateRandomScores(numScores, objectivesPerScore);
            IObjectiveScores result             = ObjectiveScoresHelper.Mean(randomScores, config);

            for (int i = 0; i < objectivesPerScore; i++)
            {
                Assert.That(result.GetObjective(i).Name, Is.EqualTo(randomScores[0].GetObjective(i).Name));
            }
        }
Ejemplo n.º 10
0
 public Stage(IBase b, IObjectiveScores <T> startingPoint, IVector stepsLength, double alpha, double beta, CountingEvaluator countingEvaluator, IAlgebraProvider algebraProvider, ITerminationCondition <T> terminationCondition, IDictionary <string, string> logTags = null)
 {
     this.b                    = b;
     this.startingPoint        = startingPoint;
     this.currentPoint         = startingPoint;
     this.stepsLength          = stepsLength;
     this.alpha                = alpha;
     this.beta                 = beta;
     this.countingEvaluator    = countingEvaluator;
     this.terminationCondition = terminationCondition;
     this.algebraProvider      = algebraProvider;
     this.logTags              = logTags;
 }
Ejemplo n.º 11
0
            private IObjectiveScores <T>[] makeAMove(IBase b, IObjectiveScores <T> startPoint)
            {
                IObjectiveScores <T> newPoint = startPoint;
                int d = b.NumDimensions;

                IObjectiveScores <T>[] points = new IObjectiveScores <T> [d];
                for (int i = 0; i < d; i++)
                {
                    newPoint  = makeAMove(i, b, newPoint);
                    points[i] = newPoint;
                }
                return(points);
            }
Ejemplo n.º 12
0
            private IVector createVector(IObjectiveScores <T> startingPoint, IObjectiveScores <T> endPoint)
            {
                IVector result   = algebraProvider.CreateVector(startingPoint.SystemConfiguration.Dimensions);
                int     count    = result.Length;
                var     varNames = startingPoint.SystemConfiguration.GetVariableNames( );

                for (int i = 0; i < count; i++)
                {
                    result[i] =
                        endPoint.SystemConfiguration.GetValue(varNames[i]).ToDouble(null) -
                        startingPoint.SystemConfiguration.GetValue(varNames[i]).ToDouble(null);
                }
                return(result);
            }
Ejemplo n.º 13
0
        public void Execute( )
        {
            RiverSystemScenario testScenario;
            RiverSystemProject  testProject;

            TestHelperRiverSystem.getAPreconfiguredTestScenarioWithAWBM(3, out testProject,
                                                                        out testScenario);

            IObjectiveEvaluator <MetaParameterSet> evaluator        = buildNewEvaluator(testScenario);
            ICandidateFactory <MetaParameterSet>   candidateFactory = new LumpedAWBMFactory(testScenario, new BasicRngFactory(123));

            int pSetNumber = 5;

            IObjectiveScores[]     scores = new IObjectiveScores[pSetNumber];
            ISystemConfiguration[] pSets  = new ISystemConfiguration[pSetNumber];


            int k = 0;

            foreach (Catchment c in testScenario.Network.catchmentList)
            {
                foreach (StandardFunctionalUnit fu in c.FunctionalUnits)
                {
                    var list = AccessorMemberInfo.GetFieldsAndPropertiesList(fu.rainfallRunoffModel.GetType(), typeof(double), typeof(ParameterAttribute));
                    foreach (MemberInfo m in list)
                    {
                        string metaParameterName = "$tag" + k++;
                        UpdateFunctionBuilder.CreateMetaParameter(testScenario, metaParameterName);
                        UpdateFunctionBuilder.CreateUpdateFunctionWithLinkToMetaParameter(testScenario, metaParameterName, m, fu.rainfallRunoffModel);
                    }
                }
            }

            for (int i = 0; i < scores.Length; i++)
            {
                pSets[i] = candidateFactory.CreateRandomCandidate();

                //Do we need to reset the model states??
                scores[i] = evaluator.EvaluateScore((MetaParameterSet)pSets[i]);

                //print Params after each iteration
                LumpedAWBMFactory.printPrams(testScenario, pSets[i]);
            }
            IObjectiveScores[] paretoScores = ParetoRanking <IObjectiveScores> .GetParetoFront(scores);


            SourceUtility.PrintObjScores(scores);
            Console.WriteLine("----------------------------------------");
            SourceUtility.PrintObjScores(paretoScores);
        }
 private static List<ObjectiveScore> Convert(IObjectiveScores scores)
 {
     var scoreList = new List<ObjectiveScore>();
     for (int i = 0; i < scores.ObjectiveCount; i++)
     {
         var singleObjective = scores.GetObjective(i);
         scoreList.Add(
             new ObjectiveScore
             {
                 Name = singleObjective.Name,
                 Maximize = singleObjective.Maximise,
                 Value = singleObjective.ValueComparable.ToString()
             });
     }
     return scoreList;
 }
Ejemplo n.º 15
0
 static IObjectiveScores[] EvaluateScoresSerial <T>(T[] population, Func <bool> isCancelled,
                                                    IClonableObjectiveEvaluator <T> evaluator) where T : ISystemConfiguration
 {
     IObjectiveScores[] result = new IObjectiveScores[population.Length];
     for (int j = 0; j < population.Length; j++)
     {
         if (!isCancelled())
         {
             result [j] = evaluator.EvaluateScore(population [j]);
         }
         else
         {
             result [j] = null;
         }
     }
     return(result);
 }
Ejemplo n.º 16
0
 public MpiObjectiveScores(IObjectiveScores <MpiSysConfig> scores, string catchmentId = "")
 {
     this.config      = scores.SystemConfiguration;
     this.CatchmentId = catchmentId;
     this.scores      = new MpiObjectiveScore[scores.ObjectiveCount];
     for (int i = 0; i < this.scores.Length; i++)
     {
         var score = scores.GetObjective(i);
         this.scores[i] = new MpiObjectiveScore
         {
             name     = score.Name,
             maximise = score.Maximise,
             text     = score.GetText(),
             value    = (double)score.ValueComparable
         };
     }
 }
        private static List <ObjectiveScore> Convert(IObjectiveScores scores)
        {
            var scoreList = new List <ObjectiveScore>();

            for (int i = 0; i < scores.ObjectiveCount; i++)
            {
                var singleObjective = scores.GetObjective(i);
                scoreList.Add(
                    new ObjectiveScore
                {
                    Name     = singleObjective.Name,
                    Maximize = singleObjective.Maximise,
                    Value    = singleObjective.ValueComparable.ToString()
                });
            }
            return(scoreList);
        }
Ejemplo n.º 18
0
        public static IObjectiveScores[] CreateTestScores(int objectivesPerScore, MpiSysConfig[] configs)
        {
            int numScores = configs.Length;

            IObjectiveScores[] scores = new IObjectiveScores[numScores];
            for (int i = 0; i < numScores; i++)
            {
                IObjectiveScore[] objectives = new IObjectiveScore[objectivesPerScore];
                for (int j = 0; j < objectivesPerScore; j++)
                {
                    objectives[j] = new DoubleObjectiveScore(String.Format("Score:{0}-{1}", i, j), FakeScore(i + j), true);
                }

                scores[i] = new MpiObjectiveScores(objectives, configs[i]);
            }

            return(scores);
        }
Ejemplo n.º 19
0
        public void Execute(string modelRunDefnFile, string modelOutputName, string runoffToMatchFile)
        {
            setRandSeed( );
            var evaluator        = ProblemDefinitionHelper.BuildEvaluator(modelRunDefnFile, modelOutputName, runoffToMatchFile);
            var candidateFactory = buildCandidateFactory(ProblemDefinitionHelper.BuildSystem(modelRunDefnFile, "runoff").Model);

            var scores = new IObjectiveScores[1000];
            var pSets  = new ICloneableSystemConfiguration[1000];

            for (int i = 0; i < scores.Length; i++)
            {
                pSets[i]  = candidateFactory.CreateRandomCandidate( );
                scores[i] = evaluator.EvaluateScore(pSets[i]);
            }
            IObjectiveScores[] paretoScores = ParetoRanking <IObjectiveScores> .GetParetoFront(scores);

            printSummary(scores);
            Console.ReadLine( );
        }
Ejemplo n.º 20
0
        public void Execute()
        {
            SCH1ObjectiveEvaluator             evaluator = new SCH1ObjectiveEvaluator();
            UnivariateRealUniformRandomSampler rand      = new UnivariateRealUniformRandomSampler(-5, 5, 0);

            IObjectiveScores[] scores = new IObjectiveScores[1000];
            for (int i = 0; i < scores.Length; i++)
            {
                scores[i] = evaluator.EvaluateScore(new UnivariateReal(rand.GetNext()));
            }
            IObjectiveScores[] paretoScores = getParetoFront(scores);
            foreach (var score in paretoScores)
            {
                Console.Write(score.GetSystemConfiguration().GetConfigurationDescription());
                Console.Write(", ");
            }
            Console.WriteLine();
            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        public IOptimizationResults <T> Evolve()
        {
            Process p = new Process();

            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.CreateNoWindow         = true;
            p.StartInfo.FileName               = "pest";
            p.StartInfo.Arguments              = this.pestFileName;
            p.StartInfo.RedirectStandardOutput = true;


            p.Start();
            StreamReader            outputReader = p.StandardOutput;
            MessageQueueModelRunner mqmr         = new MessageQueueModelRunner(this.resultFileName, this.mr, this.modelOutputName);
            ParameterSet            pSet;
            var scores = new List <IObjectiveScores>();

            while (mqmr.RunIteration(out pSet))
            {
                currentGeneration++;
            }

            p.WaitForExit();

            if (0 == currentGeneration)
            {
                throw new PestExecutionFailedException("Pest executed 0 times.", outputReader.ReadToEnd());
                //throw new Exception(String.Format("PEST ran 0 times. Pest Output: \n {0}",outputReader.ReadToEnd()));
            }

            T sysConfig = (T) new TIMEModelParameterSet(pSet);
            IObjectiveScores <T>     score  = this.evaluator.EvaluateScore(sysConfig);
            IOptimizationResults <T> result = new CSIRO.Metaheuristics.Optimization.BasicOptimizationResults <T>(new IObjectiveScores[] { score });

            this.pestProcessOutput = outputReader.ReadToEnd();

            return(result);
        }
Ejemplo n.º 22
0
            private IObjectiveScores <T> makeAMove(int baseVectorIndex, IBase b, IObjectiveScores <T> startingPoint)
            {
                double stepSize          = stepsLength[baseVectorIndex];
                bool   success           = false;
                bool   moveMetConstraint = false;
                bool   bumped            = false;
                var    sysConfig         = (T)startingPoint.SystemConfiguration.Clone( );
                var    varnames          = sysConfig.GetVariableNames( );
                int    count             = sysConfig.Dimensions;

                for (int variableIndex = 0; variableIndex < count; variableIndex++)
                {
                    var minMax = Tuple.Create(sysConfig.GetMinValue(varnames[variableIndex]), sysConfig.GetMaxValue(varnames[variableIndex]));
                    bumped = !(tryAdd(varnames[baseVectorIndex], sysConfig, b[baseVectorIndex][variableIndex] * step(stepSize, minMax.Item1.ToDouble(null), minMax.Item2.ToDouble(null))));
                    if (bumped)
                    {
                        moveMetConstraint = true;
                    }
                }
                IObjectiveScores <T> candidatePoint = evaluate(sysConfig);
                var compareResult = compareObjScores(candidatePoint, startingPoint);

                success = compareResult <= 0;
                var result = success ? candidatePoint : startingPoint;

                if (success && !moveMetConstraint)
                {
                    stepsLength[baseVectorIndex]     *= (alpha);
                    moveSuccededOnce[baseVectorIndex] = true;
                }
                else
                {
                    stepsLength[baseVectorIndex]   *= (-beta);
                    moveFailedOnce[baseVectorIndex] = true;
                }
                return(result);
            }
Ejemplo n.º 23
0
            private IBase createNewBase(IBase b, IObjectiveScores <T> startingPoint, IObjectiveScores <T> endPoint)
            {
                IVector pathOfStage = createVector(startingPoint, endPoint);

                if (pathOfStage.IsNullVector)
                {
                    pathOfStage.SetAllComponents(1.0);
                }
                IBase result     = b.CreateNew(pathOfStage.Length);
                int   colinearTo = isColinearToBase(pathOfStage);

                if (colinearTo >= 0)
                {
                    return(result);
                }
                if (result.GetBaseVector(0).IsOrthogonal(pathOfStage))
                {
                    if (result.NumDimensions > 1)
                    {
                        if (!result.GetBaseVector(1).IsOrthogonal(pathOfStage))
                        {
                            result.SetBaseVector(1, pathOfStage);
                            result.Orthonormalize(1);
                        }
                    }
                    else
                    {
                        return(result);
                    }
                }
                else
                {
                    result.SetBaseVector(0, pathOfStage);
                    result.Orthonormalize(0);
                }
                return(result);
            }
        /// <summary>
        /// Calculates the per-objective means of the specified scores.
        /// </summary>
        /// <param name="scoresArray">The scores.</param>
        /// <param name="sysConfig">The sys config.</param>
        /// <returns>A single <see cref="IObjectiveScores"/> instance where the i'th score is the
        /// mean of the i'th score in each element of the scores array.
        /// The result score names are taken from the first input element.</returns>
        /// <remarks>
        /// This method makes a few assumptions which are not checked for performance reasons:
        ///     - each element in the scores array has the same objective count. More specifically,
        ///       if an element has more scores than element 0, the additional scores will be ignored.
        ///       If an element has less scores, then an out of bounds array access error will occur.
        ///     - score names are ignored. It is assumed that every element in scores has named scores
        ///       occuring in the same order.
        /// </remarks>
        public static MpiObjectiveScores Mean(MpiObjectiveScores[] scoresArray, MpiSysConfig sysConfig)
        {
            if (scoresArray == null)
            {
                throw new ArgumentNullException("scoresArray");
            }
            if (sysConfig == null)
            {
                throw new ArgumentNullException("sysConfig");
            }
            if (scoresArray.Length < 1)
            {
                throw new ArgumentException("Scores array is empty", "scoresArray");
            }

            IObjectiveScores referenceScore = scoresArray[0];
            int objectiveCount = referenceScore.ObjectiveCount;

            double[] means = new double[objectiveCount];
            for (int objectiveIdx = 0; objectiveIdx < objectiveCount; objectiveIdx++)
            {
                foreach (MpiObjectiveScores objectiveScores in scoresArray)
                {
                    means[objectiveIdx] += (double)objectiveScores.GetObjective(objectiveIdx).ValueComparable;
                }

                means[objectiveIdx] /= scoresArray.Length;
            }
            IObjectiveScore[] meanScores = new IObjectiveScore[objectiveCount];
            for (int i = 0; i < means.Length; i++)
            {
                meanScores[i] = new DoubleObjectiveScore(referenceScore.GetObjective(i).Name, means[i], referenceScore.GetObjective(i).Maximise);
            }

            return(new MpiObjectiveScores(meanScores, sysConfig));
        }
Ejemplo n.º 25
0
            public IObjectiveScores <MpiSysConfig>[] EvaluateScore(MpiSysConfig systemConfiguration)
            {
                int rank = comm.Rank;

                if (rank == 0)
                {
                    for (int i = 1; i < comm.Size; i++)
                    {
                        comm.Send(systemConfiguration, i, Convert.ToInt32(MpiMessageTags.SystemConfigurationMsgTag));
                    }
                    if (log.IsDebugEnabled)
                    {
                        log.Info("Process " + comm.Rank + " has sent the sys configs to evaluate");
                    }
                    IObjectiveScores <MpiSysConfig>[] allscores = new IObjectiveScores <MpiSysConfig> [comm.Size - 1];
                    IObjectiveScores <MpiSysConfig>   objscore  = null;
                    if (log.IsDebugEnabled)
                    {
                        log.Info("Process " + comm.Rank + " waiting to receive results from all slave processes");
                    }
                    for (int i = 1; i < comm.Size; i++)
                    {
                        comm.Receive(i, Convert.ToInt32(MpiMessageTags.EvalSlaveResultMsgTag), out objscore);
                        allscores[i - 1] = objscore;
                    }
                    if (log.IsDebugEnabled)
                    {
                        log.Info("Process " + comm.Rank + " has received all the scores evaluated ");
                    }
                    return(allscores);
                }
                else
                {
                    throw new NotSupportedException("MpiObjectiveEvaluator is designed to work with MPI process rank 0 only");
                }
            }
Ejemplo n.º 26
0
            public Tuple <IBase, IObjectiveScores <T>, IObjectiveScores <T>[]> Evolve()
            {
                moveFailedOnce   = createNegBoolArray(b.NumDimensions);
                moveSuccededOnce = createNegBoolArray(b.NumDimensions);
                List <IObjectiveScores <T> > scores = new List <IObjectiveScores <T> >();

                while (stageIsComplete( ) == false)
                {
                    var triedPoints = makeAMove(b, currentPoint);
                    currentPoint = triedPoints.Last();
                    scores.AddRange(triedPoints);
                    LoggerMhHelper.Write(new IObjectiveScores[] { currentPoint },
                                         createTag(),
                                         Logger);
                }
                endPoint = currentPoint;
                b        = createNewBase(b, startingPoint, endPoint);

                var paretoRanking = new ParetoRanking <IObjectiveScores <T> >(scores, new ParetoComparer <IObjectiveScores <T> >());

                IObjectiveScores <T>[] paretoScores = paretoRanking.GetDominatedByParetoRank(1);

                return(Tuple.Create(b, currentPoint, paretoScores));
            }
Ejemplo n.º 27
0
 public void Write(IObjectiveScores[] scores, IDictionary<string, string> tags)
 {
     if (log.IsInfoEnabled)
         log.Info(new SysConfigLogInfo(scores, tags));
 }
Ejemplo n.º 28
0
 private IBase createNewBase(IObjectiveScores <T> currentPoint)
 {
     return(AlgebraProvider.CreateBase(currentPoint.SystemConfiguration.Dimensions));
 }
Ejemplo n.º 29
0
 private Tuple <IBase, IObjectiveScores <T>, IObjectiveScores <T>[]> performStage(IBase b, IObjectiveScores <T> currentPoint, IVector stepsLength)
 {
     stage        = new Stage(b, currentPoint, stepsLength, alpha, beta, this.countingEvaluator, this.AlgebraProvider, this.terminationCondition, this.logTags);
     stage.Logger = Logger;
     return(stage.Evolve());
 }
Ejemplo n.º 30
0
 private int compareObjScores(IObjectiveScores <T> candidatePoint, IObjectiveScores <T> startingPoint)
 {
     return(this.comparer.Compare(candidatePoint, startingPoint));
 }
Ejemplo n.º 31
0
 public StringFinderResults(IObjectiveScores <StringSpecification> res)
 {
     this.Results = new List <IObjectiveScores <StringSpecification> >();
     Results.Add(res);
 }
Ejemplo n.º 32
0
        public static IObjectiveScores[] EvaluateScores <T>(IClonableObjectiveEvaluator <T> evaluator, T[] population, Func <bool> isCancelled, ParallelOptions parallelOptions = null) where T : ISystemConfiguration
        {
            if (population.Length == 0)
            {
                return(new IObjectiveScores[0]);
            }
            if (parallelOptions == null)
            {
                parallelOptions = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = -1
                }
            }
            ;

            var procCount = System.Environment.ProcessorCount;

            IObjectiveScores[] result;
            if (evaluator.SupportsThreadSafeCloning)
            {
                // There is presumably no point cloning
                // the system more times than the max level of parallelism
                int nParallel = procCount;
                if (parallelOptions.MaxDegreeOfParallelism > 0)
                {
                    nParallel = Math.Min(nParallel, parallelOptions.MaxDegreeOfParallelism);
                }
                T[][] subPop   = MetaheuristicsHelper.MakeBins(population, nParallel);
                var   taskPkgs = new List <Tuple <T[], IClonableObjectiveEvaluator <T> > >();
                taskPkgs.Add(Tuple.Create(subPop[0], evaluator));
                for (int i = 1; i < subPop.Length; i++)
                {
                    taskPkgs.Add(Tuple.Create(subPop[i], evaluator.Clone()));
                }
                // Need to use Parallel.ForEach rather than Parallel.For to work around a Parallel.For
                // oddity in Mono 3.12.1. Need an identity to iterate over...
                var ramp = new int[subPop.Length];
                // Map of index of subpopulations to indices in the variable result:
                //var offsets = new int[subPop.Length];
                var resultBins = new IObjectiveScores[ramp.Length][];
                for (int i = 1; i < ramp.Length; i++)
                {
                    ramp[i] = i;
                }
                Parallel.ForEach(ramp, parallelOptions,
                                 (i => {
                    resultBins[i] = EvaluateScoresSerial(taskPkgs[i], isCancelled);
                })
                                 );
                result = Gather(resultBins);
            }
            else
            {
                result = new IObjectiveScores[population.Length];
                for (int i = 0; i < population.Length; i++)
                {
                    if (!isCancelled())
                    {
                        result [i] = evaluator.EvaluateScore(population [i]);
                    }
                    else
                    {
                        result [i] = null;
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 33
0
 /// <summary>
 /// Creates a FitnessAssignedScores, a union of a candidate system configuration and its objective scores, and an overall fitness score.
 /// </summary>
 /// <param name="scores">Objective scores</param>
 /// <param name="fitnessValue">Fitness value, derived from the scores and context information such as a candidate population.</param>
 public FitnessAssignedScores(IObjectiveScores scores, T fitnessValue)
 {
     this.Scores       = scores;
     this.FitnessValue = fitnessValue;
 }