Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added ProportionalTournamentSelector for XCS
  • fixed bug: if an initial population is created in XCS, the initial population also creates general classifier, not only specific ones
  • merged r9204:9466 HeuristicLab.Core from trunk to branch
File size: 5.1 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;
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 {
[9204]35
[9194]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 {
[9242]45      get { return condition.VariableDictionary.Count + action.VariableDictionary.Count; }
[9194]46    }
47
48    public int VirtualLength {
[9242]49      get { return condition.VariableDictionary.Values.Sum(x => x.VirtualLength) + action.VariableDictionary.Values.Sum(x => x.VirtualLength); }
[9194]50    }
51
52    [StorableConstructor]
53    protected VariableVector(bool deserializing) : base(deserializing) { }
54    protected VariableVector(VariableVector original, Cloner cloner)
55      : base(original, cloner) {
[9226]56      condition = (VariableVectorCondition)original.condition.Clone();
57      action = (VariableVectorAction)original.action;
[9194]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(); }
[9226]66      if (!action.All(x => x is IActionVariable)) { throw new ArgumentException("Action can only use IActionVariable"); }
[9194]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
[9226]74    public VariableVector GetEmptyCopy() {
[9242]75      return new VariableVector(Condition.VariableDictionary.Values.Select(x => x.GetEmptyCopy()), Action.VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
[9226]76    }
77
[9194]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
[9467]88    public void Randomize(IRandom random, double changeSymbolProbability, double spreadPercentage) {
89      condition.Randomize(random, changeSymbolProbability, spreadPercentage);
[9194]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) {
[9242]114      var targetCast = target as VariableVectorCondition;
[9194]115      if (targetCast == null) { return false; }
[9242]116      if (Condition.VariableDictionary.Keys.Except(targetCast.VariableDictionary.Keys).Count() != 0
117        || targetCast.VariableDictionary.Keys.Except(Condition.VariableDictionary.Keys).Count() != 0) { return false; }
[9194]118
[9242]119      double thisGenerality = Condition.VariableDictionary.Values.Sum(x => x.GetGenerality());
120      double targetGenerality = targetCast.VariableDictionary.Values.Sum(x => x.GetGenerality());
[9194]121      if (thisGenerality <= targetGenerality) { return false; }
122
[9242]123      foreach (var variables in targetCast.VariableDictionary) {
124        if (!Condition.VariableDictionary[variables.Key].IsGreaterThanOrEquallyGeneral(variables.Value)) {
[9194]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; }
[9226]134      if (!condition.Identical(targetCast.Condition)) { return false; }
135      if (!action.Identical(targetCast.Action)) { return false; }
[9194]136      return true;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.