namespace HeuristicLab.Problems.ProgramSynthesis.Push.Solution.BenchmarkSuite { using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using HeuristicLab.BenchmarkSuite; using HeuristicLab.BenchmarkSuite.Views; using HeuristicLab.Common; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Solution; [View("Push Program Debugger")] [Content(typeof(PushSolution), true)] public partial class BenchmarkSuitePushSolutionView : NamedItemView { private const string Separator = ", "; 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 = "Expected Output {0} : {1}"; private const string OutputHeaderStringFormat = "Output {0} : {1}"; private static readonly Color correctExampleFontColor = Color.FromArgb(0x5cb85c); private PooledPushInterpreter interpreter; private PushInterpreterPool pool; public BenchmarkSuitePushSolutionView() { InitializeComponent(); Name = "Push Program Debugger"; InitEvents(); } ~BenchmarkSuitePushSolutionView() { interpreter.Dispose(); } private void InitEvents() { exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged; pushDebugger.OnReset += PushDebuggerReset; } private void PushDebuggerReset(object sender, IPushInterpreter interpreter) { if (exampleComboBox.SelectedIndex < 0) return; var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex]; interpreter.InitExample(example); } private void SelectedExampleIndexChanged(object sender, EventArgs e) { 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 (Content == null) return; Name = "Push Solution"; nameTextBox.Text = Name; pool = new PushInterpreterPool(Content.Config); if (interpreter != null) { interpreter.Dispose(); } var random = Content.GetRandom(); interpreter = pool.Create(random); UpdateExamples(Evaluator.Data); if (exampleComboBox.SelectedIndex < 0) { exampleComboBox.SelectedIndex = 0; // Triggers ResetDebugging via event } InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End); InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End); pushDebugger.Content = Content; } private void InitResultGrid(DataGridView grid, int start, int end) { grid.Columns.Clear(); grid.Rows.Clear(); var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } }; 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, DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight }, SortMode = DataGridViewColumnSortMode.Automatic }; grid.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, SortMode = DataGridViewColumnSortMode.Automatic }; var outputColumn = new DataGridViewColumn { HeaderText = string.Format(OutputHeaderStringFormat, i + 1, headerTypeName), AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, CellTemplate = cellTemplate, SortMode = DataGridViewColumnSortMode.Automatic }; estimatedOutputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; grid.Columns.Add(estimatedOutputColumn); grid.Columns.Add(outputColumn); } var absoluteDiffColumn = new DataGridViewColumn { HeaderText = AbsoluteDiffHeaderText, AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader, CellTemplate = cellTemplate, SortMode = DataGridViewColumnSortMode.Automatic }; var relativeDiffColumn = new DataGridViewColumn { HeaderText = RelativeDiffHeaderText, AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader, CellTemplate = cellTemplate, SortMode = DataGridViewColumnSortMode.Automatic }; absoluteDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; grid.Columns.Add(absoluteDiffColumn); grid.Columns.Add(relativeDiffColumn); var random = Content.GetRandom(); using (var pushInterpreter = pool.Create(random)) { var rowIndex = 1; for (var i = start; i < end; i++, rowIndex++) { var example = data.Examples[i]; var row = new DataGridViewRow { HeaderCell = { Value = rowIndex.ToString(), } }; row.CreateCells(grid); var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i); var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d; var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict(); for (var j = 0; j < data.InputArgumentTypes.Length; j++) { var type = data.InputArgumentTypes[j]; var offset = inputArgumentCountDict[type]; row.Cells[j].Value = ViewHelper.StringifyInput(type, offset, Content.Config.FloatStringFormat, example, Separator); inputArgumentCountDict[type]++; } var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict(); for (var j = 0; j < data.OutputArgumentTypes.Length; j++) { var type = data.OutputArgumentTypes[j]; var offset = outputArgumentCountDict[type]; // expected row.Cells[data.InputArgumentTypes.Length + j * 2].Value = ViewHelper.StringifyOutput(type, offset, Content.Config.FloatStringFormat, example, Separator); // output row.Cells[data.InputArgumentTypes.Length + j * 2 + 1].Value = pushInterpreter.StringifyResult(type, offset); outputArgumentCountDict[type]++; } row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff.ToString("0.####"); row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff.ToString("0.##") + "%"; if (absoluteDiff.IsAlmost(data.BestResult)) { for (var j = 0; j < row.Cells.Count; j++) { row.Cells[j].Style.ForeColor = correctExampleFontColor; } } grid.Rows.Add(row); pushInterpreter.Reset(); } } } private void UpdateExamples(ProblemData data) { 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) { exampleComboBox.Items.Add(str); } } } }