using System.Linq;
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.MetaOptimization {
///
/// TODO
///
[Item("SolutionCacheAnalyzer", "")]
[StorableClass]
public sealed class SolutionCacheAnalyzer : SingleSuccessorOperator, IAnalyzer {
public bool EnabledByDefault {
get { return true; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public ScopeTreeLookupParameter ParameterConfigurationParameter {
get { return (ScopeTreeLookupParameter)Parameters["ParameterConfigurationTree"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public LookupParameter> ProblemsParameter {
get { return (LookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
}
public LookupParameter GenerationsParameter {
get { return (LookupParameter)Parameters["Generations"]; }
}
public ValueParameter StoreAllRunsParameter {
get { return (ValueParameter)Parameters["StoreAllRuns"]; }
}
public SolutionCacheAnalyzer()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("Quality", ""));
Parameters.Add(new ValueLookupParameter("Results", ""));
Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", ""));
Parameters.Add(new LookupParameter>(MetaOptimizationProblem.ProblemsParameterName, ""));
Parameters.Add(new LookupParameter("Generations", ""));
Parameters.Add(new ValueParameter("StoreAllRuns", "If true all runs ever executed are stored. Otherwise only the runs from the latest generateion are stored for caching purposes.", new BoolValue(false)));
}
[StorableConstructor]
private SolutionCacheAnalyzer(bool deserializing) : base(deserializing) { }
private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SolutionCacheAnalyzer(this, cloner);
}
public override IOperation Apply() {
ResultCollection results = ResultsParameter.ActualValue;
ItemArray solutions = ParameterConfigurationParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
ItemDictionary allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary();
bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
if (!storeAllRuns) {
allRuns.Clear();
}
foreach (var solution in solutions) {
string key = solution.ParameterInfoString;
bool first = false;
if (allRuns.Count(x => x.Key.Value == key) == 0) {
allRuns.Add(new StringValue(key), new RunCollection());
first = true; // no other runs yet
}
if (solution.Runs != null) { // Runs is null when a base-level algorithm exception happened due to invalid parameters
var runCollection = allRuns.Single(x => x.Key.Value == key).Value;
foreach (var run in solution.Runs) {
if (!((BoolValue)run.Results["Meta-FromCache"]).Value || first) {
run.Results["Meta-FromCache"] = new BoolValue(true);
runCollection.Add(run);
}
}
}
}
if (!results.ContainsKey("SolutionCache")) {
results.Add(new Result("SolutionCache", allRuns));
}
return base.Apply();
}
}
}