Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

File size: 3.3 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.Reflection;
8
9  using Attributes;
10
11  using HeuristicLab.BenchmarkSuite;
12
13  using Stack;
14
15  public static class Extensions {
16    private static readonly IDictionary<StackTypes, PropertyInfo> StackProperties = typeof(PushInterpreter)
17        .GetProperties()
18        .Select(p => new { Property = p, Attribute = p.GetCustomAttribute<PushStackAttribute>() })
19        .Where(x => x.Attribute != null)
20        .ToDictionary(x => x.Attribute.StackType, x => x.Property);
21
22    public static IEnumerable<T> GetStackEntriesByType<T>(this IPushInterpreter interpreter, StackTypes stackType) {
23      return StackProperties.ContainsKey(stackType)
24        ? ((IEnumerable)StackProperties[stackType].GetValue(interpreter)).OfType<T>()
25        : null;
26    }
27
28    public static Type GetStackEntryType(this StackTypes stackType) {
29      if (StackProperties.ContainsKey(stackType) &&
30          StackProperties[stackType].PropertyType.IsGenericType) {
31        var genericTypeDef = StackProperties[stackType].PropertyType.GetGenericTypeDefinition();
32        if (genericTypeDef == typeof(IPushStack<>))
33          return genericTypeDef.GetGenericArguments()[0];
34      }
35
36      return StackProperties.ContainsKey(stackType)
37        ? StackProperties[stackType]
38            .PropertyType
39            .GetInterfaces()
40            .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPushStack<>))
41            .GetGenericArguments()
42            .First()
43        : null;
44    }
45
46    public static void PrintStacks(this IPushInterpreter interpreter) {
47      foreach (var pair in interpreter.Stacks) {
48        var stackName = pair.Key.ToString();
49        var stack = interpreter.Stacks[pair.Key];
50
51        if (stack.IsEmpty || !stack.IsEnabled) continue;
52
53        var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key).Reverse());
54        Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stackString);
55      }
56
57      if (interpreter.CustomExpressions.Count > 0) {
58        Console.WriteLine("--------- Custom Expressions ---------");
59        foreach (var ce in interpreter.CustomExpressions) {
60          Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
61        }
62      }
63    }
64
65    public static void InitExample(this IPushInterpreter interpreter, Example example) {
66      interpreter.BooleanStack.Push(example.InputBoolean);
67      interpreter.IntegerStack.Push(example.InputInteger);
68      interpreter.FloatStack.Push(example.InputFloat);
69      interpreter.CharStack.Push(example.InputChar);
70      interpreter.StringStack.Push(example.InputString);
71      interpreter.StringVectorStack.Push(example.InputStringVector);
72      interpreter.IntegerVectorStack.Push(example.InputIntegerVector);
73      interpreter.FloatVectorStack.Push(example.InputFloatVector);
74
75      interpreter.SetInput(
76        integers: example.InputInteger,
77        floats: example.InputFloat,
78        booleans: example.InputBoolean,
79        chars: example.InputChar,
80        strings: example.InputString,
81        integerVectors: example.InputIntegerVector,
82        floatVectors: example.InputFloatVector,
83        stringVectors: example.InputStringVector);
84    }
85  }
86}
87
Note: See TracBrowser for help on using the repository browser.