Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/DynOpEqComparator.cs @ 4221

Last change on this file since 4221 was 4193, checked in by gkronber, 14 years ago

Created a feature/exploration branch for new data analysis features #1142

File size: 8.9 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;
23using System.Linq;
24using alglib;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using System.Collections.Generic;
33
34namespace HeuristicLab.Problems.DataAnalysis.Operators {
35  [Item("DynOpEqComparator", "Dynamic Operator Equalization.")]
36  [StorableClass]
37  public class DynOpEqComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
38    public IValueLookupParameter<BoolValue> MaximizationParameter {
39      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ILookupParameter<BoolValue> ResultParameter {
42      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
43    }
44    public ILookupParameter<DoubleValue> LeftSideParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
46    }
47    public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
48      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
49    }
50    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
51      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
52    }
53    public ILookupParameter<DoubleValue> BestQualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
55    }
56    public IValueLookupParameter<IntValue> BinSizeParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["BinSize"]; }
58    }
59    public ILookupParameter<ItemList<IntValue>> BinCapacityParameter {
60      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["BinCapacity"]; }
61    }
62    public ILookupParameter<ItemList<ItemList<DoubleValue>>> AcceptedBinQualitiesParameter {
63      get { return (ILookupParameter<ItemList<ItemList<DoubleValue>>>)Parameters["AcceptedBinQualities"]; }
64    }
65    public ILookupParameter<ItemList<IntValue>> AcceptedCountsParameter {
66      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["AcceptedCounts"]; }
67    }
68    public ILookupParameter<ItemList<IntValue>> TotalCountsParameter {
69      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["TotalCounts"]; }
70    }
71
72    public DynOpEqComparator()
73      : base() {
74      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
75      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
76      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
77      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
78      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The newly created child."));
79      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality"));
80      Parameters.Add(new ValueLookupParameter<IntValue>("BinSize", new IntValue(5)));
81      Parameters.Add(new LookupParameter<ItemList<IntValue>>("BinCapacity"));
82      Parameters.Add(new LookupParameter<ItemList<ItemList<DoubleValue>>>("AcceptedBinQualities"));
83      Parameters.Add(new LookupParameter<ItemList<IntValue>>("AcceptedCounts"));
84      Parameters.Add(new LookupParameter<ItemList<IntValue>>("TotalCounts"));
85    }
86
87    public override IOperation Apply() {
88      if (ResultParameter.ActualValue == null || ResultParameter.ActualValue.Value == true) {
89        var tree = SymbolicExpressionTreeParameter.ActualValue;
90        int size = tree.Size;
91        int bin = GetBinIndexForSize(size);
92        if (LeftSideParameter.ActualValue == null) {
93          ItemList<IntValue> totalCounts = TotalCountsParameter.ActualValue;
94          while (bin >= totalCounts.Count) totalCounts.Add(new IntValue(0));
95          totalCounts[bin].Value = totalCounts[bin].Value + 1;
96
97          if (!Exists(bin)) ResultParameter.ActualValue = new BoolValue(true);
98          else ResultParameter.ActualValue = new BoolValue(IsNotFull(bin));
99        } else {
100          double leftQuality = LeftSideParameter.ActualValue.Value;
101
102
103          ResultParameter.ActualValue = new BoolValue(Accept(size, bin, leftQuality));
104        }
105      }
106      return base.Apply();
107    }
108
109    private int GetBinIndexForSize(int size) {
110      return (int)Math.Floor((size - 3.0) / BinSizeParameter.ActualValue.Value);
111    }
112
113    private bool Accept(int size, int binIndex, double solutionQuality) {
114      bool accept = false;
115      if (Exists(binIndex)) {
116        if (IsNotFull(binIndex) ||
117          NewBestOfBin(solutionQuality, binIndex)) {
118          AddToBin(solutionQuality, binIndex);
119          accept = true;
120        }
121      } else if (NewBestOfRun(solutionQuality)) {
122        CreateNewBin(binIndex);
123        AddToBin(solutionQuality, binIndex);
124        accept = true;
125      }
126      return accept;
127    }
128
129    private void AddToBin(double solutionQuality, int binIndex) {
130      ItemList<DoubleValue> acceptedBinQualities = AcceptedBinQualitiesParameter.ActualValue[binIndex];
131      ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
132      if (acceptedBinQualities.Count == 0) {
133        acceptedBinQualities.Add(new DoubleValue(solutionQuality));
134        acceptedCounts[binIndex].Value = acceptedCounts[binIndex].Value + 1;
135      } else {
136        // insert at beginning if it is a new best or add at end
137        bool maximization = MaximizationParameter.ActualValue.Value;
138        if ((maximization && solutionQuality > acceptedBinQualities[0].Value) ||
139          (!maximization && solutionQuality < acceptedBinQualities[0].Value)) {
140          acceptedBinQualities.Insert(0, new DoubleValue(solutionQuality));
141        } else {
142          acceptedBinQualities.Add(new DoubleValue(solutionQuality));
143        }
144        acceptedCounts[binIndex].Value = acceptedCounts[binIndex].Value + 1;
145      }
146    }
147
148    private bool NewBestOfRun(double solutionQuality) {
149      bool maximization = MaximizationParameter.ActualValue.Value;
150      double bestQuality = BestQualityParameter.ActualValue.Value;
151      return maximization ? solutionQuality > bestQuality : solutionQuality < bestQuality;
152    }
153
154    private void CreateNewBin(int binIndex) {
155      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
156      ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
157      ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
158
159      // create empty bins of capacity one up to the newly created bin
160      for (int i = binCapacities.Count; i <= binIndex; i++) {
161        binCapacities.Add(new IntValue(1));
162        acceptedQualities.Add(new ItemList<DoubleValue>(10));
163        acceptedCounts.Add(new IntValue(0));
164      }
165    }
166
167    private bool NewBestOfBin(double solutionQuality, int binIndex) {
168      ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
169      if (acceptedQualities[binIndex].Count == 0) return true;
170      bool maximization = MaximizationParameter.ActualValue.Value;
171      IEnumerable<double> binQualities = acceptedQualities[binIndex].Select(x => x.Value);
172      // binQualities are always sorted so that the best is in bin 0
173      return maximization ? solutionQuality > binQualities.First() :
174        solutionQuality < binQualities.First();
175    }
176
177    private bool IsNotFull(int binIndex) {
178      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
179      ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
180      return acceptedQualities[binIndex].Count < binCapacities[binIndex].Value;
181    }
182
183    private bool Exists(int binIndex) {
184      // if the bin has a capacity set then it exists
185      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
186      return binIndex < binCapacities.Count;
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.