Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Solution/BenchmarkSuite/BenchmarkSuitePushSolutionView.cs @ 17460

Last change on this file since 17460 was 15289, checked in by pkimmesw, 7 years ago

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 8.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Solution.BenchmarkSuite {
2  using System;
3  using System.Drawing;
4  using System.Linq;
5  using System.Windows.Forms;
6
7  using HeuristicLab.BenchmarkSuite;
8  using HeuristicLab.BenchmarkSuite.Views;
9  using HeuristicLab.Common;
10  using HeuristicLab.Core.Views;
11  using HeuristicLab.MainForm;
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution;
15
16  [View("Push Program Debugger")]
17  [Content(typeof(PushSolution), true)]
18  public partial class BenchmarkSuitePushSolutionView : NamedItemView {
19
20    private const string Separator = ", ";
21
22    private const string exampleSplitter = " => ";
23    private const string AbsoluteDiffHeaderText = "Absolute Diff.";
24    private const string RelativeDiffHeaderText = "Relative Diff.";
25    private const string InputHeaderStringFormat = "Input {0} : {1}";
26    private const string EstimatedOutputHeaderStringFormat = "Expected Output {0} : {1}";
27    private const string OutputHeaderStringFormat = "Output {0} : {1}";
28    private static readonly Color correctExampleFontColor = Color.FromArgb(0x5cb85c);
29    private PooledPushInterpreter interpreter;
30    private PushInterpreterPool pool;
31
32    public BenchmarkSuitePushSolutionView() {
33      InitializeComponent();
34      Name = "Push Program Debugger";
35      InitEvents();
36    }
37
38    ~BenchmarkSuitePushSolutionView() {
39      interpreter.Dispose();
40    }
41
42    private void InitEvents() {
43      exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged;
44      pushDebugger.OnReset += PushDebuggerReset;
45    }
46
47    private void PushDebuggerReset(object sender, IPushInterpreter interpreter) {
48      if (exampleComboBox.SelectedIndex < 0) return;
49
50      var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex];
51
52      interpreter.InitExample(example);
53    }
54
55    private void SelectedExampleIndexChanged(object sender, EventArgs e) {
56      pushDebugger.ResetDebugging();
57    }
58
59    public new PushBenchmarkSuiteSolution Content
60    {
61      get { return (PushBenchmarkSuiteSolution)base.Content; }
62      set
63      {
64        base.Content = value;
65      }
66    }
67
68    public PushBenchmarkSuiteEvaluator Evaluator
69    {
70      get { return (PushBenchmarkSuiteEvaluator)Content.Evaluator; }
71    }
72
73
74    protected override void OnContentChanged() {
75      if (Content == null) return;
76
77      Name = "Push Solution";
78      nameTextBox.Text = Name;
79
80      pool = new PushInterpreterPool(Content.Config);
81
82      if (interpreter != null) {
83        interpreter.Dispose();
84      }
85
86      var random = Content.GetRandom();
87      interpreter = pool.Create(random);
88      UpdateExamples(Evaluator.Data);
89
90      if (exampleComboBox.SelectedIndex < 0) {
91        exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event
92      }
93
94      InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End);
95      InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End);
96
97      pushDebugger.Content = Content;
98    }
99
100    private void InitResultGrid(DataGridView grid, int start, int end) {
101      grid.Columns.Clear();
102      grid.Rows.Clear();
103
104      var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
105      var data = Evaluator.Data;
106
107      for (var i = 0; i < data.InputArgumentTypes.Length; i++) {
108        var headerTypeName = ViewHelper.GetHeaderTypeName(data.InputArgumentTypes[i]);
109        var column = new DataGridViewColumn {
110          HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName),
111          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
112          CellTemplate = cellTemplate,
113          DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight },
114          SortMode = DataGridViewColumnSortMode.Automatic
115        };
116
117        grid.Columns.Add(column);
118      }
119
120      for (var i = 0; i < data.OutputArgumentTypes.Length; i++) {
121        var headerTypeName = ViewHelper.GetHeaderTypeName(data.OutputArgumentTypes[i]);
122
123        var estimatedOutputColumn = new DataGridViewColumn {
124          HeaderText = string.Format(EstimatedOutputHeaderStringFormat, i + 1, headerTypeName),
125          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
126          CellTemplate = cellTemplate,
127          SortMode = DataGridViewColumnSortMode.Automatic
128        };
129
130        var outputColumn = new DataGridViewColumn {
131          HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName),
132          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
133          CellTemplate = cellTemplate,
134          SortMode = DataGridViewColumnSortMode.Automatic
135        };
136
137        estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
138        outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
139
140        grid.Columns.Add(estimatedOutputColumn);
141        grid.Columns.Add(outputColumn);
142      }
143
144      var absoluteDiffColumn = new DataGridViewColumn {
145        HeaderText = AbsoluteDiffHeaderText,
146        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
147        CellTemplate = cellTemplate,
148        SortMode = DataGridViewColumnSortMode.Automatic
149      };
150
151      var relativeDiffColumn = new DataGridViewColumn {
152        HeaderText = RelativeDiffHeaderText,
153        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
154        CellTemplate = cellTemplate,
155        SortMode = DataGridViewColumnSortMode.Automatic
156      };
157
158      absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
159      relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
160
161      grid.Columns.Add(absoluteDiffColumn);
162      grid.Columns.Add(relativeDiffColumn);
163
164      var random = Content.GetRandom();
165      using (var pushInterpreter = pool.Create(random)) {
166        var rowIndex = 1;
167        for (var i = start; i < end; i++, rowIndex++) {
168          var example = data.Examples[i];
169          var row = new DataGridViewRow {
170            HeaderCell = {
171              Value = rowIndex.ToString(),
172            }
173          };
174
175          row.CreateCells(grid);
176
177          var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
178          var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
179
180          var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
181          for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
182            var type = data.InputArgumentTypes[j];
183            var offset = inputArgumentCountDict[type];
184            row.Cells[j].Value = ViewHelper.StringifyInput(type, offset, Content.Config.FloatStringFormat, example, Separator);
185            inputArgumentCountDict[type]++;
186          }
187
188          var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
189          for (var j = 0; j < data.OutputArgumentTypes.Length; j++) {
190            var type = data.OutputArgumentTypes[j];
191            var offset = outputArgumentCountDict[type];
192
193            // expected
194            row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(type, offset, Content.Config.FloatStringFormat, example, Separator);
195
196            // output
197            row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = pushInterpreter.StringifyResult(type, offset);
198
199            outputArgumentCountDict[type]++;
200          }
201
202          row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff.ToString("0.####");
203          row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff.ToString("0.##") + "%";
204
205          if (absoluteDiff.IsAlmost(data.BestResult)) {
206            for (var j = 0; j < row.Cells.Count; j++) {
207              row.Cells[j].Style.ForeColor = correctExampleFontColor;
208            }
209          }
210
211          grid.Rows.Add(row);
212          pushInterpreter.Reset();
213        }
214      }
215    }
216
217    private void UpdateExamples(ProblemData data) {
218      exampleComboBox.Items.Clear();
219      if (data == null) return;
220
221      var stringRepresentations = data.Examples.Select(e =>
222        string.Join(Separator, e.InputArgs) +
223        exampleSplitter +
224        string.Join(Separator, e.OutputArgs));
225
226      foreach (var str in stringRepresentations) {
227        exampleComboBox.Items.Add(str);
228      }
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.