namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite { using System; using System.Globalization; using System.Linq; using System.Windows.Forms; using Core.Views; using HeuristicLab.BenchmarkSuite; using HeuristicLab.BenchmarkSuite.Problems; using HeuristicLab.BenchmarkSuite.Views; using Interpreter; using MainForm; using Problem; using Stack; [View("Push Program Debugger")] [Content(typeof(PushSolution), true)] public partial class BenchmarkSuitePushSolutionView : NamedItemView { private const string Separator = ", "; private const string EmptySign = "-"; private const string exampleSplitter = " => "; private const string AbsoluteDiffHeaderText = "Absolute Diff."; private const string RelativeDiffHeaderText = "Relative Diff."; private const string InputHeaderStringFormat = "Input {0} : {1}"; private const string EstimatedOutputHeaderStringFormat = "Estimated Output {0} : {1}"; private const string OutputHeaderStringFormat = "Output {0} : {1}"; private PooledPushInterpreter interpreter; private PushInterpreterPool pool; public BenchmarkSuitePushSolutionView() { InitializeComponent(); Name = "Push Program Debugger"; InitEvents(); } ~BenchmarkSuitePushSolutionView() { this.interpreter.Dispose(); } private void InitEvents() { this.exampleComboBox.SelectedIndexChanged += this.SelectedExampleIndexChanged; this.pushDebugger.OnReset += this.PushDebuggerReset; } private void PushDebuggerReset(object sender, IPushInterpreter interpreter) { if (this.exampleComboBox.SelectedIndex < 0) return; var example = Evaluator.Data.Examples[this.exampleComboBox.SelectedIndex]; interpreter.BooleanStack.Push(example.InputBoolean); interpreter.IntegerStack.Push(example.InputInteger); interpreter.FloatStack.Push(example.InputFloat); interpreter.CharStack.Push(example.InputChar); interpreter.StringStack.Push(example.InputString); interpreter.IntegerVectorStack.Push(example.InputIntegerVector.Select(x => x.ToList()).ToArray()); interpreter.FloatVectorStack.Push(example.InputFloatVector.Select(x => x.ToList()).ToArray()); interpreter.StringVectorStack.Push(example.InputStringVector.Select(x => x.ToList()).ToArray()); interpreter.BooleanVectorStack.Push(example.InputBooleanVector.Select(x => x.ToList()).ToArray()); } private void SelectedExampleIndexChanged(object sender, EventArgs e) { this.pushDebugger.ResetDebugging(); } public new PushBenchmarkSuiteSolution Content { get { return (PushBenchmarkSuiteSolution)base.Content; } set { base.Content = value; } } public PushBenchmarkSuiteEvaluator Evaluator { get { return (PushBenchmarkSuiteEvaluator)Content.Evaluator; } } protected override void OnContentChanged() { if (this.Content == null) return; this.Name = "Push Solution"; this.nameTextBox.Text = this.Name; this.pool = new PushInterpreterPool(this.Content.Config); if (this.interpreter != null) { this.interpreter.Dispose(); } this.interpreter = this.pool.Create(this.Content.Random); this.UpdateExamples(Evaluator.Data); if (this.exampleComboBox.SelectedIndex < 0) { this.exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event } this.InitResultGrid(); this.pushDebugger.Content = this.Content; } private void InitResultGrid() { this.resultsDataGrid.Columns.Clear(); this.resultsDataGrid.Rows.Clear(); var cellTemplate = new DataGridViewTextBoxCell(); var data = Evaluator.Data; for (var i = 0; i < data.InputArgumentTypes.Length; i++) { var headerTypeName = ViewHelper.GetHeaderTypeName(data.InputArgumentTypes[i]); var column = new DataGridViewColumn { HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName), AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, CellTemplate = cellTemplate }; column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; this.resultsDataGrid.Columns.Add(column); } for (var i = 0; i < data.OutputArgumentTypes.Length; i++) { var headerTypeName = ViewHelper.GetHeaderTypeName(data.OutputArgumentTypes[i]); var estimatedOutputColumn = new DataGridViewColumn { HeaderText = string.Format(EstimatedOutputHeaderStringFormat, i + 1, headerTypeName), AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, CellTemplate = cellTemplate, }; var outputColumn = new DataGridViewColumn { HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName), AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, CellTemplate = cellTemplate }; estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; this.resultsDataGrid.Columns.Add(estimatedOutputColumn); this.resultsDataGrid.Columns.Add(outputColumn); } var absoluteDiffColumn = new DataGridViewColumn { HeaderText = AbsoluteDiffHeaderText, AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader, CellTemplate = cellTemplate }; var relativeDiffColumn = new DataGridViewColumn { HeaderText = RelativeDiffHeaderText, AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader, CellTemplate = cellTemplate, }; absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; this.resultsDataGrid.Columns.Add(absoluteDiffColumn); this.resultsDataGrid.Columns.Add(relativeDiffColumn); using (var pushInterpreter = this.pool.Create(this.Content.Random)) { for (var i = 0; i < data.Examples.Length; i++) { var example = data.Examples[i]; var row = new DataGridViewRow(); var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i); var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d; row.HeaderCell.Value = row.Index + 1; row.CreateCells(this.resultsDataGrid); for (var j = 0; j < data.InputArgumentTypes.Length; j++) { row.Cells[j].Value = ViewHelper.StringifyInput(data.InputArgumentTypes[j], example, Separator); } for (var j = 0; j < data.OutputArgumentTypes.Length; j++) { row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(data.OutputArgumentTypes[j], example, Separator); row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = this.StringifyResult(data.OutputArgumentTypes[j], pushInterpreter, example, Separator); } row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff; row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff + "%"; this.resultsDataGrid.Rows.Add(row); pushInterpreter.Reset(); } } } private string StringifyResult(ExampleArgumentType type, IPushInterpreter interpreter, Example example, string valueSeparator) { switch (type) { case ExampleArgumentType.Integer: case ExampleArgumentType.IntegerVector: return interpreter.IntegerStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.IntegerStack.Peek(this.GetCount(interpreter.IntegerStack, example.OutputInteger))); case ExampleArgumentType.Float: 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))); case ExampleArgumentType.Boolean: return interpreter.BooleanStack.IsEmpty ? EmptySign : interpreter.BooleanStack.Top.ToString(); case ExampleArgumentType.Char: return interpreter.CharStack.IsEmpty ? EmptySign : interpreter.CharStack.Top.ToString(); case ExampleArgumentType.String: case ExampleArgumentType.StringVector: return interpreter.StringStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.StringStack.Peek(this.GetCount(interpreter.StringStack, example.OutputString))); default: return string.Empty; } } private int GetCount(IPushStack stack, T[] data) { return Math.Max(0, Math.Min(data.Length, stack.Count)); } private void UpdateExamples(ProblemData data) { this.exampleComboBox.Items.Clear(); if (data == null) return; var stringRepresentations = data.Examples.Select(e => string.Join(Separator, e.InputArgs) + exampleSplitter + string.Join(Separator, e.OutputArgs)); foreach (var str in stringRepresentations) { this.exampleComboBox.Items.Add(str); } } } }