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