Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs @ 17104

Last change on this file since 17104 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 5.0 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
5  using System;
6  using System.Collections;
7  using System.Globalization;
8  using System.Reflection;
9
10  using Attributes;
11
12  using HeuristicLab.BenchmarkSuite;
13
14  using Stack;
15
16  public static class Extensions {
17    private static readonly IDictionary<StackTypes, PropertyInfo> StackProperties = typeof(PushInterpreter)
18        .GetProperties()
19        .Select(p => new { Property = p, Attribute = p.GetCustomAttribute<PushStackAttribute>() })
20        .Where(x => x.Attribute != null)
21        .ToDictionary(x => x.Attribute.StackType, x => x.Property);
22
23    public static IEnumerable<T> GetStackEntriesByType<T>(this IPushInterpreter interpreter, StackTypes stackType) {
24      return StackProperties.ContainsKey(stackType)
25        ? ((IEnumerable)StackProperties[stackType].GetValue(interpreter)).OfType<T>()
26        : null;
27    }
28
29    public static string StringifyResult(this IPushInterpreter interpreter, ExampleArgumentType type, int offset) {
30      var emptyString = string.Empty;
31
32      switch (type) {
33        case ExampleArgumentType.Integer:
34          return GetEntryAsString(offset, interpreter.IntegerStack);
35
36        case ExampleArgumentType.IntegerVector:
37          return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
38
39        case ExampleArgumentType.Float:
40          return interpreter.FloatStack.Count > offset
41            ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
42            : emptyString;
43
44        case ExampleArgumentType.FloatVector:
45          return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
46
47        case ExampleArgumentType.Boolean:
48          return GetEntryAsString(offset, interpreter.BooleanStack);
49
50        case ExampleArgumentType.Char:
51          return GetEntryAsString(offset, interpreter.CharStack);
52
53        case ExampleArgumentType.Print:
54          return interpreter.PrintStack.ToString();
55
56        case ExampleArgumentType.String:
57          return GetEntryAsString(offset, interpreter.StringStack);
58
59        case ExampleArgumentType.StringVector:
60          return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
61
62        default: return string.Empty;
63      }
64    }
65
66    private static string GetEntryAsString<T>(int offset, IPushStack<T> stack) {
67      return stack.Count > offset
68       ? stack[offset].ToString()
69       : string.Empty;
70    }
71
72    private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
73      return vectorStack.Count > offset
74       ? "[" + string.Join(",", vectorStack[offset]) + "]"
75       : string.Empty;
76    }
77
78    public static void PrintStacks(this IPushInterpreter interpreter) {
79      var maxStackNameLength = interpreter.Stacks.Max(x => x.Key.ToString().Length);
80      var totalLength = maxStackNameLength + 6;
81
82      foreach (var pair in interpreter.Stacks) {
83        var stackName = pair.Key.ToString();
84        var stack = interpreter.Stacks[pair.Key];
85
86        if (stack.IsEmpty || !stack.IsEnabled)
87          continue;
88
89        var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key));
90
91        //var padLength = (totalLength - stackName.Length) / 2;
92        //var padLengthLeft = stackName.Length + padLength;
93        //var padLengthRight = padLengthLeft + padLength;
94        //Console.WriteLine("{0}\n{1}\n", stackName.PadLeft(padLengthLeft, '-').PadRight(padLengthRight, '-'), stackString);
95
96        Console.WriteLine("{0}: {1}", stackName, stackString);
97      }
98
99      if (interpreter.CustomExpressions.Count > 0) {
100        Console.WriteLine("--------- Custom Expressions ---------");
101        foreach (var ce in interpreter.CustomExpressions) {
102          Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
103        }
104      }
105    }
106
107    public static void InitExample(this IPushInterpreter interpreter, Example example) {
108      if (interpreter.Configuration.TopLevelPushInputArguments) {
109        interpreter.BooleanStack.Push(example.InputBoolean);
110        interpreter.IntegerStack.Push(example.InputInteger);
111        interpreter.FloatStack.Push(example.InputFloat);
112        interpreter.CharStack.Push(example.InputChar);
113        interpreter.StringStack.Push(example.InputString);
114        interpreter.StringVectorStack.Push(example.InputStringVector);
115        interpreter.IntegerVectorStack.Push(example.InputIntegerVector);
116        interpreter.FloatVectorStack.Push(example.InputFloatVector);
117      }
118
119      // init in expressions
120      interpreter.SetInput(
121        integers: example.InputInteger,
122        floats: example.InputFloat,
123        booleans: example.InputBoolean,
124        chars: example.InputChar,
125        strings: example.InputString,
126        integerVectors: example.InputIntegerVector,
127        floatVectors: example.InputFloatVector,
128        stringVectors: example.InputStringVector);
129    }
130  }
131}
132
Note: See TracBrowser for help on using the repository browser.