Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/PushGP.Cli/Program.cs @ 17717

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

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

File size: 1.9 KB
RevLine 
[14727]1using System;
2using System.Threading.Tasks;
[15771]3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Problems.ProgramSynthesis;
[14727]6
7namespace HeuristicLab.Algorithms.PushGP.Cli {
[15017]8
[14727]9  class Program {
10    static void Main(string[] args) {
[15017]11      string code = null;
[14727]12
[15017]13      if (args.Length == 0) {
14        var lines = new List<string>();
15
16        Console.WriteLine(@"Program: ( must start with '(' and end with ')' )");
17
18        var bracesCount = 0;
19        var linesRead = 0;
20
21        while (bracesCount > 0 || lines.Count == 0 || linesRead == 0) {
22          var line = Console.ReadLine();
23          linesRead++;
24
25          if (string.IsNullOrWhiteSpace(line))
26            continue;
27
28          line = line.Trim();
29          lines.Add(line);
30
31          var openBraces = line.Count(c => c == PushEnvironment.ProgramStartSymbol);
32          var closeBraces = line.Count(c => c == PushEnvironment.ProgramEndSymbol);
33          bracesCount += openBraces - closeBraces;
34        }
35
36        code = string.Join(" ", lines);
37      } else {
38        code = args[0];
39      }
40
[14777]41      EvaluateStepwise(code).Wait();
[14727]42
[15017]43      Console.WriteLine(@"Press any key to terminate...");
[14727]44    }
45
46    static async Task EvaluateStepwise(string code) {
[14744]47      var interpreter = new PushInterpreter(new PushConfiguration {
[14727]48        TopLevelPushCode = false
49      });
50
[14834]51      Console.WriteLine(@"Press ESC to cancel, SPACE to resume, Any other key to step...");
52      interpreter.Run(code, true);
[14727]53
54      while (!interpreter.IsCompleted) {
55        Console.Clear();
56        interpreter.PrintStacks();
57        interpreter.Step();
58
59        var input = Console.ReadKey();
60        if (input.Key == ConsoleKey.Escape) {
61          break;
[14834]62        }
63
64        if (input.Key == ConsoleKey.Spacebar) {
[14727]65          await interpreter.ResumeAsync();
66        }
67      }
68
69      Console.Clear();
70      interpreter.PrintStacks();
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.