Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Analysis/MultiObjejctiveBioBoostSolutionAnalyzer.cs @ 17777

Last change on this file since 17777 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Diagnostics;
24using HeuristicLab.BioBoost.ProblemDescription;
25using HeuristicLab.BioBoost.Representation;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using System.Linq;
34using HeuristicLab.BioBoost.Utils;
35using HEAL.Attic;
36
37namespace HeuristicLab.BioBoost.Analysis {
38
39  [Item("MultiObjectiveBioBoostSolutionAnalyzer", "An operator for analyzing the set of best bioboost solutions (multi objective).")]
40  [StorableType("FC868394-A817-4835-845C-DD32562AE60C")]
41  public class MultiObjectiveBioBoostSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
42
43    public bool EnabledByDefault { get { return true; } }
44
45    #region Parameters
46    public LookupParameter<BoolArray> MaximizationParameter {
47      get { return (LookupParameter<BoolArray>) Parameters["Maximization"]; }
48    }
49    public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
50      get { return (ScopeTreeLookupParameter<DoubleArray>) Parameters["QualityCriteria"]; }
51    }
52    public LookupParameter<BioBoostProblemData> ProblemDataParameter {
53      get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
54    }
55    public LookupParameter<ItemCollection<BioBoostCompoundSolution>> BestSolutionsParameter {
56      get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>) Parameters["BestSolutions"]; }
57    }
58    public ValueLookupParameter<ResultCollection> ResultsParameter {
59      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
60    }
61    /*public LookupParameter<DoubleValue> BestKnownQualityParameter {
62      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
63    }
64    public LookupParameter<BioBoostCompoundSolution> BestKnownSolutionParameter {
65      get { return (LookupParameter<BioBoostCompoundSolution>)Parameters["BestKnownSolution"]; }
66    } */
67    /* public LookupParameter<ItemCollection<BioBoostCompoundSolution>> SolutionHistoryParameter {
68      get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>)Parameters["SolutionHistory"]; }
69    }
70    public ValueLookupParameter<BoolValue> TrackHistoryParameter {
71      get { return (ValueLookupParameter<BoolValue>) Parameters["TrackHistory"]; }
72    } */
73    #endregion
74
75    #region Parameter Values
76    public BoolArray Maximization {
77      get { return MaximizationParameter.ActualValue;  }
78    }
79    public BioBoostProblemData ProblemData {
80      get { return ProblemDataParameter.ActualValue; }
81    }
82    public ItemCollection<BioBoostCompoundSolution> BestSolutions {
83      get { return BestSolutionsParameter.ActualValue; }
84      set { BestSolutionsParameter.ActualValue = value; }
85    }
86    // private bool TrackHistory { get { return TrackHistoryParameter.ActualValue.Value;  } }
87    #endregion
88
89    #region Construction & Cloning
90    [StorableConstructor]
91    protected MultiObjectiveBioBoostSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
92    protected MultiObjectiveBioBoostSolutionAnalyzer(MultiObjectiveBioBoostSolutionAnalyzer orig, Cloner cloner) : base(orig, cloner) {}
93    public MultiObjectiveBioBoostSolutionAnalyzer() {
94      Parameters.Add(new LookupParameter<BoolArray>("Maximization", "Whether this is a maximization problem."));
95      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("QualityCriteria", "The list of quality criteria."));
96      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The collection of problem data."));
97      Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("BestSolutions", "Pareto set of best solutions.."));
98      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best solution should be stored."));
99      /* Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this instance."));
100      Parameters.Add(new LookupParameter<BioBoostCompoundSolution>("BestKnownSolution", "The best known solution of this instance."));
101      Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("SolutionHistory", "The history of best solutions over time."));
102      Parameters.Add(new ValueLookupParameter<BoolValue>("TrackHistory", "True if all best solutions (over time) should be recorded in SolutionHistory", new BoolValue(false))); */
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new MultiObjectiveBioBoostSolutionAnalyzer(this, cloner);
106    }
107    #endregion
108
109    public override IOperation Apply() {
110      var qualities = QualitiesParameter.ActualValue.Select((q, i) => new {q, i}).ToList();
111      var maximization = Maximization;
112      var bestQualities =
113        qualities.Where(q1 => qualities.All(q2 => q1.i == q2.i || !IsDominating(q2.q, q1.q, maximization)))
114        .GroupBy(q => q.q, q => q, Enumerable.SequenceEqual).Select(g => g.First());
115      IResult bestSolutionsResult = null;
116      ItemCollection<BioBoostCompoundSolution> bestSolutions = null;
117      if (!ResultsParameter.ActualValue.TryGetValue("BestSolutions", out bestSolutionsResult)) {
118        bestSolutions = new ItemCollection<BioBoostCompoundSolution>();
119        ResultsParameter.ActualValue.Add(new Result("BestSolutions", bestSolutions));
120      } else {
121        bestSolutions = bestSolutionsResult.Value as ItemCollection<BioBoostCompoundSolution>;
122        if (bestSolutions != null) {
123          bestSolutions.Clear();
124        }
125      }
126      if (bestSolutions == null)
127        throw new InvalidOperationException("BestSolutions in Results has wrong type and cannot be updated.");
128      var solutions = bestQualities
129        .OrderByDescending(q => q.q[0])
130        .Select(q => new BioBoostCompoundSolution(ExecutionContext.Scope.SubScopes[q.i], ProblemData) {
131                       Name = string.Format("[{0}]", string.Join(", ", q.q))})
132        .ToList();
133      bestSolutions.AddRange(solutions);
134      return base.Apply();
135    }
136
137    private static bool IsDominating(DoubleArray a, DoubleArray b, BoolArray maximization) {
138      Debug.Assert(a.Length == b.Length);
139      for (int i = 0; i < a.Length; i++) {
140        if (maximization[i]) {
141          if (a[i] <= b[i]) return false;
142        } else {
143          if (a[i] >= b[i]) return false;
144        }
145      }
146      return true;
147    }
148
149  }
150}
Note: See TracBrowser for help on using the repository browser.