Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9330 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: 5.1 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.Encodings.ConditionActionEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.VariableVector {
32  [StorableClass]
33  [Item("VariableVector", "")]
34  public class VariableVector : Item, IClassifier {
35
36    [Storable]
37    protected VariableVectorCondition condition;
38    public VariableVectorCondition Condition { get { return condition; } }
39
40    [Storable]
41    protected VariableVectorAction action;
42    public VariableVectorAction Action { get { return action; } }
43
44    public int Count {
45      get { return condition.VariableDictionary.Count + action.VariableDictionary.Count; }
46    }
47
48    public int VirtualLength {
49      get { return condition.VariableDictionary.Values.Sum(x => x.VirtualLength) + action.VariableDictionary.Values.Sum(x => x.VirtualLength); }
50    }
51
52    [StorableConstructor]
53    protected VariableVector(bool deserializing) : base(deserializing) { }
54    protected VariableVector(VariableVector original, Cloner cloner)
55      : base(original, cloner) {
56      condition = (VariableVectorCondition)original.condition.Clone();
57      action = (VariableVectorAction)original.action;
58    }
59    public VariableVector() {
60      condition = new VariableVectorCondition();
61      action = new VariableVectorAction();
62    }
63    public VariableVector(IEnumerable<IVariable> condition, IEnumerable<IVariable> action)
64      : this() {
65      if (condition == null || action == null) { throw new ArgumentNullException(); }
66      if (!action.All(x => x is IActionVariable)) { throw new ArgumentException("Action can only use IActionVariable"); }
67      this.Condition.Add(condition);
68      this.Action.Add(action.Cast<IActionVariable>());
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new VariableVector(this, cloner);
72    }
73
74    public VariableVector GetEmptyCopy() {
75      return new VariableVector(Condition.VariableDictionary.Values.Select(x => x.GetEmptyCopy()), Action.VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
76    }
77
78    public override string ToString() {
79      StringBuilder sb = new StringBuilder();
80      sb.Append("[");
81      sb.Append(condition.ToString());
82      sb.Append("||");
83      sb.Append(action.ToString());
84      sb.Append("]");
85      return sb.ToString();
86    }
87
88    public void Randomize(IRandom random, double spreadPercentage) {
89      condition.Randomize(random, spreadPercentage);
90      action.Randomize(random);
91    }
92
93    ICondition IClassifier.Condition {
94      get { return Condition; }
95    }
96
97    IAction IClassifier.Action {
98      get { return Action; }
99    }
100
101    public bool MatchInput(IInput target) {
102      return Condition.Match(target);
103    }
104
105    public bool MatchAction(IAction target) {
106      return Action.Match(target);
107    }
108
109    public bool IsMoreGeneral(IClassifier target) {
110      return IsMoreGeneral(target.Condition);
111    }
112
113    public bool IsMoreGeneral(ICondition target) {
114      var targetCast = target as VariableVectorCondition;
115      if (targetCast == null) { return false; }
116      if (Condition.VariableDictionary.Keys.Except(targetCast.VariableDictionary.Keys).Count() != 0
117        || targetCast.VariableDictionary.Keys.Except(Condition.VariableDictionary.Keys).Count() != 0) { return false; }
118
119      double thisGenerality = Condition.VariableDictionary.Values.Sum(x => x.GetGenerality());
120      double targetGenerality = targetCast.VariableDictionary.Values.Sum(x => x.GetGenerality());
121      if (thisGenerality <= targetGenerality) { return false; }
122
123      foreach (var variables in targetCast.VariableDictionary) {
124        if (!Condition.VariableDictionary[variables.Key].IsGreaterThanOrEquallyGeneral(variables.Value)) {
125          return false;
126        }
127      }
128      return true;
129    }
130
131    public bool Identical(IClassifier target) {
132      var targetCast = target as VariableVector;
133      if (targetCast == null) { return false; }
134      if (!condition.Identical(targetCast.Condition)) { return false; }
135      if (!action.Identical(targetCast.Action)) { return false; }
136      return true;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.