Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/InterpreterStackStringifier.cs @ 16724

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

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

File size: 3.5 KB
RevLine 
[14952]1using System.Collections.Generic;
2using System.Linq;
[15771]3using System.Globalization;
[14952]4
[15771]5namespace HeuristicLab.Problems.ProgramSynthesis {
[15017]6  public static class InterpreterStackStringifier {
[14952]7
[15017]8    public static IEnumerable<string> StringifyStack(this IPushInterpreter interpreter, StackTypes type) {
[14952]9      switch (type) {
10        case StackTypes.Boolean: return StringifyLiteralStack(interpreter.BooleanStack);
11        case StackTypes.Integer: return StringifyLiteralStack(interpreter.IntegerStack);
[15017]12        case StackTypes.Float: return StringifyFloatStack(interpreter.FloatStack, interpreter.Configuration.FloatStringFormat);
13        case StackTypes.Char: return StringifyCharStack(interpreter.CharStack);
14        case StackTypes.String: return StringifyLiteralStack(interpreter.StringStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr);
[14952]15        case StackTypes.Name: return StringifyLiteralStack(interpreter.NameStack);
16        case StackTypes.Code: return StringifyLiteralStack(interpreter.CodeStack);
17        case StackTypes.Exec: return StringifyLiteralStack(interpreter.ExecStack);
[15017]18        case StackTypes.Print: return StringifyLiteralStack(interpreter.PrintStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr, false);
[14952]19
20        case StackTypes.BooleanVector: return StringifyVectorStack(interpreter.BooleanVectorStack);
21        case StackTypes.IntegerVector: return StringifyVectorStack(interpreter.IntegerVectorStack);
[15017]22        case StackTypes.FloatVector: return StringifyFloatVectorStack(interpreter.FloatVectorStack, interpreter.Configuration.FloatStringFormat);
23        case StackTypes.StringVector: return StringifyVectorStack(interpreter.StringVectorStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr);
[14952]24
25        default: return Enumerable.Empty<string>();
26      }
27    }
28
[15017]29    private static IEnumerable<string> StringifyCharStack(IPushStack<char> stack) {
30      var result = stack.AsEnumerable();
31
32      return result.Select(x => PushEnvironment.CharSymbol + x.Printify() + PushEnvironment.CharSymbol);
[14952]33    }
34
[15017]35    private static IEnumerable<string> StringifyFloatStack(IPushStack<double> stack, string format) {
36      var result = stack.AsEnumerable();
37
38      return result.Select(x => x.ToString(format, CultureInfo.InvariantCulture));
39    }
40
[15289]41    private static IEnumerable<string> StringifyLiteralStack(IPushStack stack, string prefix = "", string postfix = "", bool reverse = true) {
[15189]42      var result = stack.AsStrings();
[15017]43
44      if (reverse)
45        result = result.Reverse();
46
47      return result.Select(x => prefix + x.ToString() + postfix);
48    }
49
50    private static IEnumerable<string> StringifyFloatVectorStack(IPushStack<IReadOnlyList<double>> stack, string format) {
[14952]51      var separator = PushEnvironment.VectorSeparatorSymbol.ToString();
52      return stack
53        .Reverse()
[15017]54        .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => _.ToString(format, CultureInfo.InvariantCulture))) + PushEnvironment.VectorEndSymbol);
55    }
56
57    private static IEnumerable<string> StringifyVectorStack<T>(IPushStack<IReadOnlyList<T>> stack, string prefix = "", string postfix = "") {
58      var separator = PushEnvironment.VectorSeparatorSymbol.ToString();
59      return stack
60        .Reverse()
[14952]61        .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => prefix + _.ToString() + postfix)) + PushEnvironment.VectorEndSymbol);
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.