Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15189 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
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {
2  using System;
3
4  using Expressions;
5
6  using HeuristicLab.Common;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Tree;
9
10  public static class Simplifier {
11    private static readonly TreeNode<Expression> NoopNode =
12      new TreeNode<Expression>(ExpressionTable.GetStatelessExpression<ExecNoopExpression>());
13
14    public static PushProgram Simplify(PushProgram program, IReadOnlyPushConfiguration config, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
15      var bestResult = evaluator(program);
16      var tree = program.ToTree();
17      var optimizedTree = RemoveUnnecessaryExpressions(tree, tree, ref bestResult, evaluator, improveIfPossible);
18      var tmp = RemoveUnnecessaryExpressions(optimizedTree, optimizedTree, ref bestResult, evaluator, improveIfPossible);
19
20      while (tmp.Count < optimizedTree.Count) {
21        optimizedTree = tmp;
22      }
23
24      var optimizedProgram = optimizedTree.ToPushProgramWithoutNoopExpressions();
25      optimizedProgram = SimplifySubPrograms(optimizedProgram);
26
27      return optimizedProgram;
28    }
29
30    public static PushProgram SimplifySubPrograms(PushProgram program) {
31      var tree = program.ToTree();
32      var optimizedTree = SimplifySubPrograms(tree);
33      var optimizedPrgram = optimizedTree.ToPushProgram();
34
35      return optimizedPrgram;
36    }
37
38    private static TreeNode<Expression> SimplifySubPrograms(TreeNode<Expression> program) {
39      while (program.Children.Count == 1)
40        program = program.Children[0];
41
42      for (var i = 0; i < program.Children.Count; i++) {
43        var child = program.Children[i];
44        program.ReplaceNode(i, SimplifySubPrograms(child));
45      }
46
47      return program;
48    }
49
50    private static TreeNode<Expression> RemoveUnnecessaryExpressions(TreeNode<Expression> program, TreeNode<Expression> node, ref double bestResult, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
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
58        if ((improveIfPossible && currentResult <= bestResult) ||
59            (!improveIfPossible && currentResult.IsAlmost(bestResult))) {
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.