Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/08 17:09:55 (16 years ago)
Author:
gkronber
Message:

worked on #237 (FunctionLibraryInjector that injects default function libraries with a few parameters)

  • added operator
  • added helper functions in Variable, Differential and Constraints to support the function library injector
Location:
trunk/sources/HeuristicLab.Functions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/Differential.cs

    r425 r467  
    3232
    3333namespace HeuristicLab.Functions {
    34   public sealed class Differential : FunctionBase {
    35 
    36     public const string WEIGHT = "Weight";
    37     public const string OFFSET = "SampleOffset";
    38     public const string INDEX = "Variable";
     34  public sealed class Differential : Variable {
    3935
    4036    public override string Description {
     
    4642    public Differential()
    4743      : base() {
    48       AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
    49       GetVariableInfo(INDEX).Local = true;
    50       AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
    51       GetVariableInfo(WEIGHT).Local = true;
    52       AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
    53       GetVariableInfo(OFFSET).Local = true;
    54 
    55       ConstrainedDoubleData weight = new ConstrainedDoubleData();
    56       // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
    57       weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
    58       AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
    59 
    60       ConstrainedIntData variable = new ConstrainedIntData();
    61       AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
    62 
    63       ConstrainedIntData sampleOffset = new ConstrainedIntData();
    64       // initialize a sample offset for static models
    65       IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(0, 0);
    66       offsetConstraint.LowerBoundIncluded = true;
    67       offsetConstraint.UpperBoundIncluded = true;
    68       sampleOffset.AddConstraint(offsetConstraint);
    69       AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
    70 
    71       SetupInitialization();
    72       SetupManipulation();
    73 
    74       // variable can't have suboperators
    75       AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
    76     }
    77 
    78     private void SetupInitialization() {
    79       AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for differentials", typeof(CombinedOperator), VariableKind.None));
    80       GetVariableInfo(INITIALIZATION).Local = false;
    81       CombinedOperator combinedOp = new CombinedOperator();
    82       SequentialProcessor seq = new SequentialProcessor();
    83       UniformRandomizer indexRandomizer = new UniformRandomizer();
    84       indexRandomizer.Min = 0;
    85       indexRandomizer.Max = 10;
    86       indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    87       indexRandomizer.Name = "Index Randomizer";
    88       NormalRandomizer weightRandomizer = new NormalRandomizer();
    89       weightRandomizer.Mu = 1.0;
    90       weightRandomizer.Sigma = 1.0;
    91       weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
    92       weightRandomizer.Name = "Weight Randomizer";
    93       UniformRandomizer offsetRandomizer = new UniformRandomizer();
    94       offsetRandomizer.Min = 0.0;
    95       offsetRandomizer.Max = 1.0;
    96       offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
    97       offsetRandomizer.Name = "Offset Randomizer";
    98 
    99       combinedOp.OperatorGraph.AddOperator(seq);
    100       combinedOp.OperatorGraph.AddOperator(indexRandomizer);
    101       combinedOp.OperatorGraph.AddOperator(weightRandomizer);
    102       combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
    103       combinedOp.OperatorGraph.InitialOperator = seq;
    104       seq.AddSubOperator(indexRandomizer);
    105       seq.AddSubOperator(weightRandomizer);
    106       seq.AddSubOperator(offsetRandomizer);
    107       AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
    108     }
    109 
    110     private void SetupManipulation() {
    111       // manipulation operator
    112       AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for differentials", typeof(CombinedOperator), VariableKind.None));
    113       GetVariableInfo(MANIPULATION).Local = false;
    114       CombinedOperator combinedOp = new CombinedOperator();
    115       SequentialProcessor seq = new SequentialProcessor();
    116       UniformRandomizer indexRandomizer = new UniformRandomizer();
    117       indexRandomizer.Min = 0;
    118       indexRandomizer.Max = 10;
    119       indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    120       indexRandomizer.Name = "Index Randomizer";
    121       NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
    122       weightRandomAdder.Mu = 0.0;
    123       weightRandomAdder.Sigma = 0.1;
    124       weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;
    125       weightRandomAdder.Name = "Weight Adder";
    126       NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();
    127       offsetRandomAdder.Mu = 0.0;
    128       offsetRandomAdder.Sigma = 1.0;
    129       offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
    130       offsetRandomAdder.Name = "Offset Adder";
    131 
    132       combinedOp.OperatorGraph.AddOperator(seq);
    133       combinedOp.OperatorGraph.AddOperator(indexRandomizer);
    134       combinedOp.OperatorGraph.AddOperator(weightRandomAdder);
    135       combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);
    136       combinedOp.OperatorGraph.InitialOperator = seq;
    137       seq.AddSubOperator(indexRandomizer);
    138       seq.AddSubOperator(weightRandomAdder);
    139       seq.AddSubOperator(offsetRandomAdder);
    140       AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
    14144    }
    14245
  • trunk/sources/HeuristicLab.Functions/Variable.cs

    r425 r467  
    3232
    3333namespace HeuristicLab.Functions {
    34   public sealed class Variable : FunctionBase {
     34  public class Variable : FunctionBase {
    3535
    3636    public const string WEIGHT = "Weight";
    3737    public const string OFFSET = "SampleOffset";
    3838    public const string INDEX = "Variable";
     39
     40    private int minIndex;
     41    private int maxIndex;
     42    private int minOffset;
     43    private int maxOffset;
    3944
    4045    public override string Description {
     
    5459      AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
    5560      GetVariableInfo(OFFSET).Local = true;
     61      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
     62      GetVariableInfo(INITIALIZATION).Local = false;
     63      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
     64      GetVariableInfo(MANIPULATION).Local = false;
    5665
    5766      ConstrainedDoubleData weight = new ConstrainedDoubleData();
     
    6271      ConstrainedIntData variable = new ConstrainedIntData();
    6372      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
     73      minIndex = 0; maxIndex = 100;
    6474
    6575      ConstrainedIntData sampleOffset = new ConstrainedIntData();
    66       // initialize a sample offset for static models
    67       IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(0, 0);
    68       offsetConstraint.LowerBoundIncluded = true;
    69       offsetConstraint.UpperBoundIncluded = true;
    70       sampleOffset.AddConstraint(offsetConstraint);
    7176      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
    7277
     
    7984
    8085    private void SetupInitialization() {
    81       AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
    82       GetVariableInfo(INITIALIZATION).Local = false;
    8386      CombinedOperator combinedOp = new CombinedOperator();
    8487      SequentialProcessor seq = new SequentialProcessor();
    8588      UniformRandomizer indexRandomizer = new UniformRandomizer();
    86       indexRandomizer.Min = 0;
    87       indexRandomizer.Max = 10;
     89      indexRandomizer.Min = minIndex;
     90      indexRandomizer.Max = maxIndex;
    8891      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    8992      indexRandomizer.Name = "Index Randomizer";
     
    9497      weightRandomizer.Name = "Weight Randomizer";
    9598      UniformRandomizer offsetRandomizer = new UniformRandomizer();
    96       offsetRandomizer.Min = 0.0;
    97       offsetRandomizer.Max = 1.0;
     99      offsetRandomizer.Min = minOffset;
     100      offsetRandomizer.Max = maxOffset;
    98101      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
    99102      offsetRandomizer.Name = "Offset Randomizer";
     
    107110      seq.AddSubOperator(weightRandomizer);
    108111      seq.AddSubOperator(offsetRandomizer);
    109       AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
     112      HeuristicLab.Core.IVariable initOp = GetVariable(INITIALIZATION);
     113      if(initOp == null) {
     114        AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
     115      } else {
     116        initOp.Value = combinedOp;
     117      }
    110118    }
    111119
    112120    private void SetupManipulation() {
    113121      // manipulation operator
    114       AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
    115       GetVariableInfo(MANIPULATION).Local = false;
    116122      CombinedOperator combinedOp = new CombinedOperator();
    117123      SequentialProcessor seq = new SequentialProcessor();
    118124      UniformRandomizer indexRandomizer = new UniformRandomizer();
    119       indexRandomizer.Min = 0;
    120       indexRandomizer.Max = 10;
     125      indexRandomizer.Min = minIndex;
     126      indexRandomizer.Max = maxIndex;
    121127      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
    122128      indexRandomizer.Name = "Index Randomizer";
     
    140146      seq.AddSubOperator(weightRandomAdder);
    141147      seq.AddSubOperator(offsetRandomAdder);
    142       AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
     148      HeuristicLab.Core.IVariable manipulationOp = GetVariable(MANIPULATION);
     149      if(manipulationOp == null) {
     150        AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
     151      } else {
     152        manipulationOp.Value = combinedOp;
     153      }
     154    }
     155
     156    public void SetConstraints(int[] allowedIndexes, int minSampleOffset, int maxSampleOffset) {
     157      ConstrainedIntData offset = GetVariableValue<ConstrainedIntData>(OFFSET, null, false);
     158      IntBoundedConstraint rangeConstraint = new IntBoundedConstraint();
     159      this.minOffset = minSampleOffset;
     160      this.maxOffset = maxSampleOffset;
     161      rangeConstraint.LowerBound = minSampleOffset;
     162      rangeConstraint.LowerBoundEnabled = true;
     163      rangeConstraint.LowerBoundIncluded = true;
     164      rangeConstraint.UpperBound = maxSampleOffset;
     165      rangeConstraint.UpperBoundEnabled = true;
     166      rangeConstraint.UpperBoundIncluded = true;
     167      offset.AddConstraint(rangeConstraint);
     168
     169      ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
     170      Array.Sort(allowedIndexes);
     171      minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1];
     172      List<IConstraint> constraints = new List<IConstraint>();
     173      int start = allowedIndexes[0];
     174      int prev = start;
     175      for(int i = 1; i < allowedIndexes.Length; i++) {
     176        if(allowedIndexes[i] != prev + 1) {
     177          IntBoundedConstraint lastRange = new IntBoundedConstraint();
     178          lastRange.LowerBound = start;
     179          lastRange.LowerBoundEnabled = true;
     180          lastRange.LowerBoundIncluded = true;
     181          lastRange.UpperBound = prev;
     182          lastRange.UpperBoundEnabled = true;
     183          lastRange.UpperBoundIncluded = true;
     184          constraints.Add(lastRange);
     185          start = allowedIndexes[i];
     186          prev = start;
     187        }
     188        prev = allowedIndexes[i];
     189      }
     190      if(constraints.Count > 1) {
     191        OrConstraint or = new OrConstraint();
     192        foreach(IConstraint c in constraints) or.Clauses.Add(c);
     193        index.AddConstraint(or);
     194      } else {
     195        index.AddConstraint(constraints[0]);
     196      }
     197
     198      SetupInitialization();
     199      SetupManipulation();
    143200    }
    144201
Note: See TracChangeset for help on using the changeset viewer.