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.SetInput(
|
---|
56 | integers: example.InputInteger,
|
---|
57 | floats: example.InputFloat,
|
---|
58 | booleans: example.InputBoolean,
|
---|
59 | chars: example.InputChar,
|
---|
60 | strings: example.InputString,
|
---|
61 | integerVectors: example.InputIntegerVector,
|
---|
62 | floatVectors: example.InputFloatVector,
|
---|
63 | stringVectors: example.InputStringVector);
|
---|
64 |
|
---|
65 | //interpreter.BooleanStack.Push(example.InputBoolean);
|
---|
66 | //interpreter.IntegerStack.Push(example.InputInteger);
|
---|
67 | //interpreter.FloatStack.Push(example.InputFloat);
|
---|
68 | //interpreter.CharStack.Push(example.InputChar);
|
---|
69 | //interpreter.StringStack.Push(example.InputString);
|
---|
70 | //interpreter.IntegerVectorStack.Push(example.InputIntegerVector.Select(x => x.ToList()).ToList());
|
---|
71 | //interpreter.FloatVectorStack.Push(example.InputFloatVector.Select(x => x.ToList()).ToList());
|
---|
72 | //interpreter.StringVectorStack.Push(example.InputStringVector.Select(x => x.ToList()).ToList());
|
---|
73 | //interpreter.BooleanVectorStack.Push(example.InputBooleanVector.Select(x => x.ToList()).ToList());
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void SelectedExampleIndexChanged(object sender, EventArgs e) {
|
---|
77 | pushDebugger.ResetDebugging();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public new PushBenchmarkSuiteSolution Content
|
---|
81 | {
|
---|
82 | get { return (PushBenchmarkSuiteSolution)base.Content; }
|
---|
83 | set
|
---|
84 | {
|
---|
85 | base.Content = value;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public PushBenchmarkSuiteEvaluator Evaluator
|
---|
90 | {
|
---|
91 | get { return (PushBenchmarkSuiteEvaluator)Content.Evaluator; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected override void OnContentChanged() {
|
---|
95 | if (Content == null) return;
|
---|
96 |
|
---|
97 | Name = "Push Solution";
|
---|
98 | nameTextBox.Text = Name;
|
---|
99 |
|
---|
100 | pool = new PushInterpreterPool(Content.Config);
|
---|
101 |
|
---|
102 | if (interpreter != null) {
|
---|
103 | interpreter.Dispose();
|
---|
104 | }
|
---|
105 |
|
---|
106 | interpreter = pool.Create(Content.Random);
|
---|
107 | UpdateExamples(Evaluator.Data);
|
---|
108 |
|
---|
109 | if (exampleComboBox.SelectedIndex < 0) {
|
---|
110 | exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event
|
---|
111 | }
|
---|
112 |
|
---|
113 | InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End);
|
---|
114 | InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End);
|
---|
115 |
|
---|
116 | pushDebugger.Content = Content;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void InitResultGrid(DataGridView grid, int start, int end) {
|
---|
120 | grid.Columns.Clear();
|
---|
121 | grid.Rows.Clear();
|
---|
122 |
|
---|
123 | var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
|
---|
124 | var data = Evaluator.Data;
|
---|
125 |
|
---|
126 | for (var i = 0; i < data.InputArgumentTypes.Length; i++) {
|
---|
127 | var headerTypeName = ViewHelper.GetHeaderTypeName(data.InputArgumentTypes[i]);
|
---|
128 | var column = new DataGridViewColumn {
|
---|
129 | HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName),
|
---|
130 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
131 | CellTemplate = cellTemplate,
|
---|
132 | DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight },
|
---|
133 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
134 | };
|
---|
135 |
|
---|
136 | grid.Columns.Add(column);
|
---|
137 | }
|
---|
138 |
|
---|
139 | for (var i = 0; i < data.OutputArgumentTypes.Length; i++) {
|
---|
140 | var headerTypeName = ViewHelper.GetHeaderTypeName(data.OutputArgumentTypes[i]);
|
---|
141 |
|
---|
142 | var estimatedOutputColumn = new DataGridViewColumn {
|
---|
143 | HeaderText = string.Format(EstimatedOutputHeaderStringFormat, i + 1, headerTypeName),
|
---|
144 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
145 | CellTemplate = cellTemplate,
|
---|
146 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
147 | };
|
---|
148 |
|
---|
149 | var outputColumn = new DataGridViewColumn {
|
---|
150 | HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName),
|
---|
151 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
152 | CellTemplate = cellTemplate,
|
---|
153 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
154 | };
|
---|
155 |
|
---|
156 | estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
157 | outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
158 |
|
---|
159 | grid.Columns.Add(estimatedOutputColumn);
|
---|
160 | grid.Columns.Add(outputColumn);
|
---|
161 | }
|
---|
162 |
|
---|
163 | var absoluteDiffColumn = new DataGridViewColumn {
|
---|
164 | HeaderText = AbsoluteDiffHeaderText,
|
---|
165 | AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
|
---|
166 | CellTemplate = cellTemplate,
|
---|
167 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
168 | };
|
---|
169 |
|
---|
170 | var relativeDiffColumn = new DataGridViewColumn {
|
---|
171 | HeaderText = RelativeDiffHeaderText,
|
---|
172 | AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
|
---|
173 | CellTemplate = cellTemplate,
|
---|
174 | SortMode = DataGridViewColumnSortMode.Automatic
|
---|
175 | };
|
---|
176 |
|
---|
177 | absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
178 | relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
---|
179 |
|
---|
180 | grid.Columns.Add(absoluteDiffColumn);
|
---|
181 | grid.Columns.Add(relativeDiffColumn);
|
---|
182 |
|
---|
183 | using (var pushInterpreter = pool.Create(Content.Random)) {
|
---|
184 | var rowIndex = 1;
|
---|
185 | for (var i = start; i < end; i++, rowIndex++) {
|
---|
186 | var example = data.Examples[i];
|
---|
187 | var row = new DataGridViewRow {
|
---|
188 | HeaderCell = {
|
---|
189 | Value = rowIndex.ToString(),
|
---|
190 | }
|
---|
191 | };
|
---|
192 |
|
---|
193 | row.CreateCells(grid);
|
---|
194 |
|
---|
195 | var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
|
---|
196 | var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
|
---|
197 |
|
---|
198 | var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
199 | for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
|
---|
200 | var type = data.InputArgumentTypes[j];
|
---|
201 | var offset = inputArgumentCountDict[type];
|
---|
202 | row.Cells[j].Value = ViewHelper.StringifyInput(type, offset, Content.Config.FloatStringFormat, example, Separator);
|
---|
203 | inputArgumentCountDict[type]++;
|
---|
204 | }
|
---|
205 |
|
---|
206 | var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
207 | for (var j = 0; j < data.OutputArgumentTypes.Length; j++) {
|
---|
208 | var type = data.OutputArgumentTypes[j];
|
---|
209 | var offset = outputArgumentCountDict[type];
|
---|
210 |
|
---|
211 | // estimated
|
---|
212 | row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(type, offset, Content.Config.FloatStringFormat, example, Separator);
|
---|
213 |
|
---|
214 | // output
|
---|
215 | row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = StringifyResult(type, offset, pushInterpreter, example, Separator);
|
---|
216 |
|
---|
217 | outputArgumentCountDict[type]++;
|
---|
218 | }
|
---|
219 |
|
---|
220 | row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff.ToString("0.####");
|
---|
221 | row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff.ToString("0.##") + "%";
|
---|
222 |
|
---|
223 | if (absoluteDiff.IsAlmost(data.BestResult)) {
|
---|
224 | for (var j = 0; j < row.Cells.Count; j++) {
|
---|
225 | row.Cells[j].Style.ForeColor = correctExampleFontColor;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | grid.Rows.Add(row);
|
---|
230 | pushInterpreter.Reset();
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | private string StringifyResult(ExampleArgumentType type, int offset, IPushInterpreter interpreter, Example example, string valueSeparator) {
|
---|
236 | var emptyString = string.Empty;
|
---|
237 |
|
---|
238 | switch (type) {
|
---|
239 | case ExampleArgumentType.Integer:
|
---|
240 | return GetEntryAsString(offset, interpreter.IntegerStack);
|
---|
241 |
|
---|
242 | case ExampleArgumentType.IntegerVector:
|
---|
243 | return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
|
---|
244 |
|
---|
245 | case ExampleArgumentType.Float:
|
---|
246 | return interpreter.FloatStack.Count > offset
|
---|
247 | ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
|
---|
248 | : emptyString;
|
---|
249 |
|
---|
250 | case ExampleArgumentType.FloatVector:
|
---|
251 | return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
|
---|
252 |
|
---|
253 | case ExampleArgumentType.Boolean:
|
---|
254 | return GetEntryAsString(offset, interpreter.BooleanStack);
|
---|
255 |
|
---|
256 | case ExampleArgumentType.Char:
|
---|
257 | return GetEntryAsString(offset, interpreter.CharStack);
|
---|
258 |
|
---|
259 | case ExampleArgumentType.Print:
|
---|
260 | return string.Join(valueSeparator, string.Join(PushEnvironment.NewLine, interpreter.PrintStack.AsStrings().Take(example.OutputPrintLineCount)));
|
---|
261 |
|
---|
262 | case ExampleArgumentType.String:
|
---|
263 | return GetEntryAsString(offset, interpreter.StringStack);
|
---|
264 |
|
---|
265 | case ExampleArgumentType.StringVector:
|
---|
266 | return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
|
---|
267 |
|
---|
268 | default: return string.Empty;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | private static string GetEntryAsString<T>(int offset, IPushStack<T> stack) {
|
---|
273 | return stack.Count > offset
|
---|
274 | ? stack[offset].ToString()
|
---|
275 | : string.Empty;
|
---|
276 | }
|
---|
277 |
|
---|
278 | private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
279 | return vectorStack.Count > offset
|
---|
280 | ? "[" + string.Join(",", vectorStack[offset]) + "]"
|
---|
281 | : string.Empty;
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void UpdateExamples(ProblemData data) {
|
---|
285 | exampleComboBox.Items.Clear();
|
---|
286 | if (data == null) return;
|
---|
287 |
|
---|
288 | var stringRepresentations = data.Examples.Select(e =>
|
---|
289 | string.Join(Separator, e.InputArgs) +
|
---|
290 | exampleSplitter +
|
---|
291 | string.Join(Separator, e.OutputArgs));
|
---|
292 |
|
---|
293 | foreach (var str in stringRepresentations) {
|
---|
294 | exampleComboBox.Items.Add(str);
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|