Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/27/12 19:14:51 (11 years ago)
Author:
sforsten
Message:

#1980:

  • added ConditionActionClassificationProblem
  • added ConditionActionEncoding
  • added Manipulators, Crossovers and an LCSAdaptedGeneticAlgorithm
  • changed LearningClassifierSystemMainLoop
Location:
branches/LearningClassifierSystems
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/LearningClassifierSystems

    • Property svn:ignore set to
      *.suo
  • branches/LearningClassifierSystems/HeuristicLab.Encodings.CombinedIntegerVectorEncoding/3.3

    • Property svn:ignore set to
      *.user
      Plugin.cs
  • branches/LearningClassifierSystems/HeuristicLab.Encodings.CombinedIntegerVectorEncoding/3.3/CombinedIntegerVector.cs

    r8941 r9089  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
     28using HeuristicLab.Encodings.ConditionActionEncoding;
    2729using HeuristicLab.Encodings.IntegerVectorEncoding;
    2830using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3133  [StorableClass]
    3234  [Item("CombinedIntegerVector", "Represents a combined vector of integer values.")]
    33   public class CombinedIntegerVector : IntegerVector, IMatching {
     35  public class CombinedIntegerVector : IntegerVector, IClassifier {
    3436
    3537    [Storable]
    36     protected int actionPart;
     38    protected int actionLength;
     39    public int ActionLength { get { return actionLength; } }
    3740
    3841    /// <summary>
     
    4245    /// </summary>
    4346    [Storable]
    44     protected int[,] bounds;
     47    protected IntMatrix bounds;
     48    public IntMatrix Bounds { get { return (IntMatrix)bounds.Clone(); } }
    4549
    4650    [StorableConstructor]
     
    4852    public CombinedIntegerVector() : base() { }
    4953
    50     public CombinedIntegerVector(int[] elements, int actionPart, int[,] bounds)
     54    public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
    5155      : base(elements) {
    52       this.actionPart = actionPart;
     56      this.actionLength = actionPart;
    5357      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
    5458      this.bounds = bounds;
    5559    }
    5660
    57     public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, int[,] bounds)
     61    public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
    5862      : this(combinedVector.ToArray(), actionPart, bounds) { }
    5963
    60     public CombinedIntegerVector(IntegerVector condition, IntegerVector action, int[,] bounds)
     64    public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
    6165      : base(condition.Concat(action).ToArray()) {
    62       actionPart = action.Length;
     66      actionLength = action.Length;
    6367      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
    6468      this.bounds = bounds;
    6569    }
    6670
     71    public CombinedIntegerVector(IntegerVector condition, IntMatrix conditionBounds, IntegerVector action, IntMatrix actionBounds)
     72      : this(condition, action, CombineBounds(conditionBounds, actionBounds)) {
     73    }
     74
     75    private static IntMatrix CombineBounds(IntMatrix conditionBounds, IntMatrix actionBounds) {
     76      int columns = conditionBounds.Columns < actionBounds.Columns ? conditionBounds.Columns : actionBounds.Columns;
     77      IntMatrix bounds = new IntMatrix(conditionBounds.Rows + actionBounds.Rows, columns);
     78      for (int i = 0; i < conditionBounds.Rows; i++) {
     79        for (int j = 0; j < columns; j++) {
     80          bounds[i, j] = conditionBounds[i % conditionBounds.Rows, j];
     81        }
     82      }
     83
     84      for (int i = conditionBounds.Rows; i < actionBounds.Rows + conditionBounds.Rows; i++) {
     85        for (int j = 0; j < columns; j++) {
     86          bounds[i, j] = actionBounds[i % actionBounds.Rows, j];
     87        }
     88      }
     89      return bounds;
     90    }
     91
    6792    public CombinedIntegerVector(IRandom random, int length, int min, int max, int actionLenght, int actionMin, int actionMax) :
    6893      this(new IntegerVector(length, random, min, max), new IntegerVector(actionLenght, random, actionMin, actionMax), null) {
    69       bounds = new int[length + actionLenght, 2];
     94      bounds = new IntMatrix(length + actionLenght, 2);
    7095      for (int i = 0; i < length; i++) {
    7196        bounds[i, 0] = min;
     
    78103    }
    79104
    80     public CombinedIntegerVector(int length, int actionPart, int[,] bounds)
     105    public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
    81106      : base(length) {
    82       this.actionPart = actionPart;
     107      this.actionLength = actionPart;
    83108      this.bounds = bounds;
    84109    }
     
    86111    protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
    87112      : base(original, cloner) {
    88       actionPart = original.actionPart;
    89       bounds = (int[,])original.bounds.Clone();
    90     }
     113      actionLength = original.actionLength;
     114      bounds = (IntMatrix)original.bounds.Clone();
     115    }
     116
    91117    public override IDeepCloneable Clone(Cloner cloner) {
    92118      return new CombinedIntegerVector(this, cloner);
    93119    }
    94120
    95     public IMatching Condition {
     121    public IClassifier Condition {
    96122      get {
    97         int[] condition = new int[Length - actionPart];
    98         Array.Copy(this.array, condition, Length - actionPart);
     123        int[] condition = new int[Length - actionLength];
     124        Array.Copy(this.array, condition, Length - actionLength);
    99125        return new CombinedIntegerVector(condition, 0, bounds);
    100126      }
    101127    }
    102128
    103     public IMatching Action {
     129    public IClassifier Action {
    104130      get {
    105         int[] action = new int[actionPart];
    106         Array.Copy(this.array, Length - actionPart, action, 0, actionPart);
    107         return new CombinedIntegerVector(action, actionPart, bounds);
    108       }
    109     }
    110 
    111     public bool MatchCondition(IMatching target) {
     131        int[] action = new int[actionLength];
     132        Array.Copy(this.array, Length - actionLength, action, 0, actionLength);
     133
     134        IntMatrix actionBounds = GetElementsOfBoundsForAction(bounds, Length, actionLength);
     135        return new CombinedIntegerVector(action, actionLength, actionBounds);
     136      }
     137    }
     138
     139    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
     140      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
     141      int start = length - actionPartLength;
     142      for (int i = start; i < length; i++) {
     143        int pos = i % bounds.Rows;
     144        for (int j = 0; j < bounds.Columns; j++) {
     145          actionBounds[i - start, j] = bounds[pos, j];
     146        }
     147      }
     148      return actionBounds;
     149    }
     150
     151    public bool MatchCondition(IClassifier target) {
    112152      var targetVector = target as CombinedIntegerVector;
    113153      if (targetVector == null) return false;
     
    115155
    116156      int curbounds;
    117       for (int i = 0; i < this.Length - this.actionPart; i++) {
    118         curbounds = i % bounds.GetLength(0);
     157      for (int i = 0; i < this.Length - this.actionLength; i++) {
     158        curbounds = i % bounds.Rows;
    119159        //if don't care symbol is matched, next indices can be checked
    120160        if (this[i].Equals(bounds[curbounds, 1] - 1)) {
     
    129169
    130170    // no "don't care" symbols have to be considered
    131     public bool MatchAction(IMatching target) {
     171    public bool MatchAction(IClassifier target) {
    132172      var targetVector = target as CombinedIntegerVector;
    133173      if (targetVector == null) return false;
    134       if (targetVector.actionPart != this.actionPart) return false;
    135 
    136       int curPos = this.Length - this.actionPart;
    137       int curTargetPos = targetVector.Length - targetVector.actionPart;
    138       for (int i = 0; i < this.actionPart; i++) {
     174      if (targetVector.actionLength != this.actionLength) return false;
     175
     176      int curPos = this.Length - this.actionLength;
     177      int curTargetPos = targetVector.Length - targetVector.actionLength;
     178      for (int i = 0; i < this.actionLength; i++) {
    139179        if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
    140180          return false;
     
    149189      int curbounds;
    150190      for (int i = 0; i < this.Length; i++) {
    151         curbounds = i % bounds.GetLength(0);
    152         if (i >= this.Length - this.actionPart || this[i] < bounds[curbounds, 1] - 1) {
     191        curbounds = i % bounds.Rows;
     192        if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
    153193          strBuilder.Append(this[i]);
    154194        } else {
     
    171211    }
    172212
    173     public bool Equals(IMatching other) {
     213    public bool Equals(IClassifier other) {
    174214      return this.MatchAction(other) && this.MatchCondition(other);
    175215    }
    176216
    177217    public override int GetHashCode() {
    178       int result = actionPart;
     218      int result = actionLength;
    179219      for (int i = 0; i < array.Length; i++) {
    180220        result ^= array[i];
Note: See TracChangeset for help on using the changeset viewer.