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