Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added necessary interface ICondition, IAction, IInput
  • removed not used class MatchSelector and interface IMatchSelector
  • added constructors to ItemDictionary
  • added new encoding
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 int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
37
38    [StorableConstructor]
39    protected VariableVectorCondition(bool deserializing) : base(deserializing) { }
40    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public VariableVectorCondition() : base() { }
44    public VariableVectorCondition(int capacity) : base(capacity) { }
45    public VariableVectorCondition(IDictionary<StringValue, IVariable> dictionary) : base(dictionary) { }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new VariableVectorCondition(this, cloner);
48    }
49
50    public bool Match(IInput target) {
51      var targetCast = target as VariableVectorInput;
52      if (targetCast == null) { return false; }
53      if (!(targetCast.Keys.Except(this.Keys).Count() == 0 && this.Keys.Except(targetCast.Keys).Count() == 0)) { return false; }
54      foreach (var keyValuePair in targetCast) {
55        if (!this[keyValuePair.Key].MatchInput(keyValuePair.Value.Value)) {
56          return false;
57        }
58      }
59      return true;
60    }
61
62    public void Add(IEnumerable<IVariable> condition) {
63      foreach (var variable in condition) {
64        this.Add(new StringValue(variable.VariableName), variable);
65      }
66    }
67
68    public override string ToString() {
69      StringBuilder sb = new StringBuilder();
70      bool first = true;
71      foreach (var variable in this.Values) {
72        if (!first) {
73          first = false;
74        } else {
75          sb.Append(";");
76        }
77        sb.Append(variable.ToString());
78      }
79      return sb.ToString();
80    }
81
82    public void Randomize(IRandom random, double spreadPercentage) {
83      foreach (var variable in this.Values) {
84        if (variable is DoubleVariable) {
85          ((DoubleVariable)variable).Randomize(random, spreadPercentage);
86        } else {
87          variable.Randomize(random);
88        }
89      }
90    }
91
92    public bool Identical(VariableVectorCondition target) {
93      if (!(target.Keys.Except(this.Keys).Count() == 0 && this.Keys.Except(target.Keys).Count() == 0)) { return false; }
94      foreach (var keyValuePair in target) {
95        if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
96          return false;
97        }
98      }
99      return true;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.