Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/QualityTrailMultiAnalyzer.cs @ 16995

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

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.PluginInfrastructure;
32using HEAL.Attic;
33
34namespace HeuristicLab.Analysis.FitnessLandscape {
35
36  [Item("QualityTrailAnalyzer", "An analyzer that creates a quality trail which can be further analyzed by several analyzers.")]
37  [StorableType("E02FC8FB-85AD-42AB-8E14-EA509579C8BE")]
38  public class QualityTrailMultiAnalyzer : CheckedMultiOperator<IQualityTrailAnalyzer>, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return false; }
41    }
42
43    public override bool CanChangeName {
44      get { return false; }
45    }
46
47    public ValueLookupParameter<IntValue> UpdateIntervalParameter {
48      get { return (ValueLookupParameter<IntValue>)Parameters["QualityTrailUpdateInterval"]; }
49    }
50    public LookupParameter<IntValue> UpdateCounterParameter {
51      get { return (LookupParameter<IntValue>)Parameters["QualityTrailUpdateCounter"]; }
52    }
53    public ValueLookupParameter<DataTable> QualityTrailParameter {
54      get { return (ValueLookupParameter<DataTable>)Parameters["Quality Trail"]; }
55    }
56    public LookupParameter<VariableCollection> ResultsParameter {
57      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
58    }
59    public LookupParameter<DoubleValue> QualityParameter {
60      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
61    }
62    public ScopeTreeLookupParameter<DoubleValue> QualitiesParameter {
63      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Qualities"]; }
64    }
65    public OperatorParameter ResultsCollector {
66      get { return (OperatorParameter)Parameters["ResultsCollector"]; }
67    }
68    public LookupParameter<BoolValue> MaximizationParameter {
69      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
70    }
71
72    public IntValue UpdateInterval {
73      get { return UpdateIntervalParameter.Value; }
74      set { UpdateIntervalParameter.Value = value; }
75    }
76
77    [StorableConstructor]
78    protected QualityTrailMultiAnalyzer(StorableConstructorFlag _) : base(_) { }
79    protected QualityTrailMultiAnalyzer(QualityTrailMultiAnalyzer original, Cloner cloner) : base(original, cloner) { }
80
81    public QualityTrailMultiAnalyzer()
82      : base() {
83      Parameters.Add(new ValueLookupParameter<IntValue>("QualityTrailUpdateInterval", "The interval at which the contained analyzers should be applied.", new IntValue(1)));
84      Parameters.Add(new LookupParameter<IntValue>("QualityTrailUpdateCounter", "The number of times the Analyzer was called since the last update."));
85      Parameters.Add(new ValueLookupParameter<DataTable>("Quality Trail", "All historical quality values throughout the algorithm run"));
86      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Single quality value from the current iteration/generation"));
87      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Qualities", "Quality values of the whole population", "Quality", 1));
88      Parameters.Add(new LookupParameter<VariableCollection>("Results", "The collected results"));
89      Parameters.Add(new OperatorParameter("ResultsCollector"));
90      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem"));
91
92      foreach (var analyzer in ApplicationManager.Manager.GetInstances<IQualityTrailAnalyzer>())
93        Operators.Add(analyzer);
94
95      var resultsCollector = new ResultsCollector();
96      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(QualityTrailParameter.Name));
97      ResultsCollector.Value = resultsCollector;
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new QualityTrailMultiAnalyzer(this, cloner);
102    }
103
104    public override IOperation InstrumentedApply() {
105      UpdateQualityTrail();
106
107      IntValue interval = UpdateIntervalParameter.ActualValue;
108      if (interval == null) interval = new IntValue(1);
109
110      IntValue counter = UpdateCounterParameter.ActualValue;
111      if (counter == null) {
112        counter = new IntValue(interval.Value);
113        UpdateCounterParameter.ActualValue = counter;
114      } else counter.Value++;
115
116      OperationCollection next = new OperationCollection();
117      next.Add(ExecutionContext.CreateOperation(ResultsCollector.Value));
118      if (counter.Value == interval.Value) {
119        counter.Value = 0;
120        foreach (IndexedItem<IQualityTrailAnalyzer> item in Operators.CheckedItems)
121          next.Add(ExecutionContext.CreateOperation(item.Value));
122
123      }
124      next.Add(base.InstrumentedApply());
125      return next;
126    }
127
128    private void UpdateQualityTrail() {
129      DataTable qualityTrail = QualityTrailParameter.ActualValue;
130      if (qualityTrail == null) {
131        qualityTrail = new DataTable("Quality Trail");
132        qualityTrail.Rows.Add(new DataRow("Qualities"));
133        QualityTrailParameter.ActualValue = qualityTrail;
134      }
135      if (QualityParameter.ActualValue != null)
136        qualityTrail.Rows["Qualities"].Values.Add(QualityParameter.ActualValue.Value);
137      if (QualitiesParameter.ActualName != null &&
138        QualitiesParameter.ActualValue.Count() > 0 &&
139        MaximizationParameter != null)
140        if (MaximizationParameter.ActualValue.Value)
141          qualityTrail.Rows["Qualities"].Values.Add(QualitiesParameter.ActualValue.Select(dv => dv.Value).Max());
142        else
143          qualityTrail.Rows["Qualities"].Values.Add(QualitiesParameter.ActualValue.Select(dv => dv.Value).Min());
144
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.