Changeset 14328


Ignore:
Timestamp:
10/08/16 12:04:25 (3 years ago)
Author:
pkimmesw
Message:

#2665 Set .NET version to 4.5, C# version to 5.0, Added expression templates and factory

Location:
branches/PushGP/HeuristicLab.Algorithms.PushGP
Files:
23 added
41 deleted
31 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/App.config

    r14320 r14328  
    1 <?xml version="1.0" encoding="utf-8" ?>
     1<?xml version="1.0" encoding="utf-8"?>
    22<configuration>
    33    <startup>
    4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
     4        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    55    </startup>
    66</configuration>
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/HeuristicLab.Algorithms.PushGP.Cli.csproj

    r14320 r14328  
    1010    <RootNamespace>HeuristicLab.Algorithms.PushGP.Cli</RootNamespace>
    1111    <AssemblyName>HeuristicLab.Algorithms.PushGP.Cli</AssemblyName>
    12     <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
     12    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1313    <FileAlignment>512</FileAlignment>
    1414    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     15    <TargetFrameworkProfile />
    1516  </PropertyGroup>
    1617  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    3233    <ErrorReport>prompt</ErrorReport>
    3334    <WarningLevel>4</WarningLevel>
     35    <LangVersion>5</LangVersion>
    3436  </PropertyGroup>
    3537  <ItemGroup>
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/Program.cs

    r14323 r14328  
    1212        static void Main(string[] args)
    1313        {
    14             //Stepwise().Wait();
    15             PerformanceTest();
     14            Stepwise().Wait();
     15            //PerformanceTestInterpreter();
    1616            //PerformanceTestCodeGenerator();
    1717
     
    2222        static async Task Stepwise()
    2323        {
    24             var program = PushGPInterpreter.Encode("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )");
     24            var program = PushGPInterpreter.Encode(@"( CODE.QUOTE ( INTEGER.POP 1 )
     25                                                       CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* )
     26                                                       INTEGER.DUP 2 INTEGER.< CODE.IF )");
    2527            var interpreter = new PushGPInterpreter();
    2628
    27             var task = interpreter.InterpreteAsync(program);
    28             await interpreter.PauseAsync();
     29            interpreter.IntegerStack.Push(5);
     30            interpreter.InterpretAsync(program, true).Wait();
    2931
    3032            while (!interpreter.IsCompleted)
     
    4951        }
    5052
    51         static void PerformanceTest()
     53        static void PerformanceTestInterpreter()
    5254        {
    5355            var program = PushGPInterpreter.Encode("( 5 INTEGER.DUP INTEGER.+ )");
     
    5860            for (var i = 0; i < 20000000; i++)
    5961            {
    60                 interpreter.Interprete(program);
    61                 interpreter.Reset();
     62                interpreter.Interpret(program);
     63                interpreter.Clear();
    6264            }
    6365            sw.Stop();
     
    7173
    7274            sw.Start();
    73             var expressions = CodeGenerator.RandomCode(10000000);
     75            var expressions = CodeGenerator.RandomCode(3000000);
    7476            sw.Stop();
    7577
    76             Console.WriteLine($"Count: {expressions.Count()}");
    77             Console.WriteLine(sw.Elapsed);
     78            Console.WriteLine(string.Format("Generated {0} in {1}", expressions.Count(), sw.Elapsed));
    7879        }
    7980    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Boolean/BooleanPushExpression.cs

    r14323 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Text;
     5using System.Threading.Tasks;
     6using HeuristicLab.Algorithms.PushGP.Interpreter;
    27
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     8namespace HeuristicLab.Algorithms.PushGP.Expressions.Boolean
    49{
    510    public class BooleanPushExpression : Expression
    611    {
    7         public BooleanPushExpression(bool value) : base(OpCode.BooleanPush)
     12        private readonly bool value;
     13        public BooleanPushExpression(bool value)
    814        {
    9             this.Value = value;
     15            this.value = value;
    1016        }
    1117
    12         public bool Value { get; }
     18        public override bool IsCodeOp { get { return false; } }
    1319
    14         public override void Eval(IInterpreterService interpreterService)
     20        public override void Eval(IInterpreter interpreter)
    1521        {
    16             interpreterService.BooleanStack.Push(Value);
     22            interpreter.BooleanStack.Push(this.value);
    1723        }
    1824
    1925        public override string ToString()
    2026        {
    21             return $"{this.Value.ToString().ToUpper()}";
     27            return this.value.ToString().ToUpper();
    2228        }
    2329    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeDefineExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Expressions.Exec;
     2using HeuristicLab.Algorithms.PushGP.Interpreter;
    23
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     4namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    45{
    56    public class CodeDefineExpression : Expression
    67    {
    7         public CodeDefineExpression() : base(OpCode.CodeDefine)
    8         { }
     8        public override bool IsCodeOp { get { return true; } }
    99
    10         public override void Eval(IInterpreterService interpreterService)
     10        public override void Eval(IInterpreter interpreter)
    1111        {
    12             Define(
    13                 interpreterService.CodeStack,
    14                 interpreterService.NameStack,
    15                 interpreterService.CustomExpressions,
    16                 value => new ExecPushExpression(value));
     12            // not enough arguments on stack
     13            if (interpreter.NameStack.Count == 0 ||
     14                interpreter.CodeStack.Count == 0)
     15                return;
    1716
    18             // as the interpreter does not pop code expressions
    19             interpreterService.CodeStack.Pop();
     17            var name = interpreter.NameStack.Pop();
     18            var expression = new ExecPushExpression(interpreter.CodeStack.Top);
     19
     20            if (interpreter.CustomExpressions.ContainsKey(name))
     21            {
     22                interpreter.CustomExpressions[name] = expression;
     23            }
     24            else
     25            {
     26                interpreter.CustomExpressions.Add(name, expression);
     27            }
     28
     29            interpreter.CodeStack.Pop();
     30        }
     31
     32        public override string ToString()
     33        {
     34            return Symbols.CodeDefine;
    2035        }
    2136    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeDoExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    44{
    55    public class CodeDoExpression : Expression
    66    {
    7         public CodeDoExpression() : base(OpCode.CodeDo)
    8         { }
     7        public override bool IsCodeOp { get { return true; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.CodeStack.Count == 0)
     12            if (interpreter.CodeStack.Count == 0)
    1413                return;
    1514
    16             var expression = interpreterService.CodeStack.Pop();
    17             interpreterService.ExecStack.Push(expression);
     15            var expression = interpreter.CodeStack.Pop();
     16            interpreter.ExecStack.Push(expression);
     17        }
     18
     19        public override string ToString()
     20        {
     21            return Symbols.CodeDo;
    1822        }
    1923    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeIfExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    44{
    55    public class CodeIfExpression : Expression
    66    {
    7         public CodeIfExpression() : base(OpCode.CodeIf)
    8         { }
     7        public override bool IsCodeOp { get { return true; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.BooleanStack.Count == 0 ||
    14                 interpreterService.CodeStack.Count < 2)
     12            if (interpreter.BooleanStack.Count == 0 ||
     13                interpreter.CodeStack.Count < 2)
    1514                return;
    1615
    17             var condition = interpreterService.BooleanStack.Pop();
    18             var expressionFalse = interpreterService.CodeStack.Pop();
    19             var expressionTrue = interpreterService.CodeStack.Pop();
     16            var condition = interpreter.BooleanStack.Pop();
     17            var expressionFalse = interpreter.CodeStack.Pop();
     18            var expressionTrue = interpreter.CodeStack.Pop();
    2019
    21             interpreterService.ExecStack.Push(condition
     20            interpreter.ExecStack.Push(condition
    2221                ? expressionTrue
    2322                : expressionFalse);
    2423        }
     24
     25        public override string ToString()
     26        {
     27            return Symbols.CodeIf;
     28        }
    2529    }
    2630}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeNoopExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    44{
    55    public class CodeNoopExpression : Expression
    66    {
    7         public CodeNoopExpression() : base(OpCode.CodeNoop)
     7        public override bool IsCodeOp { get { return true; } }
     8
     9        public override void Eval(IInterpreter interpreter)
    810        {
    9         }
    10 
    11         public override void Eval(IInterpreterService interpreterService)
    12         {
    13             // nothing to do
     11            // do nothing
    1412        }
    1513    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeQuoteExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    44{
    55    public class CodeQuoteExpression : Expression
    66    {
    7         public CodeQuoteExpression() : base(OpCode.CodeQuote)
    8         { }
     7        public override bool IsCodeOp { get { return true; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.ExecStack.Count == 0)
     12            if (interpreter.ExecStack.Count == 0)
    1413                return;
    1514
    16             var expression = interpreterService.ExecStack.Pop();
    17             interpreterService.CodeStack.Push(expression);
     15            var expression = interpreter.ExecStack.Pop();
     16            interpreter.CodeStack.Push(expression);
     17        }
     18
     19        public override string ToString()
     20        {
     21            return Symbols.CodeQuote;
    1822        }
    1923    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecDefineExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Expressions.Exec;
     2using HeuristicLab.Algorithms.PushGP.Interpreter;
    23
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     4namespace HeuristicLab.Algorithms.PushGP.Expressions.Code
    45{
    56    public class ExecDefineExpression : Expression
    67    {
    7         public ExecDefineExpression() : base(OpCode.ExecDefine)
    8         { }
     8        public override bool IsCodeOp { get { return false; } }
    99
    10         public override void Eval(IInterpreterService interpreterService)
     10        public override void Eval(IInterpreter interpreter)
    1111        {
    12             Define(
    13                 interpreterService.ExecStack,
    14                 interpreterService.NameStack,
    15                 interpreterService.CustomExpressions,
    16                 value => new ExecPushExpression(value));
     12            // not enough arguments on stack
     13            if (interpreter.NameStack.Count == 0 ||
     14                interpreter.CodeStack.Count == 0)
     15                return;
    1716
    18             // as the interpreter does not pop code expressions
    19             interpreterService.ExecStack.Pop();
     17            var name = interpreter.NameStack.Pop();
     18            var expression = new ExecPushExpression(interpreter.ExecStack.Top);
     19
     20            if (interpreter.CustomExpressions.ContainsKey(name))
     21            {
     22                interpreter.CustomExpressions[name] = expression;
     23            }
     24            else
     25            {
     26                interpreter.CustomExpressions.Add(name, expression);
     27            }
     28
     29            interpreter.ExecStack.Pop();
     30        }
     31
     32        public override string ToString()
     33        {
     34            return Symbols.CodeDefine;
    2035        }
    2136    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecDoRangeExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec
    44{
    55    public class ExecDoRangeExpression : Expression
    66    {
    7         public ExecDoRangeExpression() : base(OpCode.ExecDoXRange)
    8         { }
     7        public override bool IsCodeOp { get { return false; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.IntegerStack.Count < 2 ||
    14                 interpreterService.ExecStack.Count == 0)
     12            if (interpreter.IntegerStack.Count < 2 ||
     13                interpreter.ExecStack.Count == 0)
    1514                return;
    1615
    17             var destinationIndex = interpreterService.IntegerStack.Pop();
    18             var currentIndex = interpreterService.IntegerStack.Pop();
    19             var loopBody = interpreterService.ExecStack.Pop();
     16            var destinationIndex = interpreter.IntegerStack.Pop();
     17            var currentIndex = interpreter.IntegerStack.Pop();
     18            var loopBody = interpreter.ExecStack.Pop();
    2019
    2120            if (destinationIndex == currentIndex)
    2221            {
    23                 interpreterService.IntegerStack.Push(currentIndex);
    24                 interpreterService.ExecStack.Push(loopBody);
     22                interpreter.IntegerStack.Push(currentIndex);
     23                interpreter.ExecStack.Push(loopBody);
    2524            }
    2625            else
     
    3029                    : currentIndex + 1;
    3130
    32                 interpreterService.IntegerStack.Push(currentIndex);
    33                 interpreterService.IntegerStack.Push(nextIndex);
    34                 interpreterService.IntegerStack.Push(destinationIndex);
     31                interpreter.IntegerStack.Push(currentIndex);
     32                interpreter.IntegerStack.Push(nextIndex);
     33                interpreter.IntegerStack.Push(destinationIndex);
    3534
    3635                var expression = new ExecDoRangeExpression();
    37                 interpreterService.ExecStack.Push(loopBody);
    38                 interpreterService.ExecStack.Push(loopBody);
    39                 interpreterService.ExecStack.Push(expression);
     36                interpreter.ExecStack.Push(loopBody);
     37                interpreter.ExecStack.Push(loopBody);
     38                interpreter.ExecStack.Push(expression);
    4039            }
     40        }
     41
     42        public override string ToString()
     43        {
     44            return Symbols.ExecDoXRange;
    4145        }
    4246    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecExpandExpression.cs

    r14323 r14328  
    1 using System.Linq;
    2 using HeuristicLab.Algorithms.PushGP.Stack;
     1using System.Text;
     2using HeuristicLab.Algorithms.PushGP.Interpreter;
    33
    4 namespace HeuristicLab.Algorithms.PushGP.Expressions
     4namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec
    55{
    66    public class ExecExpandExpression : Expression
    77    {
    8         public ExecExpandExpression(Expression[] expressions) : base(OpCode.ExecExpand)
     8        private const string prefix = "( ";
     9        private const string postifx = " )";
     10        private const string delimiter = " ";
     11
     12        private readonly Expression[] expressions;
     13        public ExecExpandExpression(Expression[] expressions)
    914        {
    10             this.Expressions = expressions;
     15            this.expressions = expressions;
    1116        }
    1217
    13         public Expression[] Expressions { get; }
     18        public override bool IsCodeOp { get { return false; } }
    1419
    15         public override void Eval(IInterpreterService interpreterService)
     20        public override void Eval(IInterpreter interpreter)
    1621        {
    17             foreach (var expression in this.Expressions)
    18             {
    19                 interpreterService.ExecStack.Add(expression);
    20             }
     22            interpreter.ExecStack.Push(this.expressions);
    2123        }
    2224
    2325        public override string ToString()
    2426        {
    25             return $"( {string.Join(" ", this.Expressions.Reverse())} )";
     27            if (this.expressions.Length == 0)
     28            {
     29                return string.Empty;
     30            }
     31
     32            var sb = new StringBuilder();
     33            sb.Append(prefix);
     34
     35            for (var i = this.expressions.Length - 1; i > 0; i--)
     36            {
     37                sb.Append(this.expressions[i] + delimiter);
     38            }
     39
     40            sb.Append(this.expressions[0] + postifx);
     41
     42            return sb.ToString();
    2643        }
    2744    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecIfExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec
    44{
    55    public class ExecIfExpression : Expression
    66    {
    7         public ExecIfExpression() : base(OpCode.ExecIf)
    8         { }
     7        public override bool IsCodeOp { get { return false; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.BooleanStack.Count == 0 ||
    14                 interpreterService.ExecStack.Count < 2)
     12            if (interpreter.BooleanStack.Count == 0 ||
     13                interpreter.ExecStack.Count < 2)
    1514                return;
    1615
    17             var condition = interpreterService.BooleanStack.Pop();
    18             var expressionTrue = interpreterService.ExecStack.Pop();
    19             var expressionFalse = interpreterService.ExecStack.Pop();
     16            var condition = interpreter.BooleanStack.Pop();
     17            var expressionTrue = interpreter.ExecStack.Pop();
     18            var expressionFalse = interpreter.ExecStack.Pop();
    2019
    21             interpreterService.ExecStack.Push(condition
     20            interpreter.ExecStack.Push(condition
    2221                ? expressionTrue
    2322                : expressionFalse);
    2423        }
     24
     25        public override string ToString()
     26        {
     27            return Symbols.ExecIf;
     28        }
    2529    }
    2630}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecPushExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec
    44{
    55    public class ExecPushExpression : Expression
    66    {
    7         public ExecPushExpression(Expression value) : base(OpCode.ExecPush)
     7        private readonly Expression value;
     8        public ExecPushExpression(Expression value)
    89        {
    9             this.Value = value;
     10            this.value = value;
    1011        }
    1112
    12         public Expression Value { get; }
     13        public override bool IsCodeOp { get { return false; } }
    1314
    14         public override void Eval(IInterpreterService interpreterService)
     15        public override void Eval(IInterpreter interpreter)
    1516        {
    16             interpreterService.ExecStack.Push(Value);
     17            interpreter.ExecStack.Push(this.value);
    1718        }
    1819
    1920        public override string ToString()
    2021        {
    21             return $"{base.ToString()}({this.Value})";
     22            return this.value.ToString();
    2223        }
    2324    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecYExpression.cs

    r14323 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec
    44{
    55    public class ExecYExpression : Expression
    66    {
    7         public ExecYExpression() : base(OpCode.ExecY)
    8         { }
     7        public override bool IsCodeOp { get { return false; } }
    98
    10         public override void Eval(IInterpreterService interpreterService)
     9        public override void Eval(IInterpreter interpreter)
    1110        {
    1211            // not enough arguments on stack
    13             if (interpreterService.ExecStack.Count == 0)
     12            if (interpreter.ExecStack.Count == 0)
    1413                return;
    1514
    1615            var expandExpression = new ExecExpandExpression(new[]
    1716            {
    18                 interpreterService.ExecStack.Top,
     17                interpreter.ExecStack.Top,
    1918                new ExecYExpression()
    2019            });
    2120
    22             interpreterService.ExecStack.Insert(interpreterService.ExecStack.Count - 1, expandExpression);
     21            interpreter.ExecStack.Insert(interpreter.ExecStack.Count - 1, expandExpression);
     22        }
     23
     24        public override string ToString()
     25        {
     26            return Symbols.ExecY;
    2327        }
    2428    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Expression.cs

    r14323 r14328  
    11using System;
    22using System.Collections.Generic;
     3using HeuristicLab.Algorithms.PushGP.Interpreter;
    34using HeuristicLab.Algorithms.PushGP.Stack;
    45
     
    78    public abstract class Expression
    89    {
    9         public Expression(OpCode opCode)
    10         {
    11             this.OpCode = opCode;
    12         }
     10        public abstract bool IsCodeOp { get; }
     11        public abstract void Eval(IInterpreter interpreter);
    1312
    14         public OpCode OpCode { get; }
    15 
    16         public abstract void Eval(IInterpreterService interpreterService);
    17 
    18         public override string ToString()
    19         {
    20             return SymbolTable.GetSymbol(this.OpCode);
    21         }
    22 
    23         protected static void PushResult<T>(IStack<T> stack, int count, Func<T[], T> templateFunc)
     13        public void PushResult<T>(IStack<T> stack, int count, Func<T[], T> templateFunc)
    2414        {
    2515            PushResult(stack, stack, count, templateFunc);
    2616        }
    2717
    28         protected static void PushResult<T, R>(IStack<T> sourceStack, IStack<R> targetStack, int count, Func<T[], R> templateFunc)
     18        public void PushResult<T, R>(IStack<T> sourceStack, IStack<R> targetStack, int count, Func<T[], R> templateFunc)
    2919        {
    3020            // not enough arguments on stack
     
    3828        }
    3929
    40         protected static void Duplicate<T>(IStack<T> stack)
     30        public void Duplicate<T>(IStack<T> stack)
    4131        {
    4232            // not enough arguments on stack
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Float/FloatPushExpression.cs

    r14323 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Float
    44{
    55    public class FloatPushExpression : Expression
    66    {
    7         public FloatPushExpression(double value) : base(OpCode.FloatPush)
     7        private readonly double value;
     8        public FloatPushExpression(double value)
    89        {
    9             this.Value = value;
     10            this.value = value;
    1011        }
    1112
    12         public double Value { get; }
     13        public override bool IsCodeOp { get { return false; } }
    1314
    14         public override void Eval(IInterpreterService interpreterService)
     15        public override void Eval(IInterpreter interpreter)
    1516        {
    16             interpreterService.FloatStack.Push(Value);
     17            interpreter.FloatStack.Push(this.value);
    1718        }
    1819
    1920        public override string ToString()
    2021        {
    21             return $"{this.Value}";
     22            return this.value.ToString();
    2223        }
    2324    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Integer/IntegerPushExpression.cs

    r14323 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Integer
    44{
    55    public class IntegerPushExpression : Expression
    66    {
    7         public IntegerPushExpression(long value) : base(OpCode.IntegerPush)
     7        private readonly long value;
     8        public IntegerPushExpression(long value)
    89        {
    9             this.Value = value;
     10            this.value = value;
    1011        }
    1112
    12         public long Value { get; }
     13        public override bool IsCodeOp { get { return false; } }
    1314
    14         public override void Eval(IInterpreterService interpreterService)
     15        public override void Eval(IInterpreter interpreter)
    1516        {
    16             interpreterService.IntegerStack.Push(Value);
     17            interpreter.IntegerStack.Push(this.value);
    1718        }
    1819
    1920        public override string ToString()
    2021        {
    21             return $"{this.Value}";
     22            return this.value.ToString();
    2223        }
    2324    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Name/NameDefineXExecExpression.cs

    r14320 r14328  
    1 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
    22
    3 namespace HeuristicLab.Algorithms.PushGP.Expressions
     3namespace HeuristicLab.Algorithms.PushGP.Expressions.Name
    44{
    55    public class NameDefineXExecExpression : Expression
    66    {
    7         public NameDefineXExecExpression(string value) : base(OpCode.NameDefineXExec)
     7        private readonly string name;
     8        public NameDefineXExecExpression(string name)
    89        {
    9             this.Value = value;
     10            this.name = name;
    1011        }
    1112
    12         public string Value { get; }
     13        public override bool IsCodeOp { get { return false; } }
    1314
    14         public override void Eval(IInterpreterService interpreterService)
     15        public override void Eval(IInterpreter interpreter)
    1516        {
    1617            Expression expression;
    17             if (interpreterService.CustomExpressions.TryGetValue(this.Value, out expression))
     18            if (interpreter.CustomExpressions.TryGetValue(this.name, out expression))
    1819            {
    19                 interpreterService.ExecStack.Push(expression);
     20                interpreter.ExecStack.Push(expression);
    2021            }
    2122            else
    2223            {
    23                 interpreterService.NameStack.Push(this.Value);
     24                interpreter.NameStack.Push(this.name);
    2425            }
    2526        }
     
    2728        public override string ToString()
    2829        {
    29             return this.Value;
     30            return this.name;
    3031        }
    3132    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs

    r14323 r14328  
    1 using System;
    2 using System.Collections.Generic;
     1using System.Collections.Generic;
    32using System.Linq;
    43using HeuristicLab.Algorithms.PushGP.Expressions;
     4using HeuristicLab.Random;
    55
    66namespace HeuristicLab.Algorithms.PushGP.Generators
     
    88    public static class CodeGenerator
    99    {
     10        private static FastRandom random = new FastRandom();
    1011        public static IEnumerable<Expression> RandomCode(int maxPoints)
    1112        {
    12             var actualPoints = StaticRandom.Next(1, maxPoints);
     13            var actualPoints = random.Next(1, maxPoints);
    1314
    1415            return RandomCodeWithSize(actualPoints).ToArray();
     
    1920            if (points == 1)
    2021            {
    21                 OpCode opCode;
    22                 Func<Expression> creator;
     22                // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type;
     23                var opCode = (OpCode)random.Next(OpCodeExtensions.Min, OpCodeExtensions.Max);
    2324
    24                 do
    25                 {
    26                     // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type;
    27                     opCode = (OpCode)StaticRandom.Next(OpCodeExtensions.Min, OpCodeExtensions.Max);
    28                 }
    29                 while (!ExpressionCreatorTable.TryGetCreator(opCode, out creator));
    30 
    31                 return new[] { creator.Invoke() };
     25                return new[] { ExpressionFactory.Create(opCode) };
    3226            }
    3327            else
     
    3529                var sizesThisLevel = Decompose(points - 1, points - 1);
    3630
    37                 return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size));
     31                return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random);
    3832            }
    3933        }
     
    4741            else
    4842            {
    49                 var thisPart = StaticRandom.Next(1, number - 1);
     43                var thisPart = random.Next(1, number - 1);
    5044
    5145                return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/Configuration.cs

    r14323 r14328  
    33    public class Configuration
    44    {
     5        public Configuration()
     6        {
     7            this.MinRandomInteger = long.MinValue;
     8            this.MaxRandomInteger = long.MaxValue;
     9            this.MinRandomFloat = double.MaxValue;
     10            this.MaxRandomFloat = double.MaxValue;
     11            this.NewErcNameProbability = 0.5;
     12        }
     13
    514        /// <summary>
    615        /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
    716        /// </summary>
    8         public long MinRandomInteger { get; set; } = long.MinValue;
     17        public long MinRandomInteger { get; set; }
    918
    1019        /// <summary>
    1120        /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
    1221        /// </summary>
    13         public long MaxRandomInteger { get; set; } = long.MaxValue;
     22        public long MaxRandomInteger { get; set; }
    1423
    1524        /// <summary>
    1625        /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
    1726        /// </summary>
    18         public double MinRandomFloag { get; set; } = double.MinValue;
     27        public double MinRandomFloat { get; set; }
    1928
    2029        /// <summary>
    2130        /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
    2231        /// </summary>
    23         public double MaxRandomFloag { get; set; } = double.MaxValue;
     32        public double MaxRandomFloat { get; set; }
    2433
    2534        /// <summary>
     
    2837        /// (rather than a name that was previously generated).
    2938        /// </summary>
    30         public double NewErcNameProbability { get; set; } = 0.5;
     39        public double NewErcNameProbability { get; set; }
    3140    }
    3241}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj

    r14323 r14328  
    1010    <RootNamespace>HeuristicLab.Algorithms.PushGP</RootNamespace>
    1111    <AssemblyName>HeuristicLab.Algorithms.PushGP</AssemblyName>
    12     <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
     12    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1313    <FileAlignment>512</FileAlignment>
     14    <TargetFrameworkProfile />
    1415  </PropertyGroup>
    1516  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    2930    <ErrorReport>prompt</ErrorReport>
    3031    <WarningLevel>4</WarningLevel>
     32    <LangVersion>5</LangVersion>
    3133  </PropertyGroup>
    3234  <ItemGroup>
     35    <Reference Include="HeuristicLab.Common-3.3">
     36      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     37    </Reference>
     38    <Reference Include="HeuristicLab.Core-3.3">
     39      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     40    </Reference>
     41    <Reference Include="HeuristicLab.Random-3.3">
     42      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
     43    </Reference>
    3344    <Reference Include="System" />
    3445    <Reference Include="System.Core" />
     
    4152  </ItemGroup>
    4253  <ItemGroup>
    43     <Compile Include="Interpreter\Configuration.cs" />
    44     <Compile Include="Data\Expression.cs" />
    45     <Compile Include="Expressions\Boolean\BooleanAndExpression.cs" />
    46     <Compile Include="Expressions\Boolean\BooleanOrExpression.cs" />
     54    <Compile Include="Exporter\Exporter.cs" />
    4755    <Compile Include="Expressions\Boolean\BooleanPushExpression.cs" />
    48     <Compile Include="Expressions\Boolean\BooleanDuplicateExpression.cs" />
    49     <Compile Include="Expressions\Boolean\BooleanDefineExpression.cs" />
    50     <Compile Include="Expressions\Boolean\BooleanPopExpression.cs" />
    51     <Compile Include="Expressions\Exec\ExecIfExpression.cs" />
     56    <Compile Include="Expressions\Code\CodeDoExpression.cs" />
     57    <Compile Include="Expressions\Code\CodeDefineExpression.cs" />
    5258    <Compile Include="Expressions\Code\CodeIfExpression.cs" />
    53     <Compile Include="Expressions\Code\CodeDoExpression.cs" />
     59    <Compile Include="Expressions\Code\CodeNoopExpression.cs" />
    5460    <Compile Include="Expressions\Code\CodeQuoteExpression.cs" />
    55     <Compile Include="Expressions\Code\CodeDuplicateExpression.cs" />
    56     <Compile Include="Expressions\Code\CodeNoopExpression.cs" />
    57     <Compile Include="Expressions\Code\CodePushExpression.cs" />
    58     <Compile Include="Expressions\Code\CodeDefineExpression.cs" />
     61    <Compile Include="Expressions\Exec\ExecDefineExpression.cs" />
    5962    <Compile Include="Expressions\Exec\ExecYExpression.cs" />
    6063    <Compile Include="Expressions\Exec\ExecDoRangeExpression.cs" />
    61     <Compile Include="Expressions\Exec\ExecDuplicateExpression.cs" />
     64    <Compile Include="Expressions\Exec\ExecIfExpression.cs" />
     65    <Compile Include="Expressions\Exec\ExecPushExpression.cs" />
    6266    <Compile Include="Expressions\Exec\ExecExpandExpression.cs" />
    63     <Compile Include="Expressions\Exec\ExecPushExpression.cs" />
    64     <Compile Include="Expressions\Exec\ExecDefineExpression.cs" />
    65     <Compile Include="Expressions\Exec\ExecPopExpression.cs" />
     67    <Compile Include="Expressions\ExpressionFactory.cs" />
    6668    <Compile Include="Expressions\Expression.cs" />
    67     <Compile Include="ExpressionCreatorTable.cs" />
    68     <Compile Include="Expressions\Float\FloatAddExpression.cs" />
    6969    <Compile Include="Expressions\Float\FloatPushExpression.cs" />
    70     <Compile Include="Expressions\Float\FloatMultiplyExpression.cs" />
    71     <Compile Include="Expressions\Float\FloatDivideExpression.cs" />
    72     <Compile Include="Expressions\Float\FloatSubtractExpression.cs" />
    73     <Compile Include="Expressions\Float\FloatDuplicateExpression.cs" />
    74     <Compile Include="Expressions\Float\FloatMaxExpression.cs" />
    75     <Compile Include="Expressions\Float\FloatMinExpression.cs" />
    76     <Compile Include="Expressions\Float\FloatEqualsExpression.cs" />
    77     <Compile Include="Expressions\Float\FloatGreaterThanExpression.cs" />
    78     <Compile Include="Expressions\Float\FloatSmallerThanExpression.cs" />
    79     <Compile Include="Expressions\Float\FloatDefineExpression.cs" />
    80     <Compile Include="Expressions\Float\FloatPopExpression.cs" />
    81     <Compile Include="Expressions\Integer\IntegerPopExpression.cs" />
    82     <Compile Include="Expressions\Integer\IntegerMinExpression.cs" />
    83     <Compile Include="Expressions\Integer\IntegerMaxExpression.cs" />
    84     <Compile Include="Expressions\Integer\IntegerDivideExpression.cs" />
    85     <Compile Include="Expressions\Integer\IntegerDuplicateExpression.cs" />
    86     <Compile Include="Expressions\Integer\IntegerEqualsExpression.cs" />
    87     <Compile Include="Expressions\Integer\IntegerSmallerThanExpression.cs" />
    88     <Compile Include="Expressions\Integer\IntegerGreaterThanExpression.cs" />
    89     <Compile Include="Expressions\Integer\IntegerDefineExpression.cs" />
    90     <Compile Include="Expressions\Integer\IntegerSubtractExpression.cs" />
    91     <Compile Include="Expressions\Integer\IntegerAddExpression.cs" />
    92     <Compile Include="Expressions\Integer\IntegerMultiplyExpression.cs" />
    9370    <Compile Include="Expressions\Integer\IntegerPushExpression.cs" />
    94     <Compile Include="Expressions\Name\NamePopExpression.cs" />
    95     <Compile Include="Expressions\Name\NamePushExpression.cs" />
    96     <Compile Include="Expressions\Name\NameDuplicateExpression.cs" />
    9771    <Compile Include="Expressions\Name\NameDefineXExecExpression.cs" />
     72    <Compile Include="Expressions\Templates\DefineExpressionTemplate.cs" />
     73    <Compile Include="Expressions\Templates\DuplicateExpressionTemplate.cs" />
     74    <Compile Include="Expressions\Templates\ExpressionTemplate.cs" />
     75    <Compile Include="Expressions\Templates\PopExpressionTemplate.cs" />
     76    <Compile Include="Expressions\Templates\PushResultExpressionTemplate.cs" />
     77    <Compile Include="Interpreter\Configuration.cs" />
    9878    <Compile Include="Generators\CodeGenerator.cs" />
    9979    <Compile Include="Generators\Configuration.cs" />
    100     <Compile Include="InstructionSets\ExecInstructionSet.cs" />
    101     <Compile Include="InstructionSets\CodeInstructionSet.cs" />
    102     <Compile Include="InstructionSets\FloatInstructionSet.cs" />
    103     <Compile Include="InstructionSets\NameInstructionSet.cs" />
    104     <Compile Include="InstructionSets\IntegerInstructionSet.cs" />
    105     <Compile Include="InstructionSets\CommonInstructionSet.cs" />
    106     <Compile Include="InstructionSets\BooleanInstructionSet.cs" />
    10780    <Compile Include="OpCode.cs" />
    10881    <Compile Include="OpCodeExtensions.cs" />
    109     <Compile Include="OperationTable.cs" />
    11082    <Compile Include="Parser\Parser.cs" />
    111     <Compile Include="StaticRandom.cs" />
    11283    <Compile Include="Stack\IStack.cs" />
    11384    <Compile Include="Interpreter\Interpreter.cs" />
    11485    <Compile Include="Properties\AssemblyInfo.cs" />
    115     <Compile Include="Stack\IInterpreterService.cs" />
     86    <Compile Include="Interpreter\IInterpreter.cs" />
    11687    <Compile Include="Stack\PushGPStack.cs" />
    11788    <Compile Include="Parser\SymbolTable.cs" />
     89    <Compile Include="Symbols.cs" />
    11890  </ItemGroup>
    11991  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Configuration.cs

    r14323 r14328  
    44namespace HeuristicLab.Algorithms.PushGP.Interpreter
    55{
    6     public class Configuration
     6    public class Configuration : Generators.Configuration
    77    {
    8         public bool IsBooleanStackEnabled { get; set; } = true;
    9         public bool IsIntegerStackEnabled { get; set; } = true;
    10         public bool IsFloatStackEnabled { get; set; } = true;
    11         public bool IsCodeStackEnabled { get; set; } = true;
    12         public bool IsNameStackEnabled { get; set; } = true;
     8        public Configuration()
     9        {
     10            this.IsBooleanStackEnabled = true;
     11            this.IsIntegerStackEnabled = true;
     12            this.IsFloatStackEnabled = true;
     13            this.IsCodeStackEnabled = true;
     14            this.IsNameStackEnabled = true;
    1315
    14         public IList<OpCode> AllowedInstructions { get; set; }
     16            this.EvalPushLimit = uint.MaxValue;
     17            this.MaxPointsInProgram = uint.MaxValue;
     18            this.MaxPointsInRandomExpression = uint.MaxValue;
     19
     20            this.TopLevelPushCode = true;
     21            this.TopLevelPopCode = true;
     22
     23            this.RandomSeedMax = 30081;
     24            this.RandomSeedMin = 0;
     25            this.RandomSeed = null;
     26        }
     27
     28        public bool IsBooleanStackEnabled { get; set; }
     29        public bool IsIntegerStackEnabled { get; set; }
     30        public bool IsFloatStackEnabled { get; set; }
     31        public bool IsCodeStackEnabled { get; set; }
     32        public bool IsNameStackEnabled { get; set; }
     33
     34        public IList<OpCode> AllowedInclassions { get; set; }
    1535
    1636        public IList<Expression> PredefinedExpressions { get; set; }
    1737
    1838        /// <summary>
    19         /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
    20         /// </summary>
    21         public long MinRandomInteger { get; set; } = long.MinValue;
    22 
    23         /// <summary>
    24         /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
    25         /// </summary>
    26         public long MaxRandomInteger { get; set; } = long.MaxValue;
    27 
    28         /// <summary>
    29         /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
    30         /// </summary>
    31         public double MinRandomFloag { get; set; } = double.MinValue;
    32 
    33         /// <summary>
    34         /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
    35         /// </summary>
    36         public double MaxRandomFloag { get; set; } = double.MaxValue;
    37 
    38         /// <summary>
    3939        /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
    40         /// The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
     40        /// The execution of a single Push inclassion counts as one execution, as does the processing of a single literal,
    4141        /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
    4242        /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
     
    4444        /// is up to the calling program.
    4545        /// </summary>
    46         public uint EvalPushLimit { get; set; } = uint.MaxValue;
     46        public uint EvalPushLimit { get; set; }
    4747
    4848        /// <summary>
    4949        /// This is the maximum size of an item on the CODE stack, expressed as a number of points.
    50         /// A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be exceeded
    51         /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the instruction.
     50        /// A point is an inclassion, a literal, or a pair of parentheses. Any inclassion that would cause this limit to be exceeded
     51        /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the inclassion.
    5252        /// </summary>
    53         public uint MaxPointsInProgram { get; set; } = uint.MaxValue;
     53        public uint MaxPointsInProgram { get; set; }
    5454
    5555        /// <summary>
    56         /// The maximum number of points in an expression produced by the CODE.RAND instruction.
     56        /// The maximum number of points in an expression produced by the CODE.RAND inclassion.
    5757        /// </summary>
    58         public uint MaxPointsInRandomExpression { get; set; } = uint.MaxValue;
     58        public uint MaxPointsInRandomExpression { get; set; }
    5959
    6060        /// <summary>
     
    6262        /// will be pushed onto the CODE stack prior to execution.
    6363        /// </summary>
    64         public bool TopLevelPushCode { get; set; } = true;
     64        public bool TopLevelPushCode { get; set; }
    6565
    6666        /// <summary>
    6767        /// When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.
    6868        /// </summary>
    69         public bool TopLevelPopCode { get; set; } = true;
     69        public bool TopLevelPopCode { get; set; }
    7070
    71         /// <summary>
    72         /// The probability that the selection of the ephemeral
    73         /// random NAME constant for inclusion in randomly generated code will produce a new name
    74         /// (rather than a name that was previously generated).
    75         /// </summary>
    76         public double NewErcNameProbability { get; set; } = 0.5;
     71        public ushort RandomSeedMax { get; set; }
     72        public ushort RandomSeedMin { get; set; }
    7773
    78         public ushort RandomSeedMax { get; set; } = 30081;
    79         public ushort RandomSeedMin { get; set; } = 0;
    80 
    81         public ushort? RandomSeed { get; set; } = null;
     74        public ushort? RandomSeed { get; set; }
    8275    }
    8376}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Interpreter.cs

    r14323 r14328  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Threading;
    34using System.Threading.Tasks;
     
    78namespace HeuristicLab.Algorithms.PushGP.Interpreter
    89{
    9     public class PushGPInterpreter : IInterpreterService
     10    public class PushGPInterpreter : IInterpreter
    1011    {
    11         private static Parser parser = new Parser();
    1212        private Task currentTask;
    1313
     
    1616            this.Configuration = config ?? new Configuration();
    1717
    18             this.CodeStack = new PushGPStack<Expression>();
    19             this.ExecStack = new PushGPStack<Expression>();
    20             this.NameStack = new PushGPStack<string>();
    21             this.BooleanStack = new PushGPStack<bool>();
    22             this.IntegerStack = new PushGPStack<long>();
    23             this.FloatStack = new PushGPStack<double>();
    24         }
    25 
    26         public Configuration Configuration { get; }
    27 
    28         public bool IsPaused { get; private set; } = false;
    29         public bool IsAborted { get; private set; } = false;
    30         public bool IsRunning => this.ExecStack.Count > 0 && !IsPaused && !IsAborted;
    31         public bool IsCompleted => this.ExecStack.Count == 0;
    32         public bool CanStep => !IsCompleted && !IsAborted && IsPaused;
    33 
    34         public IStack<Expression> CodeStack { get; }
    35         public IStack<Expression> ExecStack { get; }
    36         public IStack<string> NameStack { get; }
    37         public IStack<bool> BooleanStack { get; }
    38         public IStack<long> IntegerStack { get; }
    39         public IStack<double> FloatStack { get; }
    40 
    41         public IDictionary<string, Expression> CustomExpressions { get; } = new Dictionary<string, Expression>();
     18            this.CodeStack = new PushGPStack<Expression>(128);
     19            this.ExecStack = new PushGPStack<Expression>(128);
     20            this.NameStack = new PushGPStack<string>(16);
     21            this.BooleanStack = new PushGPStack<bool>(16);
     22            this.IntegerStack = new PushGPStack<long>(16);
     23            this.FloatStack = new PushGPStack<double>(16);
     24
     25            this.CustomExpressions = new Dictionary<string, Expression>();
     26        }
     27
     28        public Configuration Configuration { get; private set; }
     29
     30        public bool IsPaused { get; private set; }
     31        public bool IsAborted { get; private set; }
     32        public bool IsRunning { get { return this.ExecStack.Count > 0 && !IsPaused && !IsAborted; } }
     33        public bool IsCompleted { get { return this.ExecStack.Count == 0; } }
     34        public bool CanStep { get { return !IsCompleted && !IsAborted && IsPaused; } }
     35
     36        public IStack<Expression> CodeStack { get; private set; }
     37        public IStack<Expression> ExecStack { get; private set; }
     38        public IStack<string> NameStack { get; private set; }
     39        public IStack<bool> BooleanStack { get; private set; }
     40        public IStack<long> IntegerStack { get; private set; }
     41        public IStack<double> FloatStack { get; private set; }
     42
     43        public IDictionary<string, Expression> CustomExpressions { get; private set; }
    4244
    4345        public void Reset()
     
    4749            this.currentTask = null;
    4850
     51            this.Clear();
     52        }
     53
     54        public void Clear()
     55        {
    4956            this.ExecStack.Clear();
    5057            this.CodeStack.Clear();
     
    5360            this.IntegerStack.Clear();
    5461            this.FloatStack.Clear();
    55         }
    56 
    57         public async void AbortAndReset()
     62
     63            this.CustomExpressions.Clear();
     64        }
     65
     66        public void Interpret(string code)
     67        {
     68            var program = Encode(code);
     69            this.Interpret(program);
     70        }
     71
     72        public Task InterpretAsync(string code, bool paused = false, CancellationToken token = default(CancellationToken))
     73        {
     74            var program = Encode(code);
     75
     76            this.currentTask = this.InterpretAsync(program, paused, token);
     77
     78            return currentTask;
     79        }
     80
     81        public Task InterpretAsync(Expression program, bool paused = false, CancellationToken token = default(CancellationToken))
     82        {
     83            this.IsPaused = paused;
     84            this.currentTask = Task.Run(() => this.Interpret(program), token);
     85
     86            return currentTask;
     87        }
     88
     89        public void Interpret(Expression program)
     90        {
     91            this.IsAborted = false;
     92
     93            /* Push top expression so the loop is able to enter
     94             * If the top expression is a single statement then the loop has nothing to do
     95             * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */
     96            this.ExecStack.Push(program);
     97            this.CodeStack.Push(program);
     98
     99            // run top expression
     100            this.ExecStack.Pop().Eval(this);
     101
     102            this.Interpret();
     103        }
     104
     105        private void Interpret()
     106        {
     107            if (!IsRunning)
     108            {
     109                return;
     110            }
     111
     112            while (IsRunning)
     113            {
     114                this.DoStep();
     115            }
     116
     117            if (this.Configuration.TopLevelPushCode)
     118            {
     119                this.CodeStack.Pop();
     120            }
     121        }
     122
     123        private void DoStep()
     124        {
     125            var expression = this.ExecStack.Pop();
     126
     127            if (!expression.IsCodeOp)
     128            {
     129                this.CodeStack.Push(expression);
     130            }
     131
     132            expression.Eval(this);
     133
     134            if (!expression.IsCodeOp)
     135            {
     136                CodeStack.Pop();
     137            }
     138        }
     139
     140        private Task InterpreteAsync()
     141        {
     142            this.currentTask = Task.Run(() => this.Interpret());
     143
     144            return this.currentTask;
     145        }
     146
     147        public async Task AbortAsync()
     148        {
     149            if (this.IsAborted || this.IsCompleted)
     150                return;
     151
     152            this.IsAborted = true;
     153
     154            if (this.currentTask != null)
     155            {
     156                await this.currentTask;
     157            }
     158        }
     159
     160        public async Task AbortAndReset()
    58161        {
    59162            await this.AbortAsync();
     
    61164        }
    62165
    63         public void Interprete(string code)
    64         {
    65             var program = Encode(code);
    66             this.Interprete(program);
    67         }
    68 
    69         public Task InterpreteAsync(string code, CancellationToken token = default(CancellationToken))
    70         {
    71             var program = Encode(code);
    72 
    73             this.currentTask = this.InterpreteAsync(program, token);
    74 
    75             return currentTask;
    76         }
    77 
    78         public Task InterpreteAsync(Expression program, CancellationToken token = default(CancellationToken))
    79         {
    80             this.currentTask = Task.Run(() => this.Interprete(program), token);
    81 
    82             return currentTask;
    83         }
    84 
    85         public void Interprete(Expression program)
    86         {
    87             this.ExecStack.Push(program);
    88             this.CodeStack.Push(program);
    89 
    90             // run top expression
    91             this.ExecStack.Pop().Eval(this);
    92 
    93             this.Interprete();
    94         }
    95 
    96         private void Interprete()
    97         {
    98             while (IsRunning)
    99             {
    100                 this.DoStep();
    101             }
    102 
    103             if (this.Configuration.TopLevelPushCode)
    104             {
    105                 this.CodeStack.Pop();
    106             }
    107         }
    108 
    109         private void DoStep()
    110         {
    111             var expression = this.ExecStack.Pop();
    112             var isNotCodeOp = !expression.OpCode.IsCodeOp();
    113 
    114             if (isNotCodeOp)
    115             {
    116                 this.CodeStack.Push(expression);
    117             }
    118 
    119             expression.Eval(this);
    120 
    121             if (isNotCodeOp)
    122             {
    123                 CodeStack.Pop();
    124             }
    125         }
    126 
    127         private Task InterpreteAsync()
    128         {
    129             this.currentTask = Task.Run(() =>
    130             {
    131                 this.Interprete();
    132             });
    133 
    134             return this.currentTask;
    135         }
    136 
    137         public async Task AbortAsync()
    138         {
    139             if (this.IsAborted || this.IsCompleted)
    140                 return;
    141 
    142             this.IsAborted = true;
    143 
    144             if (this.currentTask != null)
    145             {
    146                 await this.currentTask;
    147             }
    148         }
    149 
    150166        public async Task PauseAsync()
    151167        {
     
    163179        public Task ResumeAsync()
    164180        {
    165             if (this.IsPaused)
     181            if (this.IsPaused || !this.IsAborted)
    166182            {
    167183                this.IsPaused = false;
     
    177193            {
    178194                DoStep();
     195
     196                if (IsCompleted && this.Configuration.TopLevelPopCode)
     197                {
     198                    this.CodeStack.Pop();
     199                }
    179200            }
    180201        }
     
    182203        public static Expression Encode(string code)
    183204        {
    184             return parser.Parse(code);
     205            return Parser.Parse(code);
    185206        }
    186207
     
    212233        private void PrintStack<T>(string name, IStack<T> stack)
    213234        {
    214             System.Console.WriteLine($"--- {name} ---\n{stack}\n---------------------------------------------------");
     235            Console.WriteLine(string.Format(
     236                "--- {0} ---\n{1}\n---------------------------------------------------",
     237                name,
     238                stack));
    215239        }
    216240    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/OpCode.cs

    r14320 r14328  
    1818        /// <summary>
    1919        /// BOOLEAN.DEFINE:
    20         /// Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN stack onto the EXEC stack.
     20        /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the BOOLEAN stack onto the EXEC stack.
    2121        /// </summary>
    2222        BooleanDefine,
     
    2424        /// <summary>
    2525        /// BOOLEAN.DUP:
    26         /// Duplicates the top item on the BOOLEAN stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!)
    27         /// </summary>
    28         BooleanDup,
     26        /// Duplicatelicates the top item on the BOOLEAN stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!)
     27        /// </summary>
     28        BooleanDuplicate,
    2929
    3030        /// <summary>
     
    109109        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    110110        /// </summary>
    111         BooleanYankDup,
     111        BooleanYankDuplicate,
    112112        #endregion Boolean
    113113
     
    123123        /// <summary>
    124124        /// CODE.APPEND:
    125         /// Pushes the result of appending the top two pieces of code. If one of the pieces of code is a single instruction or literal (that is, something not surrounded by parentheses) then it is surrounded by parentheses first.
     125        /// Pushes the result of appending the top two pieces of code. If one of the pieces of code is a single inclassion or literal (that is, something not surrounded by parentheses) then it is surrounded by parentheses first.
    126126        /// </summary>
    127127        CodeAppend,
     
    129129        /// <summary>
    130130        /// CODE.ATOM:
    131         /// Pushes TRUE onto the BOOLEAN stack if the top piece of code is a single instruction or a literal, and FALSE otherwise (that is, if it is something surrounded by parentheses).
     131        /// Pushes TRUE onto the BOOLEAN stack if the top piece of code is a single inclassion or a literal, and FALSE otherwise (that is, if it is something surrounded by parentheses).
    132132        /// </summary>
    133133        CodeAtom,
     
    165165        /// <summary>
    166166        /// CODE.DEFINE:
    167         /// Defines the name on top of the NAME stack as an instruction that will push the top item of the CODE stack onto the EXEC stack.
     167        /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the CODE stack onto the EXEC stack.
    168168        /// </summary>
    169169        CodeDefine,
     
    171171        /// <summary>
    172172        /// CODE.DEFINITION:
    173         /// Pushes the definition associated with the top NAME on the NAME stack (if any) onto the CODE stack. This extracts the definition for inspection/manipulation, rather than for immediate execution (although it may then be executed with a call to CODE.DO or a similar instruction).
     173        /// Pushes the definition associated with the top NAME on the NAME stack (if any) onto the CODE stack. This extracts the definition for inspection/manipulation, rather than for immediate execution (although it may then be executed with a call to CODE.DO or a similar inclassion).
    174174        /// </summary>
    175175        CodeDefinition,
     
    178178        /// CODE.DESCREPANCY:
    179179        /// Pushes a measure of the discrepancy between the top two CODE stack items onto the INTEGER stack. This will be zero if the top two items are equivalent, and will be higher the 'more different' the items are from one another. The calculation is as follows:
    180         /// 1. Construct a list of all of the unique items in both of the lists (where uniqueness is determined by equalp). Sub-lists and atoms all count as items.
     180        /// 1. Conclass a list of all of the unique items in both of the lists (where uniqueness is determined by equalp). Sub-lists and atoms all count as items.
    181181        /// 2. Initialize the result to zero.
    182182        /// 3. For each unique item increment the result by the difference between the number of occurrences of the item in the two pieces of code.
     
    199199        /// <summary>
    200200        /// CODE.DO*COUNT:
    201         /// An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
     201        /// An iteration inclassion that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
    202202        /// ( 0 '1 - IntegerArg' CODE.QUOTE <CodeArg> CODE.DO*RANGE )
    203203        /// </summary>
     
    206206        /// <summary>
    207207        /// CODE.DO*RANGE:
    208         /// An iteration instruction that executes the top item on the CODE stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to CODE.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.
     208        /// An iteration inclassion that executes the top item on the CODE stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to CODE.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.
    209209        /// </summary>
    210210        CodeDoXRange,
     
    218218        /// <summary>
    219219        /// CODE.DUP:
    220         /// Duplicates the top item on the CODE stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
    221         /// </summary>
    222         CodeDup,
     220        /// Duplicatelicates the top item on the CODE stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
     221        /// </summary>
     222        CodeDuplicate,
    223223
    224224        /// <summary>
    225225        /// CODE.EXTRACT:
    226         /// Pushes the sub-expression of the top item of the CODE stack that is indexed by the top item of the INTEGER stack. The indexing here counts "points," where each parenthesized expression and each literal/instruction is considered a point, and it proceeds in depth first order. The entire piece of code is at index 0; if it is a list then the first item in the list is at index 1, etc. The integer used as the index is taken modulo the number of points in the overall expression (and its absolute value is taken in case it is negative) to ensure that it is within the meaningful range.
     226        /// Pushes the sub-expression of the top item of the CODE stack that is indexed by the top item of the INTEGER stack. The indexing here counts "points," where each parenthesized expression and each literal/inclassion is considered a point, and it proceeds in depth first order. The entire piece of code is at index 0; if it is a list then the first item in the list is at index 1, etc. The integer used as the index is taken modulo the number of points in the overall expression (and its absolute value is taken in case it is negative) to ensure that it is within the meaningful range.
    227227        /// </summary>
    228228        CodeExtract,
     
    265265
    266266        /// <summary>
     267        /// CODE.INSTRUCTIONS:
     268        /// Pushes a list of all active instructions in the interpreter's current configuration.
     269        /// </summary>
     270        CodeInstructions,
     271
     272        /// <summary>
    267273        /// CODE.INSERT:
    268274        /// Pushes the result of inserting the second item of the CODE stack into the first item, at the position indexed by the top item of the INTEGER stack (and replacing whatever was there formerly). The indexing is computed as in CODE.EXTRACT.
     
    272278        /// <summary>
    273279        /// CODE.INSTRUCTIONS:
    274         /// Pushes a list of all active instructions in the interpreter's current configuration.
    275         /// </summary>
    276         CodeInstructions,
     280        /// Pushes a list of all active inclassions in the interpreter's current configuration.
     281        /// </summary>
     282        CodeInclassions,
    277283
    278284        /// <summary>
     
    311317
    312318        /// <summary>
     319        /// CODE.NULL: Pushes TRUE onto the BOOLEAN stack if the top item of the CODE stack is an empty list, or FALSE otherwise.
     320        /// </summary>
     321        CodeNull,
     322
     323        /// <summary>
     324        /// CODE.POSITION:
     325        /// Pushes onto the INTEGER stack the position of the second item on the CODE stack within the first item (which is coerced to a list if necessary). Pushes -1 if no match is found.
     326        /// </summary>
     327        CodePosition,
     328
     329        /// <summary>
    313330        /// CODE.QUOTE:
    314331        /// Specifies that the next expression submitted for execution will instead be pushed literally onto the CODE stack. This can be implemented by moving the top item on the EXEC stack onto the CODE stack.
     
    334351
    335352        /// <summary>
    336         /// CODE.SIZE: Pushes the number of "points" in the top piece of CODE onto the INTEGER stack. Each instruction, literal, and pair of parentheses counts as a point.
     353        /// CODE.SIZE: Pushes the number of "points" in the top piece of CODE onto the INTEGER stack. Each inclassion, literal, and pair of parentheses counts as a point.
    337354        /// </summary>
    338355        CodeSize,
     
    364381        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    365382        /// </summary>
    366         CodeYankDup,
     383        CodeYankDuplicate,
    367384        #endregion Code
    368385
     
    380397        /// <summary>
    381398        /// EXEC.DEFINE:
    382         /// Defines the name on top of the NAME stack as an instruction that will push the top item of the EXEC stack back onto the EXEC stack.
     399        /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the EXEC stack back onto the EXEC stack.
    383400        /// </summary>
    384401        ExecDefine,
     
    386403        /// <summary>
    387404        /// EXEC.DO*COUNT:
    388         /// An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
     405        /// An iteration inclassion that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
    389406        /// ( 0 <1 - IntegerArg> EXEC.DO*RANGE <ExecArg> )
    390407        /// </summary>
     
    393410        /// <summary>
    394411        /// EXEC.DO*RANGE:
    395         /// An iteration instruction that executes the top item on the EXEC stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to EXEC.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.
     412        /// An iteration inclassion that executes the top item on the EXEC stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to EXEC.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.
    396413        /// </summary>
    397414        ExecDoXRange,
     
    405422        /// <summary>
    406423        /// EXEC.DUP:
    407         /// Duplicates the top item on the EXEC stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). This may be thought of as a "DO TWICE" instruction.
    408         /// </summary>
    409         ExecDup,
     424        /// Duplicatelicates the top item on the EXEC stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). This may be thought of as a "DO TWICE" inclassion.
     425        /// </summary>
     426        ExecDuplicate,
    410427
    411428        /// <summary>
    412429        /// EXEC.FLUSH:
    413         /// Empties the EXEC stack. This may be thought of as a "HALT" instruction.
     430        /// Empties the EXEC stack. This may be thought of as a "HALT" inclassion.
    414431        /// </summary>
    415432        ExecFlush,
     
    428445
    429446        /// <summary>
    430         /// EXEC.POP: Pops the EXEC stack. This may be thought of as a "DONT" instruction.
     447        /// EXEC.POP: Pops the EXEC stack. This may be thought of as a "DONT" inclassion.
    431448        /// </summary>
    432449        ExecPop,
     
    439456
    440457        /// <summary>
     458        /// EXEC.S:
     459        /// The Push implementation of the "S combinator". Pops 3 items from the EXEC stack, which we will call A, B, and C (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by another instance of C, followed by another instance of A.
     460        /// </summary>
     461        ExecS,
     462
     463        /// <summary>
    441464        /// EXEC.SHOVE:
    442         /// Inserts the top EXEC item "deep" in the stack, at the position indexed by the top INTEGER. This may be thought of as a "DO LATER" instruction.
     465        /// Inserts the top EXEC item "deep" in the stack, at the position indexed by the top INTEGER. This may be thought of as a "DO LATER" inclassion.
    443466        /// </summary>
    444467        ExecShove,
     
    464487        /// <summary>
    465488        /// EXEC.YANK:
    466         /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack. This may be thought of as a "DO SOONER" instruction.
     489        /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack. This may be thought of as a "DO SOONER" inclassion.
    467490        /// </summary>
    468491        ExecYank,
     
    472495        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    473496        /// </summary>
    474         ExecYankDup,
     497        ExecYankDuplicate,
    475498        #endregion Exec
    476499
     
    533556        /// <summary>
    534557        /// FLOAT.DEFINE:
    535         /// Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT stack onto the EXEC stack.
     558        /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the FLOAT stack onto the EXEC stack.
    536559        /// </summary>
    537560        FloatDefine,
     
    539562        /// <summary>
    540563        /// FLOAT.DUP:
    541         /// Duplicates the top item on the FLOAT stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
    542         /// </summary>
    543         FloatDup,
     564        /// Duplicatelicates the top item on the FLOAT stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
     565        /// </summary>
     566        FloatDuplicate,
    544567
    545568        /// <summary>
     
    636659        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    637660        /// </summary>
    638         FloatYankDup,
     661        FloatYankDuplicate,
    639662
    640663        #endregion Float
     
    692715
    693716        /// <summary>
    694         /// INTEGER.COS:
    695         /// Pushes the cosine of the top item.
    696         /// </summary>
    697         IntegerCos,
    698 
    699         /// <summary>
    700717        /// INTEGER.DEFINE:
    701         /// Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER stack onto the EXEC stack.
     718        /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the INTEGER stack onto the EXEC stack.
    702719        /// </summary>
    703720        IntegerDefine,
     
    705722        /// <summary>
    706723        /// INTEGER.DUP:
    707         /// Duplicates the top item on the INTEGER stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
    708         /// </summary>
    709         IntegerDup,
     724        /// Duplicatelicates the top item on the INTEGER stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
     725        /// </summary>
     726        IntegerDuplicate,
    710727
    711728        /// <summary>
     
    769786
    770787        /// <summary>
    771         /// INTEGER.SIN:
    772         /// Pushes the sine of the top item.
    773         /// </summary>
    774         IntegerSin,
    775 
    776         /// <summary>
    777788        /// INTEGER.STACKDEPTH:
    778789        /// Pushes the stack depth onto the INTEGER stack.
     
    787798
    788799        /// <summary>
    789         /// INTEGER.TAN:
    790         /// Pushes the tangent of the top item.
    791         /// </summary>
    792         IntegerTan,
    793 
    794         /// <summary>
    795800        /// INTEGER.YANK:
    796801        /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack.
     
    802807        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    803808        /// </summary>
    804         IntegerYankDup,
     809        IntegerYankDuplicate,
    805810
    806811        #endregion Integer
     
    823828        /// <summary>
    824829        /// NAME.DUP:
    825         /// Duplicates the top item on the NAME stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
    826         /// </summary>
    827         NameDup,
     830        /// Duplicatelicates the top item on the NAME stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).
     831        /// </summary>
     832        NameDuplicate,
    828833
    829834        /// <summary>
     
    890895        /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack.
    891896        /// </summary>
    892         NameYankDup,
     897        NameYankDuplicate,
    893898        #endregion Name
    894899    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/OpCodeExtensions.cs

    r14323 r14328  
    66    public static class OpCodeExtensions
    77    {
    8         public readonly static byte Min = 0;
     8        public const byte Min = 0;
    99        public readonly static byte Max = Enum.GetValues(typeof(OpCode)).Cast<byte>().Last();
    10 
    11         public static bool IsCodeOp(this OpCode opCode)
    12         {
    13             return opCode >= OpCode.CodeEquals && opCode < OpCode.CodeYankDup;
    14         }
    1510    }
    1611}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/Parser.cs

    r14323 r14328  
    22using System.Globalization;
    33using HeuristicLab.Algorithms.PushGP.Expressions;
     4using HeuristicLab.Algorithms.PushGP.Expressions.Boolean;
     5using HeuristicLab.Algorithms.PushGP.Expressions.Code;
     6using HeuristicLab.Algorithms.PushGP.Expressions.Exec;
     7using HeuristicLab.Algorithms.PushGP.Expressions.Float;
     8using HeuristicLab.Algorithms.PushGP.Expressions.Integer;
     9using HeuristicLab.Algorithms.PushGP.Expressions.Name;
    410
    511namespace HeuristicLab.Algorithms.PushGP
    612{
    7     public class Parser
     13    public static class Parser
    814    {
    9         private readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
     15        private static readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
    1016
    11         private const string OpenBrace = "(";
    12         private const string CloseBrace = ")";
    13         private readonly char[] symbolTrim = new[] { '\r', '\n' };
     17        private const string openBrace = "(";
     18        private const string closeBrace = ")";
     19        private const char delimiter = ' ';
     20        private static readonly char[] symbolTrim = new[] { '\r', '\n' };
    1421
    15         public Expression Parse(string source, int startIndex = 0)
     22        public static Expression Parse(string source, int startIndex = 0)
    1623        {
    17             var symbols = source.Split(' ');
     24            var symbols = source.Split(delimiter);
    1825
    1926            int endIndex;
    20             return this.Parse(symbols, 0, out endIndex);
     27            return Parse(symbols, 0, out endIndex);
    2128        }
    2229
    23         private Expression Parse(string[] symbols, int startIndex, out int endIndex)
     30        private static Expression Parse(string[] symbols, int startIndex, out int endIndex)
    2431        {
    2532            var expressions = new List<Expression>();
     
    2835            {
    2936                var symbol = symbols[i].TrimEnd(symbolTrim);
     37
    3038                if (string.IsNullOrWhiteSpace(symbol))
    3139                {
     
    3341                }
    3442
    35                 if (symbol == OpenBrace)
     43                if (symbol == openBrace)
    3644                {
    3745                    var subExpression = Parse(symbols, i + 1, out endIndex);
     
    4048                    continue;
    4149                }
    42                 else if (symbol == CloseBrace)
     50                else if (symbol == closeBrace)
    4351                {
    4452                    endIndex = i;
     
    4654                        ? new ExecExpandExpression(expressions.ToArray())
    4755                        : new CodeNoopExpression() as Expression;
    48                 }
    49 
    50                 if (string.IsNullOrWhiteSpace(symbol))
    51                 {
    52                     continue;
    5356                }
    5457
     
    6164                }
    6265
    63                 // instruction
     66                // expression
    6467                OpCode opCode;
    6568                if (SymbolTable.TryGetOpCode(symbol, out opCode))
    6669                {
    67                     expression = ExpressionCreatorTable.GetCreator(opCode).Invoke();
     70                    expression = ExpressionFactory.Create(opCode);
    6871                    expressions.Insert(0, expression);
    6972                    continue;
    7073                }
    7174
    72                 // identifier - custom instruction or named literals
     75                // identifier - custom expression or named literals
    7376                expressions.Insert(0, new NameDefineXExecExpression(symbol));
    7477            }
     
    8083        }
    8184
    82         private bool TryParseLiteral(string word, out Expression expression)
     85        private static bool TryParseLiteral(string word, out Expression expression)
    8386        {
    8487            long longValue;
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/SymbolTable.cs

    r14323 r14328  
    11using System;
    22using System.Collections.Generic;
    3 using System.Linq;
    43
    54namespace HeuristicLab.Algorithms.PushGP
     
    109        {
    1110            #region Code
    12             { "CODE.NOOP", OpCode.CodeNoop },
    13             { "CODE.PUSH", OpCode.CodePush },
    14             { "CODE.IF", OpCode.CodeIf },
    15             { "CODE.DEFINE", OpCode.CodeDefine },
    16             { "CODE.DUP", OpCode.CodeDup },
    17             { "CODE.QUOTE", OpCode.CodeQuote },
    18             { "CODE.DO", OpCode.CodeDo },
     11            { Symbols.CodeIf, OpCode.CodeIf },
     12            { Symbols.CodeDefine, OpCode.CodeDefine },
     13            { Symbols.CodeDuplicate, OpCode.CodeDuplicate },
     14            { Symbols.CodeQuote, OpCode.CodeQuote },
     15            { Symbols.CodeDo, OpCode.CodeDo },
     16            { Symbols.CodeEquals, OpCode.CodeEquals },
     17            { Symbols.CodeAppend, OpCode.CodeAppend },
     18            { Symbols.CodeAtom, OpCode.CodeAtom },
     19            { Symbols.CodeCar, OpCode.CodeCar },
     20            { Symbols.CodeCdr, OpCode.CodeCdr },
     21            { Symbols.CodeCons, OpCode.CodeCons },
     22            { Symbols.CodeContainer, OpCode.CodeContainer },
     23            { Symbols.CodeContains, OpCode.CodeContains },
     24            { Symbols.CodeDefinition, OpCode.CodeDefinition },
     25            { Symbols.CodeDiscrepancy, OpCode.CodeDiscrepancy },
     26            { Symbols.CodeDoX, OpCode.CodeDoX },
     27            { Symbols.CodeDoXCount, OpCode.CodeDoXCount },
     28            { Symbols.CodeDoXRange, OpCode.CodeDoXRange },
     29            { Symbols.CodeDoXTimes, OpCode.CodeDoXTimes },
     30            { Symbols.CodeExtract, OpCode.CodeExtract },
     31            { Symbols.CodeFlush, OpCode.CodeFlush },
     32            { Symbols.CodeFromFloat, OpCode.CodeFromFloat },
     33            { Symbols.CodeFromInteger, OpCode.CodeFromInteger },
     34            { Symbols.CodeFromName, OpCode.CodeFromName },
     35            { Symbols.CodeInsert, OpCode.CodeInsert },
     36            { Symbols.CodeInstructions, OpCode.CodeInstructions },
     37            { Symbols.CodeLength, OpCode.CodeLength },
     38            { Symbols.CodeList, OpCode.CodeList },
     39            { Symbols.CodeMember, OpCode.CodeMember },
     40            { Symbols.CodeNoop, OpCode.CodeNoop },
     41            { Symbols.CodeNth, OpCode.CodeNth },
     42            { Symbols.CodeNthCdr, OpCode.CodeNthCdr },
     43            { Symbols.CodeNull, OpCode.CodeNull },
     44            { Symbols.CodePosition, OpCode.CodePosition },
     45            { Symbols.CodeRand, OpCode.CodeRand },
     46            { Symbols.CodeRot, OpCode.CodeRot },
     47            { Symbols.CodeShove, OpCode.CodeShove },
     48            { Symbols.CodeSize, OpCode.CodeSize },
     49            { Symbols.CodeStackDepth, OpCode.CodeStackDepth },
     50            { Symbols.CodeSubst, OpCode.CodeSubst },
     51            { Symbols.CodeSwap, OpCode.CodeSwap },
     52            { Symbols.CodeYank, OpCode.CodeYank },
     53            { Symbols.CodeYankDuplicate, OpCode.CodeYankDuplicate },
    1954            #endregion Code
    2055
    2156            #region Exec
    22             { "EXEC.EXPAND", OpCode.ExecExpand },
    23             { "EXEC.PUSH", OpCode.ExecPush },
    24             { "EXEC.IF", OpCode.ExecIf },
    25             { "EXEC.DEFINE", OpCode.ExecDefine },
    26             { "EXEC.DUP", OpCode.ExecDup },
    27             { "EXEC.POP", OpCode.ExecPop },
    28             { "EXEC.Y", OpCode.ExecY },
    29             { "EXEC.DO*RANGE", OpCode.ExecDoXRange },
     57            { Symbols.ExecIf, OpCode.ExecIf },
     58            { Symbols.ExecDefine, OpCode.ExecDefine },
     59            { Symbols.ExecDuplicate, OpCode.ExecDuplicate },
     60            { Symbols.ExecPop, OpCode.ExecPop },
     61            { Symbols.ExecY, OpCode.ExecY },
     62            { Symbols.ExecDoXRange, OpCode.ExecDoXRange },
     63            { Symbols.ExecEquals, OpCode.ExecEquals },
     64            { Symbols.ExecDoXCount, OpCode.ExecDoXCount },
     65            { Symbols.ExecDoXTimes, OpCode.ExecDoXTimes },
     66            { Symbols.ExecFlush, OpCode.ExecFlush },
     67            { Symbols.ExecK, OpCode.ExecK },
     68            { Symbols.ExecRot, OpCode.ExecRot },
     69            { Symbols.ExecS, OpCode.ExecS },
     70            { Symbols.ExecShove, OpCode.ExecShove },
     71            { Symbols.ExecStackDepth, OpCode.ExecStackDepth },
     72            { Symbols.ExecSwap, OpCode.ExecSwap },
     73            { Symbols.ExecYank, OpCode.ExecYank },
     74            { Symbols.ExecYankDuplicate, OpCode.ExecYankDuplicate },
    3075            #endregion Exec
    3176
    3277            #region Name
    33             { "NAME.DEFINE*EXEC", OpCode.NameDefineXExec },
    34             { "NAME.POP", OpCode.NamePop },
    35             { "NAME.DUP", OpCode.NameDup },
     78            { Symbols.NamePop, OpCode.NamePop },
     79            { Symbols.NameDuplicate, OpCode.NameDuplicate },
     80            { Symbols.NameEquals, OpCode.NameEquals },
     81            { Symbols.NameFlush, OpCode.NameFlush },
     82            { Symbols.NameQuote, OpCode.NameQuote },
     83            { Symbols.NameRand, OpCode.NameRand },
     84            { Symbols.NameRandBoundName, OpCode.NameRand },
     85            { Symbols.NameRot, OpCode.NameRot },
     86            { Symbols.NameShove, OpCode.NameShove },
     87            { Symbols.NameStackDepth, OpCode.NameStackDepth },
     88            { Symbols.NameSwap, OpCode.NameSwap },
     89            { Symbols.NameYank, OpCode.NameYank },
     90            { Symbols.NameYankDuplicate, OpCode.NameYankDuplicate },
    3691            #endregion Name
    3792
    3893            #region Boolean
    39             { "BOOLEAN.PUSH", OpCode.BooleanPush },
    40             { "BOOLEAN.POP", OpCode.BooleanPop },
    41             { "BOOLEAN.DUP", OpCode.BooleanDup },
    42             { "BOOLEAN.AND", OpCode.BooleanAnd },
    43             { "BOOLEAN.OR", OpCode.BooleanOr },
     94            { Symbols.BooleanPop, OpCode.BooleanPop },
     95            { Symbols.BooleanDuplicate, OpCode.BooleanDuplicate },
     96            { Symbols.BooleanAnd, OpCode.BooleanAnd },
     97            { Symbols.BooleanOr, OpCode.BooleanOr },
     98            { Symbols.BooleanEquals, OpCode.BooleanEquals },
     99            { Symbols.BooleanFlush, OpCode.BooleanFlush },
     100            { Symbols.BooleanFromFloat, OpCode.BooleanFromFloat },
     101            { Symbols.BooleanFromInteger, OpCode.BooleanFromInteger },
     102            { Symbols.BooleanNot, OpCode.BooleanNot },
     103            { Symbols.BooleanRand, OpCode.BooleanRand },
     104            { Symbols.BooleanRot, OpCode.BooleanRot },
     105            { Symbols.BooleanShove, OpCode.BooleanShove },
     106            { Symbols.BooleanStackDepth, OpCode.BooleanStackDepth },
     107            { Symbols.BooleanSwap, OpCode.BooleanSwap },
     108            { Symbols.BooleanYank, OpCode.BooleanYank },
     109            { Symbols.BooleanYankDuplicate, OpCode.BooleanYankDuplicate },
    44110            #endregion Boolean
    45111
    46112            #region Integer
    47             { "INTEGER.PUSH", OpCode.IntegerPush },
    48             { "INTEGER.POP", OpCode.IntegerPop },
    49             { "INTEGER.DUP", OpCode.IntegerDup },
    50             { "INTEGER.DEFINE", OpCode.IntegerDefine },
    51             { "INTEGER.+", OpCode.IntegerAdd },
    52             { "INTEGER.-", OpCode.IntegerSubtract },
    53             { "INTEGER.*", OpCode.IntegerMultiply },
    54             { "INTEGER./", OpCode.IntegerDivide },
    55             { "INTEGER.<", OpCode.IntegerSmallerThan },
    56             { "INTEGER.>", OpCode.IntegerGreaterThan },
    57             { "INTEGER.=", OpCode.IntegerEquals },
    58             { "INTEGER.MIN", OpCode.IntegerMin },
    59             { "INTEGER.MAX", OpCode.IntegerMax },
     113            { Symbols.IntegerPop, OpCode.IntegerPop },
     114            { Symbols.IntegerDuplicate, OpCode.IntegerDuplicate },
     115            { Symbols.IntegerDefine, OpCode.IntegerDefine },
     116            { Symbols.IntegerAdd, OpCode.IntegerAdd },
     117            { Symbols.IntegerSubtract, OpCode.IntegerSubtract },
     118            { Symbols.IntegerMultiply, OpCode.IntegerMultiply },
     119            { Symbols.IntegerDivide, OpCode.IntegerDivide },
     120            { Symbols.IntegerSmallerThan, OpCode.IntegerSmallerThan },
     121            { Symbols.IntegerGreaterThan, OpCode.IntegerGreaterThan },
     122            { Symbols.IntegerEquals, OpCode.IntegerEquals },
     123            { Symbols.IntegerMin, OpCode.IntegerMin },
     124            { Symbols.IntegerMax, OpCode.IntegerMax },
     125            { Symbols.IntegerModulo, OpCode.IntegerModulo },
     126            { Symbols.IntegerFlush, OpCode.IntegerFlush },
     127            { Symbols.IntegerFromBoolean, OpCode.IntegerFromBoolean },
     128            { Symbols.IntegerFromFloat, OpCode.IntegerFromFloat },
     129            { Symbols.IntegerRand, OpCode.IntegerRand },
     130            { Symbols.IntegerRot, OpCode.IntegerRot },
     131            { Symbols.IntegerShove, OpCode.IntegerShove },
     132            { Symbols.IntegerStackDepth, OpCode.IntegerStackDepth },
     133            { Symbols.IntegerSwap, OpCode.IntegerSwap },
     134            { Symbols.IntegerYank, OpCode.IntegerYank },
     135            { Symbols.IntegerYankDuplicate, OpCode.IntegerYankDuplicate },
    60136            #endregion Integer
    61137
    62138            #region Float
    63             { "FLOAT.PUSH", OpCode.FloatPush },
    64             { "FLOAT.POP", OpCode.FloatPop },
    65             { "FLOAT.DUP", OpCode.FloatDup },
    66             { "FLOAT.DEFINE", OpCode.FloatDefine },
    67             { "FLOAT.+", OpCode.FloatAdd },
    68             { "FLOAT.-", OpCode.FloatSubtract },
    69             { "FLOAT.*", OpCode.FloatMultiply },
    70             { "FLOAT./", OpCode.FloatDivide },
    71             { "FLOAT.<", OpCode.FloatSmallerThan },
    72             { "FLOAT.>", OpCode.FloatGreaterThan },
    73             { "FLOAT.=", OpCode.FloatEquals },
    74             { "FLOAT.MIN", OpCode.FloatMin },
    75             { "FLOAT.MAX", OpCode.FloatMax },
     139            { Symbols.FloatPop, OpCode.FloatPop },
     140            { Symbols.FloatDuplicate, OpCode.FloatDuplicate },
     141            { Symbols.FloatDefine, OpCode.FloatDefine },
     142            { Symbols.FloatAdd, OpCode.FloatAdd },
     143            { Symbols.FloatSubtract, OpCode.FloatSubtract },
     144            { Symbols.FloatMultiply, OpCode.FloatMultiply },
     145            { Symbols.FloatDivide, OpCode.FloatDivide },
     146            { Symbols.FloatSmallerThan, OpCode.FloatSmallerThan },
     147            { Symbols.FloatGreaterThan, OpCode.FloatGreaterThan },
     148            { Symbols.FloatEquals, OpCode.FloatEquals },
     149            { Symbols.FloatMin, OpCode.FloatMin },
     150            { Symbols.FloatMax, OpCode.FloatMax },
     151            { Symbols.FloatCos, OpCode.FloatCos },
     152            { Symbols.FloatSin, OpCode.FloatSin },
     153            { Symbols.FloatTan, OpCode.FloatTan },
     154            { Symbols.FloatFromBoolean, OpCode.FloatFromBoolean },
     155            { Symbols.FloatFromInteger, OpCode.FloatFromInteger },
     156            { Symbols.FloatRand, OpCode.FloatRand },
     157            { Symbols.FloatRot, OpCode.FloatRot },
     158            { Symbols.FloatShove, OpCode.FloatShove },
     159            { Symbols.FloatStackDepth, OpCode.FloatStackDepth },
     160            { Symbols.FloatSwap, OpCode.FloatSwap },
     161            { Symbols.FloatYank, OpCode.FloatYank },
     162            { Symbols.FloatYankDuplicate, OpCode.FloatYankDuplicate },
    76163            #endregion Float
    77164        };
     
    81168            OpCode opCode;
    82169            if (symbolTable.TryGetValue(operationName, out opCode)) return opCode;
    83             else throw new NotSupportedException("Symbol: " + operationName);
     170            else throw new NotSupportedException("Operation not supported: " + operationName);
    84171        }
    85172
     
    88175            return symbolTable.TryGetValue(operationName, out opCode);
    89176        }
    90 
    91         public static string GetSymbol(OpCode opCode)
    92         {
    93             var symbol = symbolTable.SingleOrDefault(pair => pair.Value == opCode);
    94 
    95             return !symbol.Equals(default(KeyValuePair<string, OpCode>))
    96                 ? symbol.Key
    97                 : null;
    98         }
    99177    }
    100178}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs

    r14323 r14328  
    22using System.Collections;
    33using System.Collections.Generic;
     4using System.Text;
    45
    56namespace HeuristicLab.Algorithms.PushGP.Stack
    67{
    78    /// <summary>
    8     /// While Push's stacks are generally treated as genuine stacks---that is, instructions take their arguments from the tops of
    9     /// the stacks and push their results onto the tops of the stacks---a few instructions (like YANK and SHOVE) do allow direct access
     9    /// While Push's stacks are generally treated as genuine stacks---that is, inclassions take their arguments from the tops of
     10    /// the stacks and push their results onto the tops of the stacks---a few inclassions (like YANK and SHOVE) do allow direct access
    1011    /// to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random access memory
    11     /// structures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value
     12    /// classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value
    1213    /// bindings supported by the NAME data type and DEFINE methods; see below).
    1314    /// </summary>
     
    1516    public class PushGPStack<T> : IStack<T>
    1617    {
    17         private List<T> data = new List<T>();
     18        private readonly List<T> data;
     19        private const string delimiter = " ";
    1820
    19         public T Top => this.data[Count - 1];
    20         public int Count => data.Count;
    21         public bool IsEmpty => this.Count == 0;
     21        public PushGPStack(int capacity = 0)
     22        {
     23            this.data = new List<T>();
     24        }
     25
     26        public T Top { get { return this.data[Count - 1]; } }
     27        public int Count { get { return this.data.Count; } }
     28        public int Capacity { get { return this.data.Capacity; } }
     29        public bool IsEmpty { get { return this.Count == 0; } }
    2230
    2331        public bool IsReadOnly
     
    6169        public T Pop()
    6270        {
    63             var index = Count - 1;
    64             var value = this.data[index];
    65             this.data.RemoveAt(index);
     71            var value = this.data[Count - 1];
     72            this.data.RemoveAt(Count - 1);
    6673
    6774            return value;
     
    7582            this.data.RemoveRange(startIndex, count);
    7683
    77             items.Reverse();
     84            // is faster than remove range??
     85            for (var i = this.Count - 1; i > startIndex - 1; i--)
     86            {
     87                this.data.RemoveAt(i);
     88            }
     89
    7890            return items.ToArray();
    7991        }
     
    8698        public void Push(params T[] items)
    8799        {
    88             this.data.AddRange(items);
     100            foreach (var item in items)
     101            {
     102                this.data.Add(item);
     103            }
    89104        }
    90105
     
    112127        {
    113128            var index = Count - 1;
    114             if (this.data.Count > 0 && this.data[index].Equals(item))
     129            if (this.Count > 0 && this.data[index].Equals(item))
    115130            {
    116131                this.data.RemoveAt(index);
     
    125140        public bool TryPop(out T item)
    126141        {
    127             if (this.data.Count > 0)
     142            if (this.Count > 0)
    128143            {
    129144                item = this.Pop();
     
    144159        public override string ToString()
    145160        {
    146             return string.Join(", ", this.data);
     161            if (this.Count == 0)
     162            {
     163                return string.Empty;
     164            }
     165
     166            var sb = new StringBuilder();
     167
     168            for (var i = this.Count - 1; i > 0; i--)
     169            {
     170                sb.Append(this.data[i] + delimiter);
     171            }
     172
     173            sb.Append(this.data[0]);
     174
     175            return sb.ToString();
    147176        }
    148177    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r14323 r14328  
    99    <RootNamespace>HeuristicLab.Tests</RootNamespace>
    1010    <AssemblyName>HeuristicLab.Tests</AssemblyName>
    11     <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
     11    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1212    <FileAlignment>512</FileAlignment>
    1313    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     
    1717    <IsCodedUITest>False</IsCodedUITest>
    1818    <TestProjectType>UnitTest</TestProjectType>
     19    <TargetFrameworkProfile />
    1920  </PropertyGroup>
    2021  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    3435    <ErrorReport>prompt</ErrorReport>
    3536    <WarningLevel>4</WarningLevel>
     37    <LangVersion>5</LangVersion>
    3638  </PropertyGroup>
    3739  <ItemGroup>
     
    5153  </Choose>
    5254  <ItemGroup>
    53     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\MixedExpressionTests.cs" />
    54     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\NameExpressionTests.cs" />
    55     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\FloatExpressionTests.cs" />
    56     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\BooleanExpressionTests.cs" />
    57     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\ExecExpressionTests.cs" />
    58     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\CodeExpressionTests.cs" />
    59     <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\IntegerExpressionTests.cs" />
    60     <Compile Include="HeuristicLab.Algorithms.PushGP\InterpreterTest.cs" />
     55    <Compile Include="Expressions\ExampleTests.cs" />
     56    <Compile Include="Expressions\NameExpressionTests.cs" />
     57    <Compile Include="Expressions\FloatExpressionTests.cs" />
     58    <Compile Include="Expressions\BooleanExpressionTests.cs" />
     59    <Compile Include="Expressions\ExecExpressionTests.cs" />
     60    <Compile Include="Expressions\CodeExpressionTests.cs" />
     61    <Compile Include="Expressions\IntegerExpressionTests.cs" />
     62    <Compile Include="InterpreterTest.cs" />
     63    <Compile Include="Problems\Checksum.cs" />
    6164    <Compile Include="Properties\AssemblyInfo.cs" />
    6265  </ItemGroup>
     
    6871  </ItemGroup>
    6972  <ItemGroup>
    70     <Folder Include="HeuristicLab.Algorithms.PushGP\Problems\" />
     73    <Folder Include="Problems\Data\" />
    7174  </ItemGroup>
    7275  <Choose>
Note: See TracChangeset for help on using the changeset viewer.