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