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.StructureIdentification/3.3
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs

    r2212 r2216  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.GP.Interfaces;
     26using HeuristicLab.GP.SantaFe;
    2627
    2728namespace HeuristicLab.GP.StructureIdentification {
    28   public class FunctionLibraryInjector : OperatorBase {
    29     private const string FUNCTIONLIBRARY = "FunctionLibrary";
    30     private const string TARGETVARIABLE = "TargetVariable";
     29  public class FunctionLibraryInjector : FunctionLibraryInjectorBase {
    3130    private const string MINTIMEOFFSET = "MinTimeOffset";
    3231    private const string MAXTIMEOFFSET = "MaxTimeOffset";
     
    5756    private const string XOR_ALLOWED = "Xor";
    5857
     58    private int minTimeOffset;
     59    private int maxTimeOffset;
    5960
    6061    public override string Description {
     
    6465    public FunctionLibraryInjector()
    6566      : base() {
    66       AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
    6767      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
    6868      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
    69       AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default function library", typeof(FunctionLibrary), VariableKind.New));
    7069
    7170      AddVariable(DIFFERENTIALS_ALLOWED, false);
     
    102101
    103102    public override IOperation Apply(IScope scope) {
    104       FunctionLibrary functionLibrary = new FunctionLibrary();
    105       int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
    106 
    107103      // try to get minTimeOffset (use 0 as default if not available)
    108104      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
    109       int minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
     105      minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
    110106      // try to get maxTimeOffset (use 0 as default if not available)
    111107      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
    112       int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
     108      maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
     109
     110      return base.Apply(scope);
     111    }
     112
     113    protected override FunctionLibrary CreateFunctionLibrary() {
     114      FunctionLibrary functionLibrary = new FunctionLibrary();
    113115
    114116      Variable variable = new Variable();
     
    218220      differential.SetConstraints(minTimeOffset, maxTimeOffset);
    219221
    220       scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), functionLibrary));
    221 
    222       return null;
     222      return functionLibrary;
    223223    }
    224224
     
    230230      if (GetVariableValue<BoolData>(condName, null, false).Data) functionLib.AddFunction(op);
    231231    }
    232 
    233     private void SetAllowedSubOperators(IFunction f, List<IFunction> gs) {
    234       for (int i = 0; i < f.MaxSubTrees; i++) {
    235         SetAllowedSubOperators(f, i, gs);
    236       }
    237     }
    238 
    239     private void SetAllowedSubOperators(IFunction f, int i, List<IFunction> gs) {
    240       gs.ForEach((g) => f.AddAllowedSubFunction(g, i));
    241     }
    242232  }
    243233}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/ModelAnalyzerExporter.cs

    r2212 r2216  
    2121
    2222using HeuristicLab.GP.Interfaces;
     23using System;
    2324
    2425namespace HeuristicLab.GP.StructureIdentification {
    25   public class ModelAnalyzerExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
     26  public class ModelAnalyzerExporter : IFunctionTreeSerializer, IFunctionTreeNameGenerator {
    2627    #region IFunctionTreeExporter Members
    2728
     
    5354      if(tree.SubTrees.Count > 0) result += ")";
    5455      return result;
     56    }
     57
     58    public IFunctionTree Import(string tree) {
     59      throw new UnknownFunctionException(tree);
     60    }
     61
     62    public bool TryImport(string tree, out IFunctionTree importedTree) {
     63      try {
     64        importedTree = Import(tree);
     65        return true;
     66      }
     67      catch (UnknownFunctionException) {
     68        importedTree = null;
     69        return false;
     70      }
    5571    }
    5672
     
    104120
    105121    #endregion
     122
     123
    106124  }
    107125
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/SymbolicExpressionExporter.cs

    r2212 r2216  
    2222using System.Text;
    2323using HeuristicLab.GP.Interfaces;
     24using System;
     25using System.Collections.Generic;
    2426
    2527namespace HeuristicLab.GP.StructureIdentification {
    26   public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
     28  public class SymbolicExpressionExporter : IFunctionTreeSerializer, IFunctionTreeNameGenerator {
    2729    private StringBuilder builder;
    2830    private string currentIndent;
     
    4850      } catch(UnknownFunctionException) {
    4951        exported = "";
     52        return false;
     53      }
     54    }
     55
     56    public IFunctionTree Import(string tree) {
     57      Queue<string> tokens = new Queue<string>(tree.Split(' ', '\n', '\t'));
     58      return ParseTree(tokens);
     59    }
     60
     61    private IFunctionTree ParseTree(Queue<string> tokens) {
     62      Expect("(", tokens);
     63      string funSymb = tokens.Peek();
     64      double constValue;
     65      if (double.TryParse(funSymb, out constValue)) {
     66        throw new UnknownFunctionException(funSymb);
     67      }
     68      switch (funSymb) {
     69        case "variable": return ParseVariable(tokens);
     70        case "differential": return ParseDifferential(tokens);
     71        default:
     72          IFunctionTree fNode = ParseFunction(tokens);
     73          Expect(")", tokens);
     74          return fNode;
     75      }
     76
     77    }
     78
     79    private IFunctionTree ParseFunction(Queue<string> tokens) {
     80      throw new UnknownFunctionException(tokens.Dequeue());
     81    }
     82
     83    private IFunctionTree ParseDifferential(Queue<string> tokens) {
     84      throw new UnknownFunctionException(tokens.Dequeue());
     85    }
     86
     87    private IFunctionTree ParseVariable(Queue<string> tokens) {
     88      throw new UnknownFunctionException(tokens.Dequeue());
     89    }
     90
     91    private void Expect(string p, Queue<string> tokens) {
     92      throw new NotImplementedException();
     93    }
     94
     95    public bool TryImport(string tree, out IFunctionTree importedTree) {
     96      try {
     97        importedTree = Import(tree);
     98        return true;
     99      }
     100      catch (UnknownFunctionException) {
     101        importedTree = null;
    50102        return false;
    51103      }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Constant.cs

    r2212 r2216  
    3131    public override string Description {
    3232      get { return "Returns the value of local variable 'Value'."; }
    33     }
    34 
    35     public override IEnumerable<string> LocalParameterNames {
    36       get {
    37         return new string[] { VALUE };
    38       }
    3933    }
    4034
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/ConstantFunctionTree.cs

    r2212 r2216  
    2626
    2727namespace HeuristicLab.GP.StructureIdentification {
    28   public class ConstantFunctionTree : FunctionTreeBase {
    29     private static readonly IList<IFunctionTree> subTrees = new List<IFunctionTree>().AsReadOnly();
     28  public class ConstantFunctionTree : TerminalTreeNode {
    3029    public double Value { get; set; }
    3130
    32     public ConstantFunctionTree(Constant constant) {
    33       Function = constant;
     31    public ConstantFunctionTree(Constant constant) : base(constant){
    3432    }
    3533
    36     protected ConstantFunctionTree(ConstantFunctionTree original) {
    37       Function = original.Function;
     34    protected ConstantFunctionTree(ConstantFunctionTree original) : base(original){
    3835      Value = original.Value;
    39     }
    40 
    41     public override IList<IFunctionTree> SubTrees {
    42       get {
    43         return subTrees;
    44       }
    4536    }
    4637
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/VariableFunctionTree.cs

    r2212 r2216  
    2626
    2727namespace HeuristicLab.GP.StructureIdentification {
    28   public class VariableFunctionTree : FunctionTreeBase {
    29     private static readonly IList<IFunctionTree> subTrees = new List<IFunctionTree>().AsReadOnly();
     28  public class VariableFunctionTree : TerminalTreeNode {
    3029    public double Weight { get; set; }
    3130    public string VariableName { get; set; }
    3231    public int SampleOffset { get; set; }
    3332
    34     public VariableFunctionTree(Variable variable) {
    35       Function = variable;
     33    public VariableFunctionTree(Variable variable)
     34      : base(variable) {
    3635    }
    3736
    38     protected VariableFunctionTree(VariableFunctionTree original) {
    39       Function = original.Function;
     37    protected VariableFunctionTree(VariableFunctionTree original)
     38      : base(original) {
    4039      Weight = original.Weight;
    4140      VariableName = original.VariableName;
    4241      SampleOffset = original.SampleOffset;
    43     }
    44 
    45     public override IList<IFunctionTree> SubTrees {
    46       get {
    47         return subTrees;
    48       }
    4942    }
    5043
     
    6659    public override IOperation CreateInitOperation(IScope scope) {
    6760      Scope myVariableScope = new Scope();
    68       scope.AddSubScope(myVariableScope); 
     61      scope.AddSubScope(myVariableScope);
    6962      myVariableScope.AddVariable(CreateWeightVariable());
    7063      myVariableScope.AddVariable(CreateVariableNameVariable());
Note: See TracChangeset for help on using the changeset viewer.