#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[Item("SchemaCleanupOperator", "Operator which removes the schemas from the global scope after they have been evaluated.")]
[StorableClass]
public class DiversificationStatisticsOperator : SingleSuccessorOperator {
private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
private const string NumberOfSchemasParameterName = "NumberOfSchemas";
private const string AverageSchemaLengthParameterName = "AverageSchemaLength";
private const string ResultCollectionParameterName = "Results";
private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
private readonly object locker = new object();
public ILookupParameter NumberOfChangedTreesParameter {
get { return (ILookupParameter)Parameters[NumberOfChangedTreesParameterName]; }
}
public ILookupParameter NumberOfSchemasParameter {
get { return (ILookupParameter)Parameters[NumberOfSchemasParameterName]; }
}
public ILookupParameter AverageSchemaLengthParameter {
get { return (ILookupParameter)Parameters[AverageSchemaLengthParameterName]; }
}
public ILookupParameter ResultCollectionParameter {
get { return (ILookupParameter)Parameters[ResultCollectionParameterName]; }
}
public ILookupParameter EvaluatedSolutionsParameter {
get { return (ILookupParameter)Parameters[EvaluatedSolutionsParameterName]; }
}
private int evaluatedSolutionsTracker;
public DiversificationStatisticsOperator() {
Parameters.Add(new LookupParameter(NumberOfChangedTreesParameterName));
Parameters.Add(new LookupParameter(NumberOfSchemasParameterName));
Parameters.Add(new LookupParameter(AverageSchemaLengthParameterName));
Parameters.Add(new LookupParameter(ResultCollectionParameterName));
Parameters.Add(new LookupParameter(EvaluatedSolutionsParameterName));
}
public override void InitializeState() {
base.InitializeState();
evaluatedSolutionsTracker = 0;
}
public override void ClearState() {
base.ClearState();
evaluatedSolutionsTracker = 0;
}
protected DiversificationStatisticsOperator(DiversificationStatisticsOperator original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) { return new DiversificationStatisticsOperator(this, cloner); }
[StorableConstructor]
protected DiversificationStatisticsOperator(bool deserializing) : base(deserializing) { }
public override IOperation Apply() {
var results = ResultCollectionParameter.ActualValue;
DataTable table;
if (!results.ContainsKey("NumberOfChangedTrees")) {
table = new DataTable();
results.Add(new Result("NumberOfChangedTrees", table));
var row = new DataRow("Changed trees") { VisualProperties = { StartIndexZero = true } };
table.Rows.Add(row);
}
if (!results.ContainsKey("AverageSchemaLength")) {
table = new DataTable();
results.Add(new Result("AverageSchemaLength", table));
var row = new DataRow("Average schema length") { VisualProperties = { StartIndexZero = true } };
table.Rows.Add(row);
}
if (!results.ContainsKey("NumberOfSchemas")) {
table = new DataTable();
results.Add(new Result("NumberOfSchemas", table));
var row = new DataRow("Number of schemas") { VisualProperties = { StartIndexZero = true } };
table.Rows.Add(row);
}
if (!results.ContainsKey("EvaluatedSolutionsPerGeneration")) {
table = new DataTable();
results.Add(new Result("EvaluatedSolutionsPerGeneration", table));
var row = new DataRow("Evaluated solutions") { VisualProperties = { StartIndexZero = true } };
table.Rows.Add(row);
row.Values.Add(0);
}
lock (locker) {
// count the extra evaluations performed after diversification
EvaluatedSolutionsParameter.ActualValue.Value += NumberOfChangedTreesParameter.ActualValue.Value;
}
var evaluatedSolutions = EvaluatedSolutionsParameter.ActualValue.Value;
if (evaluatedSolutions > 0)
evaluatedSolutions -= evaluatedSolutionsTracker;
((DataTable)results["NumberOfChangedTrees"].Value).Rows["Changed trees"].Values.Add(NumberOfChangedTreesParameter.ActualValue.Value);
((DataTable)results["AverageSchemaLength"].Value).Rows["Average schema length"].Values.Add(AverageSchemaLengthParameter.ActualValue.Value);
((DataTable)results["NumberOfSchemas"].Value).Rows["Number of schemas"].Values.Add(NumberOfSchemasParameter.ActualValue.Value);
((DataTable)results["EvaluatedSolutionsPerGeneration"].Value).Rows["Evaluated solutions"].Values.Add(evaluatedSolutions);
evaluatedSolutionsTracker += evaluatedSolutions;
return base.Apply();
}
}
}