Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14392


Ignore:
Timestamp:
11/15/16 21:10:17 (7 years ago)
Author:
pkimmesw
Message:

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

Location:
branches/PushGP/HeuristicLab.Algorithms.PushGP
Files:
67 added
19 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/HeuristicLab.Algorithms.PushGP.Cli.csproj

    r14328 r14392  
    3636  </PropertyGroup>
    3737  <ItemGroup>
     38    <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     39      <SpecificVersion>False</SpecificVersion>
     40      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     41    </Reference>
     42    <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     43      <SpecificVersion>False</SpecificVersion>
     44      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     45    </Reference>
    3846    <Reference Include="System" />
    3947    <Reference Include="System.Core" />
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/Program.cs

    r14328 r14392  
    2222        static async Task Stepwise()
    2323        {
    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 )");
    2724            var interpreter = new PushGPInterpreter();
    2825
    29             interpreter.IntegerStack.Push(5);
    30             interpreter.InterpretAsync(program, true).Wait();
     26            interpreter.InterpretAsync("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )", true).Wait();
    3127
    3228            while (!interpreter.IsCompleted)
     
    7268            var sw = new Stopwatch();
    7369
     70            var interpreter = new PushGPInterpreter();
     71            var generator = new CodeGenerator(interpreter);
     72
    7473            sw.Start();
    75             var expressions = CodeGenerator.RandomCode(3000000);
     74            var expressions = generator.RandomCode(3000000);
    7675            sw.Stop();
    7776
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Expression.cs

    r14328 r14392  
    1 using System;
    2 using System.Collections.Generic;
    3 using HeuristicLab.Algorithms.PushGP.Interpreter;
    4 using HeuristicLab.Algorithms.PushGP.Stack;
     1using HeuristicLab.Algorithms.PushGP.Interpreter;
     2using HeuristicLab.Common;
    53
    64namespace HeuristicLab.Algorithms.PushGP.Expressions
    75{
    8     public abstract class Expression
     6    public abstract class Expression : DeepCloneable
    97    {
    10         public abstract bool IsCodeOp { get; }
     8        public Expression() { }
     9
     10        public Expression(string stringRepresenation) : this(stringRepresenation, stringRepresenation.GetHashCode())
     11        { }
     12
     13        public Expression(string stringRepresenation, int id)
     14        {
     15            this.StringRepresentation = stringRepresenation;
     16            this.Id = id;
     17        }
     18
     19        // copy constructor
     20        public Expression(Expression original, Cloner cloner) : base(original, cloner)
     21        {
     22            this.StringRepresentation = original.StringRepresentation;
     23            this.Id = original.Id;
     24        }
     25
     26        protected int Id { get; set; }
     27        protected string StringRepresentation { get; set; }
     28
    1129        public abstract void Eval(IInterpreter interpreter);
    1230
    13         public void PushResult<T>(IStack<T> stack, int count, Func<T[], T> templateFunc)
     31        public override string ToString()
    1432        {
    15             PushResult(stack, stack, count, templateFunc);
     33            return this.StringRepresentation;
    1634        }
    1735
    18         public void PushResult<T, R>(IStack<T> sourceStack, IStack<R> targetStack, int count, Func<T[], R> templateFunc)
     36        public override bool Equals(object obj)
    1937        {
    20             // not enough arguments on stack
    21             if (sourceStack.Count < count)
    22                 return;
    23 
    24             var values = sourceStack.Pop(count);
    25             var result = templateFunc(values);
    26 
    27             targetStack.Push(result);
     38            return obj != null && this.GetType() == obj.GetType();
    2839        }
    2940
    30         public void Duplicate<T>(IStack<T> stack)
     41        public override int GetHashCode()
    3142        {
    32             // not enough arguments on stack
    33             if (stack.Count == 0)
    34                 return;
    35 
    36             var value = stack.Top;
    37             stack.Push(value);
     43            return this.Id;
    3844        }
    3945
    40         public static void Define<T>(IStack<T> stack, IStack<string> nameStack, IDictionary<string, Expression> customExpressions, Func<T, Expression> creator)
     46        public override IDeepCloneable Clone(Cloner cloner)
    4147        {
    42             // not enough arguments on stack
    43             if (nameStack.Count == 0 ||
    44                 stack.Count == 0)
    45                 return;
    46 
    47             var name = nameStack.Pop();
    48             var expression = creator(stack.Top);
    49 
    50             if (customExpressions.ContainsKey(name))
    51             {
    52                 customExpressions[name] = expression;
    53             }
    54             else
    55             {
    56                 customExpressions.Add(name, expression);
    57             }
     48            return this;
    5849        }
    5950    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs

    r14328 r14392  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Linq;
    34using HeuristicLab.Algorithms.PushGP.Expressions;
     5using HeuristicLab.Algorithms.PushGP.Interpreter;
     6using HeuristicLab.Core;
    47using HeuristicLab.Random;
    58
    69namespace HeuristicLab.Algorithms.PushGP.Generators
    710{
    8     public static class CodeGenerator
     11    /// <summary>
     12    /// The following is the standard Push random code generation algorithm, which is used for the CODE.RAND instruction.
     13    /// It may also be useful for the initialization of programs in evolutionary computation systems, and it is used for this purpose in PushGP.
     14    /// It produces a uniform distribution of sizes and what seems to be a reasonable distribution of shapes, in a reasonable amount of time.
     15    /// </summary>
     16    /// <see cref="http://faculty.hampshire.edu/lspector/push3-description.html#RandomCode"/>
     17    public class CodeGenerator
    918    {
    10         private static FastRandom random = new FastRandom();
    11         public static IEnumerable<Expression> RandomCode(int maxPoints)
     19        private static IRandom random = new FastRandom();
     20        private readonly IInterpreter interpreter;
     21
     22        public CodeGenerator(IInterpreter interpreter)
    1223        {
    13             var actualPoints = random.Next(1, maxPoints);
    14 
    15             return RandomCodeWithSize(actualPoints).ToArray();
     24            this.interpreter = interpreter;
    1625        }
    1726
    18         private static IEnumerable<Expression> RandomCodeWithSize(int points)
     27        /// <summary>
     28        /// Generates a random program with a given max size
     29        /// </summary>
     30        /// <param name="maxPoints"></param>
     31        /// <returns>Returns an ExpandExpression if maxPoints smaller or equal to Configuration.MaxProgramPoints and Noop otherwise</returns>
     32        public Expression RandomProgram(int maxPoints)
     33        {
     34            return ExecExpandExpression.TryCreate(interpreter.CodeGenerator.RandomCode(maxPoints).ToArray(), interpreter.Configuration);
     35        }
     36
     37        public IEnumerable<Expression> RandomCode(int maxPoints)
     38        {
     39            var actualPoints = random.Next(1, Math.Max(1, maxPoints));
     40
     41            return RandomCodeWithSize(actualPoints);
     42        }
     43
     44        private IEnumerable<Expression> RandomCodeWithSize(int points)
    1945        {
    2046            if (points == 1)
    2147            {
    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);
    24 
    25                 return new[] { ExpressionFactory.Create(opCode) };
     48                var index = random.Next(0, ExpressionTable.Count + this.interpreter.CustomExpressions.Count - 2);
     49                return new[] { this.CreateExpression(index) };
    2650            }
    2751            else
    2852            {
    2953                var sizesThisLevel = Decompose(points - 1, points - 1);
    30 
    3154                return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random);
    3255            }
    3356        }
    3457
    35         private static IEnumerable<int> Decompose(int number, int maxParts)
     58        private IEnumerable<int> Decompose(int number, int maxParts)
    3659        {
    3760            if (number == 1 || maxParts == 1)
     
    4265            {
    4366                var thisPart = random.Next(1, number - 1);
    44 
    4567                return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
    4668            }
    4769        }
     70
     71        private Expression CreateExpression(int index)
     72        {
     73            return (index >= 0 && index < ExpressionTable.Count)
     74                ? ExpressionTable.GetExpression(index)
     75                : new NameDefineXExecExpression(this.interpreter.CustomExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key);
     76        }
    4877    }
    4978}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj

    r14328 r14392  
    5353  <ItemGroup>
    5454    <Compile Include="Exporter\Exporter.cs" />
    55     <Compile Include="Expressions\Boolean\BooleanPushExpression.cs" />
    56     <Compile Include="Expressions\Code\CodeDoExpression.cs" />
    57     <Compile Include="Expressions\Code\CodeDefineExpression.cs" />
    58     <Compile Include="Expressions\Code\CodeIfExpression.cs" />
    59     <Compile Include="Expressions\Code\CodeNoopExpression.cs" />
    60     <Compile Include="Expressions\Code\CodeQuoteExpression.cs" />
    61     <Compile Include="Expressions\Exec\ExecDefineExpression.cs" />
    62     <Compile Include="Expressions\Exec\ExecYExpression.cs" />
    63     <Compile Include="Expressions\Exec\ExecDoRangeExpression.cs" />
    64     <Compile Include="Expressions\Exec\ExecIfExpression.cs" />
    65     <Compile Include="Expressions\Exec\ExecPushExpression.cs" />
    66     <Compile Include="Expressions\Exec\ExecExpandExpression.cs" />
    67     <Compile Include="Expressions\ExpressionFactory.cs" />
     55    <Compile Include="Expressions\BooleanExpressions.cs" />
     56    <Compile Include="Expressions\CodeExpressions.cs" />
     57    <Compile Include="Expressions\DoCountExpressions.cs" />
     58    <Compile Include="Expressions\DoTimesExpressions.cs" />
     59    <Compile Include="Expressions\ERCExpressions.cs" />
     60    <Compile Include="Expressions\DoRangeExpressions.cs" />
     61    <Compile Include="Expressions\LoopExpression.cs" />
     62    <Compile Include="Expressions\PushResultExpression.cs" />
     63    <Compile Include="Expressions\EqualsExpressions.cs" />
     64    <Compile Include="Expressions\RandExpressions.cs" />
     65    <Compile Include="Expressions\SetExpressions.cs" />
     66    <Compile Include="Expressions\YankDuplicateExpressions.cs" />
     67    <Compile Include="Expressions\YankExpressions.cs" />
     68    <Compile Include="Expressions\StackdepthExpressions.cs" />
     69    <Compile Include="Expressions\ShoveExpressions.cs" />
     70    <Compile Include="Expressions\RotateExpressions.cs" />
     71    <Compile Include="Expressions\SwapExpressions.cs" />
     72    <Compile Include="Expressions\FlushExpressions.cs" />
     73    <Compile Include="Expressions\FloatExpressions.cs" />
     74    <Compile Include="Expressions\IntegerExpressions.cs" />
     75    <Compile Include="Expressions\DefineExpressions.cs" />
     76    <Compile Include="Expressions\DuplicateExpressions.cs" />
     77    <Compile Include="Expressions\PopExpressions.cs" />
     78    <Compile Include="Expressions\PushExpressions.cs" />
     79    <Compile Include="Expressions\ExecExpressions.cs" />
    6880    <Compile Include="Expressions\Expression.cs" />
    69     <Compile Include="Expressions\Float\FloatPushExpression.cs" />
    70     <Compile Include="Expressions\Integer\IntegerPushExpression.cs" />
    71     <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" />
     81    <Compile Include="Expressions\NameExpressions.cs" />
     82    <Compile Include="StateFullExpressionAttribute.cs" />
     83    <Compile Include="Generators\BooleanGenerator.cs" />
     84    <Compile Include="Generators\FloatGenerator.cs" />
     85    <Compile Include="Generators\IntegerGenerator.cs" />
     86    <Compile Include="Generators\NameGenerator.cs" />
    7787    <Compile Include="Interpreter\Configuration.cs" />
    7888    <Compile Include="Generators\CodeGenerator.cs" />
    79     <Compile Include="Generators\Configuration.cs" />
    80     <Compile Include="OpCode.cs" />
    81     <Compile Include="OpCodeExtensions.cs" />
    8289    <Compile Include="Parser\Parser.cs" />
    8390    <Compile Include="Stack\IStack.cs" />
    84     <Compile Include="Interpreter\Interpreter.cs" />
     91    <Compile Include="Interpreter\PushGPInterpreter.cs" />
    8592    <Compile Include="Properties\AssemblyInfo.cs" />
    8693    <Compile Include="Interpreter\IInterpreter.cs" />
    8794    <Compile Include="Stack\PushGPStack.cs" />
    88     <Compile Include="Parser\SymbolTable.cs" />
    89     <Compile Include="Symbols.cs" />
     95    <Compile Include="ExpressionTable.cs" />
    9096  </ItemGroup>
    9197  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj.user

    r14329 r14392  
    22<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    33  <PropertyGroup>
    4     <ProjectView>ShowAllFiles</ProjectView>
     4    <ProjectView>ProjectFiles</ProjectView>
    55  </PropertyGroup>
    66</Project>
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Configuration.cs

    r14328 r14392  
    44namespace HeuristicLab.Algorithms.PushGP.Interpreter
    55{
    6     public class Configuration : Generators.Configuration
     6    public class Configuration
    77    {
    88        public Configuration()
     
    1414            this.IsNameStackEnabled = true;
    1515
    16             this.EvalPushLimit = uint.MaxValue;
    17             this.MaxPointsInProgram = uint.MaxValue;
    18             this.MaxPointsInRandomExpression = uint.MaxValue;
     16            this.EvalPushLimit = 1024;
     17            this.MaxPointsInProgram = 4096;
     18            this.MaxPointsInRandomExpression = 4096;
    1919
    2020            this.TopLevelPushCode = true;
    21             this.TopLevelPopCode = true;
     21            this.TopLevelPopCode = false;
    2222
    2323            this.RandomSeedMax = 30081;
    2424            this.RandomSeedMin = 0;
    2525            this.RandomSeed = null;
     26
     27            this.MinRandomInteger = -128;
     28            this.MaxRandomInteger = 128;
     29            this.MinRandomFloat = -128D;
     30            this.MaxRandomFloat = 128D;
     31            this.NewErcNameProbability = 0.5;
    2632        }
    2733
     
    3238        public bool IsNameStackEnabled { get; set; }
    3339
    34         public IList<OpCode> AllowedInclassions { get; set; }
     40        public IList<string> AllowedInstructions { get; set; }
    3541
    3642        public IList<Expression> PredefinedExpressions { get; set; }
     
    3844        /// <summary>
    3945        /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
    40         /// The execution of a single Push inclassion counts as one execution, as does the processing of a single literal,
     46        /// The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
    4147        /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
    4248        /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
     
    4854        /// <summary>
    4955        /// This is the maximum size of an item on the CODE stack, expressed as a number of points.
    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.
     56        /// A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be exceeded
     57        /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the instruction.
    5258        /// </summary>
    5359        public uint MaxPointsInProgram { get; set; }
    5460
    5561        /// <summary>
    56         /// The maximum number of points in an expression produced by the CODE.RAND inclassion.
     62        /// The maximum number of points in an expression produced by the CODE.RAND instruction.
    5763        /// </summary>
    5864        public uint MaxPointsInRandomExpression { get; set; }
     
    6975        public bool TopLevelPopCode { get; set; }
    7076
     77        /// <summary>
     78        /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
     79        /// </summary>
     80        public int MinRandomInteger { get; set; }
     81
     82        /// <summary>
     83        /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
     84        /// </summary>
     85        public int MaxRandomInteger { get; set; }
     86
     87        /// <summary>
     88        /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
     89        /// </summary>
     90        public double MinRandomFloat { get; set; }
     91
     92        /// <summary>
     93        /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
     94        /// </summary>
     95        public double MaxRandomFloat { get; set; }
     96
     97        /// <summary>
     98        /// The probability that the selection of the ephemeral
     99        /// random NAME constant for inclusion in randomly generated code will produce a new name
     100        /// (rather than a name that was previously generated).
     101        /// </summary>
     102        public double NewErcNameProbability { get; set; }
     103
    71104        public ushort RandomSeedMax { get; set; }
    72105        public ushort RandomSeedMin { get; set; }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/IInterpreter.cs

    r14328 r14392  
    33using System.Threading.Tasks;
    44using HeuristicLab.Algorithms.PushGP.Expressions;
     5using HeuristicLab.Algorithms.PushGP.Generators;
    56using HeuristicLab.Algorithms.PushGP.Stack;
    67
     
    1516        IStack<long> IntegerStack { get; }
    1617        IStack<double> FloatStack { get; }
     18        IDictionary<string, Expression> CustomExpressions { get; }
    1719
    18         IDictionary<string, Expression> CustomExpressions { get; }
     20        Configuration Configuration { get; }
     21
     22        CodeGenerator CodeGenerator { get; }
     23
     24        bool IsNameQuoteFlagSet { get; set; }
    1925
    2026        void Interpret(string code);
     
    2228        void Interpret(Expression program);
    2329        Task InterpretAsync(Expression program, bool paused = false, CancellationToken token = default(CancellationToken));
    24         Task AbortAndReset();
     30        Task AbortAndResetAsync();
    2531        Task AbortAsync();
    2632        Task PauseAsync();
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/Parser.cs

    r14328 r14392  
    22using System.Globalization;
    33using HeuristicLab.Algorithms.PushGP.Expressions;
    4 using HeuristicLab.Algorithms.PushGP.Expressions.Boolean;
    5 using HeuristicLab.Algorithms.PushGP.Expressions.Code;
    6 using HeuristicLab.Algorithms.PushGP.Expressions.Exec;
    7 using HeuristicLab.Algorithms.PushGP.Expressions.Float;
    8 using HeuristicLab.Algorithms.PushGP.Expressions.Integer;
    9 using HeuristicLab.Algorithms.PushGP.Expressions.Name;
    104
    115namespace HeuristicLab.Algorithms.PushGP
     
    5145                {
    5246                    endIndex = i;
    53                     return expressions.Count != 0
    54                         ? new ExecExpandExpression(expressions.ToArray())
    55                         : new CodeNoopExpression() as Expression;
     47                    return new ExecExpandExpression(expressions.ToArray());
    5648                }
    5749
     
    6557
    6658                // expression
    67                 OpCode opCode;
    68                 if (SymbolTable.TryGetOpCode(symbol, out opCode))
     59                if (ExpressionTable.TryGetStatelessExpression(symbol, out expression) ||
     60                    ExpressionTable.TryGetStatefullExpression(symbol, out expression))
    6961                {
    70                     expression = ExpressionFactory.Create(opCode);
    7162                    expressions.Insert(0, expression);
    7263                    continue;
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/IStack.cs

    r14323 r14392  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23
    34namespace HeuristicLab.Algorithms.PushGP.Stack
     
    56    public interface IStack<T> : IEnumerable<T>, ICollection<T>
    67    {
     8        void Swap(int count);
     9        void Yank(int index);
    710        bool IsEmpty { get; }
    811        void Push(T item);
    9 
    1012        void Push(params T[] items);
    11 
    1213        void Push(IEnumerable<T> items);
    13 
     14        void PushResult(int count, Func<T[], T> templateFunc);
    1415        void Insert(int index, T item);
    15 
    1616        void Insert(int index, params T[] items);
    17 
    1817        void Insert(int index, IEnumerable<T> items);
    19 
     18        T ReverseElementAt(int offset);
     19        void SetTop(T value);
     20        void RemoveTop();
     21        void Remove(int count);
     22        void RemoveAt(int index);
     23        void RemoveAt(int index, int count);
    2024        T Pop();
    21 
    2225        T[] Pop(int count);
    23 
    2426        bool TryPop(out T item);
    25 
    2627        T ElementAt(int index);
    27 
    2828        T Top { get; }
     29        T TopOrDefault { get; }
     30        T Bottom { get; }
     31        T BottomOrDefault { get; }
     32        T this[int key] { get; set; }
    2933    }
    3034}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs

    r14328 r14392  
    22using System.Collections;
    33using System.Collections.Generic;
    4 using System.Text;
    54
    65namespace HeuristicLab.Algorithms.PushGP.Stack
     
    1716    {
    1817        private readonly List<T> data;
    19         private const string delimiter = " ";
     18        private const string Delimiter = " ";
    2019
    2120        public PushGPStack(int capacity = 0)
     
    2524
    2625        public T Top { get { return this.data[Count - 1]; } }
     26        public T TopOrDefault { get { return Count > 0 ? this.data[Count - 1] : default(T); } }
     27        public T Bottom { get { return this.data[0]; } }
     28        public T BottomOrDefault { get { return Count > 0 ? this.data[0] : default(T); } }
    2729        public int Count { get { return this.data.Count; } }
    2830        public int Capacity { get { return this.data.Capacity; } }
    2931        public bool IsEmpty { get { return this.Count == 0; } }
    3032
    31         public bool IsReadOnly
    32         {
    33             get
    34             {
    35                 throw new NotImplementedException();
    36             }
    37         }
     33        public bool IsReadOnly { get { return false; } }
    3834
    3935        public void Add(T item)
     
    6561        {
    6662            return this.data[index];
     63        }
     64
     65
     66        public T ReverseElementAt(int offset)
     67        {
     68            return this.data[this.Count - 1 - offset];
     69        }
     70
     71        public void SetTop(T value)
     72        {
     73            this.data[Count - 1] = value;
     74        }
     75
     76        public T this[int key]
     77        {
     78            get { return this.data[key]; }
     79            set { this.data[key] = value; }
     80        }
     81
     82        public void Swap(int count)
     83        {
     84            var top = this.Top;
     85            var bottomIndex = this.Count - count;
     86
     87            for (var i = this.Count - 1; i > bottomIndex; i--)
     88            {
     89                this.data[i] = this.data[i - 1];
     90            }
     91
     92            this.data[bottomIndex] = top;
     93        }
     94
     95        public void Yank(int index)
     96        {
     97            var item = this.ElementAt(index);
     98            this.data.RemoveAt(index);
     99            this.data.Add(item);
    67100        }
    68101
     
    78111        {
    79112            var startIndex = Count - count;
    80             var items = this.data.GetRange(startIndex, count);
    81 
    82             this.data.RemoveRange(startIndex, count);
    83 
    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 
    90             return items.ToArray();
     113
     114            var items = new T[count];
     115            this.data.CopyTo(startIndex, items, 0, count);
     116            this.Remove(count);
     117
     118            return items;
    91119        }
    92120
     
    96124        }
    97125
     126        public void Push(object[] items)
     127        {
     128            for (var i = 0; i < items.Length; i++)
     129            {
     130                this.data.Add((T)items[i]);
     131            }
     132        }
     133
     134        public void PushResult(int count, Func<T[], T> templateFunc)
     135        {
     136            var startIndex = Count - count;
     137            var items = new T[count];
     138
     139            this.data.CopyTo(startIndex, items, 0, count);
     140            this.Remove(count - 1);
     141
     142            this.data[Count - 1] = templateFunc(items);
     143        }
     144
    98145        public void Push(params T[] items)
    99146        {
    100             foreach (var item in items)
    101             {
    102                 this.data.Add(item);
     147            for (var i = 0; i < items.Length; i++)
     148            {
     149                this.data.Add(items[i]);
    103150            }
    104151        }
     
    138185        }
    139186
     187        public void RemoveTop()
     188        {
     189            this.data.RemoveAt(this.Count - 1);
     190        }
     191
     192        public void Remove(int count)
     193        {
     194            for (var i = 0; i < count; i++)
     195            {
     196                this.data.RemoveAt(Count - 1);
     197            }
     198        }
     199
     200        public void RemoveAt(int index)
     201        {
     202            this.data.RemoveAt(index);
     203        }
     204
     205        public void RemoveAt(int index, int count)
     206        {
     207            this.data.RemoveRange(index, count);
     208        }
     209
    140210        public bool TryPop(out T item)
    141211        {
     
    159229        public override string ToString()
    160230        {
    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();
     231            return string.Join(Delimiter, this.data);
    176232        }
    177233    }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r14328 r14392  
    88    <AppDesignerFolder>Properties</AppDesignerFolder>
    99    <RootNamespace>HeuristicLab.Tests</RootNamespace>
    10     <AssemblyName>HeuristicLab.Tests</AssemblyName>
     10    <AssemblyName>F</AssemblyName>
    1111    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1212    <FileAlignment>512</FileAlignment>
     
    3838  </PropertyGroup>
    3939  <ItemGroup>
     40    <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     41      <SpecificVersion>False</SpecificVersion>
     42      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     43    </Reference>
     44    <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     45      <SpecificVersion>False</SpecificVersion>
     46      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     47    </Reference>
    4048    <Reference Include="System" />
     49    <Reference Include="System.IO.Compression" />
    4150  </ItemGroup>
    4251  <Choose>
     
    5362  </Choose>
    5463  <ItemGroup>
    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" />
     64    <Compile Include="Benchmark\Example.cs" />
     65    <Compile Include="Benchmark\ExampleConverter.cs" />
     66    <Compile Include="Benchmark\Problem\CountOdds.cs" />
     67    <Compile Include="Benchmark\Problem\Problem.cs" />
     68    <Compile Include="Interpreter\Expressions\CommonTests.cs" />
     69    <Compile Include="Interpreter\Expressions\ExampleTests.cs" />
     70    <Compile Include="Interpreter\Expressions\NameExpressionTests.cs" />
     71    <Compile Include="Interpreter\Expressions\FloatExpressionTests.cs" />
     72    <Compile Include="Interpreter\Expressions\BooleanExpressionTests.cs" />
     73    <Compile Include="Interpreter\Expressions\ExecExpressionTests.cs" />
     74    <Compile Include="Interpreter\Expressions\CodeExpressionTests.cs" />
     75    <Compile Include="Interpreter\Expressions\IntegerExpressionTests.cs" />
     76    <Compile Include="Interpreter\InterpreterTest.cs" />
    6477    <Compile Include="Properties\AssemblyInfo.cs" />
    6578  </ItemGroup>
     
    7184  </ItemGroup>
    7285  <ItemGroup>
    73     <Folder Include="Problems\Data\" />
     86    <EmbeddedResource Include="Benchmark\Data\BenchmarkExamples.zip" />
    7487  </ItemGroup>
     88  <ItemGroup />
    7589  <Choose>
    7690    <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Note: See TracChangeset for help on using the changeset viewer.