#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
namespace HeuristicLab.OSGAEvaluator {
[Item("OSGAPredictionCountsAnalyzer", "An analyzer which records the counts of the rejected offspring predicted by the evaluator without evaluating all rows.")]
[StorableClass]
public class OSGAPredictionCountsAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
private const string EvaluatorParameterName = "Evaluator";
private const string ResultCollectionParameterName = "Results";
public ILookupParameter EvaluatorParameter {
get { return (ILookupParameter)Parameters[EvaluatorParameterName]; }
}
public OSGAPredictionCountsAnalyzer() {
Parameters.Add(new LookupParameter(EvaluatorParameterName));
}
protected OSGAPredictionCountsAnalyzer(OSGAPredictionCountsAnalyzer original, Cloner cloner) : base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new OSGAPredictionCountsAnalyzer(this, cloner);
}
public override IOperation Apply() {
var evaluator = EvaluatorParameter.ActualValue as SymbolicRegressionSingleObjectiveOsgaEvaluator;
if (evaluator == null)
return base.Apply();
var rejectedStats = evaluator.RejectedStats;
var totalStats = evaluator.TotalStats;
if (rejectedStats.Rows == 0 || totalStats.Rows == 0)
return base.Apply();
ResultCollection predictionResults;
DataTable rejectedStatsTable, totalStatsTable, rejectedStatsPerGenerationTable;
if (!ResultCollection.ContainsKey("OS Prediction")) {
predictionResults = new ResultCollection();
rejectedStatsTable = new DataTable("Rejected Stats");
foreach (var rowName in rejectedStats.RowNames) {
rejectedStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
}
predictionResults.Add(new Result("Rejected Stats", rejectedStatsTable));
totalStatsTable = new DataTable("Total Stats");
foreach (var rowName in totalStats.RowNames) {
totalStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
}
predictionResults.Add(new Result("Total Stats", totalStatsTable));
rejectedStatsPerGenerationTable = new DataTable("Rejected Per Generation");
foreach (var rowName in rejectedStats.RowNames) {
rejectedStatsPerGenerationTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Line, LineWidth = 1 } });
rejectedStatsPerGenerationTable.Rows[rowName].Values.Add(0d);
}
predictionResults.Add(new Result("Rejected Stats Per Generation", rejectedStatsPerGenerationTable));
ResultCollection.Add(new Result("OS Prediction", predictionResults));
} else {
predictionResults = (ResultCollection)ResultCollection["OS Prediction"].Value;
rejectedStatsTable = (DataTable)predictionResults["Rejected Stats"].Value;
totalStatsTable = (DataTable)predictionResults["Total Stats"].Value;
rejectedStatsPerGenerationTable = (DataTable)predictionResults["Rejected Stats Per Generation"].Value;
}
int i = 0;
foreach (var rowName in rejectedStats.RowNames) {
// pad values with a 0 on each side to prevent clipping
rejectedStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, rejectedStats.Columns).Select(j => (double)rejectedStats[i, j])).Concat(new[] { 0d }));
++i;
}
i = 0;
foreach (var rowName in totalStats.RowNames) {
totalStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, totalStats.Columns).Select(j => (double)totalStats[i, j])).Concat(new[] { 0d }));
var row = rejectedStatsPerGenerationTable.Rows[rowName];
var sum = row.Values.Sum();
row.Values.Add(totalStats[i, 0] - sum);
++i;
}
return base.Apply();
}
}
}