Exemplo n.º 1
0
        private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover <IRegressionProblemData> crossover)
        {
            var twister   = new MersenneTwister(31415);
            var dataset   = Util.CreateRandomDataset(twister, Rows, Columns);
            var grammar   = new FullFunctionalExpressionGrammar();
            var stopwatch = new Stopwatch();

            grammar.MaximumFunctionArguments   = 0;
            grammar.MaximumFunctionDefinitions = 0;
            grammar.MinimumFunctionArguments   = 0;
            grammar.MinimumFunctionDefinitions = 0;

            var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);

            foreach (ISymbolicExpressionTree tree in trees)
            {
                Util.InitTree(tree, twister, new List <string>(dataset.VariableNames));
            }
            var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
            var problem     = new SymbolicRegressionSingleObjectiveProblem();

            problem.ProblemData = problemData;

            var globalScope = new Scope("Global Scope");

            globalScope.Variables.Add(new Core.Variable("Random", twister));
            var context = new ExecutionContext(null, problem, globalScope);

            context = new ExecutionContext(context, crossover, globalScope);

            stopwatch.Start();
            for (int i = 0; i != PopulationSize; ++i)
            {
                var parent0      = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
                var scopeParent0 = new Scope();
                scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
                context.Scope.SubScopes.Add(scopeParent0);

                var parent1      = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
                var scopeParent1 = new Scope();
                scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
                context.Scope.SubScopes.Add(scopeParent1);

                crossover.Execute(context, new CancellationToken());

                context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
                context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
            }
            stopwatch.Stop();
            double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;

            Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
                              msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");

            foreach (var tree in trees)
            {
                HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests.Util.IsValid(tree);
            }
        }
 public override IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
   try {
     //create new executionContext for the instrumented operation to account for parameter lookup and name translation
     var executionContext = new ExecutionContext(context.Parent, instrumentedOperator, context.Scope);
     instrumentedOperator.ExecutionContext = executionContext;
     instrumentedOperator.cancellationToken = cancellationToken;
     foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
       param.ExecutionContext = executionContext;
     IOperation next = instrumentedOperator.InstrumentedApply();
     return next;
   }
   finally {
     foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType<ILookupParameter>())
       param.ExecutionContext = null;
     instrumentedOperator.ExecutionContext = null;
   }
 }
Exemplo n.º 3
0
 public override IOperation Execute(IExecutionContext context, CancellationToken cancellationToken)
 {
     try {
         //create new executionContext for the instrumented operation to account for parameter lookup and name translation
         var executionContext = new ExecutionContext(context.Parent, instrumentedOperator, context.Scope);
         instrumentedOperator.ExecutionContext  = executionContext;
         instrumentedOperator.cancellationToken = cancellationToken;
         foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType <ILookupParameter>())
         {
             param.ExecutionContext = executionContext;
         }
         IOperation next = instrumentedOperator.InstrumentedApply();
         return(next);
     }
     finally {
         foreach (ILookupParameter param in instrumentedOperator.Parameters.OfType <ILookupParameter>())
         {
             param.ExecutionContext = null;
         }
         instrumentedOperator.ExecutionContext = null;
     }
 }
Exemplo n.º 4
0
 private ExecutionContext(ExecutionContext original, Cloner cloner)
   : base(original, cloner) {
   parent = cloner.Clone(original.parent);
   parameterizedItem = cloner.Clone(original.parameterizedItem);
   scope = cloner.Clone(original.scope);
 }
Exemplo n.º 5
0
    public override void Prepare() {
      base.Prepare();
      globalScope.Clear();
      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));

      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
        ExecutionContext context = null;
        if (Problem != null) {
          foreach (var item in Problem.ExecutionContextItems)
            context = new ExecutionContext(context, item, globalScope);
        }
        context = new ExecutionContext(context, this, globalScope);
        context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
        engine.Prepare(context);
      }
    }
    private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
      var twister = new MersenneTwister(31415);
      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
      var grammar = new FullFunctionalExpressionGrammar();
      var stopwatch = new Stopwatch();

      grammar.MaximumFunctionArguments = 0;
      grammar.MaximumFunctionDefinitions = 0;
      grammar.MinimumFunctionArguments = 0;
      grammar.MinimumFunctionDefinitions = 0;

      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
      foreach (ISymbolicExpressionTree tree in trees) {
        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
      }
      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
      var problem = new SymbolicRegressionSingleObjectiveProblem();
      problem.ProblemData = problemData;

      var globalScope = new Scope("Global Scope");
      globalScope.Variables.Add(new Core.Variable("Random", twister));
      var context = new ExecutionContext(null, problem, globalScope);
      context = new ExecutionContext(context, crossover, globalScope);

      stopwatch.Start();
      for (int i = 0; i != PopulationSize; ++i) {
        var parent0 = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
        var scopeParent0 = new Scope();
        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
        context.Scope.SubScopes.Add(scopeParent0);

        var parent1 = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
        var scopeParent1 = new Scope();
        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
        context.Scope.SubScopes.Add(scopeParent1);

        crossover.Execute(context, new CancellationToken());

        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
      }
      stopwatch.Stop();
      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
      Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");

      foreach (var tree in trees)
        HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests.Util.IsValid(tree);
    }