source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Analysis/BestBioBoostSolutionAnalyzer.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 7 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 7.7 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.BioBoost.ProblemDescription;
3using HeuristicLab.BioBoost.Representation;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using System.Linq;
12using System.Linq.Expressions;
13using System.Security.Cryptography;
14
15namespace HeuristicLab.BioBoost.Analysis {
16
17  [Item("BestBioBoostSolutionAnalyzer", "An operator for analyzing the best bioboost solution.")]
18  [StorableClass]
19  public class BestBioBoostSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
20
21    public bool EnabledByDefault { get { return true; } }
22
23    #region Parameters
24    public LookupParameter<BoolValue> MaximizationParameter {
25      get { return (LookupParameter<BoolValue>) Parameters["Maximization"]; }
26    }
27    public ScopeTreeLookupParameter<DoubleValue> TotalCostParameter {
28      get { return (ScopeTreeLookupParameter<DoubleValue>) Parameters["TotalCost"]; }
29    }
30    public LookupParameter<BioBoostProblemData> ProblemDataParameter {
31      get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
32    }
33    public LookupParameter<BioBoostCompoundSolution> BestSolutionParameter {
34      get { return (LookupParameter<BioBoostCompoundSolution>) Parameters["BestSolution"]; }
35    }
36    public ValueLookupParameter<ResultCollection> ResultsParameter {
37      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
38    }
39    public LookupParameter<DoubleValue> BestKnownQualityParameter {
40      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
41    }
42    public LookupParameter<DoubleValue> BestQualityParameter {
43      get { return (LookupParameter<DoubleValue>)Parameters["BestQuality"]; }
44    }
45    public LookupParameter<BioBoostCompoundSolution> BestKnownSolutionParameter {
46      get { return (LookupParameter<BioBoostCompoundSolution>)Parameters["BestKnownSolution"]; }
47    }
48    public LookupParameter<ItemCollection<BioBoostCompoundSolution>> SolutionHistoryParameter {
49      get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>)Parameters["SolutionHistory"]; }
50    }
51    public ValueLookupParameter<BoolValue> TrackHistoryParameter {
52      get { return (ValueLookupParameter<BoolValue>) Parameters["TrackHistory"]; }
53    }
54    #endregion
55
56    #region Parameter Values
57    public bool Maximization {
58      get { return MaximizationParameter.ActualValue.Value;  }
59    }
60    public BioBoostProblemData ProblemData {
61      get { return ProblemDataParameter.ActualValue; }
62    }
63    public BioBoostCompoundSolution BestSolution {
64      get { return BestSolutionParameter.ActualValue; }
65      set { BestSolutionParameter.ActualValue = value; }
66    }
67    private bool TrackHistory { get { return TrackHistoryParameter.ActualValue.Value;  } }
68    #endregion
69
70    #region Construction & Cloning
71    [StorableConstructor]
72    protected BestBioBoostSolutionAnalyzer(bool isDeserializing) : base(isDeserializing) {}
73    protected BestBioBoostSolutionAnalyzer(BestBioBoostSolutionAnalyzer orig, Cloner cloner) : base(orig, cloner) {}
74    public BestBioBoostSolutionAnalyzer() {
75      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether this is a maximization problem."));
76      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TotalCost", "The total cost of the whole scenario."));
77      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The collection of problem data."));
78      Parameters.Add(new LookupParameter<BioBoostCompoundSolution>("BestSolution", "Aggregation of solution components."));
79      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best solution should be stored."));
80      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this instance."));
81      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The quality of the best solution of this run."));
82      Parameters.Add(new LookupParameter<BioBoostCompoundSolution>("BestKnownSolution", "The best known solution of this instance."));
83      Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("SolutionHistory", "The history of best solutions over time."));
84      Parameters.Add(new ValueLookupParameter<BoolValue>("TrackHistory", "True if all best solutions (over time) should be recorded in SolutionHistory", new BoolValue(false)));
85    }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new BestBioBoostSolutionAnalyzer(this, cloner);
88    }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    public void AfterDeserialization() {
92      if (!Parameters.ContainsKey("Maximization"))
93        Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether this is a maximization problem."));
94      if (!Parameters.ContainsKey("SolutionHistory"))
95        Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("SolutionHistory", "The history of best solutions over time."));
96      if (!Parameters.ContainsKey("TrackHistory"))
97        Parameters.Add(new ValueLookupParameter<BoolValue>("TrackHistory", "True if all best solutions (over time) should be recorded in SolutionHistory", new BoolValue(false)));
98      if (!Parameters.ContainsKey("BestQuality"))
99        Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The quality of the best solution of this run."));
100    }
101    #endregion
102
103    IEnumerable<IScope> GetSubScopes(IScope scope, int depth) {
104      if (depth == 1)
105        return scope.SubScopes;
106      return scope.SubScopes.Select(s => GetSubScopes(s, depth - 1)).Aggregate((l1, l2) => l1.Concat(l2));
107    }
108
109    public override IOperation Apply() {
110      var costs = TotalCostParameter.ActualValue.Select((cost, idx) => new {cost.Value, idx}).OrderBy(p => p.Value);
111      var best = Maximization ? costs.Last() : costs.First();
112      var bestScope = TotalCostParameter.Depth == 0 ? ExecutionContext.Scope : GetSubScopes(ExecutionContext.Scope, TotalCostParameter.Depth).ToList()[best.idx];
113      var newBestSolution = new BioBoostCompoundSolution(bestScope, ProblemData);
114      if (BestSolution == null) {
115        BestSolution = newBestSolution;
116        ResultsParameter.ActualValue.Add(new Result("BestSolution", BestSolution));
117      } else {
118        BestSolution.UpdateTo(newBestSolution);
119      }
120      if (BestKnownQualityParameter.ActualValue == null ||
121          Maximization && best.Value > BestKnownQualityParameter.ActualValue.Value ||
122          !Maximization && best.Value < BestKnownQualityParameter.ActualValue.Value) {
123        BestKnownQualityParameter.ActualValue = new DoubleValue(best.Value);
124        BestKnownSolutionParameter.ActualValue = BestSolution;
125      }
126      if (TrackHistory &&
127          (BestQualityParameter.ActualValue == null ||
128           Maximization && best.Value > BestQualityParameter.ActualValue.Value ||
129           !Maximization && best.Value < BestQualityParameter.ActualValue.Value)) {
130        IResult solutionHistoryResult = null;
131        if (!ResultsParameter.ActualValue.TryGetValue("SolutionHistory", out solutionHistoryResult)) {
132          solutionHistoryResult = new Result("SolutionHistory", new ItemCollection<BioBoostCompoundSolution>());
133          ResultsParameter.ActualValue.Add(solutionHistoryResult);
134        }
135        ((ItemCollection<BioBoostCompoundSolution>) solutionHistoryResult.Value).Add(
136          (BioBoostCompoundSolution) newBestSolution.SlimClone());
137      }
138      return base.Apply();
139    }
140
141  }
142}
Note: See TracBrowser for help on using the repository browser.