Exemplo n.º 1
0
        public override void Evaluate(Solution solution)
        {
#if DEBUG
            if (_counter == 0)
            {
                if (Model.Setting.Logging)
                {
                    LogArrayValues(FeatureTarget.Values, "targetFeatures");
                    if (Model.Setting.NumberOfInteractions > 0)
                    {
                        LogArrayValues(InteractionTarget.Values, "targetInteraction");
                    }
                }
            }

            _counter++;
#endif
            Console.WriteLine(_counter);
            Distribution variantTarget = null;
            if (!Model.Setting.NoVariantCalculation)
            {
                var localScaledVariants = new double[ScaledVariantTarget.Values.Length];
                Array.Copy(ScaledVariantTarget.Values, localScaledVariants, ScaledVariantTarget.Values.Length);
                variantTarget = new Distribution(localScaledVariants);
            }
            var doubleVal  = new double[Model.Setting.NumberOfFeatures];
            var interacVal = new double[Model.Setting.NumberOfInteractions];
            var values     = new XReal(solution);
            for (var i = 0; i < Model.Setting.NumberOfFeatures; i++)
            {
                doubleVal[i] = values.GetValue(i);
            }

            for (var i = 0; i < Model.Setting.NumberOfInteractions; i++)
            {
                interacVal[i] = values.GetValue(i + Model.Setting.NumberOfFeatures);
            }

            //the distribution from the NSGA2
            var nsgaFv      = new Distribution(doubleVal);
            var interacDist = new Distribution(interacVal);

            var watch = Stopwatch.StartNew();
            //calculate the variant values
            Distribution variantResult = null;

            if (_counter > 9500)
            {
                var variantValuesWithoutInteraction = FeatureMatrix.Dot(doubleVal);



                if (Model.Setting.NumberOfInteractions > 0)
                {
                    var interacVals    = InteractionMatrix.Dot(interacVal);
                    var variantResults = variantValuesWithoutInteraction.Add(interacVals);
                    variantResult = new Distribution(variantResults);
                }
                else
                {
                    variantResult = new Distribution(variantValuesWithoutInteraction);
                }
            }
            watch.Stop();
            calcTime += watch.ElapsedMilliseconds;

            //scale the variant target distribution to the size of the calculated variant distribution

            var       scaleWatch = Stopwatch.StartNew();
            FMScaling fms        = new FMScaling(Model);
            if (Model.Setting.ScaleToGlobalMinMax)
            {
                var change = false;
                if (currentMin > variantResult.Values.Min())
                {
                    currentMin = variantResult.Values.Min();
                    change     = true;
                }
                if (currentMax < variantResult.Values.Max())
                {
                    currentMax = variantResult.Values.Max();
                    change     = true;
                }
                if (change)
                {
                    ScaledVariantTarget = FMScaling.InteractionToScale(ScaledVariantTarget, currentMin, currentMax);
                }
            }
            else
            {
                if (!Model.Setting.NoVariantCalculation)
                {
                    variantTarget = FMScaling.InteractionToScale(variantTarget, variantResult.Values.Min(),
                                                                 variantResult.Values.Max());
                }
            }

            scaleWatch.Stop();
            scaleTime += scaleWatch.ElapsedMilliseconds;

            IntergenSolution s = (IntergenSolution)solution;
            s.FoundAtEval = _counter;


            var testWatch = Stopwatch.StartNew();
            //calculate the fitness values for features and variants



            var fc            = new FitnessCalculator(Model, _counter);
            var fitnessValues = fc.Calculate(variantResult, variantTarget, nsgaFv, FeatureTarget, interacDist, InteractionTarget);
            solution.Objective[0] = fitnessValues.FeatureVal;

            if (Model.Setting.NoVariantCalculation)
            {
                if (Model.Setting.NumberOfInteractions > 0)
                {
                    solution.Objective[1] = fitnessValues.InteracVal;
                }
            }
            else
            {
                //interacs and variants
                if (Model.Setting.NumberOfInteractions > 0)
                {
                    solution.Objective[1] = fitnessValues.InteracVal;
                    solution.Objective[2] = fitnessValues.VariantVal;
                }
                else
                {
                    solution.Objective[1] = fitnessValues.VariantVal;
                }
            }



            /* if (Model.Setting.UseKs)
             * {
             *  PerfomKs(variantResult, variantTarget, nsgaFv, FeatureTarget, solution);
             *
             * }
             * else if (Model.Setting.UseCmv)
             * {
             *  PerformCmv(variantResult, variantTarget, nsgaFv, FeatureTarget, InteractionTarget, interacDist, solution);
             * }
             * else if (Model.Setting.UseEuclidean) {
             *  var bd = new BinnedDistance(variantTarget, variantResult, Model.VariantDynamicHist, _counter);
             *  var bd2 = new BinnedDistance(nsgaFv, FeatureTarget, Model.FeaturesDynamicHist, _counter);
             *
             *  solution.Objective[0] = bd2.EuclidianDist();
             *  solution.Objective[1] = bd.EuclidianDist();
             *  if (Model.Setting.NumberOfInteractions > 0)
             *  {
             *      var interac = new BinnedDistance(interacDist, InteractionTarget, Model.InteracDynamicHist, _counter);
             *      solution.Objective[2] = interac.EuclidianDist();
             *  }
             * }
             * else if (Model.Setting.UseChiSquared)
             * {
             *  var bd = new BinnedDistance(variantTarget, variantResult, Model.VariantDynamicHist,
             *      _counter);
             *  var bd2 = new BinnedDistance(nsgaFv, FeatureTarget, Model.FeaturesDynamicHist, _counter);
             *
             *
             *  solution.Objective[0] = bd2.ChiSquaredDist();
             *  solution.Objective[1] = bd.ChiSquaredDist();
             *  if (Model.Setting.NumberOfInteractions > 0)
             *  {
             *      var interac = new BinnedDistance(interacDist, InteractionTarget, Model.InteracDynamicHist, _counter);
             *      solution.Objective[2] = interac.ChiSquaredDist();
             *  }
             * }
             * else if (Model.Setting.EuclAndCmv)
             * {
             *  PerformCmv(variantTarget, variantResult, nsgaFv, FeatureTarget, InteractionTarget, interacDist, solution);
             *
             *  var variant = new BinnedDistance(variantTarget, variantResult, Model.VariantDynamicHist, _counter);
             *  var feat = new BinnedDistance(nsgaFv, FeatureTarget, Model.FeaturesDynamicHist, _counter);
             *  var interac = new BinnedDistance(interacDist, InteractionTarget, Model.InteracDynamicHist, _counter);
             *
             *  solution.Objective[3] = feat.EuclidianDist();
             *  solution.Objective[4] = variant.EuclidianDist();
             *  solution.Objective[5] = interac.EuclidianDist();
             * }
             * else if (Model.Setting.ChiAndCmv)
             * {
             *  PerformCmv(variantTarget, variantResult, nsgaFv, FeatureTarget, InteractionTarget, interacDist, solution);
             * var variant = new BinnedDistance(variantTarget, variantResult, Model.VariantDynamicHist,_counter);
             *  var feat = new BinnedDistance(nsgaFv, FeatureTarget, Model.FeaturesDynamicHist, _counter);
             *  var interac = new BinnedDistance(interacDist, InteractionTarget, Model.InteracDynamicHist, _counter);
             *  solution.Objective[3] = feat.ChiSquaredDist();
             *  solution.Objective[4] = variant.ChiSquaredDist();
             *  solution.Objective[5] = interac.ChiSquaredDist();
             * }
             */
            testWatch.Stop();
            fitnessTime += testWatch.ElapsedMilliseconds;

            //report our progress to the model for GUI
            if (_counter % 200 == 0)
            {
                if (!Model.Setting.Parallel /*|| (Model.Parallel && parallelIndex % 50 == 0) */)
                {
                    if (Model.Setting.DrawDensity && Model.Setting.DrawHistogram)
                    {
                        RIntegrator.FeatureHistAndDens(nsgaFv.Values, FeatureTarget.Values);
                        RIntegrator.VariantHistAndDens(variantResult.Values, variantTarget.Values);
                    }
                    else if (Model.Setting.DrawDensity)
                    {
                        RIntegrator.PlotFeatureTarget(nsgaFv.Values, FeatureTarget.Values, Model.Setting.FeatureAdjust);
                        if (Model.Setting.NoVariantCalculation)
                        {
                            if (_counter > 9500)
                            {
                                RIntegrator.PlotVariantTarget(variantResult.Values);
                            }
                        }
                        else
                        {
                            RIntegrator.PlotVariantTarget(variantResult.Values, variantTarget.Values);
                        }

                        if (Model.Setting.NumberOfInteractions > 0)
                        {
                            RIntegrator.PlotInteracTarget(interacDist.Values, InteractionTarget.Values);
                        }
                    }
                    else if (Model.Setting.DrawHistogram)
                    {
                        RIntegrator.FeatureComparisonHist(nsgaFv.Values, FeatureTarget.Values);

                        if (Model.Setting.NoVariantCalculation)
                        {
                            RIntegrator.PlotVariantTarget(variantResult.Values);
                        }
                        else
                        {
                            RIntegrator.VariantComparisonHisto(variantResult.Values, variantTarget.Values);
                        }
                    }


                    else
                    {
                    }

                    //ReportProgress(solution);
                }
                ReportProgress(solution, fitnessValues);
                //Worker.ReportProgress((int)(counter * 100 / (double)Model.MaxEvaluations), new UserProgress { VariantP = solution.Objective[1], FeatureP = solution.Objective[0] });
            }

            if (_counter == Model.Setting.MaxEvaluations)
            {
                Console.WriteLine("FitnessTime: " + fitnessTime);
                Console.WriteLine("ScaleTime: " + scaleTime);
                Console.WriteLine("CalcTime: " + calcTime);
            }

            var cont = new SolutionContainer
            {
                //TargetVariant = variantTarget,
                Variant     = variantResult,
                Features    = nsgaFv,
                Interaction = interacDist,
                //TargetFeatures = FeatureTarget,
                FeatureTVal = fitnessValues.FeatureVal,
                InteracTVal = fitnessValues.InteracVal,
                VariantTVal = fitnessValues.VariantVal,

                CalcTime    = watch.ElapsedMilliseconds,
                FitnessTime = testWatch.ElapsedMilliseconds,
                ScaleTime   = scaleWatch.ElapsedMilliseconds,
                TestName    = GetUsedTest(),
                FoundAtEval = _counter,
            };


            //if (_counter > 7000)
            //  {
            //    cont.Write(Model.Setting.LogFolder + "evalstep" + _counter + ".json");
            // }

            //cont = null;
            //Console.WriteLine(cont.FoundAtEval + "\t" +  cont.FeatureTVal + "\t" + cont.VariantTVal);



            if (Model.Setting.ChiAndCmv || Model.Setting.EuclAndCmv)
            {
                cont.AdditionalFeatureCmv = solution.Objective[4];
                cont.AdditionalVariantCmv = solution.Objective[5];
            }

            //if (!Model.Setting.Parallel)Model.History.Add(counter, cont);
            Model.AddSolutionToHistory(cont);

            if (!Model.Setting.Logging)
            {
                return;
            }
            var usedTest = GetUsedTest();

            //LogArrayValues(nsgaFV.Values, "Features");
            //LogArrayValues(variantResult.Values, "Variants");
            //LogArrayValues(ScaledVariantTarget.Values, "targetVariants");

            if (Model.Setting.NumberOfInteractions > 0)
            {
                LogSingleValue(fitnessValues.InteracVal, "InteracFitn" + usedTest);
            }
            if (!Model.Setting.NoVariantCalculation)
            {
                LogSingleValue(fitnessValues.VariantVal, "VarFitn" + usedTest);
            }
            LogSingleValue(fitnessValues.FeatureVal, "FeatFitn" + usedTest);
        }
Exemplo n.º 2
0
        public static void Plot(Solution solution)
        {
            if (!_model.Setting.DrawDensity && !_model.Setting.DrawHistogram)
            {
                return;
            }
            _counter++;

            if (_counter % (_model.Setting.PlotStepSize / _model.Setting.PopulationSize) != 0)
            {
                return;
            }

            var doubleVal  = new double[_model.Setting.NumberOfFeatures];
            var interacVal = new double[_model.Setting.NumberOfInteractions];
            var values     = new XReal(solution);

            for (var i = 0; i < _model.Setting.NumberOfFeatures; i++)
            {
                doubleVal[i] = values.GetValue(i);
            }
            if (_model.Setting.NumberOfInteractions > 0)
            {
                for (var i = 0; i < _model.Setting.NumberOfInteractions; i++)
                {
                    interacVal[i] = values.GetValue(i + _model.Setting.NumberOfFeatures);
                }
            }

            if (_model.Setting.DrawDensity)
            {
                RIntegrator.PlotFeatureTarget(doubleVal, _model.DStore.SelectedFeatureDistribution.Values,
                                              _model.Setting.FeatureAdjust);
            }
            if (_model.Setting.DrawHistogram)
            {
                RIntegrator.FeatureHistAndDens(doubleVal, _model.DStore.SelectedFeatureDistribution.Values);
            }

            if (_model.Setting.NumberOfInteractions > 0)
            {
                if (_model.Setting.DrawDensity)
                {
                    RIntegrator.PlotInteracTarget(interacVal, _model.DStore.SelectedInteractionDistribution.Values);
                }
                if (_model.Setting.DrawHistogram)
                {
                    RIntegrator.InteracHist(interacVal, _model.DStore.SelectedInteractionDistribution.Values);
                }
                if (!_model.Setting.NoVariantCalculation)
                {
                    //interactions and variants
                    var variantValuesWithoutInteraction = _model.FeatureMatrix.Dot(doubleVal);
                    var interacVals    = _model.InteractionMatrix.Dot(interacVal);
                    var variantResults = variantValuesWithoutInteraction.Add(interacVals);
                    if (_model.ScaledVariantTarget != null)
                    {
                        FMScaling.InteractionToScale(_model.ScaledVariantTarget, variantResults.Min(),
                                                     variantResults.Max());
                        if (_model.Setting.DrawDensity)
                        {
                            RIntegrator.PlotVariantTarget(variantResults, _model.ScaledVariantTarget.Values);
                        }
                        if (_model.Setting.DrawHistogram)
                        {
                            RIntegrator.VariantComparisonHisto(variantResults, _model.ScaledVariantTarget.Values);
                        }
                    }
                    else
                    {
                        if (_model.Setting.DrawDensity)
                        {
                            RIntegrator.PlotVariantTarget(variantResults);
                        }
                    }
                }
                else
                {
                    //interactions and no variants
                }
            }
            else
            {
                if (!_model.Setting.NoVariantCalculation)
                {
                    //no interactions and variants
                    var variantResults = _model.FeatureMatrix.Dot(doubleVal);
                    //FMScaling.InteractionToScale(_model.ScaledVariantTarget, variantResults.Min(),
                    //    variantResults.Max());
                    if (_model.Setting.DrawDensity)
                    {
                        RIntegrator.PlotVariantTarget(variantResults);
                    }
                    if (_model.Setting.DrawHistogram)
                    {
                        RIntegrator.VariantComparisonHisto(variantResults, _model.ScaledVariantTarget.Values);
                    }
                }
            }
        }