Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/10/17 11:23:05 (7 years ago)
Author:
pkimmesw
Message:

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/BenchmarkSuitePushSolutionView.cs

    r14908 r14952  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite {
    22  using System;
     3  using System.Collections.Generic;
     4  using System.Drawing;
    35  using System.Globalization;
    46  using System.Linq;
     
    68  using Core.Views;
    79  using HeuristicLab.BenchmarkSuite;
    8   using HeuristicLab.BenchmarkSuite.Problems;
    910  using HeuristicLab.BenchmarkSuite.Views;
     11  using HeuristicLab.Common;
     12
    1013  using Interpreter;
    1114  using MainForm;
     
    1821
    1922    private const string Separator = ", ";
    20     private const string EmptySign = "-";
    2123
    2224    private const string exampleSplitter = " => ";
     
    2628    private const string EstimatedOutputHeaderStringFormat = "Estimated Output {0} : {1}";
    2729    private const string OutputHeaderStringFormat = "Output {0} : {1}";
    28 
     30    private static readonly Color correctExampleFontColor = Color.FromArgb(0x5cb85c);
    2931    private PooledPushInterpreter interpreter;
    3032    private PushInterpreterPool pool;
     
    98100      }
    99101
    100       InitResultGrid();
     102      InitResultGrid(trainingResultsDataGrid, Evaluator.DataBounds.TrainingRange.Start, Evaluator.DataBounds.TrainingRange.End);
     103      InitResultGrid(testResultsDataGrid, Evaluator.DataBounds.TestRange.Start, Evaluator.DataBounds.TestRange.End);
     104
    101105      pushDebugger.Content = Content;
    102106    }
    103107
    104     private void InitResultGrid() {
    105       resultsDataGrid.Columns.Clear();
    106       resultsDataGrid.Rows.Clear();
    107 
    108       var cellTemplate = new DataGridViewTextBoxCell();
     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 } };
    109113      var data = Evaluator.Data;
    110114
     
    114118          HeaderText = string.Format(InputHeaderStringFormat, i + 1, headerTypeName),
    115119          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
    116           CellTemplate = cellTemplate
     120          CellTemplate = cellTemplate,
     121          DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleRight }
    117122        };
    118123
    119         column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    120         resultsDataGrid.Columns.Add(column);
     124        grid.Columns.Add(column);
    121125      }
    122126
     
    139143        outputColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    140144
    141         resultsDataGrid.Columns.Add(estimatedOutputColumn);
    142         resultsDataGrid.Columns.Add(outputColumn);
     145        grid.Columns.Add(estimatedOutputColumn);
     146        grid.Columns.Add(outputColumn);
    143147      }
    144148
     
    158162      relativeDiffColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    159163
    160       resultsDataGrid.Columns.Add(absoluteDiffColumn);
    161       resultsDataGrid.Columns.Add(relativeDiffColumn);
     164      grid.Columns.Add(absoluteDiffColumn);
     165      grid.Columns.Add(relativeDiffColumn);
    162166
    163167      using (var pushInterpreter = pool.Create(Content.Random)) {
    164         for (var i = 0; i < data.Examples.Length; i++) {
     168        var rowIndex = 1;
     169        for (var i = start; i < end; i++, rowIndex++) {
    165170          var example = data.Examples[i];
    166           var row = new DataGridViewRow();
     171          var row = new DataGridViewRow {
     172            HeaderCell = {
     173              Value = rowIndex.ToString(),
     174            }
     175          };
     176
     177          row.CreateCells(grid);
     178
    167179          var absoluteDiff = Evaluator.Evaluate(pushInterpreter, Content.Program, i);
    168180          var relativeDiff = absoluteDiff / Math.Abs(data.BestResult - data.WorstResult) * 100d;
    169181
    170           row.HeaderCell.Value = row.Index + 1;
    171           row.CreateCells(resultsDataGrid);
    172 
     182          var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
    173183          for (var j = 0; j < data.InputArgumentTypes.Length; j++) {
    174             row.Cells[j].Value = ViewHelper.StringifyInput(data.InputArgumentTypes[j], example, Separator);
     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]++;
    175188          }
    176189
     190          var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
    177191          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 = StringifyResult(data.OutputArgumentTypes[j], pushInterpreter, example, Separator);
     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]++;
    180197          }
    181198
    182           row.Cells[data.TotalArgumentCount + 1].Value = absoluteDiff;
    183           row.Cells[data.TotalArgumentCount + 2].Value = relativeDiff + "%";
    184 
    185           resultsDataGrid.Rows.Add(row);
     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);
    186209          pushInterpreter.Reset();
    187210        }
     
    189212    }
    190213
    191     private string StringifyResult(ExampleArgumentType type, IPushInterpreter interpreter, Example example, string valueSeparator) {
     214    private string StringifyResult(ExampleArgumentType type, int offset, IPushInterpreter interpreter, Example example, string valueSeparator) {
     215      var emptyString = string.Empty;
     216
    192217      switch (type) {
    193218        case ExampleArgumentType.Integer:
    194         case ExampleArgumentType.IntegerVector: return interpreter.IntegerStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.IntegerStack.Peek(GetCount(interpreter.IntegerStack, example.OutputInteger)));
     219          return GetEntryAsString(offset, interpreter.IntegerStack);
     220
     221        case ExampleArgumentType.IntegerVector:
     222          return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
    195223
    196224        case ExampleArgumentType.Float:
    197         case ExampleArgumentType.FloatVector: return interpreter.FloatStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.FloatStack.Peek(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();
     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)));
    201242
    202243        case ExampleArgumentType.String:
    203         case ExampleArgumentType.StringVector: return interpreter.StringStack.IsEmpty ? EmptySign : string.Join(valueSeparator, interpreter.StringStack.Peek(GetCount(interpreter.StringStack, example.OutputString)));
     244          return GetEntryAsString(offset, interpreter.StringStack);
     245
     246        case ExampleArgumentType.StringVector:
     247          return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
     248
    204249        default: return string.Empty;
    205250      }
    206251    }
    207252
     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
    208265    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) {
    209270      return Math.Max(0, Math.Min(data.Length, stack.Count));
    210271    }
Note: See TracChangeset for help on using the changeset viewer.