Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/QualityTrailMultiAnalyzer.cs @ 7128

Last change on this file since 7128 was 7128, checked in by epitzer, 12 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

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