Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/VariableVectorCondition.cs @ 9275

Last change on this file since 9275 was 9242, checked in by sforsten, 12 years ago

#1980:

  • fixed several bugs (crossover, subsumption, serialization etc.)
  • added ModuloOperator
  • CombinedIntegerVectorClassificationProblem\Data and VariableVectorClassificationProblem\Data inherit from ConditionActionClassificationProblem\Data
  • it can now be set how often the analyzers have to be executed
  • VariableVectorAction, VariableVectorCondition and VariableVectorInput now inherit from Item
File size: 4.3 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.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.ConditionActionEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.VariableVector {
31  [StorableClass]
32  [Item("VariableVectorCondition", "")]
33  public class VariableVectorCondition : Item, ICondition {
34
35    [Storable]
36    private Dictionary<string, IVariable> variableDictionary;
37    public Dictionary<string, IVariable> VariableDictionary {
38      get { return variableDictionary; }
39      protected set { variableDictionary = value; }
40    }
41
42    public IOrderedEnumerable<string> Order { get { return VariableDictionary.Keys.OrderBy(x => x); } }
43
44    public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
45
46    [StorableConstructor]
47    protected VariableVectorCondition(bool deserializing) { }
48    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner) {
49      VariableDictionary = new Dictionary<string, IVariable>(original.VariableDictionary.Count);
50      foreach (var item in original.VariableDictionary) {
51        VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
52      }
53    }
54    public VariableVectorCondition()
55      : base() {
56      VariableDictionary = new Dictionary<string, IVariable>();
57    }
58    public VariableVectorCondition(int capacity)
59      : base() {
60      VariableDictionary = new Dictionary<string, IVariable>(capacity);
61    }
62    public VariableVectorCondition(IDictionary<string, IVariable> dictionary)
63      : base() {
64      VariableDictionary = new Dictionary<string, IVariable>(dictionary);
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new VariableVectorCondition(this, cloner);
68    }
69
70    public bool Match(IInput target) {
71      var targetCast = target as VariableVectorInput;
72      if (targetCast == null) { return false; }
73      foreach (var key in VariableDictionary.Keys) {
74        if (!targetCast.InputDictionary.ContainsKey(key) || !VariableDictionary[key].MatchInput(targetCast.InputDictionary[key])) {
75          return false;
76        }
77      }
78      return true;
79    }
80
81    public void Add(IEnumerable<IVariable> condition) {
82      foreach (var variable in condition) {
83        VariableDictionary.Add(variable.VariableName, variable);
84      }
85    }
86
87    public override string ToString() {
88      StringBuilder sb = new StringBuilder();
89      bool first = true;
90      foreach (var variable in VariableDictionary.Values) {
91        if (first) {
92          first = false;
93        } else {
94          sb.Append(";");
95        }
96        sb.Append(variable.ToString());
97      }
98      return sb.ToString();
99    }
100
101    public void Randomize(IRandom random, double spreadPercentage) {
102      foreach (var variable in VariableDictionary.Values) {
103        if (variable is DoubleVariable) {
104          ((DoubleVariable)variable).Randomize(random, spreadPercentage);
105        } else {
106          variable.Randomize(random);
107        }
108      }
109    }
110
111    public bool Identical(VariableVectorCondition target) {
112      if (!this.Order.SequenceEqual(target.Order)) { return false; }
113      foreach (var keyValuePair in target.VariableDictionary) {
114        if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) {
115          return false;
116        }
117      }
118      return true;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.