public Solution[] Seed(Solution[] solutions)
        {
            //Console.WriteLine($"Solution obj coeffs len input = {solutions[0].ObjectCoefficients.Length}");
            var numberOfPoints = _positivePoints.Length;

            foreach (var solution in solutions)
            {
                //Console.ReadKey();
                //Console.WriteLine("Solution");
                //Console.WriteLine(new StringBuilder().AppendArray(solution.ObjectCoefficients).ToString());
                var newObjectCoefficients = new List <double>(solution.ObjectCoefficients.Length);
                var constraints           = _constraintsBuilder.BuildConstraints(solution);

                foreach (var constraint in constraints)
                {
                    //Console.WriteLine("Constraint");
                    var bestFitnessScore  = 0.0;
                    var bestLimitingValue = 0.0;

                    for (var i = 0; i < numberOfPoints; i++)
                    {
                        var leftSideValue = constraint.GetLeftSideValue(_positivePoints[i]);
                        constraint.LimitingValue = leftSideValue;

                        var singleConstraintAsSolution = new Solution(constraint.Terms.Length + 1)
                        {
                            ObjectCoefficients = constraint.GetAllCoefficients()
                        };

                        var newFitnessScore = _evaluator.Evaluate(singleConstraintAsSolution);

                        if (newFitnessScore > bestFitnessScore)
                        {
                            bestFitnessScore  = newFitnessScore;
                            bestLimitingValue = leftSideValue;
                        }
                    }

                    constraint.LimitingValue = bestLimitingValue;
                    newObjectCoefficients.AddRange(constraint.GetAllCoefficients());
                }

                solution.ObjectCoefficients = newObjectCoefficients.ToArray();
                //Console.WriteLine(new StringBuilder().AppendArray(solution.ObjectCoefficients).ToString());
            }
            //Console.WriteLine($"Solution obj coeffs len output = {solutions[0].ObjectCoefficients.Length}");
            //Console.WriteLine("#####################");
            //Console.WriteLine("Seeding finished!");

            return(solutions);
        }
Пример #2
0
        public override double Evaluate(Solution solution)
        {
            var numberOfPositivePointsSatisfyingConstraints = 0;
            var numberOfNegativePointsSatisfyingConstraints = 0;
            var numberOfPositivePoints = _positivePoints.Length;
            var numberOfNegativePoints = _negativePoints.Length;
            var constraints            = _constraintsBuilder.BuildConstraints(solution);

            //TODO
            //var mean = constraints.Sum(c => c.GetTermsCoefficients().Min(tc => Math.Abs(tc)));
            //var modifier = mean > 1 ? 1.0 / mean : 1;

            for (var i = 0; i < numberOfPositivePoints; i++)
            {
                if (constraints.IsSatisfyingConstraints(_positivePoints[i]))
                {
                    numberOfPositivePointsSatisfyingConstraints++;
                }
            }

            for (var i = 0; i < numberOfNegativePoints; i++)
            {
                if (constraints.IsSatisfyingConstraints(_negativePoints[i]))
                {
                    numberOfNegativePointsSatisfyingConstraints++;
                }
            }

            //Console.Write($"Positive satisfying = {numberOfPositivePointsSatisfyingConstraints} ### ");
            //Console.Write($"Negative satisfying = {numberOfNegativePointsSatisfyingConstraints}\n");
            //Thread.Sleep(100);

            //TODO
            return((double)numberOfPositivePointsSatisfyingConstraints / (_positivePoints.Length + numberOfNegativePointsSatisfyingConstraints));
            //return ((double)numberOfPositivePointsSatisfyingConstraints / (_positivePoints.Length + numberOfNegativePointsSatisfyingConstraints)) * modifier;
        }
        public MathModel SynthesizeModel(Point[] trainingPoints)
        {
            var means = trainingPoints.Means();
            var standardDeviations = trainingPoints.StandardDeviations(means);

            if (Parameters.UseDataNormalization)
            {
                trainingPoints           = _pointsNormalizer.ApplyProcessing(trainingPoints);
                NormalizedTrainingPoints = trainingPoints.DeepCopyByExpressionTree();
            }

            var evolutionEnginesFactory = new EnginesFactory();
            var evolutionEngine         = evolutionEnginesFactory.Create(Parameters.EvolutionParameters);
            var positiveTrainingPoints  = trainingPoints.Where(tp => tp.ClassificationType == ClassificationType.Positive).ToArray();
            var negativeTrainingPoints  = trainingPoints.Where(tp => tp.ClassificationType == ClassificationType.Negative).ToArray();
            var evaluator = new Evaluator(positiveTrainingPoints, negativeTrainingPoints, _constraintsBuilder);

            //HACK TODO

            //var numberOfConstraintsCoefficients = Parameters.MaximumNumberOfConstraints * (Parameters.NumberOfDimensions + 1);
            //var constraintsCoefficients = new List<double>(numberOfConstraintsCoefficients);
            //var benchmarkConstraintsIndexer = 0;

            //for (var i = 0; i < Parameters.MaximumNumberOfConstraints; i++)
            //{
            //    if (benchmarkConstraintsIndexer >= Benchmark.Constraints.Length)
            //        benchmarkConstraintsIndexer = 0;

            //    constraintsCoefficients.AddRange(Benchmark.Constraints[benchmarkConstraintsIndexer].Terms.Select(t => t.Coefficient));
            //    constraintsCoefficients.Add(Benchmark.Constraints[benchmarkConstraintsIndexer++].LimitingValue);
            //}

            //PopulationGeneratorBase.ObjectCoefficients = constraintsCoefficients.ToArray();

            //

            Solution bestSolution;

            if (Parameters.UseSeeding)
            {
                var singleConstraintModel = Parameters.TypeOfBenchmark == BenchmarkType.Balln && Parameters.AllowQuadraticTerms
                    ? Benchmark.Constraints.Take(1).DeepCopyByExpressionTree().ToArray()
                    : new[] { new LinearConstraint(Benchmark.Constraints.First().Terms.Where(t => t.Type == TermType.Linear).ToArray(), 0) };
                var singleConstraintBuilder   = new ConstraintsBuilder(singleConstraintModel);
                var singleConstraintEvaluator = new Evaluator(positiveTrainingPoints, negativeTrainingPoints, singleConstraintBuilder);
                var seedingProcessor          = new SeedingProcessor(singleConstraintEvaluator, _constraintsBuilder, positiveTrainingPoints);

                bestSolution = evolutionEngine.RunEvolution(evaluator, seedingProcessor);
            }
            else
            {
                bestSolution = evolutionEngine.RunEvolution(evaluator);
            }

            CoreEvolutionSteps = evolutionEngine.EvolutionSteps;

            Statistics.EvolutionStatistics = evolutionEngine.Statistics;
            var synthesizedConstraints = _constraintsBuilder.BuildConstraints(bestSolution);

            if (Parameters.UseDataNormalization)
            {
                NormalizedSynthesizedConstraints = synthesizedConstraints.DeepCopyByExpressionTree();
                _constaintsDenormalizer          = new StandardScoreConstraintsDenormalizer(means, standardDeviations);
                synthesizedConstraints           = _constaintsDenormalizer.ApplyProcessing(synthesizedConstraints);
            }

            if (Parameters.TrackEvolutionSteps)
            {
                var evolutionStepsAsSolutions = evolutionEngine.EvolutionStepsSimple.ToList();

                foreach (var evolutionStepsAsSolution in evolutionStepsAsSolutions)
                {
                    var evolutionStep = _constraintsBuilder.BuildConstraints(evolutionStepsAsSolution);

                    if (Parameters.UseDataNormalization)
                    {
                        NormalizedEvolutionSteps.Add(evolutionStep.DeepCopyByExpressionTree());
                        evolutionStep = _constaintsDenormalizer.ApplyProcessing(evolutionStep);
                    }

                    EvolutionSteps.Add(evolutionStep);
                }
            }

            if (Parameters.UseRedundantConstraintsRemoving)
            {
                _stoper.Restart();
                synthesizedConstraints = _redundantConstraintsRemover.ApplyProcessing(synthesizedConstraints);
                _stoper.Stop();
                Statistics.RedundantConstraintsRemovingTime = _stoper.Elapsed;
            }

            MathModel = new MathModel(synthesizedConstraints, Benchmark);
            Statistics.NumberOfConstraints = synthesizedConstraints.Length;
            Statistics.MeanAngle           = Parameters.TypeOfBenchmark != BenchmarkType.Balln ? _meanAngleCalculator.Calculate(synthesizedConstraints, Benchmark.Constraints) : double.NaN;

            return(MathModel);
        }