[5359] | 1 | using System.Linq;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Data;
|
---|
| 5 | using HeuristicLab.Operators;
|
---|
| 6 | using HeuristicLab.Optimization;
|
---|
| 7 | using HeuristicLab.Parameters;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 |
|
---|
| 10 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 11 | /// <summary>
|
---|
| 12 | /// TODO
|
---|
| 13 | /// </summary>
|
---|
| 14 | [Item("SolutionCacheAnalyzer", "")]
|
---|
| 15 | [StorableClass]
|
---|
| 16 | public sealed class SolutionCacheAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
| 17 |
|
---|
[7173] | 18 | public bool EnabledByDefault {
|
---|
| 19 | get { return true; }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[5359] | 22 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 23 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 24 | }
|
---|
| 25 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
| 26 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
| 27 | }
|
---|
| 28 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 29 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 30 | }
|
---|
| 31 | public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
| 32 | get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
| 33 | }
|
---|
| 34 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
| 35 | get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
| 36 | }
|
---|
| 37 | public ValueParameter<BoolValue> StoreAllRunsParameter {
|
---|
| 38 | get { return (ValueParameter<BoolValue>)Parameters["StoreAllRuns"]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public SolutionCacheAnalyzer()
|
---|
| 42 | : base() {
|
---|
| 43 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
| 44 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
| 45 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
| 46 | Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
|
---|
| 47 | Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
|
---|
| 48 | Parameters.Add(new ValueParameter<BoolValue>("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)));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [StorableConstructor]
|
---|
| 52 | private SolutionCacheAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 53 | private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new SolutionCacheAnalyzer(this, cloner);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IOperation Apply() {
|
---|
| 59 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 60 | ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
|
---|
| 61 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 62 | ItemDictionary<StringValue, RunCollection> allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary<StringValue, RunCollection>();
|
---|
| 63 | bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
|
---|
| 64 |
|
---|
| 65 | if (!storeAllRuns) {
|
---|
| 66 | allRuns.Clear();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | foreach (var solution in solutions) {
|
---|
| 70 | string key = solution.ParameterInfoString;
|
---|
| 71 | bool first = false;
|
---|
| 72 | if (allRuns.Count(x => x.Key.Value == key) == 0) {
|
---|
| 73 | allRuns.Add(new StringValue(key), new RunCollection());
|
---|
| 74 | first = true; // no other runs yet
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[6090] | 77 | if (solution.Runs != null) { // Runs is null when a base-level algorithm exception happened due to invalid parameters
|
---|
| 78 | var runCollection = allRuns.Single(x => x.Key.Value == key).Value;
|
---|
| 79 | foreach (var run in solution.Runs) {
|
---|
[6489] | 80 | if (!((BoolValue)run.Results["Meta-FromCache"]).Value || first) {
|
---|
| 81 | run.Results["Meta-FromCache"] = new BoolValue(true);
|
---|
[6090] | 82 | runCollection.Add(run);
|
---|
| 83 | }
|
---|
[5359] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (!results.ContainsKey("SolutionCache")) {
|
---|
| 89 | results.Add(new Result("SolutionCache", allRuns));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | return base.Apply();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|