Пример #1
0
 private Root(Root original, Cloner cloner) : base(original, cloner) { }
    private void Initialize(IEnumerable<string> variableNames, int nConstants) {
      #region symbol declaration
      var add = new Addition();
      var sub = new Subtraction();
      var mul = new Multiplication();
      var div = new Division();
      var mean = new Average();
      var log = new Logarithm();
      var pow = new Power();
      var square = new Square();
      var root = new Root();
      var sqrt = new SquareRoot();
      var exp = new Exponential();

      // we use our own random number generator here because we assume 
      // that grammars are only initialized once when setting the grammar in the problem.
      // This means everytime the grammar parameter in the problem is changed
      // we initialize the constants to new values
      var rand = new MersenneTwister();
      // warm up
      for (int i = 0; i < 1000; i++) rand.NextDouble();

      var constants = new List<Constant>(nConstants);
      for (int i = 0; i < nConstants; i++) {
        var constant = new Constant();
        do {
          var constVal = rand.NextDouble() * 20.0 - 10.0;
          constant.Name = string.Format("{0:0.000}", constVal);
          constant.MinValue = constVal;
          constant.MaxValue = constVal;
          constant.ManipulatorSigma = 0.0;
          constant.ManipulatorMu = 0.0;
          constant.MultiplicativeManipulatorSigma = 0.0;
        } while (constants.Any(c => c.Name == constant.Name)); // unlikely, but it could happen that the same constant value is sampled twice. so we resample if necessary.
        constants.Add(constant);
      }

      var variables = new List<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>();
      foreach (var variableName in variableNames) {
        var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
        variableSymbol.Name = variableName;
        variableSymbol.WeightManipulatorMu = 0.0;
        variableSymbol.WeightManipulatorSigma = 0.0;
        variableSymbol.WeightMu = 1.0;
        variableSymbol.WeightSigma = 0.0;
        variableSymbol.MultiplicativeWeightManipulatorSigma = 0.0;
        variableSymbol.AllVariableNames = new[] { variableName };
        variableSymbol.VariableNames = new[] { variableName };
        variables.Add(variableSymbol);
      }

      #endregion

      AddSymbol(add);
      AddSymbol(sub);
      AddSymbol(mul);
      AddSymbol(div);
      AddSymbol(mean);
      AddSymbol(log);
      AddSymbol(pow);
      AddSymbol(square);
      AddSymbol(root);
      AddSymbol(sqrt);
      AddSymbol(exp);
      constants.ForEach(AddSymbol);
      variables.ForEach(AddSymbol);

      #region subtree count configuration
      SetSubtreeCount(add, 2, 2);
      SetSubtreeCount(sub, 2, 2);
      SetSubtreeCount(mul, 2, 2);
      SetSubtreeCount(div, 2, 2);
      SetSubtreeCount(mean, 2, 2);
      SetSubtreeCount(log, 1, 1);
      SetSubtreeCount(pow, 2, 2);
      SetSubtreeCount(square, 1, 1);
      SetSubtreeCount(root, 2, 2);
      SetSubtreeCount(sqrt, 1, 1);
      SetSubtreeCount(exp, 1, 1);
      constants.ForEach((c) => SetSubtreeCount(c, 0, 0));
      variables.ForEach((v) => SetSubtreeCount(v, 0, 0));
      #endregion

      var functions = new ISymbol[] { add, sub, mul, div, mean, log, pow, root, square, sqrt };
      var terminalSymbols = variables.Concat<ISymbol>(constants);
      var allSymbols = functions.Concat(terminalSymbols);

      #region allowed child symbols configuration
      foreach (var s in allSymbols) {
        AddAllowedChildSymbol(StartSymbol, s);
      }
      foreach (var parentSymb in functions)
        foreach (var childSymb in allSymbols) {
          AddAllowedChildSymbol(parentSymb, childSymb);
        }

      #endregion
    }
    private void Initialize() {
      #region symbol declaration
      var add = new Addition();
      var sub = new Subtraction();
      var mul = new Multiplication();
      var div = new Division();
      var mean = new Average();
      var sin = new Sine();
      var cos = new Cosine();
      var tan = new Tangent();
      var log = new Logarithm();
      var pow = new Power();
      var square = new Square();
      var root = new Root();
      var sqrt = new SquareRoot();
      var exp = new Exponential();

      var airyA = new AiryA();
      var airyB = new AiryB();
      var bessel = new Bessel();
      var cosineIntegral = new CosineIntegral();
      var dawson = new Dawson();
      var erf = new Erf();
      var expIntegralEi = new ExponentialIntegralEi();
      var fresnelCosineIntegral = new FresnelCosineIntegral();
      var fresnelSineIntegral = new FresnelSineIntegral();
      var gamma = new Gamma();
      var hypCosineIntegral = new HyperbolicCosineIntegral();
      var hypSineIntegral = new HyperbolicSineIntegral();
      var norm = new Norm();
      var psi = new Psi();
      var sineIntegral = new SineIntegral();

      var @if = new IfThenElse();
      var gt = new GreaterThan();
      var lt = new LessThan();
      var and = new And();
      var or = new Or();
      var not = new Not();
      var xor = new Xor();
      var variableCondition = new VariableCondition();

      var timeLag = new TimeLag();
      var integral = new Integral();
      var derivative = new Derivative();

      var constant = new Constant();
      constant.MinValue = -20;
      constant.MaxValue = 20;
      var variableSymbol = new Variable();
      var laggedVariable = new LaggedVariable();
      var autoregressiveVariable = new AutoregressiveTargetVariable();
      #endregion

      #region group symbol declaration
      var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
      var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan });
      var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
      var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, 
        fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral});
      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol });
      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });

      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root });

      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });

      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
      #endregion

      AddSymbol(realValuedSymbols);
      AddSymbol(powerSymbols);
      AddSymbol(conditionalSymbols);
      AddSymbol(timeSeriesSymbols);

      #region subtree count configuration
      SetSubtreeCount(arithmeticSymbols, 2, 2);
      SetSubtreeCount(trigonometricSymbols, 1, 1);
      SetSubtreeCount(pow, 2, 2);
      SetSubtreeCount(root, 2, 2);
      SetSubtreeCount(square, 1, 1);
      SetSubtreeCount(sqrt, 1, 1);
      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
      SetSubtreeCount(specialFunctions, 1, 1);
      SetSubtreeCount(terminalSymbols, 0, 0);

      SetSubtreeCount(@if, 3, 3);
      SetSubtreeCount(variableCondition, 2, 2);
      SetSubtreeCount(comparisonSymbols, 2, 2);
      SetSubtreeCount(and, 2, 2);
      SetSubtreeCount(or, 2, 2);
      SetSubtreeCount(not, 1, 1);
      SetSubtreeCount(xor, 2, 2);

      SetSubtreeCount(timeLag, 1, 1);
      SetSubtreeCount(integral, 1, 1);
      SetSubtreeCount(derivative, 1, 1);
      SetSubtreeCount(laggedVariable, 0, 0);
      SetSubtreeCount(autoregressiveVariable, 0, 0);
      #endregion

      #region allowed child symbols configuration
      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
      AddAllowedChildSymbol(StartSymbol, powerSymbols);
      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
      AddAllowedChildSymbol(StartSymbol, specialFunctions);

      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
      AddAllowedChildSymbol(DefunSymbol, specialFunctions);

      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);

      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
      AddAllowedChildSymbol(powerSymbols, constant, 1);

      AddAllowedChildSymbol(square, realValuedSymbols, 0);
      AddAllowedChildSymbol(square, conditionSymbols, 0);
      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);

      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);

      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
      AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
      AddAllowedChildSymbol(@if, conditionSymbols, 1);
      AddAllowedChildSymbol(@if, realValuedSymbols, 1);
      AddAllowedChildSymbol(@if, powerSymbols, 1);
      AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
      AddAllowedChildSymbol(@if, conditionSymbols, 2);
      AddAllowedChildSymbol(@if, realValuedSymbols, 2);
      AddAllowedChildSymbol(@if, powerSymbols, 2);
      AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);

      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);

      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
      AddAllowedChildSymbol(variableCondition, powerSymbols);
      AddAllowedChildSymbol(variableCondition, conditionSymbols);
      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);


      AddAllowedChildSymbol(timeLag, realValuedSymbols);
      AddAllowedChildSymbol(timeLag, powerSymbols);
      AddAllowedChildSymbol(timeLag, conditionSymbols);

      AddAllowedChildSymbol(integral, realValuedSymbols);
      AddAllowedChildSymbol(integral, powerSymbols);
      AddAllowedChildSymbol(integral, conditionSymbols);

      AddAllowedChildSymbol(derivative, realValuedSymbols);
      AddAllowedChildSymbol(derivative, powerSymbols);
      AddAllowedChildSymbol(derivative, conditionSymbols);
      #endregion
    }
    private void Initialize() {
      var add = new Addition();
      var sub = new Subtraction();
      var mul = new Multiplication();
      var div = new Division();
      var mean = new Average();
      var sin = new Sine();
      var cos = new Cosine();
      var tan = new Tangent();
      var log = new Logarithm();
      var pow = new Power();
      pow.InitialFrequency = 0.0;
      var square = new Square();
      square.InitialFrequency = 0.0;
      var root = new Root();
      root.InitialFrequency = 0.0;
      var sqrt = new SquareRoot();
      sqrt.InitialFrequency = 0.0;
      var airyA = new AiryA();
      airyA.InitialFrequency = 0.0;
      var airyB = new AiryB();
      airyB.InitialFrequency = 0.0;
      var bessel = new Bessel();
      bessel.InitialFrequency = 0.0;
      var cosineIntegral = new CosineIntegral();
      cosineIntegral.InitialFrequency = 0.0;
      var dawson = new Dawson();
      dawson.InitialFrequency = 0.0;
      var erf = new Erf();
      erf.InitialFrequency = 0.0;
      var expIntegralEi = new ExponentialIntegralEi();
      expIntegralEi.InitialFrequency = 0.0;
      var fresnelCosineIntegral = new FresnelCosineIntegral();
      fresnelCosineIntegral.InitialFrequency = 0.0;
      var fresnelSineIntegral = new FresnelSineIntegral();
      fresnelSineIntegral.InitialFrequency = 0.0;
      var gamma = new Gamma();
      gamma.InitialFrequency = 0.0;
      var hypCosineIntegral = new HyperbolicCosineIntegral();
      hypCosineIntegral.InitialFrequency = 0.0;
      var hypSineIntegral = new HyperbolicSineIntegral();
      hypSineIntegral.InitialFrequency = 0.0;
      var norm = new Norm();
      norm.InitialFrequency = 0.0;
      var psi = new Psi();
      psi.InitialFrequency = 0.0;
      var sineIntegral = new SineIntegral();
      sineIntegral.InitialFrequency = 0.0;

      var exp = new Exponential();
      var @if = new IfThenElse();
      var gt = new GreaterThan();
      var lt = new LessThan();
      var and = new And();
      var or = new Or();
      var not = new Not();
      var xor = new Xor();

      var timeLag = new TimeLag();
      timeLag.InitialFrequency = 0.0;
      var integral = new Integral();
      integral.InitialFrequency = 0.0;
      var derivative = new Derivative();
      derivative.InitialFrequency = 0.0;

      var variableCondition = new VariableCondition();
      variableCondition.InitialFrequency = 0.0;

      var constant = new Constant();
      constant.MinValue = -20;
      constant.MaxValue = 20;
      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
      var laggedVariable = new LaggedVariable();
      laggedVariable.InitialFrequency = 0.0;
      var autoregressiveVariable = new AutoregressiveTargetVariable();
      autoregressiveVariable.InitialFrequency = 0.0;
      autoregressiveVariable.Enabled = false;

      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, square, pow, sqrt, root, exp,
        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral,
        @if, gt, lt, and, or, not,xor, timeLag, integral, derivative, constant, variableSymbol, laggedVariable,autoregressiveVariable, variableCondition };
      var unaryFunctionSymbols = new List<Symbol>() { square, sqrt, sin, cos, tan, log, exp, not, timeLag, integral, derivative,
        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral
      };

      var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
      var ternarySymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or, xor };
      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable, autoregressiveVariable };

      foreach (var symb in allSymbols)
        AddSymbol(symb);

      foreach (var funSymb in ternarySymbols) {
        SetSubtreeCount(funSymb, 1, 3);
      }
      foreach (var funSymb in unaryFunctionSymbols) {
        SetSubtreeCount(funSymb, 1, 1);
      }
      foreach (var funSymb in binaryFunctionSymbols) {
        SetSubtreeCount(funSymb, 2, 2);
      }
      foreach (var terminalSymbol in terminalSymbols) {
        SetSubtreeCount(terminalSymbol, 0, 0);
      }

      SetSubtreeCount(@if, 3, 3);


      // allow each symbol as child of the start symbol
      foreach (var symb in allSymbols) {
        AddAllowedChildSymbol(StartSymbol, symb);
        AddAllowedChildSymbol(DefunSymbol, symb);
      }

      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
      foreach (var parent in allSymbols.Except(terminalSymbols)) {
        foreach (var child in allSymbols)
          AddAllowedChildSymbol(parent, child);
      }
    }