Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • deleted not needed interface IMatching
  • finished VariableVector encoding
  • the algorithm LearningClassifierSystem is now independent of a specific encoding. It still needs the ConditionActionEncoding.
  • merged r9191:9203 HeuristicLab.Core from trunk to branch
File size: 3.7 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.Data;
28using HeuristicLab.Encodings.ConditionActionEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.VariableVector {
32  [StorableClass]
33  [Item("VariableVectorCondition", "")]
34  public class VariableVectorCondition : ItemDictionary<StringValue, IVariable>, ICondition {
35
36    public IOrderedEnumerable<StringValue> Order { get { return this.Keys.OrderBy(x => x.Value); } }
37
38    public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
39
40    [StorableConstructor]
41    protected VariableVectorCondition(bool deserializing) : base(deserializing) { }
42    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner)
43      : base(original, cloner) {
44    }
45    public VariableVectorCondition() : base() { }
46    public VariableVectorCondition(int capacity) : base(capacity) { }
47    public VariableVectorCondition(IDictionary<StringValue, IVariable> dictionary) : base(dictionary) { }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new VariableVectorCondition(this, cloner);
50    }
51
52    public bool Match(IInput target) {
53      var targetCast = target as VariableVectorInput;
54      if (targetCast == null) { return false; }
55      if (this.Order.SequenceEqual(targetCast.Order)) { return false; }
56      foreach (var keyValuePair in targetCast) {
57        if (!this[keyValuePair.Key].MatchInput(keyValuePair.Value.Value)) {
58          return false;
59        }
60      }
61      return true;
62    }
63
64    public void Add(IEnumerable<IVariable> condition) {
65      foreach (var variable in condition) {
66        this.Add(new StringValue(variable.VariableName), variable);
67      }
68    }
69
70    public override string ToString() {
71      StringBuilder sb = new StringBuilder();
72      bool first = true;
73      foreach (var variable in this.Values) {
74        if (!first) {
75          first = false;
76        } else {
77          sb.Append(";");
78        }
79        sb.Append(variable.ToString());
80      }
81      return sb.ToString();
82    }
83
84    public void Randomize(IRandom random, double spreadPercentage) {
85      foreach (var variable in this.Values) {
86        if (variable is DoubleVariable) {
87          ((DoubleVariable)variable).Randomize(random, spreadPercentage);
88        } else {
89          variable.Randomize(random);
90        }
91      }
92    }
93
94    public bool Identical(VariableVectorCondition target) {
95      if (this.Order.SequenceEqual(target.Order)) { return false; }
96      foreach (var keyValuePair in target) {
97        if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
98          return false;
99        }
100      }
101      return true;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.