#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
[Item("SolutionCachingAnalyzer", "An operator that records solutions.")]
[StorableClass]
public class SolutionCachingAnalyzer : SingleSuccessorOperator, IAnalyzer {
private const string ResultsParameterName = "Results";
#region IAnalyzer Members
public bool EnabledByDefault {
get { return false; }
}
#endregion
#region Parameter properties
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters[ResultsParameterName]; }
}
public ILookupParameter GenerationsParameter {
get { return (ILookupParameter)Parameters["Generations"]; }
}
public ScopeTreeLookupParameter SolutionParameter {
get { return (ScopeTreeLookupParameter)Parameters["Solution"]; }
}
#endregion
#region Properties
public ResultCollection Results {
get { return ResultsParameter.ActualValue; }
}
#endregion
[StorableConstructor]
private SolutionCachingAnalyzer(bool deserializing) : base(deserializing) { }
private SolutionCachingAnalyzer(SolutionCachingAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public SolutionCachingAnalyzer()
: base() {
Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection where the analysis values should be stored."));
Parameters.Add(new LookupParameter("Generations", "Nr of generations."));
Parameters.Add(new ScopeTreeLookupParameter("Solution", "The solutions that should be stored.", "TSPTour", 1));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SolutionCachingAnalyzer(this, cloner);
}
public override IOperation Apply() {
ItemArray solutions = SolutionParameter.ActualValue;
PermutationSolutionDictionary solDict;
if (!Results.ContainsKey("SolutionCache")) {
solDict = new PermutationSolutionDictionary();
Results.Add(new Result("SolutionCache", solDict));
} else {
solDict = (PermutationSolutionDictionary)Results["SolutionCache"].Value;
}
foreach (var sol in solutions) {
PermutationInformation info = new PermutationInformation();
info.Generation = GenerationsParameter.ActualValue.Value;
info.ProducedBy = ProducedBy.Mutation;
solDict.Add(new PermutationWrapper(sol), info);
}
return base.Apply();
}
}
}