Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14907 was 14897, checked in by pkimmesw, 8 years ago

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 9.3 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite {
2  using System;
3  using System.Globalization;
4  using System.Linq;
5  using System.Windows.Forms;
6  using Core.Views;
7  using HeuristicLab.BenchmarkSuite;
8  using HeuristicLab.BenchmarkSuite.Problems;
9  using HeuristicLab.BenchmarkSuite.Views;
10  using Interpreter;
11  using MainForm;
12  using Problem;
13  using Stack;
14
15  [View("Push Program Debugger")]
16  [Content(typeof(PushSolution), true)]
17  public partial class BenchmarkSuitePushSolutionView : NamedItemView {
18
19    private const string Separator = ", ";
20    private const string EmptySign = "-";
21
22    private const string exampleSplitter = " => ";
23    private const string AbsoluteDiffHeaderText = "Absolute Diff.";
24    private const string RelativeDiffHeaderText = "Relative Diff.";
25    private const string InputHeaderStringFormat = "Input {0} : {1}";
26    private const string EstimatedOutputHeaderStringFormat = "Estimated Output {0} : {1}";
27    private const string OutputHeaderStringFormat = "Output {0} : {1}";
28
29    private PooledPushInterpreter interpreter;
30    private PushInterpreterPool pool;
31
32    public BenchmarkSuitePushSolutionView() {
33      InitializeComponent();
34      Name = "Push Program Debugger";
35      InitEvents();
36    }
37
38    ~BenchmarkSuitePushSolutionView() {
39      this.interpreter.Dispose();
40    }
41
42    private void InitEvents() {
43      this.exampleComboBox.SelectedIndexChanged += this.SelectedExampleIndexChanged;
44      this.pushDebugger.OnReset += this.PushDebuggerReset;
45    }
46
47    private void PushDebuggerReset(object sender, IPushInterpreter interpreter) {
48      if (this.exampleComboBox.SelectedIndex < 0) return;
49
50      var example = Evaluator.Data.Examples[this.exampleComboBox.SelectedIndex];
51
52      interpreter.BooleanStack.Push(example.InputBoolean);
53      interpreter.IntegerStack.Push(example.InputInteger);
54      interpreter.FloatStack.Push(example.InputFloat);
55      interpreter.CharStack.Push(example.InputChar);
56      interpreter.StringStack.Push(example.InputString);
57      interpreter.IntegerVectorStack.Push(example.InputIntegerVector.Select(x => x.ToList()).ToArray());
58      interpreter.FloatVectorStack.Push(example.InputFloatVector.Select(x => x.ToList()).ToArray());
59      interpreter.StringVectorStack.Push(example.InputStringVector.Select(x => x.ToList()).ToArray());
60      interpreter.BooleanVectorStack.Push(example.InputBooleanVector.Select(x => x.ToList()).ToArray());
61    }
62
63    private void SelectedExampleIndexChanged(object sender, EventArgs e) {
64      this.pushDebugger.ResetDebugging();
65    }
66
67    public new PushBenchmarkSuiteSolution Content
68    {
69      get { return (PushBenchmarkSuiteSolution)base.Content; }
70      set
71      {
72        base.Content = value;
73      }
74    }
75
76    public PushBenchmarkSuiteEvaluator Evaluator
77    {
78      get { return (PushBenchmarkSuiteEvaluator)Content.Evaluator; }
79    }
80
81    protected override void OnContentChanged() {
82      if (this.Content == null) return;
83
84      this.Name = "Push Solution";
85      this.nameTextBox.Text = this.Name;
86
87      this.pool = new PushInterpreterPool(this.Content.Config);
88
89      if (this.interpreter != null) {
90        this.interpreter.Dispose();
91      }
92
93      this.interpreter = this.pool.Create(this.Content.Random);
94      this.UpdateExamples(Evaluator.Data);
95
96      if (this.exampleComboBox.SelectedIndex < 0) {
97        this.exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event
98      }
99
100      this.InitResultGrid();
101      this.pushDebugger.Content = this.Content;
102    }
103
104    private void InitResultGrid() {
105      this.resultsDataGrid.Columns.Clear();
106      this.resultsDataGrid.Rows.Clear();
107
108      var cellTemplate = new DataGridViewTextBoxCell();
109      var data = Evaluator.Data;
110
111      for (var i = 0; i < data.InputArgumentTypes.Length; i++) {
112        var headerTypeName = ViewHelper.GetHeaderTypeName(data.InputArgumentTypes[i]);
113        var column = new DataGridViewColumn {
114          HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName),
115          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
116          CellTemplate = cellTemplate
117        };
118
119        column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
120        this.resultsDataGrid.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        };
131
132        var outputColumn = new DataGridViewColumn {
133          HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName),
134          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
135          CellTemplate = cellTemplate
136        };
137
138        estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
139        outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
140
141        this.resultsDataGrid.Columns.Add(estimatedOutputColumn);
142        this.resultsDataGrid.Columns.Add(outputColumn);
143      }
144
145      var absoluteDiffColumn = new DataGridViewColumn {
146        HeaderText = AbsoluteDiffHeaderText,
147        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
148        CellTemplate = cellTemplate
149      };
150
151      var relativeDiffColumn = new DataGridViewColumn {
152        HeaderText = RelativeDiffHeaderText,
153        AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
154        CellTemplate = cellTemplate,
155      };
156
157      absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
158      relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
159
160      this.resultsDataGrid.Columns.Add(absoluteDiffColumn);
161      this.resultsDataGrid.Columns.Add(relativeDiffColumn);
162
163      using (var pushInterpreter = this.pool.Create(this.Content.Random)) {
164        for (var i = 0; i < data.Examples.Length; i++) {
165          var example = data.Examples[i];
166          var row = new DataGridViewRow();
167          var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
168          var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
169
170          row.HeaderCell.Value = row.Index + 1;
171          row.CreateCells(this.resultsDataGrid);
172
173          for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
174            row.Cells[j].Value = ViewHelper.StringifyInput(data.InputArgumentTypes[j], example, Separator);
175          }
176
177          for (var j = 0; j < data.OutputArgumentTypes.Length; j++) {
178            row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(data.OutputArgumentTypes[j], example, Separator);
179            row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = this.StringifyResult(data.OutputArgumentTypes[j], pushInterpreter, example, Separator);
180          }
181
182          row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff;
183          row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff + "%";
184
185          this.resultsDataGrid.Rows.Add(row);
186          pushInterpreter.Reset();
187        }
188      }
189    }
190
191    private string StringifyResult(ExampleArgumentType type, IPushInterpreter interpreter, Example example, string valueSeparator) {
192      switch (type) {
193        case ExampleArgumentType.Integer:
194        case ExampleArgumentType.IntegerVector: return interpreter.IntegerStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.IntegerStack.Peek(this.GetCount(interpreter.IntegerStack, example.OutputInteger)));
195
196        case ExampleArgumentType.Float:
197        case ExampleArgumentType.FloatVector: return interpreter.FloatStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.FloatStack.Peek(this.GetCount(interpreter.FloatStack, example.OutputFloat)).Select(d => d.ToString(CultureInfo.CurrentUICulture)));
198
199        case ExampleArgumentType.Boolean: return interpreter.BooleanStack.IsEmpty ? EmptySign : interpreter.BooleanStack.Top.ToString();
200        case ExampleArgumentType.Char: return interpreter.CharStack.IsEmpty ? EmptySign : interpreter.CharStack.Top.ToString();
201
202        case ExampleArgumentType.String:
203        case ExampleArgumentType.StringVector: return interpreter.StringStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.StringStack.Peek(this.GetCount(interpreter.StringStack, example.OutputString)));
204        default: return string.Empty;
205      }
206    }
207
208    private int GetCount<T>(IPushStack<T> stack, T[] data) {
209      return Math.Max(0, Math.Min(data.Length, stack.Count));
210    }
211
212    private void UpdateExamples(ProblemData data) {
213      this.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        this.exampleComboBox.Items.Add(str);
223      }
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.