1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Globalization;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using Core.Views;
|
---|
9 | using HeuristicLab.BenchmarkSuite;
|
---|
10 | using HeuristicLab.BenchmarkSuite.Views;
|
---|
11 | using HeuristicLab.Common;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
|
---|
13 |
|
---|
14 | using Interpreter;
|
---|
15 | using MainForm;
|
---|
16 | using Problem;
|
---|
17 | using Stack;
|
---|
18 |
|
---|
19 | [View("Push Program Debugger")]
|
---|
20 | [Content(typeof(PushSolution), true)]
|
---|
21 | public partial class BenchmarkSuitePushSolutionView : NamedItemView {
|
---|
22 |
|
---|
23 | private const string Separator = ", ";
|
---|
24 |
|
---|
25 | private const string exampleSplitter = " => ";
|
---|
26 | private const string AbsoluteDiffHeaderText = "Absolute Diff.";
|
---|
27 | private const string RelativeDiffHeaderText = "Relative Diff.";
|
---|
28 | private const string InputHeaderStringFormat = "Input {0} : {1}";
|
---|
29 | private const string EstimatedOutputHeaderStringFormat = "Estimated Output {0} : {1}";
|
---|
30 | private const string OutputHeaderStringFormat = "Output {0} : {1}";
|
---|
31 | private static readonly Color correctExampleFontColor = Color.FromArgb(0x5cb85c);
|
---|
32 | private PooledPushInterpreter interpreter;
|
---|
33 | private PushInterpreterPool pool;
|
---|
34 |
|
---|
35 | public BenchmarkSuitePushSolutionView() {
|
---|
36 | InitializeComponent();
|
---|
37 | Name = "Push Program Debugger";
|
---|
38 | InitEvents();
|
---|
39 | }
|
---|
40 |
|
---|
41 | ~BenchmarkSuitePushSolutionView() {
|
---|
42 | interpreter.Dispose();
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void InitEvents() {
|
---|
46 | exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged;
|
---|
47 | pushDebugger.OnReset += PushDebuggerReset;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void PushDebuggerReset(object sender, IPushInterpreter interpreter) {
|
---|
51 | if (exampleComboBox.SelectedIndex < 0) return;
|
---|
52 |
|
---|
53 | var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex];
|
---|
54 |
|
---|
55 | interpreter.InitExample(example);
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void SelectedExampleIndexChanged(object sender, EventArgs e) {
|
---|
59 | pushDebugger.ResetDebugging();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public new PushBenchmarkSuiteSolution Content
|
---|
63 | {
|
---|
64 | get { return (PushBenchmarkSuiteSolution)base.Content; }
|
---|
65 | set
|
---|
66 | {
|
---|
67 | base.Content = value;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public PushBenchmarkSuiteEvaluator Evaluator
|
---|
72 | {
|
---|
73 | get { return (PushBenchmarkSuiteEvaluator)Content.Evaluator; }
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | protected override void OnContentChanged() {
|
---|
78 | if (Content == null) return;
|
---|
79 |
|
---|
80 | Name = "Push Solution";
|
---|
81 | nameTextBox.Text = Name;
|
---|
82 |
|
---|
83 | pool = new PushInterpreterPool(Content.Config);
|
---|
84 |
|
---|
85 | if (interpreter != null) {
|
---|
86 | interpreter.Dispose();
|
---|
87 | }
|
---|
88 |
|
---|
89 | var random = Content.GetRandom();
|
---|
90 | interpreter = pool.Create(random);
|
---|
91 | UpdateExamples(Evaluator.Data);
|
---|
92 |
|
---|
93 | if (exampleComboBox.SelectedIndex < 0) {
|
---|
94 | exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event
|
---|
95 | }
|
---|
96 |
|
---|
97 | InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End);
|
---|
98 | InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End);
|
---|
99 |
|
---|
100 | pushDebugger.Content = Content;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void InitResultGrid(DataGridView grid, int start, int end) {
|
---|
104 | grid.Columns.Clear();
|
---|
105 | grid.Rows.Clear();
|
---|
106 |
|
---|
107 | var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
|
---|
108 | var data = Evaluator.Data;
|
---|
109 |
|
---|
110 | for (var i = 0; i < data.InputArgumentTypes.Length; i++) {
|
---|
111 | var headerTypeName = ViewHelper.GetHeaderTypeName(data.InputArgumentTypes[i]);
|
---|
112 | var column = new DataGridViewColumn {
|
---|
113 | HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName),
|
---|
114 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
115 | CellTemplate = cellTemplate,
|
---|
116 | DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight },
|
---|
117 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
118 | };
|
---|
119 |
|
---|
120 | grid.Columns.Add(column);
|
---|
121 | }
|
---|
122 |
|
---|
123 | for (var i = 0; i < data.OutputArgumentTypes.Length; i++) {
|
---|
124 | var headerTypeName = ViewHelper.GetHeaderTypeName(data.OutputArgumentTypes[i]);
|
---|
125 |
|
---|
126 | var estimatedOutputColumn = new DataGridViewColumn {
|
---|
127 | HeaderText = string.Format(EstimatedOutputHeaderStringFormat, i + 1, headerTypeName),
|
---|
128 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
129 | CellTemplate = cellTemplate,
|
---|
130 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
131 | };
|
---|
132 |
|
---|
133 | var outputColumn = new DataGridViewColumn {
|
---|
134 | HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName),
|
---|
135 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
136 | CellTemplate = cellTemplate,
|
---|
137 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
138 | };
|
---|
139 |
|
---|
140 | estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
141 | outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
142 |
|
---|
143 | grid.Columns.Add(estimatedOutputColumn);
|
---|
144 | grid.Columns.Add(outputColumn);
|
---|
145 | }
|
---|
146 |
|
---|
147 | var absoluteDiffColumn = new DataGridViewColumn {
|
---|
148 | HeaderText = AbsoluteDiffHeaderText,
|
---|
149 | AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
|
---|
150 | CellTemplate = cellTemplate,
|
---|
151 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
152 | };
|
---|
153 |
|
---|
154 | var relativeDiffColumn = new DataGridViewColumn {
|
---|
155 | HeaderText = RelativeDiffHeaderText,
|
---|
156 | AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
|
---|
157 | CellTemplate = cellTemplate,
|
---|
158 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
159 | };
|
---|
160 |
|
---|
161 | absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
162 | relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
163 |
|
---|
164 | grid.Columns.Add(absoluteDiffColumn);
|
---|
165 | grid.Columns.Add(relativeDiffColumn);
|
---|
166 |
|
---|
167 | var random = Content.GetRandom();
|
---|
168 | using (var pushInterpreter = pool.Create(random)) {
|
---|
169 | var rowIndex = 1;
|
---|
170 | for (var i = start; i < end; i++, rowIndex++) {
|
---|
171 | var example = data.Examples[i];
|
---|
172 | var row = new DataGridViewRow {
|
---|
173 | HeaderCell = {
|
---|
174 | Value = rowIndex.ToString(),
|
---|
175 | }
|
---|
176 | };
|
---|
177 |
|
---|
178 | row.CreateCells(grid);
|
---|
179 |
|
---|
180 | var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
|
---|
181 | var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
|
---|
182 |
|
---|
183 | var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
184 | for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
|
---|
185 | var type = data.InputArgumentTypes[j];
|
---|
186 | var offset = inputArgumentCountDict[type];
|
---|
187 | row.Cells[j].Value = ViewHelper.StringifyInput(type, offset, Content.Config.FloatStringFormat, example, Separator);
|
---|
188 | inputArgumentCountDict[type]++;
|
---|
189 | }
|
---|
190 |
|
---|
191 | var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
192 | for (var j = 0; j < data.OutputArgumentTypes.Length; j++) {
|
---|
193 | var type = data.OutputArgumentTypes[j];
|
---|
194 | var offset = outputArgumentCountDict[type];
|
---|
195 |
|
---|
196 | // estimated
|
---|
197 | row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(type, offset, Content.Config.FloatStringFormat, example, Separator);
|
---|
198 |
|
---|
199 | // output
|
---|
200 | row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = StringifyResult(type, offset, pushInterpreter, example, Separator);
|
---|
201 |
|
---|
202 | outputArgumentCountDict[type]++;
|
---|
203 | }
|
---|
204 |
|
---|
205 | row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff.ToString("0.####");
|
---|
206 | row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff.ToString("0.##") + "%";
|
---|
207 |
|
---|
208 | if (absoluteDiff.IsAlmost(data.BestResult)) {
|
---|
209 | for (var j = 0; j < row.Cells.Count; j++) {
|
---|
210 | row.Cells[j].Style.ForeColor = correctExampleFontColor;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | grid.Rows.Add(row);
|
---|
215 | pushInterpreter.Reset();
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private string StringifyResult(ExampleArgumentType type, int offset, IPushInterpreter interpreter, Example example, string valueSeparator) {
|
---|
221 | var emptyString = string.Empty;
|
---|
222 |
|
---|
223 | switch (type) {
|
---|
224 | case ExampleArgumentType.Integer:
|
---|
225 | return GetEntryAsString(offset, interpreter.IntegerStack);
|
---|
226 |
|
---|
227 | case ExampleArgumentType.IntegerVector:
|
---|
228 | return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
|
---|
229 |
|
---|
230 | case ExampleArgumentType.Float:
|
---|
231 | return interpreter.FloatStack.Count > offset
|
---|
232 | ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
|
---|
233 | : emptyString;
|
---|
234 |
|
---|
235 | case ExampleArgumentType.FloatVector:
|
---|
236 | return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
|
---|
237 |
|
---|
238 | case ExampleArgumentType.Boolean:
|
---|
239 | return GetEntryAsString(offset, interpreter.BooleanStack);
|
---|
240 |
|
---|
241 | case ExampleArgumentType.Char:
|
---|
242 | return GetEntryAsString(offset, interpreter.CharStack);
|
---|
243 |
|
---|
244 | case ExampleArgumentType.Print:
|
---|
245 | return string.Join(valueSeparator, string.Join(PushEnvironment.NewLine, interpreter.PrintStack.AsStrings().Take(example.OutputPrintLineCount)));
|
---|
246 |
|
---|
247 | case ExampleArgumentType.String:
|
---|
248 | return GetEntryAsString(offset, interpreter.StringStack);
|
---|
249 |
|
---|
250 | case ExampleArgumentType.StringVector:
|
---|
251 | return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
|
---|
252 |
|
---|
253 | default: return string.Empty;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | private static string GetEntryAsString<T>(int offset, IPushStack<T> stack) {
|
---|
258 | return stack.Count > offset
|
---|
259 | ? stack[offset].ToString()
|
---|
260 | : string.Empty;
|
---|
261 | }
|
---|
262 |
|
---|
263 | private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
264 | return vectorStack.Count > offset
|
---|
265 | ? "[" + string.Join(",", vectorStack[offset]) + "]"
|
---|
266 | : string.Empty;
|
---|
267 | }
|
---|
268 |
|
---|
269 | private void UpdateExamples(ProblemData data) {
|
---|
270 | exampleComboBox.Items.Clear();
|
---|
271 | if (data == null) return;
|
---|
272 |
|
---|
273 | var stringRepresentations = data.Examples.Select(e =>
|
---|
274 | string.Join(Separator, e.InputArgs) +
|
---|
275 | exampleSplitter +
|
---|
276 | string.Join(Separator, e.OutputArgs));
|
---|
277 |
|
---|
278 | foreach (var str in stringRepresentations) {
|
---|
279 | exampleComboBox.Items.Add(str);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|