Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13017


Ignore:
Timestamp:
10/16/15 10:36:09 (9 years ago)
Author:
gkronber
Message:

#2069 refactored grammar, symbols, and interpreter

Location:
branches/Robocode.TrunkInt
Files:
1 added
16 deleted
12 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode.Views/3.3/CodeNodeView.Designer.cs

    r13013 r13017  
    112112      // splitContainer.Panel1
    113113      //
     114      this.splitContainer.Panel1.Controls.Add(this.prefixCode);
    114115      this.splitContainer.Panel1.Controls.Add(this.prefixLabel);
    115       this.splitContainer.Panel1.Controls.Add(this.prefixCode);
    116116      //
    117117      // splitContainer.Panel2
    118118      //
     119      this.splitContainer.Panel2.Controls.Add(this.suffixCode);
    119120      this.splitContainer.Panel2.Controls.Add(this.suffixLabel);
    120       this.splitContainer.Panel2.Controls.Add(this.suffixCode);
    121121      this.splitContainer.Size = new System.Drawing.Size(582, 261);
    122122      this.splitContainer.SplitterDistance = 130;
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode.Views/3.3/CodeNodeView.cs

    r13013 r13017  
    2626
    2727namespace HeuristicLab.Problems.GeneticProgramming.Views.Robocode {
    28   [View("CodeNode View")]
    29   [Content(typeof(CodeNode), IsDefaultView = true)]
     28  [View("CodeSymbol View")]
     29  [Content(typeof(CodeSymbol), IsDefaultView = true)]
    3030  public partial class CodeNodeView : ItemView {
    31     public new CodeNode Content {
    32       get { return (CodeNode)base.Content; }
     31    public new CodeSymbol Content {
     32      get { return (CodeSymbol)base.Content; }
    3333      set { base.Content = value; }
    3434    }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/EnemyCollection.cs

    r13013 r13017  
    5151
    5252    public static EnemyCollection ReloadEnemies(string robocodeDir) {
    53       EnemyCollection robotList = new EnemyCollection(); ;
     53      EnemyCollection robotList = new EnemyCollection();
    5454
    5555      try {
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs

    r13013 r13017  
    5555    private void Initialize() {
    5656      #region Symbols
    57       var block = new Block();
    58       var stat = new Stat();
    59       var ifThenElseStat = new IfThenElseStat();
    60       var whileStat = new WhileStat();
     57      var block = new SimpleSymbol("Block", "A group of statements", 1, byte.MaxValue);
     58      var stat = new SimpleSymbol("Statement", "A statement.", 1, 1);
     59      var ifThenElseStat = new SimpleSymbol("IfThenElseStat", "An if statement.", 2, 3);
     60      var whileStat = new SimpleSymbol("WhileStat", "A while statement.", 2, 2);
    6161      whileStat.Enabled = false;
    6262
    63       var logicalExpr = new LogicalExpression();
    64       var numericalExpr = new NumericalExpression();
    65 
    66       var equal = new Equal();
    67       var lessThan = new LessThan();
    68       var lessThanOrEqual = new LessThanOrEqual();
    69       var greaterThan = new GreaterThan();
    70       var greaterThanOrEqual = new GreaterThanOrEqual();
    71 
    72       var conjunction = new Conjunction();
    73       var disjunction = new Disjunction();
    74       var negation = new Negation();
    75 
    76       var addition = new Addition();
    77       var subtraction = new Subtraction();
    78       var multiplication = new Multiplication();
    79       var division = new Division();
    80       var modulus = new Modulus();
     63      var boolExpr = new SimpleSymbol("BooleanExpression", "A Boolean expression.", 1, 1);
     64      var numericalExpr = new SimpleSymbol("NumericalExpression", "A numerical expression.", 1, 1);
     65
     66      var equal = new SimpleSymbol("Equal", "Equal comparator.", 2, 2);
     67      var lessThan = new SimpleSymbol("LessThan", "LessThan comparator.", 2, 2);
     68      var lessThanOrEqual = new SimpleSymbol("LessThanOrEqual", "LessThanOrEqual comparator.", 2, 2);
     69      var greaterThan = new SimpleSymbol("GreaterThan", "GreaterThan comparator.", 2, 2);
     70      var greaterThanOrEqual = new SimpleSymbol("GreaterThanOrEqual", "GreaterThanOrEqual comparator.", 2, 2);
     71
     72      var conjunction = new SimpleSymbol("ConditionalAnd", "Conjunction comparator.", 2, byte.MaxValue);
     73      var disjunction = new SimpleSymbol("ConditionalOr", "Disjunction comparator.", 2, byte.MaxValue);
     74      var negation = new SimpleSymbol("Negation", "A negation.", 1, 1);
     75
     76      var addition = new SimpleSymbol("Addition", "Addition operator.", 2, byte.MaxValue);
     77      var subtraction = new SimpleSymbol("Subtraction", "Subtraction operator.", 2, byte.MaxValue);
     78      var multiplication = new SimpleSymbol("Multiplication", "Multiplication operator.", 2, byte.MaxValue);
     79      var division = new SimpleSymbol("Division", "Division operator.", 2, byte.MaxValue);
     80      var modulus = new SimpleSymbol("Modulus", "Modulus operator.", 2, byte.MaxValue);
    8181
    8282      var number = new Number();
    83       var logicalVal = new LogicalValue();
    84 
    85       var ahead = new Ahead();
    86       var back = new Back();
    87       var fire = new Fire();
     83      var logicalVal = new BooleanValue();
     84
     85      var ahead = new SimpleSymbol("Ahead", "Immediately moves your robot ahead (forward) by distance measured in pixels.", 1, 1);
     86      var back = new SimpleSymbol("Back", "Immediately moves your robot backward by distance measured in pixels.", 1, 1);
     87      var fire = new SimpleSymbol("Fire", "Immediately fires a bullet.", 1, 1);
    8888      var shotPower = new ShotPower();
    8989
    90       var getEnergy = new GetEnergy();
    91       var getGunHeading = new GetGunHeading();
    92       var getHeading = new GetHeading();
    93       var getRadarHeading = new GetRadarHeading();
    94       var getX = new GetX();
    95       var getY = new GetY();
    96 
    97       var turnLeft = new TurnLeft();
    98       var turnRight = new TurnRight();
    99       var turnGunLeft = new TurnGunLeft();
    100       var turnGunRight = new TurnGunRight();
    101       var turnRadarLeft = new TurnRadarLeft();
    102       var turnRadarRight = new TurnRadarRight();
    103 
    104       var onBulletHit = new OnBulletHit();
    105       var onBulletMissed = new OnBulletMissed();
    106       var onHitByBullet = new OnHitByBullet();
    107       var onHitRobot = new OnHitRobot();
    108       var onHitWall = new OnHitWall();
    109       var onScannedRobot = new OnScannedRobot();
    110 
    111       var run = new Run();
    112       var tank = new Tank();
    113 
    114       var doNothing = new DoNothing();
    115       var emptyEvent = new EmptyEvent();
     90      var getEnergy = new SimpleSymbol("GetEnergy", "Returns the robot's current energy.", 0, 0);
     91      var getHeading = new SimpleSymbol("GetHeading", "Returns the direction that the robot's body is facing, in degrees.", 0, 0);
     92      var getGunHeading = new SimpleSymbol("GetGunHeading", "Returns the direction that the robot's gun is facing, in degrees.", 0, 0);
     93      var getRadarHeading = new SimpleSymbol("GetRadarHeading", "Returns the direction that the robot's radar is facing, in degrees.", 0, 0);
     94      var getX = new SimpleSymbol("GetX", "Returns the X position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0);
     95      var getY = new SimpleSymbol("GetY", "Returns the Y position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0);
     96
     97      var turnLeft = new SimpleSymbol("TurnLeft", "Immediately turns the robot's body to the left by degrees.", 1, 1);
     98      var turnRight = new SimpleSymbol("TurnRight", "Immediately turns the robot's body to the right by degrees.", 1, 1);
     99      var turnGunLeft = new SimpleSymbol("TurnGunLeft", "Immediately turns the robot's gun to the left by degrees.", 1, 1);
     100      var turnGunRight = new SimpleSymbol("TurnGunRight", "Immediately turns the robot's gun to the right by degrees.", 1, 1);
     101      var turnRadarLeft = new SimpleSymbol("TurnRadarLeft", "Immediately turns the robot's radar to the left by degrees.", 1, 1);
     102      var turnRadarRight = new SimpleSymbol("TurnRadarRight", "Immediately turns the robot's radar to the right by degrees.", 1, 1);
     103
     104      var onBulletHit = new SimpleSymbol("OnBulletHit", "This method is called when one of your bullets hits another robot.", 2, 10);
     105      var onBulletMissed = new SimpleSymbol("OnBulletMissed", "This method is called when one of your bullets misses, i.e. hits a wall.", 2, 10);
     106      var onHitByBullet = new SimpleSymbol("OnHitByBullet", "This method is called when your robot is hit by a bullet.", 2, 10);
     107      var onHitRobot = new SimpleSymbol("OnHitRobot", "This method is called when your robot collides with another robot.", 2, 10);
     108      var onHitWall = new SimpleSymbol("OnHitWall", "This method is called when your robot collides with a wall.", 2, 10);
     109      var onScannedRobot = new SimpleSymbol("OnScannedRobot", "This method is called when your robot sees another robot, i.e. when the robot's radar scan \"hits\" another robot.", 2, 10);
     110
     111      var run = new SimpleSymbol("Run", "The main method in every robot.", 1, 10);
     112      var tank = new SimpleSymbol("Tank", "The root of a Robocode Tank program.", 2, 7);
     113
     114      var doNothing = new SimpleSymbol("DoNothing", "Do nothing this turn, meaning that the robot will skip its turn.", 0, 0);
     115      var emptyEvent = new SimpleSymbol("EmptyEvent", "This is a placeholder for an empty event.", 0, 0);
    116116      #endregion
    117117
     
    127127      var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
    128128      var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);
    129       var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
     129      var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { boolExpr, numericalExpr });
    130130      var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols);
    131131      var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols);
     
    183183
    184184      // IfStat
    185       AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0);
     185      AddAllowedChildSymbol(ifThenElseStat, boolExpr, 0);
    186186      AddAllowedChildSymbol(ifThenElseStat, stat, 1);
    187187      AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1);
     
    192192
    193193      // WhileStat
    194       AddAllowedChildSymbol(whileStat, logicalExpr, 0);
     194      AddAllowedChildSymbol(whileStat, boolExpr, 0);
    195195      AddAllowedChildSymbol(whileStat, stat, 1);
    196196      AddAllowedChildSymbol(whileStat, emptyEvent, 1);
     
    206206
    207207      // Logical Expressions
    208       AddAllowedChildSymbol(logicalExpr, logicalVal);
    209       AddAllowedChildSymbol(logicalExpr, logicalOperators);
     208      AddAllowedChildSymbol(boolExpr, logicalVal);
     209      AddAllowedChildSymbol(boolExpr, logicalOperators);
    210210      AddAllowedChildSymbol(logicalOperators, logicalVal);
    211211      AddAllowedChildSymbol(logicalOperators, logicalOperators);
     
    216216      AddAllowedChildSymbol(robocodeFunctions, numericalExpr);
    217217      foreach (var a in robocodeActions.Symbols) {
    218         if (a is Fire) AddAllowedChildSymbol(a, shotPower);
     218        if (a.Name == fire.Name) AddAllowedChildSymbol(a, shotPower);
    219219        else AddAllowedChildSymbol(a, numericalExpr);
    220220      }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/HeuristicLab.Problems.Robocode-3.3.csproj

    r13015 r13017  
    103103    <Compile Include="Grammar.cs" />
    104104    <Compile Include="Interpreter.cs" />
    105     <Compile Include="RobocodeProblem.cs" />
     105    <Compile Include="Problem.cs" />
    106106    <None Include="Properties\AssemblyInfo.cs.frame" />
    107107    <Compile Include="Solution.cs" />
    108     <Compile Include="Symbols\Event Methods\Events\EmptyEvent.cs" />
    109     <Compile Include="Symbols\Logical Expressions\Logical Comparators\ILogicalComparator.cs" />
    110     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\INumericalOperator.cs" />
    111     <Compile Include="Symbols\Numerical Methods\INumericalMethod.cs" />
    112     <Compile Include="Symbols\Stat.cs" />
    113     <Compile Include="Symbols\Void Methods\Ahead.cs" />
    114     <Compile Include="Symbols\Void Methods\Back.cs" />
    115     <Compile Include="Symbols\Block.cs" />
    116     <Compile Include="Symbols\Statements\IfThenElseStat.cs" />
    117     <Compile Include="Symbols\Statements\WhileStat.cs" />
     108    <Compile Include="Symbols\CodeNode.cs">
     109      <SubType>Code</SubType>
     110    </Compile>
    118111    <Compile Include="Symbols\Logical Expressions\BooleanTreeNode.cs" />
    119     <Compile Include="Symbols\Logical Expressions\Conjunction.cs" />
    120     <Compile Include="Symbols\Logical Expressions\Disjunction.cs" />
    121     <Compile Include="Symbols\Logical Expressions\Logical Comparators\Equal.cs" />
    122     <Compile Include="Symbols\Logical Expressions\Logical Comparators\GreaterThan.cs" />
    123     <Compile Include="Symbols\Logical Expressions\Logical Comparators\GreaterThanOrEqual.cs" />
    124     <Compile Include="Symbols\Logical Expressions\Logical Comparators\LessThan.cs" />
    125     <Compile Include="Symbols\Logical Expressions\Logical Comparators\LessThanOrEqual.cs" />
    126     <Compile Include="Symbols\Logical Expressions\LogicalExpression.cs" />
    127     <Compile Include="Symbols\Logical Expressions\LogicalValue.cs" />
    128     <Compile Include="Symbols\Logical Expressions\Negation.cs" />
     112    <Compile Include="Symbols\Logical Expressions\BooleanValue.cs" />
    129113    <Compile Include="Symbols\Numerical Expressions\NumberTreeNode.cs" />
    130114    <Compile Include="Symbols\Numerical Expressions\Number.cs" />
    131     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Addition.cs" />
    132     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Division.cs" />
    133     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Modulus.cs" />
    134     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Multiplication.cs" />
    135     <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Subtraction.cs" />
    136     <Compile Include="Symbols\Numerical Expressions\NumericalExpression.cs" />
    137     <Compile Include="Symbols\CodeNode.cs" />
    138     <Compile Include="Symbols\Tank.cs" />
    139     <Compile Include="Symbols\Void Methods\DoNothing.cs" />
    140     <Compile Include="Symbols\Event Methods\Events\OnBulletHit.cs" />
    141     <Compile Include="Symbols\Event Methods\Events\OnBulletMissed.cs" />
    142     <Compile Include="Symbols\Event Methods\Events\OnHitByBullet.cs" />
    143     <Compile Include="Symbols\Event Methods\Events\OnHitRobot.cs" />
    144     <Compile Include="Symbols\Event Methods\Events\OnHitWall.cs" />
    145     <Compile Include="Symbols\Event Methods\Events\OnScannedRobot.cs" />
    146     <Compile Include="Symbols\Void Methods\Fire.cs" />
    147     <Compile Include="Symbols\Numerical Methods\GetEnergy.cs" />
    148     <Compile Include="Symbols\Numerical Methods\GetGunHeading.cs" />
    149     <Compile Include="Symbols\Numerical Methods\GetHeading.cs" />
    150115    <Compile Include="Plugin.cs" />
    151116    <Compile Include="Properties\AssemblyInfo.cs" />
    152     <Compile Include="Symbols\Numerical Methods\GetRadarHeading.cs" />
    153     <Compile Include="Symbols\Numerical Methods\GetX.cs" />
    154     <Compile Include="Symbols\Numerical Methods\GetY.cs" />
    155     <Compile Include="Symbols\Program.cs" />
    156     <Compile Include="Symbols\Event Methods\Run.cs" />
    157117    <Compile Include="Symbols\Numerical Expressions\ShotPower.cs" />
    158118    <Compile Include="Symbols\Numerical Expressions\ShotPowerTreeNode.cs" />
    159     <Compile Include="Symbols\Void Methods\TurnGunLeft.cs" />
    160     <Compile Include="Symbols\Void Methods\TurnGunRight.cs" />
    161     <Compile Include="Symbols\Void Methods\TurnLeft.cs" />
    162     <Compile Include="Symbols\Void Methods\TurnRadarLeft.cs" />
    163     <Compile Include="Symbols\Void Methods\TurnRadarRight.cs" />
    164     <Compile Include="Symbols\Void Methods\TurnRight.cs" />
    165119  </ItemGroup>
    166120  <ItemGroup>
    167121    <Content Include="BattleRunner.java" />
     122  </ItemGroup>
     123  <ItemGroup>
     124    <Folder Include="Symbols\Event Methods\Events\" />
     125    <Folder Include="Symbols\Logical Expressions\Logical Comparators\" />
     126    <Folder Include="Symbols\Numerical Expressions\Numerical Operators\" />
     127    <Folder Include="Symbols\Numerical Methods\" />
     128    <Folder Include="Symbols\Statements\" />
     129    <Folder Include="Symbols\Void Methods\" />
    168130  </ItemGroup>
    169131  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs

    r13013 r13017  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Diagnostics;
     25using System.Diagnostics.Contracts;
    2426using System.Globalization;
    2527using System.IO;
    2628using System.Linq;
    2729using System.Reflection;
     30using System.Runtime.Remoting.Messaging;
    2831using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2932
    3033namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    3134  public static class Interpreter {
     35    // TODO performance: it would probably be useful to implement the BattleRunner in such a way that we don't have to restart the java process each time, e.g. using console IO to load & run robots
    3236    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) {
    3337      if (robotName == null)
     
    148152    public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) {
    149153      var tankNode = node;
    150       while (!(tankNode.Symbol is Tank))
     154      while (tankNode.Symbol.Name != "Tank")
    151155        tankNode = tankNode.GetSubtree(0);
    152156
    153       string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
     157      string result = Interpret(tankNode);
    154158      result = result.Replace("class output", "class " + robotName);
    155159      return result;
    156160    }
     161
     162    private static string Interpret(ISymbolicExpressionTreeNode node) {
     163      switch (node.Symbol.Name) {
     164        case "Block": return InterpretBlock(node);
     165        case "Statement": return InterpretStat(node);
     166        case "DoNothing": return string.Empty;
     167        case "EmptyEvent": return string.Empty;
     168
     169        case "GetEnergy": return "getEnergy()";
     170        case "GetHeading": return "getHeading()";
     171        case "GetGunHeading": return "getGunHeading()";
     172        case "GetRadarHeading": return "getRadarHeading()";
     173        case "GetX": return "getX()";
     174        case "GetY": return "getY()";
     175
     176        case "Addition": return InterpretBinaryOperator(" + ", node);
     177        case "Subtraction": return InterpretBinaryOperator(" - ", node);
     178        case "Multiplication": return InterpretBinaryOperator(" * ", node);
     179        case "Division": return InterpretBinaryOperator(" / ", node);
     180        case "Modulus": return InterpretBinaryOperator(" % ", node);
     181
     182        case "Equal": return InterpretComparison(" == ", node);
     183        case "LessThan": return InterpretComparison(" < ", node);
     184        case "LessThanOrEqual": return InterpretComparison(" <= ", node);
     185        case "GreaterThan": return InterpretComparison("  >", node);
     186        case "GreaterThanOrEqual": return InterpretComparison(" >= ", node);
     187        case "ConditionalAnd": return InterpretBinaryOperator(" && ", node);
     188        case "ConditionalOr": return InterpretBinaryOperator(" || ", node);
     189        case "Negation": return InterpretFunc1("!", node);
     190
     191        case "IfThenElseStat": return InterpretIf(node);
     192        case "WhileStat": return InterpretWhile(node);
     193        case "BooleanExpression": return InterpretChild(node);
     194        case "NumericalExpression": return InterpretChild(node);
     195        case "Number": return InterpretNumber(node);
     196        case "BooleanValue": return InterpretBoolValue(node);
     197
     198
     199        case "Ahead": return InterpretFunc1("setAhead", node);
     200        case "Back": return InterpretFunc1("setBack", node);
     201        case "Fire": return InterpretFunc1("setFire", node);
     202        case "TurnLeft": return InterpretFunc1("setTurnLeft", node);
     203        case "TurnRight": return InterpretFunc1("setTurnRight", node);
     204        case "TurnGunLeft": return InterpretFunc1("setTurnGunLeft", node);
     205        case "TurnGunRight": return InterpretFunc1("setTurnGunRight", node);
     206        case "TurnRadarLeft": return InterpretFunc1("setTurnRadarLeft", node);
     207        case "TurnRadarRight": return InterpretFunc1("setTurnRadarRight", node);
     208        case "ShotPower": return InterpetShotPower(node);
     209
     210        case "OnBulletHit": return InterpretOnBulletHit(node);
     211        case "OnBulletMissed": return InterpretOnBulletMissed(node);
     212        case "OnHitByBullet": return InterpretOnHitByBullet(node);
     213        case "OnHitRobot": return InterpretOnHitRobot(node);
     214        case "OnHitWall": return InterpretOnHitWall(node);
     215        case "OnScannedRobot": return InterpretOnScannedRobot(node);
     216
     217        case "Run": return InterpretRun(node);
     218        case "Tank": return InterpretTank(node);
     219        case "CodeSymbol": return InterpretCodeSymbol(node);
     220
     221        default: throw new ArgumentException(string.Format("Found an unknown symbol {0} in a robocode solution", node.Symbol.Name));
     222      }
     223    }
     224
     225    private static string InterpretCodeSymbol(ISymbolicExpressionTreeNode node) {
     226      var sy = node.Symbol as CodeSymbol;
     227      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     228      return string.Format(
     229@"{0}
     230{1}
     231{2}", sy.Prefix, code, sy.Suffix);
     232    }
     233
     234    private static string InterpretBoolValue(ISymbolicExpressionTreeNode node) {
     235      var boolNode = node as BooleanTreeNode;
     236      return string.Format(NumberFormatInfo.InvariantInfo, "{0}", boolNode.Value).ToLower();
     237    }
     238
     239    private static string InterpretNumber(ISymbolicExpressionTreeNode node) {
     240      var numberNode = node as NumberTreeNode;
     241      return string.Format(NumberFormatInfo.InvariantInfo, "{0}", numberNode.Value);
     242    }
     243
     244    private static string InterpetShotPower(ISymbolicExpressionTreeNode node) {
     245      var shotPowerNode = node as ShotPowerTreeNode;
     246      return string.Format(NumberFormatInfo.InvariantInfo, "{0:E}", shotPowerNode.Value);
     247    }
     248
     249
     250    internal static string InterpretBlock(ISymbolicExpressionTreeNode node) {
     251      string result = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     252      return string.Format("{{ {0} }}", result + Environment.NewLine);
     253    }
     254
     255    internal static string InterpretStat(ISymbolicExpressionTreeNode node) {
     256      // must only have one sub-tree
     257      Contract.Assert(node.SubtreeCount == 1);
     258      return Interpret(node.GetSubtree(0)) + " ;" + Environment.NewLine;
     259    }
     260
     261    internal static string InterpretIf(ISymbolicExpressionTreeNode node) {
     262      ISymbolicExpressionTreeNode condition = null, truePart = null, falsePart = null;
     263      string[] parts = new string[3];
     264
     265      if (node.SubtreeCount < 2 || node.SubtreeCount > 3)
     266        throw new Exception("Unexpected number of children. Expected 2 or 3 children.");
     267
     268      condition = node.GetSubtree(0);
     269      truePart = node.GetSubtree(1);
     270      if (node.SubtreeCount == 3)
     271        falsePart = node.GetSubtree(2);
     272
     273      parts[0] = Interpret(condition);
     274      parts[1] = Interpret(truePart);
     275      if (falsePart != null) parts[2] = Interpret(falsePart);
     276
     277      return string.Format("if ({0}) {{ {1} }} else {{ {2} }}", Interpret(condition), Interpret(truePart),
     278        falsePart == null ? string.Empty : Interpret(falsePart));
     279    }
     280
     281    internal static string InterpretWhile(ISymbolicExpressionTreeNode node) {
     282      var cond = Interpret(node.GetSubtree(0));
     283      var body = Interpret(node.GetSubtree(1));
     284      return string.Format("while ({0}) {{ {2} {1} {2} }} {2}", cond, body, Environment.NewLine);
     285    }
     286
     287    public static string InterpretBinaryOperator(string opSy, ISymbolicExpressionTreeNode node) {
     288      if (node.SubtreeCount < 2)
     289        throw new ArgumentException(string.Format("Expected at least two children in {0}.", node.Symbol), "node");
     290
     291      string result = string.Join(opSy, node.Subtrees.Select(Interpret));
     292      return "(" + result + ")";
     293    }
     294
     295    public static string InterpretChild(ISymbolicExpressionTreeNode node) {
     296      if (node.SubtreeCount != 1)
     297        throw new ArgumentException(string.Format("Expected exactly one child in {0}.", node.Symbol), "node");
     298
     299      return Interpret(node.GetSubtree(0));
     300    }
     301
     302    public static string InterpretComparison(string compSy, ISymbolicExpressionTreeNode node) {
     303      ISymbolicExpressionTreeNode lhs = null, rhs = null;
     304      if (node.SubtreeCount != 2)
     305        throw new ArgumentException(string.Format("Expected exactly two children in {0}.", node.Symbol), "node");
     306
     307      lhs = node.GetSubtree(0);
     308      rhs = node.GetSubtree(1);
     309
     310      return Interpret(lhs) + " == " + Interpret(rhs);
     311    }
     312
     313    public static string InterpretFunc1(string functionId, ISymbolicExpressionTreeNode node) {
     314      if (node.SubtreeCount != 1)
     315        throw new ArgumentException(string.Format("Expected 1 child in {0}.", node.Symbol.Name), "node");
     316
     317      return string.Format("{0}({1})", functionId, Interpret(node.GetSubtree(0)));
     318    }
     319
     320    public static string InterpretOnScannedRobot(ISymbolicExpressionTreeNode node) {
     321      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     322      return string.Format(
     323@"public void onScannedRobot(ScannedRobotEvent e) {{
     324  double absoluteBearing = getHeading() + e.getBearing();
     325  double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
     326  setTurnGunRight(bearingFromGun);
     327{0}
     328  execute();
     329}}", code);
     330    }
     331
     332
     333    public static string InterpretOnHitWall(ISymbolicExpressionTreeNode node) {
     334      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     335      return string.Format(
     336@"public void onHitWall(HitWallEvent e) {{
     337{0}
     338execute();
     339}}", code);
     340    }
     341
     342    public static string InterpretOnHitRobot(ISymbolicExpressionTreeNode node) {
     343      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     344      return string.Format(
     345@"public void onHitRobot(HitRobotEvent e) {{
     346{0}
     347execute();
     348}}", code);
     349    }
     350
     351    public static string InterpretOnHitByBullet(ISymbolicExpressionTreeNode node) {
     352      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     353      return string.Format(
     354@"public void onHitByBullet(HitByBulletEvent e) {{
     355{0}
     356execute();
     357}}", code);
     358    }
     359
     360    public static string InterpretOnBulletMissed(ISymbolicExpressionTreeNode node) {
     361      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     362      return string.Format(
     363@"public void onBulletMissed(BulletMissedEvent e) {{
     364{0}
     365execute();
     366}}", code);
     367    }
     368
     369    public static string InterpretOnBulletHit(ISymbolicExpressionTreeNode node) {
     370      var Prefix = "public void onBulletHit(BulletHitEvent e) {";
     371      var Suffix =
     372@"execute();
     373}";
     374      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     375      return Prefix + code + Environment.NewLine + Suffix;
     376    }
     377
     378    public static string InterpretRun(ISymbolicExpressionTreeNode node) {
     379      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     380      return string.Format(
     381@"public void run() {{
     382  setAdjustGunForRobotTurn(true);
     383  turnRadarRightRadians(Double.POSITIVE_INFINITY);
     384{0}
     385  execute();
     386}}", code);
     387    }
     388
     389    public static string InterpretTank(ISymbolicExpressionTreeNode node) {
     390      string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret));
     391      return string.Format(
     392@"package Evaluation;
     393import robocode.*;
     394import robocode.Robot;
     395import robocode.util.*;
     396import static robocode.util.Utils.normalRelativeAngleDegrees;
     397import java.awt.*;
     398
     399public class output extends AdvancedRobot {{
     400{0}
     401}}", code);
     402    }
    157403  }
    158404}
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Problem.cs

    r13016 r13017  
    3232  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 360)]
    3333  [Item("Robocode Problem", "Evolution of a robocode program in java using genetic programming.")]
    34   public class RobocodeProblem : SymbolicExpressionTreeProblem {
     34  public class Problem : SymbolicExpressionTreeProblem {
    3535    #region Parameter Names
    36     private const string MaxTankProgramLengthParameterName = "MaxProgramLength";
    37     private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";
    3836    private const string RobocodePathParamaterName = "RobocodePath";
    3937    private const string NrOfRoundsParameterName = "NrOfRounds";
     
    4240
    4341    #region Parameters
    44     //public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
    45     //  get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
    46     //}
    47     //public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
    48     //  get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
    49     //}
    5042    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
    5143      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
     
    5749      get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
    5850    }
    59 
    60     //public int MaxTankProgramDepth {
    61     //  get { return MaxTankProgramDepthParameter.Value.Value; }
    62     //  set { MaxTankProgramDepthParameter.Value.Value = value; }
    63     //}
    64     //
    65     //public int MaxTankProgramLength {
    66     //  get { return MaxTankProgramLengthParameter.Value.Value; }
    67     //  set { MaxTankProgramLengthParameter.Value.Value = value; }
    68     //}
    6951
    7052    public string RobocodePath {
     
    8567
    8668    [StorableConstructor]
    87     protected RobocodeProblem(bool deserializing) : base(deserializing) { }
    88     protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
     69    protected Problem(bool deserializing) : base(deserializing) { }
     70    protected Problem(Problem original, Cloner cloner)
    8971      : base(original, cloner) {
    9072      RegisterEventHandlers();
    9173    }
    9274
    93     public RobocodeProblem()
     75    public Problem()
    9476      : base() {
    95       DirectoryValue robocodeDir = new DirectoryValue { Value = @"C:\robocode" };
     77      DirectoryValue robocodeDir = new DirectoryValue { Value = @"robocode" };
    9678
    9779      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
     
    11193
    11294    public override IDeepCloneable Clone(Cloner cloner) {
    113       return new RobocodeProblem(this, cloner);
     95      return new Problem(this, cloner);
    11496    }
    11597
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/CodeNode.cs

    r13013 r13017  
    2727namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    2828  [StorableClass]
    29   public abstract class CodeNode : Symbol {
    30     public abstract string Prefix { get; set; }
    31     public abstract string Suffix { get; set; }
     29  // a symbol that can represent any user-defined fragment of code
     30  public sealed class CodeSymbol : Symbol {
     31    public override int MinimumArity { get { return 1; } }
     32    public override int MaximumArity { get { return 10; } }
     33
     34    public string Prefix { get; set; }
     35    public string Suffix { get; set; }
     36
     37    public override bool CanChangeName { get { return false; } } // cannot change, otherwise we cannot detect these symbols in the interpreter
     38    public override bool CanChangeDescription { get { return false; } }
    3239
    3340    [StorableConstructor]
    34     protected CodeNode(bool deserializing) : base(deserializing) { }
    35     protected CodeNode(CodeNode original, Cloner cloner)
     41    private CodeSymbol(bool deserializing) : base(deserializing) { }
     42    private CodeSymbol(CodeSymbol original, Cloner cloner)
    3643      : base(original, cloner) {
    3744      Prefix = original.Prefix;
    3845      Suffix = original.Suffix;
    3946    }
    40     protected CodeNode(string name, string description)
    41       : base(name, description) {
     47    public CodeSymbol()
     48      : base("CodeSymbol", "The CodeSymbol symbol can represent any user-defined fragment of code.") {
    4249      Prefix = string.Empty;
    4350      Suffix = string.Empty;
    4451    }
    4552
    46     public abstract string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children);
     53    public override IDeepCloneable Clone(Cloner cloner) {
     54      return new CodeSymbol(this, cloner);
     55    }
    4756  }
    4857}
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Logical Expressions/BooleanTreeNode.cs

    r13013 r13017  
    4242    }
    4343
    44     public BooleanTreeNode() : base(new LogicalValue()) { }
     44    public BooleanTreeNode(BooleanValue boolValueSy) : base(boolValueSy) { }
    4545
    4646    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Number.cs

    r13013 r13017  
    2828namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    2929  [StorableClass]
    30   public class Number : CodeNode {
     30  public class Number : Symbol {
    3131    public override int MinimumArity { get { return 0; } }
    3232    public override int MaximumArity { get { return 0; } }
    33 
    34     [Storable]
    35     public override string Prefix { get; set; }
    36 
    37     [Storable]
    38     public override string Suffix { get; set; }
    3933
    4034    [StorableConstructor]
     
    4539
    4640    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    47       return new NumberTreeNode();
     41      return new NumberTreeNode(this);
    4842    }
    4943
     
    5246    }
    5347
    54     public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
     48    public string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    5549      return ((NumberTreeNode)node).Value.ToString(CultureInfo.InvariantCulture);
    5650    }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumberTreeNode.cs

    r13013 r13017  
    4242    }
    4343
    44     public NumberTreeNode() : base(new Number()) { }
     44    public NumberTreeNode(Number numberSy) : base(numberSy) { }
    4545
    4646    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPower.cs

    r13013 r13017  
    2828namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    2929  [StorableClass]
    30   public class ShotPower : CodeNode {
     30  public class ShotPower : Symbol {
    3131    public override int MinimumArity { get { return 0; } }
    3232    public override int MaximumArity { get { return 0; } }
    33 
    34     [Storable]
    35     public override string Prefix { get; set; }
    36 
    37     [Storable]
    38     public override string Suffix { get; set; }
    3933
    4034    [StorableConstructor]
     
    4539
    4640    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    47       return new ShotPowerTreeNode();
     41      return new ShotPowerTreeNode(this);
    4842    }
    4943
     
    5246    }
    5347
    54     public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
     48    public string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    5549      return ((ShotPowerTreeNode)node).Value.ToString(CultureInfo.InvariantCulture);
    5650    }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPowerTreeNode.cs

    r13013 r13017  
    4343    }
    4444
    45     public ShotPowerTreeNode() : base(new ShotPower()) { }
     45    public ShotPowerTreeNode(ShotPower powerSy) : base(powerSy) { }
    4646
    4747    public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset for help on using the changeset viewer.