Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/VariableVectorAction.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.9 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 System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.ConditionActionEncoding;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.VariableVector {
33  [StorableClass]
34  [Item("VariableVectorAction", "")]
35  public class VariableVectorAction : ItemDictionary<StringValue, IActionVariable>, IAction {
36
37    public IOrderedEnumerable<StringValue> Order { get { return this.Keys.OrderBy(x => x.Value); } }
38
39    public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
40
41    [StorableConstructor]
42    protected VariableVectorAction(bool deserializing) : base(deserializing) { }
43    protected VariableVectorAction(VariableVectorAction original, Cloner cloner)
44      : base(original, cloner) {
45    }
46    public VariableVectorAction() : base() { }
47    public VariableVectorAction(int capacity) : base(capacity) { }
48    public VariableVectorAction(IDictionary<StringValue, IActionVariable> dictionary) : base(dictionary) { }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new VariableVectorAction(this, cloner);
51    }
52
53    protected override void OnItemsAdded(IEnumerable<KeyValuePair<StringValue, IActionVariable>> items) {
54      foreach (var item in items) {
55        if (!(item.Value is StringVariable || item.Value is IntVariable)) {
56          throw new ArgumentException("Value has to be of type 'StringVariable' or 'IntVariable'");
57        }
58      }
59      base.OnItemsAdded(items);
60    }
61
62    public bool Match(IAction target) {
63      var targetCast = target as VariableVectorAction;
64      if (targetCast == null) { return false; }
65      if (this.Order.SequenceEqual(targetCast.Order)) { return false; }
66      foreach (var keyValuePair in targetCast) {
67        if (!this[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
68          return false;
69        }
70      }
71      return true;
72    }
73
74    public void Add(IEnumerable<IActionVariable> action) {
75      foreach (var variable in action) {
76        this.Add(new StringValue(variable.VariableName), variable);
77      }
78    }
79
80    public override string ToString() {
81      StringBuilder sb = new StringBuilder();
82      bool first = true;
83      foreach (var variable in this.Values) {
84        if (!first) {
85          first = false;
86        } else {
87          sb.Append(";");
88        }
89        sb.Append(variable.ToString());
90      }
91      return sb.ToString();
92    }
93
94    public void Randomize(IRandom random) {
95      foreach (var variable in this.Values) {
96        variable.Randomize(random);
97      }
98    }
99
100    public bool Identical(VariableVectorCondition target) {
101      if (this.Order.SequenceEqual(target.Order)) { return false; }
102      foreach (var keyValuePair in target) {
103        if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
104          return false;
105        }
106      }
107      return true;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.