public override void Evaluate(Solution solution) { var s = (IntergenSolution)solution; 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 variantValuesWithoutInteraction = FeatureMatrix.Dot(doubleVal); var interacVals = InteractionMatrix.Dot(interacVal); var variantResults = variantValuesWithoutInteraction.Add(interacVals); var variantResult = new Distribution(variantResults); var fc = new FitnessCalculator(Model, s.FoundAtEval); var fitnessValues = fc.Calculate(null, null, null, null, interacDist, InteractionTarget); solution.Objective[0] = fitnessValues.InteracVal; FitnessTracker.AddInterac(s.FoundAtEval, fitnessValues.InteracVal); }
public override void Evaluate(Solution solution) { var s = (IntergenSolution)solution; 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 multiplicationTime = new Stopwatch(); multiplicationTime.Start(); var variantValuesWithoutInteraction = FeatureMatrix.Dot(doubleVal); var interacVals = InteractionMatrix.Dot(interacVal); var variantResults = variantValuesWithoutInteraction.Add(interacVals); multiplicationTime.Stop(); FitnessTracker.AddMultiplicationTime(multiplicationTime.ElapsedMilliseconds); var variantResult = new Distribution(variantResults); var localScaledVariants = new double[VariantTarget.Values.Length]; Array.Copy(VariantTarget.Values, localScaledVariants, VariantTarget.Values.Length); var localVariantTarget = new Distribution(localScaledVariants); localVariantTarget = FMScaling.InteractionToScale(localVariantTarget, variantResults.Min(), variantResults.Max()); var fc = new FitnessCalculator(Model, s.FoundAtEval); var fitnessValues = fc.Calculate(variantResult, localVariantTarget, nsgaFv, FeatureTarget, interacDist, InteractionTarget); solution.Objective[0] = fitnessValues.FeatureVal; solution.Objective[1] = fitnessValues.InteracVal; solution.Objective[2] = fitnessValues.VariantVal; FitnessTracker.AddCalcTime(fc.GetFitnessTime()); FitnessTracker.Add(s.FoundAtEval, fitnessValues.FeatureVal, fitnessValues.InteracVal, fitnessValues.VariantVal); }
public override void Evaluate(Solution solution) { var s = (IntergenSolution)solution; var doubleVal = new double[Model.Setting.NumberOfFeatures]; var values = new XReal(solution); for (var i = 0; i < Model.Setting.NumberOfFeatures; i++) { doubleVal[i] = values.GetValue(i); } //the distribution from the NSGA2 var nsgaFv = new Distribution(doubleVal); var variantResults = FeatureMatrix.Dot(doubleVal); var variantResult = new Distribution(variantResults); var localScaledVariants = new double[VariantTarget.Values.Length]; Array.Copy(VariantTarget.Values, localScaledVariants, VariantTarget.Values.Length); var localVariantTarget = new Distribution(localScaledVariants); localVariantTarget = FMScaling.InteractionToScale(localVariantTarget, variantResults.Min(), variantResults.Max()); var fc = new FitnessCalculator(Model, s.FoundAtEval); var fitnessValues = fc.Calculate(variantResult, localVariantTarget, nsgaFv, FeatureTarget, null, null); solution.Objective[0] = fitnessValues.FeatureVal; solution.Objective[1] = fitnessValues.VariantVal; FitnessTracker.AddFeatVar(s.FoundAtEval, fitnessValues.FeatureVal, fitnessValues.VariantVal); }
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); }