Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Variable/DoubleVariable.cs @ 9334

Last change on this file since 9334 was 9334, checked in by sforsten, 11 years ago

#1980:

  • added Algorithms.GAssist
  • adapted Problems.DecisionListClassification and Encodings.DecisionList
File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Algorithms.GAssist;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.DecisionList {
31  [StorableClass]
32  [Item("DoubleVariable", "")]
33  public class DoubleVariable : Variable<double> {
34
35    [Storable]
36    private IDiscretizer discretizer;
37
38    [Storable]
39    private IList<int> curIntervals;
40
41    [Storable]
42    private int maxIntervals;
43
44    [StorableConstructor]
45    protected DoubleVariable(bool deserializing) : base(deserializing) { }
46    protected DoubleVariable(DoubleVariable original, Cloner cloner)
47      : base(original, cloner) {
48      discretizer = (IDiscretizer)original.discretizer.Clone();
49      curIntervals = new List<int>(original.curIntervals);
50      maxIntervals = original.maxIntervals;
51    }
52    public DoubleVariable(string variableName, int maxIntervals)
53      : base(variableName) {
54      this.maxIntervals = maxIntervals;
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new DoubleVariable(this, cloner);
58    }
59
60    public override void Randomize(IRandom random, double onePercentage, IEnumerable<IDiscretizer> discretizers) {
61      discretizer = discretizers.ElementAt(random.Next(0, discretizers.Count()));
62      int microIntervals = discretizer.NumberOfMicroIntervals(variableName);
63      int maxAllowedIntervals = Math.Min(maxIntervals, microIntervals);
64      int numberOfIntervals = 2;
65      if (maxAllowedIntervals > 2) {
66        numberOfIntervals = random.Next(2, maxAllowedIntervals);
67      }
68
69      attributes = new List<bool>(numberOfIntervals);
70      var curIntervals = new List<int>(numberOfIntervals);
71
72      for (int i = 0; i < numberOfIntervals - 1; i++) {
73        int microInt = random.Next(1, microIntervals - (numberOfIntervals - i - 1));
74        microIntervals -= microInt;
75        curIntervals.Add(microInt);
76        attributes[i] = random.NextDouble() < onePercentage;
77      }
78      // add last interval
79      curIntervals.Add(microIntervals);
80      attributes[numberOfIntervals - 1] = random.NextDouble() < onePercentage;
81    }
82
83    public override double ComputeTheoryLength() {
84      int intervalCount = 0;
85      bool curSet = attributes.First();
86      // first interval started
87      if (curSet) {
88        intervalCount++;
89      }
90      foreach (var attribute in attributes) {
91        if (curSet != attribute) {
92          intervalCount++;
93          curSet = attribute;
94        }
95      }
96      // if the last last interval is not set
97      if (!curSet) {
98        intervalCount--;
99      }
100      return intervalCount;
101    }
102
103    public override bool Match(string input) {
104      return Match(double.Parse(input));
105    }
106
107    public bool Match(double input) {
108      var realCutpoints = GetValuesToCutPoints(discretizer.GetCutPoints(variableName), curIntervals);
109      int pos = 0;
110      while (input >= realCutpoints[pos]) {
111        pos++;
112      }
113      return attributes[pos];
114    }
115
116    private IList<double> GetValuesToCutPoints(IEnumerable<double> cutpoints, IEnumerable<int> microIntervals) {
117      var intervalValues = new List<double>();
118      var cutpointList = cutpoints.ToList();
119      int cur = 0;
120      foreach (var microInterval in microIntervals) {
121        cur += microInterval;
122        intervalValues.Add(cutpointList[cur]);
123      }
124      return intervalValues;
125    }
126
127    public override void Split(IRandom random) {
128      // cannot create more than maximal allowed intervals
129      if (curIntervals.Count >= maxIntervals) { return; }
130      int selectedInterval = random.Next(0, curIntervals.Count);
131      // a single microinterval cannot be split
132      if (curIntervals[selectedInterval] <= 1) { return; }
133
134      // split interval
135      int splitPart1 = random.Next(1, curIntervals[selectedInterval]);
136      int splitPart2 = curIntervals[selectedInterval] - splitPart1;
137
138      // resize old interval
139      curIntervals[selectedInterval] = splitPart1;
140      // insert new interval at correct position
141      curIntervals.Insert(selectedInterval + 1, splitPart2);
142
143      // set new interval to the same bool value as old one
144      attributes.Insert(selectedInterval + 1, attributes[selectedInterval]);
145      return;
146    }
147
148    public override void Merge(IRandom random) {
149      // cannot merge a single interval
150      if (curIntervals.Count == 1) { return; }
151      int selectedInterval = random.Next(0, curIntervals.Count);
152      int neighbour;
153      //if selected interval is the leftmost one, the neighbour on the right side is used
154      if (selectedInterval == 0) {
155        neighbour = selectedInterval + 1;
156        //if the selected interval is the rightmost, the neighbour on the left side is used
157      } else if (selectedInterval == curIntervals.Count - 1) {
158        neighbour = selectedInterval - 1;
159      } else {
160        if (random.Next() < 0.5) {
161          neighbour = selectedInterval - 1;
162        } else {
163          neighbour = selectedInterval + 1;
164        }
165      }
166
167      bool value;
168      if (curIntervals[selectedInterval] > curIntervals[neighbour]) {
169        value = attributes[selectedInterval];
170      } else if (curIntervals[selectedInterval] < curIntervals[neighbour]) {
171        value = attributes[neighbour];
172      } else {
173        if (random.Next() < 0.5) {
174          value = attributes[selectedInterval];
175        } else {
176          value = attributes[neighbour];
177        }
178      }
179
180      curIntervals[selectedInterval] += curIntervals[neighbour];
181      curIntervals.RemoveAt(neighbour);
182
183      attributes[selectedInterval] = value;
184      attributes.RemoveAt(neighbour);
185      return;
186    }
187
188    public override void Reinitialize(IRandom random, double onePercentage, IEnumerable<IDiscretizer> descretizers) {
189      Randomize(random, onePercentage, descretizers);
190      return;
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.