Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/Simplifier.cs @ 16724

Last change on this file since 16724 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 2.6 KB
Line 
1using System;
2using HeuristicLab.Common;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  public static class Simplifier {
6    private static readonly TreeNode<Expression> NoopNode =
7      new TreeNode<Expression>(ExpressionTable.GetStatelessExpression<ExecNoopExpression>());
8
9    public static PushProgram Simplify(PushProgram program, IReadOnlyPushConfiguration config, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
10      var bestResult = evaluator(program);
11      var tree = program.ToTree();
12      var optimizedTree = RemoveUnnecessaryExpressions(tree, tree, ref bestResult, evaluator, improveIfPossible);
13      var tmp = RemoveUnnecessaryExpressions(optimizedTree, optimizedTree, ref bestResult, evaluator, improveIfPossible);
14
15      while (tmp.Count < optimizedTree.Count) {
16        optimizedTree = tmp;
17      }
18
19      var optimizedProgram = optimizedTree.ToPushProgramWithoutNoopExpressions();
20      optimizedProgram = SimplifySubPrograms(optimizedProgram);
21
22      return optimizedProgram;
23    }
24
25    public static PushProgram SimplifySubPrograms(PushProgram program) {
26      var tree = program.ToTree();
27      var optimizedTree = SimplifySubPrograms(tree);
28      var optimizedPrgram = optimizedTree.ToPushProgram();
29
30      return optimizedPrgram;
31    }
32
33    private static TreeNode<Expression> SimplifySubPrograms(TreeNode<Expression> program) {
34      while (program.Children.Count == 1)
35        program = program.Children[0];
36
37      for (var i = 0; i < program.Children.Count; i++) {
38        var child = program.Children[i];
39        program.ReplaceNode(i, SimplifySubPrograms(child));
40      }
41
42      return program;
43    }
44
45    private static TreeNode<Expression> RemoveUnnecessaryExpressions(TreeNode<Expression> program, TreeNode<Expression> node, ref double bestResult, Func<PushProgram, double> evaluator, bool improveIfPossible = false) {
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
53        if ((improveIfPossible && currentResult <= bestResult) ||
54            (!improveIfPossible && currentResult.IsAlmost(bestResult))) {
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}
Note: See TracBrowser for help on using the repository browser.