Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs @ 18242

Last change on this file since 18242 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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