public override IOperation Apply()
        {
            IEnumerable <int> rows = GenerateRowsToEvaluate();

            if (!rows.Any())
            {
                return(base.Apply());
            }

            var results = ResultCollection;

            // create empty parameter and result values
            if (ValidationBestSolutions == null)
            {
                ValidationBestSolutions         = new ItemList <S>();
                ValidationBestSolutionQualities = new ItemList <DoubleArray>();
                results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
                results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
            }

            //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
            IList <double[]> trainingBestQualities;

            if (UpdateAlways.Value)
            {
                trainingBestQualities = new List <double[]>();
            }
            else
            {
                trainingBestQualities = ValidationBestSolutionQualities.Select(x => x.ToArray()).ToList();
            }

            #region find best trees
            IList <int> nonDominatedIndexes            = new List <int>();
            ISymbolicExpressionTree[] tree             = SymbolicExpressionTree.ToArray();
            bool[]            maximization             = Maximization.ToArray();
            List <double[]>   newNonDominatedQualities = new List <double[]>();
            var               evaluator    = EvaluatorParameter.ActualValue;
            var               problemData  = ProblemDataParameter.ActualValue;
            IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);

            var qualities = tree
                            .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
                            .ToArray();
            for (int i = 0; i < tree.Length; i++)
            {
                if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
                    IsNonDominated(qualities[i], qualities, maximization))
                {
                    if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer()))
                    {
                        newNonDominatedQualities.Add(qualities[i]);
                        nonDominatedIndexes.Add(i);
                    }
                }
            }
            #endregion
            #region update Pareto-optimal solution archive
            if (nonDominatedIndexes.Count > 0)
            {
                ItemList <DoubleArray> nonDominatedQualities = new ItemList <DoubleArray>();
                ItemList <S>           nonDominatedSolutions = new ItemList <S>();
                // add all new non-dominated solutions to the archive
                foreach (var index in nonDominatedIndexes)
                {
                    S solution = CreateSolution(tree[index], qualities[index]);
                    nonDominatedSolutions.Add(solution);
                    nonDominatedQualities.Add(new DoubleArray(qualities[index]));
                }
                // add old non-dominated solutions only if they are not dominated by one of the new solutions
                for (int i = 0; i < trainingBestQualities.Count; i++)
                {
                    if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization))
                    {
                        if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer()))
                        {
                            nonDominatedSolutions.Add(ValidationBestSolutions[i]);
                            nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
                        }
                    }
                }

                results[ValidationBestSolutionsParameter.Name].Value         = nonDominatedSolutions;
                results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
            }
            #endregion
            return(base.Apply());
        }
Beispiel #2
0
        public override IOperation Apply()
        {
            var results = ResultCollection;

            // create empty parameter and result values
            if (TrainingBestSolutions == null)
            {
                TrainingBestSolutions         = new ItemList <T>();
                TrainingBestSolutionQualities = new ItemList <DoubleArray>();
                results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
                results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
            }

            if (!results.ContainsKey(TrainingBestSolutionParameterName))
            {
                results.Add(new Result(TrainingBestSolutionParameterName, "", typeof(ISymbolicDataAnalysisSolution)));
            }

            //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
            List <double[]> trainingBestQualities;

            if (UpdateAlways)
            {
                trainingBestQualities = new List <double[]>();
            }
            else
            {
                trainingBestQualities = TrainingBestSolutionQualities.Select(x => x.ToArray()).ToList();
            }

            ISymbolicExpressionTree[] trees     = SymbolicExpressionTree.ToArray();
            List <double[]>           qualities = Qualities.Select(x => x.ToArray()).ToList();

            bool[] maximization = Maximization.ToArray();

            var nonDominatedIndividuals = new[] { new { Tree = default(ISymbolicExpressionTree), Qualities = default(double[]) } }.ToList();

            nonDominatedIndividuals.Clear();

            // build list of new non-dominated solutions
            for (int i = 0; i < trees.Length; i++)
            {
                if (IsNonDominated(qualities[i], nonDominatedIndividuals.Select(ind => ind.Qualities), maximization) &&
                    IsNonDominated(qualities[i], trainingBestQualities, maximization))
                {
                    for (int j = nonDominatedIndividuals.Count - 1; j >= 0; j--)
                    {
                        if (IsBetterOrEqual(qualities[i], nonDominatedIndividuals[j].Qualities, maximization))
                        {
                            nonDominatedIndividuals.RemoveAt(j);
                        }
                    }
                    nonDominatedIndividuals.Add(new { Tree = trees[i], Qualities = qualities[i] });
                }
            }

            var nonDominatedSolutions = nonDominatedIndividuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList();

            nonDominatedSolutions.ForEach(s => s.Solution.Name = string.Join(",", s.Qualities.Select(q => q.ToString())));

            #region update Pareto-optimal solution archive
            if (nonDominatedSolutions.Count > 0)
            {
                //add old non-dominated solutions only if they are not dominated by one of the new solutions
                for (int i = 0; i < trainingBestQualities.Count; i++)
                {
                    if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization))
                    {
                        nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() });
                    }
                }

                //assumes the the first objective is always the accuracy
                var sortedNonDominatedSolutions = maximization[0]
          ? nonDominatedSolutions.OrderByDescending(x => x.Qualities[0])
          : nonDominatedSolutions.OrderBy(x => x.Qualities[0]);
                var trainingBestSolution = sortedNonDominatedSolutions.Select(s => s.Solution).First();
                results[TrainingBestSolutionParameterName].Value = trainingBestSolution;
                TrainingBestSolutions = new ItemList <T>(sortedNonDominatedSolutions.Select(x => x.Solution));
                results[TrainingBestSolutionsParameter.Name].Value = TrainingBestSolutions;
                TrainingBestSolutionQualities = new ItemList <DoubleArray>(sortedNonDominatedSolutions.Select(x => new DoubleArray(x.Qualities)));
                results[TrainingBestSolutionQualitiesParameter.Name].Value = TrainingBestSolutionQualities;
            }
            #endregion
            return(base.Apply());
        }