Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/30/09 19:41:58 (15 years ago)
Author:
gkronber
Message:

GP Refactoring #713

  • cleaned code
  • reintegrated GP.Boolean and GP.SantaFe
  • worked on serialization of function trees
Location:
branches/GP-Refactoring-713/sources/HeuristicLab.GP.Boolean/3.3/Symbols
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Boolean/3.3/Symbols/Variable.cs

    r2211 r2216  
    2626using HeuristicLab.Core;
    2727using System.Xml;
    28 using HeuristicLab.Constraints;
    29 using HeuristicLab.DataAnalysis;
    3028using HeuristicLab.Operators;
    3129using HeuristicLab.Random;
    3230
    3331namespace HeuristicLab.GP.Boolean {
    34   public sealed class Variable : FunctionBase {
    35     public const string INDEX = "Variable";
    36 
    37     private int minIndex;
    38     private int maxIndex;
    39 
    40     public override string Description {
    41       get { return ""; }
    42     }
     32  public sealed class Variable : Terminal {
     33    public const string VARIABLENAME = "Variable";
    4334
    4435    public Variable()
    4536      : base() {
    46       AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
    47       GetVariableInfo(INDEX).Local = true;
    48       AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
    49       GetVariableInfo(INITIALIZATION).Local = false;
    50       AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
    51       GetVariableInfo(MANIPULATION).Local = false;
    52 
    53       MinArity = 0; MaxArity = 0;
    54 
    55       ConstrainedIntData variable = new ConstrainedIntData();
    56       AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
    57       minIndex = 0; maxIndex = 100;
    58 
    5937      SetupInitialization();
    6038      SetupManipulation();
     39    }
    6140
     41    public override HeuristicLab.GP.Interfaces.IFunctionTree GetTreeNode() {
     42      return new VariableFunctionTree(this);
    6243    }
    6344
     
    6546      CombinedOperator combinedOp = new CombinedOperator();
    6647      SequentialProcessor seq = new SequentialProcessor();
    67       UniformRandomizer indexRandomizer = new UniformRandomizer();
    68       indexRandomizer.Min = minIndex;
    69       indexRandomizer.Max = maxIndex + 1; // uniform randomizer generates numbers in the range [min, max[
    70       indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    71       indexRandomizer.Name = "Index Randomizer";
     48      UniformItemChooser variableRandomizer = new UniformItemChooser();
     49      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
     50      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
     51      variableRandomizer.Name = "Variable randomizer";
    7252
    7353      combinedOp.OperatorGraph.AddOperator(seq);
    74       combinedOp.OperatorGraph.AddOperator(indexRandomizer);
     54      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
    7555      combinedOp.OperatorGraph.InitialOperator = seq;
    76       seq.AddSubOperator(indexRandomizer);
     56      seq.AddSubOperator(variableRandomizer);
    7757      Initializer = combinedOp;
    7858    }
     
    8262      CombinedOperator combinedOp = new CombinedOperator();
    8363      SequentialProcessor seq = new SequentialProcessor();
    84       UniformRandomizer indexRandomizer = new UniformRandomizer();
    85       indexRandomizer.Min = minIndex;
    86       indexRandomizer.Max = maxIndex + 1;
    87       indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    88       indexRandomizer.Name = "Index Randomizer";
     64      UniformItemChooser variableRandomizer = new UniformItemChooser();
     65      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
     66      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
     67      variableRandomizer.Name = "Variable randomizer";
    8968
    9069      combinedOp.OperatorGraph.AddOperator(seq);
    91       combinedOp.OperatorGraph.AddOperator(indexRandomizer);
     70      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
    9271      combinedOp.OperatorGraph.InitialOperator = seq;
    93       seq.AddSubOperator(indexRandomizer);
     72      seq.AddSubOperator(variableRandomizer);
    9473      Manipulator = combinedOp;
    95     }
    96 
    97     public void SetConstraints(int[] allowedIndexes) {
    98       //ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
    99       Array.Sort(allowedIndexes);
    100       minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1];
    101       List<IConstraint> constraints = new List<IConstraint>();
    102       int start = allowedIndexes[0];
    103       int prev = start;
    104       for(int i = 1; i < allowedIndexes.Length; i++) {
    105         if(allowedIndexes[i] != prev + 1) {
    106           IntBoundedConstraint lastRange = new IntBoundedConstraint();
    107           lastRange.LowerBound = start;
    108           lastRange.LowerBoundEnabled = true;
    109           lastRange.LowerBoundIncluded = true;
    110           lastRange.UpperBound = prev;
    111           lastRange.UpperBoundEnabled = true;
    112           lastRange.UpperBoundIncluded = true;
    113           constraints.Add(lastRange);
    114           start = allowedIndexes[i];
    115           prev = start;
    116         }
    117         prev = allowedIndexes[i];
    118       }
    119       IntBoundedConstraint range = new IntBoundedConstraint();
    120       range.LowerBound = start;
    121       range.LowerBoundEnabled = true;
    122       range.LowerBoundIncluded = true;
    123       range.UpperBound = prev;
    124       range.UpperBoundEnabled = true;
    125       range.UpperBoundIncluded = true;
    126       constraints.Add(range);
    127       if(constraints.Count > 1) {
    128         OrConstraint or = new OrConstraint();
    129         foreach(IConstraint c in constraints) or.Clauses.Add(c);
    130         index.AddConstraint(or);
    131       } else {
    132         index.AddConstraint(constraints[0]);
    133       }
    134 
    135       SetupInitialization();
    136       SetupManipulation();
    13774    }
    13875  }
Note: See TracChangeset for help on using the changeset viewer.