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