Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/BenchmarkSuitePushSolutionView.cs @ 15287

Last change on this file since 15287 was 15273, checked in by pkimmesw, 8 years ago

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

File size: 10.5 KB
RevLine 
[14897]1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite {
2  using System;
[14952]3  using System.Collections.Generic;
4  using System.Drawing;
[14897]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;
[14952]11  using HeuristicLab.Common;
[15017]12  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
[14952]13
[14897]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}";
[14952]31    private static readonly Color correctExampleFontColor = Color.FromArgb(0x5cb85c);
[14897]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() {
[14908]42      interpreter.Dispose();
[14897]43    }
44
45    private void InitEvents() {
[14908]46      exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged;
47      pushDebugger.OnReset += PushDebuggerReset;
[14897]48    }
49
50    private void PushDebuggerReset(object sender, IPushInterpreter interpreter) {
[14908]51      if (exampleComboBox.SelectedIndex < 0) return;
[14897]52
[14908]53      var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex];
[14897]54
[15273]55      interpreter.InitExample(example);
[14897]56    }
57
58    private void SelectedExampleIndexChanged(object sender, EventArgs e) {
[14908]59      pushDebugger.ResetDebugging();
[14897]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
[15273]76
[14897]77    protected override void OnContentChanged() {
[14908]78      if (Content == null) return;
[14897]79
[14908]80      Name = "Push Solution";
81      nameTextBox.Text = Name;
[14897]82
[14908]83      pool = new PushInterpreterPool(Content.Config);
[14897]84
[14908]85      if (interpreter != null) {
86        interpreter.Dispose();
[14897]87      }
88
[15273]89      var random = Content.GetRandom();
90      interpreter = pool.Create(random);
[14908]91      UpdateExamples(Evaluator.Data);
[14897]92
[14908]93      if (exampleComboBox.SelectedIndex < 0) {
94        exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event
[14897]95      }
96
[14952]97      InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End);
98      InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End);
99
[14908]100      pushDebugger.Content = Content;
[14897]101    }
102
[14952]103    private void InitResultGrid(DataGridView grid, int start, int end) {
104      grid.Columns.Clear();
105      grid.Rows.Clear();
[14897]106
[14952]107      var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
[14897]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,
[14952]115          CellTemplate = cellTemplate,
[15032]116          DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight },
117          SortMode = DataGridViewColumnSortMode.Automatic
[14897]118        };
119
[14952]120        grid.Columns.Add(column);
[14897]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,
[15032]130          SortMode = DataGridViewColumnSortMode.Automatic
[14897]131        };
132
133        var outputColumn = new DataGridViewColumn {
134          HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName),
135          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
[15032]136          CellTemplate = cellTemplate,
137          SortMode = DataGridViewColumnSortMode.Automatic
[14897]138        };
139
140        estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
141        outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
142
[14952]143        grid.Columns.Add(estimatedOutputColumn);
144        grid.Columns.Add(outputColumn);
[14897]145      }
146
147      var absoluteDiffColumn = new DataGridViewColumn {
148        HeaderText = AbsoluteDiffHeaderText,
149        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
[15032]150        CellTemplate = cellTemplate,
151        SortMode = DataGridViewColumnSortMode.Automatic
[14897]152      };
153
154      var relativeDiffColumn = new DataGridViewColumn {
155        HeaderText = RelativeDiffHeaderText,
156        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
157        CellTemplate = cellTemplate,
[15032]158        SortMode = DataGridViewColumnSortMode.Automatic
[14897]159      };
160
161      absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
162      relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
163
[14952]164      grid.Columns.Add(absoluteDiffColumn);
165      grid.Columns.Add(relativeDiffColumn);
[14897]166
[15273]167      var random = Content.GetRandom();
168      using (var pushInterpreter = pool.Create(random)) {
[14952]169        var rowIndex = 1;
170        for (var i = start; i < end; i++, rowIndex++) {
[14897]171          var example = data.Examples[i];
[14952]172          var row = new DataGridViewRow {
173            HeaderCell = {
174              Value = rowIndex.ToString(),
175            }
176          };
177
178          row.CreateCells(grid);
179
[14897]180          var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
181          var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
182
[14952]183          var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
[14897]184          for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
[14952]185            var type = data.InputArgumentTypes[j];
186            var offset = inputArgumentCountDict[type];
[15032]187            row.Cells[j].Value = ViewHelper.StringifyInput(type, offset, Content.Config.FloatStringFormat, example, Separator);
[14952]188            inputArgumentCountDict[type]++;
[14897]189          }
190
[14952]191          var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
[14897]192          for (var j = 0; j < data.OutputArgumentTypes.Length; j++) {
[14952]193            var type = data.OutputArgumentTypes[j];
194            var offset = outputArgumentCountDict[type];
[15189]195
196            // estimated
[15032]197            row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(type, offset, Content.Config.FloatStringFormat, example, Separator);
[15189]198
199            // output
[14952]200            row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = StringifyResult(type, offset, pushInterpreter, example, Separator);
[15189]201
[14952]202            outputArgumentCountDict[type]++;
[14897]203          }
204
[14952]205          row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff.ToString("0.####");
206          row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff.ToString("0.##") + "%";
[14897]207
[14952]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);
[14897]215          pushInterpreter.Reset();
216        }
217      }
218    }
219
[14952]220    private string StringifyResult(ExampleArgumentType type, int offset, IPushInterpreter interpreter, Example example, string valueSeparator) {
221      var emptyString = string.Empty;
222
[14897]223      switch (type) {
224        case ExampleArgumentType.Integer:
[14952]225          return GetEntryAsString(offset, interpreter.IntegerStack);
[14897]226
[14952]227        case ExampleArgumentType.IntegerVector:
228          return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
229
[14897]230        case ExampleArgumentType.Float:
[14952]231          return interpreter.FloatStack.Count > offset
232            ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
233            : emptyString;
[14897]234
[14952]235        case ExampleArgumentType.FloatVector:
236          return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
[14897]237
[14952]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:
[15189]245          return string.Join(valueSeparator, string.Join(PushEnvironment.NewLine, interpreter.PrintStack.AsStrings().Take(example.OutputPrintLineCount)));
[14952]246
[14897]247        case ExampleArgumentType.String:
[14952]248          return GetEntryAsString(offset, interpreter.StringStack);
249
250        case ExampleArgumentType.StringVector:
251          return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
252
[14897]253        default: return string.Empty;
254      }
255    }
256
[14952]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
[15017]263    private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
[14952]264      return vectorStack.Count > offset
265       ? "[" + string.Join(",", vectorStack[offset]) + "]"
266       : string.Empty;
267    }
268
[14897]269    private void UpdateExamples(ProblemData data) {
[14908]270      exampleComboBox.Items.Clear();
[14897]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) {
[14908]279        exampleComboBox.Items.Add(str);
[14897]280      }
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.