Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.Diagnostics;
5using HeuristicLab.BioBoost.ProblemDescription;
6using HeuristicLab.BioBoost.Representation;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Operators;
11using HeuristicLab.Optimization;
12using HeuristicLab.Parameters;
13using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
14using System.Linq;
15using System.Linq.Expressions;
16using HeuristicLab.BioBoost.Utils;
17using NetTopologySuite.Utilities;
18
19namespace HeuristicLab.BioBoost.Analysis {
20
21  [Item("MultiObjectiveBioBoostSolutionAnalyzer", "An operator for analyzing the set of best bioboost solutions (multi objective).")]
22  [StorableClass]
23  public class MultiObjectiveBioBoostSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
24
25    public bool EnabledByDefault { get { return true; } }
26
27    #region Parameters
28    public LookupParameter<BoolArray> MaximizationParameter {
29      get { return (LookupParameter<BoolArray>) Parameters["Maximization"]; }
30    }
31    public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
32      get { return (ScopeTreeLookupParameter<DoubleArray>) Parameters["QualityCriteria"]; }
33    }
34    public LookupParameter<BioBoostProblemData> ProblemDataParameter {
35      get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
36    }
37    public LookupParameter<ItemCollection<BioBoostCompoundSolution>> BestSolutionsParameter {
38      get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>) Parameters["BestSolutions"]; }
39    }
40    public ValueLookupParameter<ResultCollection> ResultsParameter {
41      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
42    }
43    /*public LookupParameter<DoubleValue> BestKnownQualityParameter {
44      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
45    }
46    public LookupParameter<BioBoostCompoundSolution> BestKnownSolutionParameter {
47      get { return (LookupParameter<BioBoostCompoundSolution>)Parameters["BestKnownSolution"]; }
48    } */
49    /* public LookupParameter<ItemCollection<BioBoostCompoundSolution>> SolutionHistoryParameter {
50      get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>)Parameters["SolutionHistory"]; }
51    }
52    public ValueLookupParameter<BoolValue> TrackHistoryParameter {
53      get { return (ValueLookupParameter<BoolValue>) Parameters["TrackHistory"]; }
54    } */
55    #endregion
56
57    #region Parameter Values
58    public BoolArray Maximization {
59      get { return MaximizationParameter.ActualValue;  }
60    }
61    public BioBoostProblemData ProblemData {
62      get { return ProblemDataParameter.ActualValue; }
63    }
64    public ItemCollection<BioBoostCompoundSolution> BestSolutions {
65      get { return BestSolutionsParameter.ActualValue; }
66      set { BestSolutionsParameter.ActualValue = value; }
67    }
68    // private bool TrackHistory { get { return TrackHistoryParameter.ActualValue.Value;  } }
69    #endregion
70
71    #region Construction & Cloning
72    [StorableConstructor]
73    protected MultiObjectiveBioBoostSolutionAnalyzer(bool isDeserializing) : base(isDeserializing) {}
74    protected MultiObjectiveBioBoostSolutionAnalyzer(MultiObjectiveBioBoostSolutionAnalyzer orig, Cloner cloner) : base(orig, cloner) {}
75    public MultiObjectiveBioBoostSolutionAnalyzer() {
76      Parameters.Add(new LookupParameter<BoolArray>("Maximization", "Whether this is a maximization problem."));
77      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("QualityCriteria", "The list of quality criteria."));
78      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The collection of problem data."));
79      Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("BestSolutions", "Pareto set of best solutions.."));
80      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best solution should be stored."));
81      /* Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this instance."));
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 MultiObjectiveBioBoostSolutionAnalyzer(this, cloner);
88    }
89    #endregion
90
91    public override IOperation Apply() {
92      var qualities = QualitiesParameter.ActualValue.Select((q, i) => new {q, i}).ToList();
93      var maximization = Maximization;
94      var bestQualities =
95        qualities.Where(q1 => qualities.All(q2 => q1.i == q2.i || !IsDominating(q2.q, q1.q, maximization)))
96        .GroupBy(q => q.q, q => q, Enumerable.SequenceEqual).Select(g => g.First());
97      IResult bestSolutionsResult = null;
98      ItemCollection<BioBoostCompoundSolution> bestSolutions = null;
99      if (!ResultsParameter.ActualValue.TryGetValue("BestSolutions", out bestSolutionsResult)) {
100        bestSolutions = new ItemCollection<BioBoostCompoundSolution>();
101        ResultsParameter.ActualValue.Add(new Result("BestSolutions", bestSolutions));
102      } else {
103        bestSolutions = bestSolutionsResult.Value as ItemCollection<BioBoostCompoundSolution>;
104        if (bestSolutions != null) {
105          bestSolutions.Clear();
106        }
107      }
108      if (bestSolutions == null)
109        throw new InvalidOperationException("BestSolutions in Results has wrong type and cannot be updated.");
110      var solutions = bestQualities
111        .OrderByDescending(q => q.q[0])
112        .Select(q => new BioBoostCompoundSolution(ExecutionContext.Scope.SubScopes[q.i], ProblemData) {
113                       Name = string.Format("[{0}]", string.Join(", ", q.q))})
114        .ToList();
115      bestSolutions.AddRange(solutions);
116      return base.Apply();
117    }
118
119    private static bool IsDominating(DoubleArray a, DoubleArray b, BoolArray maximization) {
120      Debug.Assert(a.Length == b.Length);
121      for (int i = 0; i < a.Length; i++) {
122        if (maximization[i]) {
123          if (a[i] <= b[i]) return false;
124        } else {
125          if (a[i] >= b[i]) return false;
126        }
127      }
128      return true;
129    }
130
131  }
132}
Note: See TracBrowser for help on using the repository browser.