Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/Simplifier.cs @ 15287

Last change on this file since 15287 was 15017, checked in by pkimmesw, 8 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 2.8 KB
RevLine 
[14777]1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {
2  using System;
3
4  using Expressions;
5
[15017]6  using HeuristicLab.Common;
[14952]7  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
[14777]8  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Tree;
9
10  public static class Simplifier {
11    private static readonly TreeNode<Expression> NoopNode =
[15017]12      new TreeNode<Expression>(ExpressionTable.GetStatelessExpression<ExecNoopExpression>());
[14777]13
[15017]14    public static PushProgram Simplify(PushProgram program, IReadOnlyPushConfiguration config, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
[14777]15      var bestResult = evaluator(program);
16      var tree = program.ToTree();
[15017]17      var optimizedTree = RemoveUnnecessaryExpressions(tree, tree, ref bestResult, evaluator, improveIfPossible);
18      var tmp = RemoveUnnecessaryExpressions(optimizedTree, optimizedTree, ref bestResult, evaluator, improveIfPossible);
[14777]19
[15017]20      while (tmp.Count < optimizedTree.Count) {
21        optimizedTree = tmp;
22      }
23
24      var optimizedProgram = optimizedTree.ToPushProgramWithoutNoopExpressions();
25      optimizedProgram = SimplifySubPrograms(optimizedProgram);
26
27      return optimizedProgram;
[14777]28    }
29
[15017]30    public static PushProgram SimplifySubPrograms(PushProgram program) {
31      var tree = program.ToTree();
32      var optimizedTree = SimplifySubPrograms(tree);
33      var optimizedPrgram = optimizedTree.ToPushProgram();
[14952]34
[15017]35      return optimizedPrgram;
36    }
[14952]37
[15017]38    private static TreeNode<Expression> SimplifySubPrograms(TreeNode<Expression> program) {
39      while (program.Children.Count == 1)
40        program = program.Children[0];
[14952]41
[15017]42      for (var i = 0; i < program.Children.Count; i++) {
43        var child = program.Children[i];
44        program.ReplaceNode(i, SimplifySubPrograms(child));
[14952]45      }
46
[15017]47      return program;
[14952]48    }
49
[15017]50    private static TreeNode<Expression> RemoveUnnecessaryExpressions(TreeNode<Expression> program, TreeNode<Expression> node, ref double bestResult, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
[14777]51      for (var i = 0; i < node.Children.Count; i++) {
52        var child = node.Children[i];
53        node.ReplaceNode(i, NoopNode);
54
55        var currentProgram = program.ToPushProgram();
56        var currentResult = evaluator(currentProgram);
57
[15017]58        if ((improveIfPossible && currentResult <= bestResult) ||
59            (!improveIfPossible && currentResult.IsAlmost(bestResult))) {
[14777]60          bestResult = currentResult;
61        } else if (child.Value.IsProgram) {
62          node.ReplaceNode(i, child);
63          var subProgram = RemoveUnnecessaryExpressions(program, child, ref bestResult, evaluator);
64          node.ReplaceNode(i, subProgram);
65        } else {
66          node.ReplaceNode(i, child);
67        }
68      }
69
70      return node;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.