Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.Views/BenchmarkSuitePushSolutionView.cs @ 16843

Last change on this file since 16843 was 15771, checked in by bburlacu, 6 years ago

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

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