Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/VariableVectorAction.cs @ 9352

Last change on this file since 9352 was 9242, checked in by sforsten, 11 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.7 KB
RevLine 
[9194]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("VariableVectorAction", "")]
[9242]33  public class VariableVectorAction : Item, IAction {
[9194]34
[9242]35    [Storable]
36    private Dictionary<string, IActionVariable> variableDictionary;
37    public Dictionary<string, IActionVariable> VariableDictionary {
38      get { return variableDictionary; }
39      protected set { variableDictionary = value; }
40    }
[9204]41
[9242]42    public IOrderedEnumerable<string> Order { get { return VariableDictionary.Keys.OrderBy(x => x); } }
[9194]43
[9242]44    public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
45
[9194]46    [StorableConstructor]
[9226]47    protected VariableVectorAction(bool deserializing) { }
48    protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
[9242]49      VariableDictionary = new Dictionary<string, IActionVariable>(original.VariableDictionary.Count);
50      foreach (var item in original.VariableDictionary) {
51        VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
[9226]52      }
[9194]53    }
[9242]54    public VariableVectorAction()
55      : base() {
56      VariableDictionary = new Dictionary<string, IActionVariable>();
57    }
58    public VariableVectorAction(int capacity)
59      : base() {
60      VariableDictionary = new Dictionary<string, IActionVariable>(capacity);
61    }
62    public VariableVectorAction(IDictionary<string, IActionVariable> dictionary)
63      : base() {
64      VariableDictionary = new Dictionary<string, IActionVariable>(dictionary);
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new VariableVectorAction(this, cloner);
68    }
[9226]69
70    public VariableVectorAction GetEmptyCopy() {
[9242]71      VariableVectorAction emptyCopy = new VariableVectorAction(VariableDictionary.Count);
72      emptyCopy.Add(VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
[9226]73      return emptyCopy;
[9194]74    }
75
[9226]76    public VariableVectorAction GetSetCopy() {
[9242]77      VariableVectorAction setCopy = new VariableVectorAction(VariableDictionary.Count);
78      setCopy.Add(VariableDictionary.Values.Select(x => x.GetSetCopy()));
[9226]79      return setCopy;
[9194]80    }
81
82    public bool Match(IAction target) {
83      var targetCast = target as VariableVectorAction;
84      if (targetCast == null) { return false; }
[9226]85      if (!this.Order.SequenceEqual(targetCast.Order)) { return false; }
[9242]86      foreach (var keyValuePair in targetCast.VariableDictionary) {
87        if (!VariableDictionary[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
[9194]88          return false;
89        }
90      }
91      return true;
92    }
93
94    public void Add(IEnumerable<IActionVariable> action) {
95      foreach (var variable in action) {
[9242]96        VariableDictionary.Add(variable.VariableName, variable);
[9194]97      }
98    }
99
100    public override string ToString() {
101      StringBuilder sb = new StringBuilder();
102      bool first = true;
[9242]103      foreach (var variable in VariableDictionary.Values) {
[9226]104        if (first) {
[9194]105          first = false;
106        } else {
107          sb.Append(";");
108        }
109        sb.Append(variable.ToString());
110      }
111      return sb.ToString();
112    }
113
114    public void Randomize(IRandom random) {
[9242]115      foreach (var variable in VariableDictionary.Values) {
[9194]116        variable.Randomize(random);
117      }
118    }
119
[9226]120    public bool Identical(VariableVectorAction target) {
[9242]121      if (!this.Order.SequenceEqual(target.Order)) { return false; }
122      foreach (var keyValuePair in target.VariableDictionary) {
123        if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) {
[9194]124          return false;
125        }
126      }
127      return true;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.