Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs @ 15289

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

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 4.4 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      foreach (var pair in interpreter.Stacks) {
80        var stackName = pair.Key.ToString();
81        var stack = interpreter.Stacks[pair.Key];
82
83        if (stack.IsEmpty || !stack.IsEnabled) continue;
84
85        var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key).Reverse());
86        Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stackString);
87      }
88
89      if (interpreter.CustomExpressions.Count > 0) {
90        Console.WriteLine("--------- Custom Expressions ---------");
91        foreach (var ce in interpreter.CustomExpressions) {
92          Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
93        }
94      }
95    }
96
97    public static void InitExample(this IPushInterpreter interpreter, Example example) {
98      interpreter.BooleanStack.Push(example.InputBoolean);
99      interpreter.IntegerStack.Push(example.InputInteger);
100      interpreter.FloatStack.Push(example.InputFloat);
101      interpreter.CharStack.Push(example.InputChar);
102      interpreter.StringStack.Push(example.InputString);
103      interpreter.StringVectorStack.Push(example.InputStringVector);
104      interpreter.IntegerVectorStack.Push(example.InputIntegerVector);
105      interpreter.FloatVectorStack.Push(example.InputFloatVector);
106
107      interpreter.SetInput(
108        integers: example.InputInteger,
109        floats: example.InputFloat,
110        booleans: example.InputBoolean,
111        chars: example.InputChar,
112        strings: example.InputString,
113        integerVectors: example.InputIntegerVector,
114        floatVectors: example.InputFloatVector,
115        stringVectors: example.InputStringVector);
116    }
117  }
118}
119
Note: See TracBrowser for help on using the repository browser.