[9565] | 1 | using System;
|
---|
[9601] | 2 | using System.Diagnostics;
|
---|
| 3 | using System.IO;
|
---|
[9565] | 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Xml;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
[9601] | 8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[9565] | 9 |
|
---|
[9601] | 10 | namespace HeuristicLab.Problems.Robocode {
|
---|
| 11 | public class Interpreter {
|
---|
[9565] | 12 |
|
---|
[9601] | 13 | private struct TankStats {
|
---|
| 14 | public int moves, shots;
|
---|
| 15 | public TankStats(int moves, int shots) {
|
---|
| 16 | this.moves = moves;
|
---|
| 17 | this.shots = shots;
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 | static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
[9565] | 21 |
|
---|
[9601] | 22 | public static string BuildTankProgram(ISymbolicExpressionTree tree) {
|
---|
| 23 | XmlDocument solutionCandidateRoot = new XmlDocument();
|
---|
| 24 | solutionCandidateRoot.Load("../tank.xml");
|
---|
[9565] | 25 |
|
---|
[9601] | 26 | TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot.FirstChild, null);
|
---|
| 27 | return solutionCandidateRoot.InnerText;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public static double EvaluateTankProgram(ISymbolicExpressionTree tree, XmlNode solutionCandidateRoot, string path) {
|
---|
| 31 | //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null);
|
---|
| 32 | //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves;
|
---|
| 33 | string interpretedProgram = InterpretProgramTree(tree.Root);
|
---|
| 34 | Random random = new Random();
|
---|
| 35 | string formattedPath = path.Replace("/", "\\");
|
---|
| 36 | string outputname = "";
|
---|
| 37 | string output = "";
|
---|
| 38 | bool noError = true;
|
---|
| 39 | do {
|
---|
| 40 | try {
|
---|
| 41 | outputname = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
|
---|
| 42 | output = /*solutionCandidateRoot.InnerText*/interpretedProgram.Replace("class output", "class " + outputname);
|
---|
| 43 | File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);
|
---|
| 44 | noError = true;
|
---|
[9565] | 45 | }
|
---|
[9601] | 46 | catch (Exception e) { noError = false; }
|
---|
| 47 | } while (!noError);
|
---|
[9565] | 48 |
|
---|
[9601] | 49 | //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
---|
| 50 | //path.Replace("\\", "\\\\");
|
---|
| 51 | //path.Replace(" ", "\\ ");
|
---|
| 52 | #region Compile code file
|
---|
[9565] | 53 |
|
---|
[9601] | 54 | ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
|
---|
| 55 | javaCompileInfo.FileName = "cmd.exe";
|
---|
| 56 | javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java";
|
---|
| 57 | javaCompileInfo.RedirectStandardOutput = true;
|
---|
| 58 | javaCompileInfo.RedirectStandardError = true;
|
---|
| 59 | javaCompileInfo.UseShellExecute = false;
|
---|
| 60 | javaCompileInfo.CreateNoWindow = true;
|
---|
[9565] | 61 |
|
---|
[9601] | 62 | Process javaCompile = new Process();
|
---|
| 63 | javaCompile.StartInfo = javaCompileInfo;
|
---|
| 64 | javaCompile.Start();
|
---|
[9565] | 65 |
|
---|
[9601] | 66 | /*Evaluator.tankStatLog = "=====================================================\r\n";
|
---|
| 67 | Evaluator.tankStatLog += "Tree Compilation Output: \r\n";
|
---|
| 68 | Evaluator.tankStatLog += "=====================================================\r\n";*/
|
---|
[9565] | 69 |
|
---|
[9601] | 70 | string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
|
---|
| 71 | cmdOutput += javaCompile.StandardError.ReadToEnd();
|
---|
[9612] | 72 |
|
---|
[9601] | 73 | //Evaluator.tankStatLog += cmdOutput;
|
---|
| 74 | //Console.WriteLine(cmdOutput);
|
---|
| 75 | //Console.ReadLine();
|
---|
[9565] | 76 |
|
---|
[9601] | 77 | /*Evaluator.tankStatLog = "=====================================================\r\n";
|
---|
| 78 | Evaluator.tankStatLog += "End of Tree Compilation Output: \r\n";
|
---|
| 79 | Evaluator.tankStatLog += "=====================================================\r\n";*/
|
---|
[9565] | 80 |
|
---|
[9601] | 81 | javaCompile.WaitForExit();
|
---|
[9612] | 82 | if (javaCompile.ExitCode != 0) {
|
---|
| 83 | return -1000.0;
|
---|
| 84 | }
|
---|
[9601] | 85 | #endregion
|
---|
[9565] | 86 |
|
---|
| 87 |
|
---|
[9601] | 88 | try { File.Delete(path + "/robots/robot.database"); }
|
---|
| 89 | catch (Exception e) { }
|
---|
[9565] | 90 |
|
---|
[9601] | 91 | #region evaluate code
|
---|
[9565] | 92 |
|
---|
[9601] | 93 | ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
|
---|
| 94 | evaluateCodeInfo.FileName = "cmd.exe";
|
---|
| 95 | //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";
|
---|
| 96 | evaluateCodeInfo.Arguments = "/C java -classpath " + formattedPath + "\\libs;" + formattedPath + "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" + formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." + outputname + "* false";
|
---|
| 97 | //Console.WriteLine(javaCompileInfo.Arguments);
|
---|
| 98 | evaluateCodeInfo.RedirectStandardOutput = true;
|
---|
| 99 | evaluateCodeInfo.RedirectStandardError = true;
|
---|
| 100 | evaluateCodeInfo.UseShellExecute = false;
|
---|
| 101 | evaluateCodeInfo.CreateNoWindow = true;
|
---|
[9565] | 102 |
|
---|
[9601] | 103 | Process evaluateCode = new Process();
|
---|
| 104 | evaluateCode.StartInfo = evaluateCodeInfo;
|
---|
| 105 | evaluateCode.Start();
|
---|
[9565] | 106 |
|
---|
[9601] | 107 | StringBuilder q = new StringBuilder();
|
---|
| 108 | string scoreString = "";
|
---|
[9612] | 109 | evaluateCode.WaitForExit();
|
---|
| 110 |
|
---|
| 111 | try {
|
---|
[9601] | 112 | scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(
|
---|
| 113 | new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
|
---|
| 114 | q.Append("Output: " + scoreString + "\r\n");
|
---|
| 115 | q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");
|
---|
[9612] | 116 | cmdOutput += q.ToString();
|
---|
| 117 | }catch {
|
---|
| 118 | return -1000.0;
|
---|
[9601] | 119 | }
|
---|
[9565] | 120 |
|
---|
[9601] | 121 | #endregion
|
---|
[9565] | 122 |
|
---|
[9601] | 123 | double evaluation = -900;
|
---|
| 124 | try {
|
---|
| 125 | File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
|
---|
| 126 | File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
|
---|
| 127 | evaluation = Double.Parse(scoreString);
|
---|
| 128 | }
|
---|
| 129 | catch (Exception e) { evaluation = -900; }
|
---|
| 130 | Random rand = new Random();
|
---|
| 131 | evaluation += rand.NextDouble() / 100;
|
---|
[9565] | 132 |
|
---|
[9601] | 133 | /*Evaluator.tankStatLog = "=====================================================\r\n";
|
---|
| 134 | Evaluator.tankStatLog += "START OF TREE - Score: " + evaluation + " (" + tankStats.shots + "/" + tankStats.moves + ")" + "\r\n" + " Compilation Output: " + cmdOutput + "\r\n";
|
---|
| 135 | Evaluator.tankStatLog += "=====================================================\r\n";
|
---|
[9565] | 136 |
|
---|
[9601] | 137 | Evaluator.tankStatLog += solutionCandidateRoot.InnerText + "\r\n";
|
---|
[9565] | 138 |
|
---|
[9601] | 139 | Evaluator.tankStatLog += "=====================================================\r\n";
|
---|
| 140 | Evaluator.tankStatLog += "END OF TREE\r\n";
|
---|
| 141 | Evaluator.tankStatLog += "=====================================================\r\n";
|
---|
[9565] | 142 |
|
---|
[9601] | 143 | File.AppendAllText("../TankLog.txt", Evaluator.tankStatLog);*/
|
---|
| 144 | return evaluation;
|
---|
| 145 | }
|
---|
[9565] | 146 |
|
---|
[9601] | 147 | public static double EvaluateTankProgram(ISymbolicExpressionTree tree, ScopeList coevolutionIndividuals, XmlNode solutionCandidateRoot, string path) {
|
---|
| 148 | Random random = new Random();
|
---|
| 149 | string formattedPath = path.Replace("/", "\\");
|
---|
[9565] | 150 |
|
---|
[9601] | 151 | // Interpret and compile main tree
|
---|
| 152 | //XmlNode mainTreeNode = solutionCandidateRoot.Clone();
|
---|
| 153 | //EvaluateTankProgram(tree.Root, mainTreeNode, null);
|
---|
| 154 | string interpretedMainTree = InterpretProgramTree(tree.Root);
|
---|
| 155 | string mainTreeName = writeTreeToFile(interpretedMainTree, path, random);
|
---|
| 156 | compileCodefile(/*mainTreeName,*/ mainTreeName, formattedPath);
|
---|
[9565] | 157 |
|
---|
[9601] | 158 | // Interpret and compile coevolutionIndividuals
|
---|
| 159 | String[] coevolutionIndividualsNames = new String[coevolutionIndividuals.Count];
|
---|
| 160 | for (int i = 0; i < coevolutionIndividuals.Count; i++) {
|
---|
| 161 | //XmlNode individualTreeNode = solutionCandidateRoot.Clone();
|
---|
| 162 | //EvaluateTankProgram(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root, individualTreeNode, null);
|
---|
| 163 | string interpretedIndividual = InterpretProgramTree(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root);
|
---|
| 164 | coevolutionIndividualsNames[i] = writeTreeToFile(/*individualTreeNode,*/ interpretedIndividual, path, random);
|
---|
| 165 | compileCodefile(coevolutionIndividualsNames[i], formattedPath);
|
---|
| 166 | }
|
---|
[9565] | 167 |
|
---|
[9601] | 168 | // Force Robocode to update its robot database by deleting the existing one
|
---|
| 169 | try { File.Delete(path + "/robots/robot.database"); }
|
---|
| 170 | catch (Exception e) { }
|
---|
[9565] | 171 |
|
---|
[9601] | 172 | // Run evaluator
|
---|
| 173 | double evaluation = runEvaluator(mainTreeName, coevolutionIndividualsNames, formattedPath);
|
---|
| 174 | try {
|
---|
| 175 | File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".java");
|
---|
| 176 | File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".class");
|
---|
| 177 | foreach (string s in coevolutionIndividualsNames) {
|
---|
| 178 | File.Delete(path + "/robots/Evaluation/" + s + ".java");
|
---|
| 179 | File.Delete(path + "/robots/Evaluation/" + s + ".class");
|
---|
[9565] | 180 | }
|
---|
[9601] | 181 | }
|
---|
| 182 | catch (Exception e) { }
|
---|
| 183 | Random rand = new Random();
|
---|
| 184 | evaluation += rand.NextDouble() / 100;
|
---|
[9565] | 185 |
|
---|
[9601] | 186 | return evaluation;
|
---|
| 187 | }
|
---|
[9565] | 188 |
|
---|
[9601] | 189 | private static string writeTreeToFile(/*XmlNode node,*/ string interpretedTree, string path, Random random) {
|
---|
| 190 | string outputName = "";
|
---|
| 191 | string outputString = "";
|
---|
| 192 | bool noError = true;
|
---|
| 193 | do {
|
---|
| 194 | try {
|
---|
| 195 | outputName = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
|
---|
| 196 | outputString = /*node.InnerText*/interpretedTree.Replace("class output", "class " + outputName);
|
---|
| 197 | File.WriteAllText(path + "/robots/Evaluation/" + outputName + ".java", outputString, System.Text.Encoding.Default);
|
---|
| 198 | noError = true;
|
---|
[9565] | 199 | }
|
---|
[9601] | 200 | catch (Exception e) { noError = false; }
|
---|
| 201 | } while (!noError);
|
---|
[9565] | 202 |
|
---|
[9601] | 203 | return outputName;
|
---|
| 204 | }
|
---|
[9565] | 205 |
|
---|
[9601] | 206 | private static bool compileCodefile(string filename, string formattedPath) {
|
---|
| 207 | ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
|
---|
| 208 | javaCompileInfo.FileName = "cmd.exe";
|
---|
| 209 | javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + filename + ".java";
|
---|
| 210 | javaCompileInfo.RedirectStandardOutput = true;
|
---|
| 211 | javaCompileInfo.RedirectStandardError = true;
|
---|
| 212 | javaCompileInfo.UseShellExecute = false;
|
---|
| 213 | javaCompileInfo.CreateNoWindow = true;
|
---|
[9565] | 214 |
|
---|
[9601] | 215 | Process javaCompile = new Process();
|
---|
| 216 | javaCompile.StartInfo = javaCompileInfo;
|
---|
| 217 | javaCompile.Start();
|
---|
[9565] | 218 |
|
---|
[9601] | 219 | string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
|
---|
| 220 | cmdOutput += javaCompile.StandardError.ReadToEnd();
|
---|
[9565] | 221 |
|
---|
[9601] | 222 | javaCompile.WaitForExit();
|
---|
[9565] | 223 |
|
---|
[9601] | 224 | if (cmdOutput.Equals(""))
|
---|
| 225 | return true;
|
---|
| 226 | return false;
|
---|
| 227 | }
|
---|
[9565] | 228 |
|
---|
[9601] | 229 | private static double runEvaluator(string mainTreeFilename, string[] coevolutionIndividualsNames, string formattedPath) {
|
---|
| 230 | ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
|
---|
| 231 | evaluateCodeInfo.FileName = "cmd.exe";
|
---|
| 232 | //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";
|
---|
| 233 | evaluateCodeInfo.Arguments = "/C java -classpath " + formattedPath + "\\libs;" + formattedPath + "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" + formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." + mainTreeFilename + "* ";
|
---|
| 234 | if (coevolutionIndividualsNames != null) {
|
---|
| 235 | foreach (string s in coevolutionIndividualsNames)
|
---|
| 236 | evaluateCodeInfo.Arguments += "Evaluation." + s + "* ";
|
---|
| 237 | } else
|
---|
| 238 | evaluateCodeInfo.Arguments += "false";
|
---|
| 239 | //Console.WriteLine(javaCompileInfo.Arguments);
|
---|
| 240 | evaluateCodeInfo.RedirectStandardOutput = true;
|
---|
| 241 | evaluateCodeInfo.RedirectStandardError = true;
|
---|
| 242 | evaluateCodeInfo.UseShellExecute = false;
|
---|
| 243 | evaluateCodeInfo.CreateNoWindow = true;
|
---|
[9565] | 244 |
|
---|
[9601] | 245 | Process evaluateCode = new Process();
|
---|
| 246 | evaluateCode.StartInfo = evaluateCodeInfo;
|
---|
| 247 | evaluateCode.Start();
|
---|
[9565] | 248 |
|
---|
[9601] | 249 | StringBuilder q = new StringBuilder();
|
---|
| 250 | string scoreString = "";
|
---|
| 251 | while (!evaluateCode.HasExited) {
|
---|
| 252 | scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(
|
---|
| 253 | new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
|
---|
| 254 | q.Append("Output: " + scoreString + "\r\n");
|
---|
| 255 | q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");
|
---|
| 256 | }
|
---|
| 257 | string cmdOutput = q.ToString();
|
---|
| 258 | try {
|
---|
| 259 | double evaluation = Double.Parse(scoreString);
|
---|
| 260 | return evaluation;
|
---|
| 261 | }
|
---|
| 262 | catch (Exception e) { }
|
---|
[9565] | 263 |
|
---|
[9601] | 264 | return -900;
|
---|
| 265 | }
|
---|
[9565] | 266 |
|
---|
[9601] | 267 | private static TankStats EvaluateTankProgram(ISymbolicExpressionTreeNode node, XmlNode docNode, string method) {
|
---|
| 268 | TankStats tankStats = new TankStats(0, 0);
|
---|
[9565] | 269 |
|
---|
[9601] | 270 | //string log = "Doing node: \r\n";
|
---|
| 271 | /*if (node.Symbol is ProgramRootSymbol ||
|
---|
| 272 | node.Symbol is StartSymbol)
|
---|
| 273 | tankStats = EvaluateTankProgram(node.GetSubtree(0), ref docNode, null);
|
---|
| 274 | else*/
|
---|
| 275 | if (node.Symbol is Constant)
|
---|
| 276 | docNode[method]["code"].InnerText += " " + ((ConstantTreeNode)node).Value + " ";
|
---|
| 277 | else if (node.Symbol is ShotPower)
|
---|
| 278 | docNode[method]["code"].InnerText += " " + ((ShotPowerTreeNode)node).Value + " ";
|
---|
| 279 | else if (node.Symbol is LogicalValue)
|
---|
| 280 | docNode[method]["code"].InnerText += " " + ((BooleanTreeNode)node).Value.ToString().ToLower() + " ";
|
---|
| 281 | else if (node.Symbol is Block) {
|
---|
| 282 | docNode[method]["code"].InnerText += "{\r\n";
|
---|
| 283 | foreach (ISymbolicExpressionTreeNode n in node.Subtrees)
|
---|
| 284 | EvaluateTankProgram(n, docNode, method);
|
---|
| 285 | docNode[method]["code"].InnerText += "}\r\n";
|
---|
| 286 | } else if (node.Symbol is IfStatement) {
|
---|
| 287 | docNode[method]["code"].InnerText += "if(";
|
---|
| 288 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 289 | docNode[method]["code"].InnerText += ")\r\n";
|
---|
| 290 | EvaluateTankProgram(node.GetSubtree(1), docNode, method);
|
---|
| 291 | if (node.SubtreeCount == 3)
|
---|
| 292 | EvaluateTankProgram(node.GetSubtree(2), docNode, method);
|
---|
| 293 | } else if (node.Symbol is ElseStatement) {
|
---|
| 294 | docNode[method]["code"].InnerText += "else\r\n";
|
---|
| 295 | if (node.SubtreeCount == 1)
|
---|
| 296 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 297 | } else if (node.Symbol is WhileLoop) {
|
---|
| 298 | docNode[method]["code"].InnerText += "While(";
|
---|
| 299 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 300 | docNode[method]["code"].InnerText += ")\r\n";
|
---|
| 301 | EvaluateTankProgram(node.GetSubtree(1), docNode, method);
|
---|
| 302 | } else if (node.Symbol is Ahead) {
|
---|
| 303 | docNode[method]["code"].InnerText += "setAhead(";
|
---|
| 304 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 305 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 306 | tankStats.moves = 1;
|
---|
| 307 | } else if (node.Symbol is Back) {
|
---|
| 308 | docNode[method]["code"].InnerText += "setBack(";
|
---|
| 309 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 310 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 311 | tankStats.moves = 1;
|
---|
| 312 | } else if (node.Symbol is SetAdjustGunForRobotTurn) {
|
---|
| 313 | docNode[method]["code"].InnerText += "setAdjustGunForRobotTurn(";
|
---|
| 314 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 315 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 316 | tankStats.moves = 1;
|
---|
| 317 | } else if (node.Symbol is SetAdjustRadarForGunTurn) {
|
---|
| 318 | docNode[method]["code"].InnerText += "setAdjustRadarForGunTurn(";
|
---|
| 319 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 320 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 321 | tankStats.moves = 1;
|
---|
| 322 | } else if (node.Symbol is SetAdjustRadarForRobotTurn) {
|
---|
| 323 | docNode[method]["code"].InnerText += "setAdjustRadarForRobotTurn(";
|
---|
| 324 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 325 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 326 | tankStats.moves = 1;
|
---|
| 327 | } else if (node.Symbol is TurnGunLeft) {
|
---|
| 328 | docNode[method]["code"].InnerText += "setTurnGunLeft(";
|
---|
| 329 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 330 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 331 | tankStats.moves = 1;
|
---|
| 332 | } else if (node.Symbol is TurnGunRight) {
|
---|
| 333 | docNode[method]["code"].InnerText += "setTurnGunRight(";
|
---|
| 334 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 335 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 336 | tankStats.moves = 1;
|
---|
| 337 | } else if (node.Symbol is TurnLeft) {
|
---|
| 338 | docNode[method]["code"].InnerText += "setTurnLeft(";
|
---|
| 339 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 340 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 341 | tankStats.moves = 1;
|
---|
| 342 | } else if (node.Symbol is TurnRadarLeft) {
|
---|
| 343 | docNode[method]["code"].InnerText += "setTurnRadarLeft(";
|
---|
| 344 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 345 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 346 | tankStats.moves = 1;
|
---|
| 347 | } else if (node.Symbol is TurnRadarRight) {
|
---|
| 348 | docNode[method]["code"].InnerText += "setTurnRadarRight(";
|
---|
| 349 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 350 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 351 | tankStats.moves = 1;
|
---|
| 352 | } else if (node.Symbol is TurnRight) {
|
---|
| 353 | docNode[method]["code"].InnerText += "setTurnRight(";
|
---|
| 354 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 355 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 356 | tankStats.moves = 1;
|
---|
| 357 | } else if (node.Symbol is Fire) {
|
---|
| 358 | docNode[method]["code"].InnerText += "setFire(";
|
---|
| 359 | EvaluateTankProgram(node.GetSubtree(0), docNode, method);
|
---|
| 360 | docNode[method]["code"].InnerText += ");\r\n";
|
---|
| 361 | tankStats.shots = 1;
|
---|
| 362 | } else if (node.Symbol is Run ||
|
---|
| 363 | node.Symbol is OnBulletHit ||
|
---|
| 364 | node.Symbol is OnBulletMissed ||
|
---|
| 365 | node.Symbol is OnHitByBullet ||
|
---|
| 366 | node.Symbol is OnHitRobot ||
|
---|
| 367 | node.Symbol is OnHitWall ||
|
---|
| 368 | node.Symbol is OnScannedRobot) {
|
---|
| 369 | tankStats = new TankStats();
|
---|
| 370 | foreach (SymbolicExpressionTreeNode n in node.Subtrees) {
|
---|
| 371 | string methodName = node.Symbol.GetType().ToString().Split('.').Last();
|
---|
| 372 | try {
|
---|
| 373 | var tempStats = EvaluateTankProgram(n, docNode, methodName);
|
---|
| 374 | tankStats.moves += tempStats.moves;
|
---|
| 375 | tankStats.shots += tempStats.shots;
|
---|
| 376 | }
|
---|
| 377 | catch (Exception e) {
|
---|
| 378 | //log += "NULL EXCEPTION ON:::::::: -" + methodName + "-\r\n";
|
---|
| 379 | //foreach (XmlNode f in docNode[methodName])
|
---|
| 380 | // log += f.Name + "\r\n";
|
---|
| 381 | }
|
---|
[9565] | 382 | }
|
---|
[9601] | 383 | } else if (node.Symbol is Tank) {
|
---|
| 384 | foreach (SymbolicExpressionTreeNode n in node.Subtrees) {
|
---|
| 385 | try {
|
---|
| 386 | var tempStats = EvaluateTankProgram(n, docNode, method);
|
---|
| 387 | tankStats.moves += tempStats.moves;
|
---|
| 388 | tankStats.shots += tempStats.shots;
|
---|
| 389 | }
|
---|
| 390 | catch (Exception e) { }
|
---|
| 391 | }
|
---|
| 392 | } else
|
---|
| 393 | tankStats = EvaluateTankProgram(node.GetSubtree(0), docNode, null);
|
---|
[9565] | 394 |
|
---|
[9601] | 395 | //log += tankStats.shots + " / " + tankStats.moves + "\r\n";
|
---|
| 396 | //File.AppendAllText("../TankLog.txt", log);
|
---|
| 397 |
|
---|
| 398 | return tankStats;
|
---|
[9565] | 399 | }
|
---|
[9601] | 400 |
|
---|
| 401 | public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) {
|
---|
| 402 | var tankNode = node;
|
---|
| 403 | while (!(tankNode.Symbol is Tank))
|
---|
| 404 | tankNode = tankNode.GetSubtree(0);
|
---|
| 405 | return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
[9565] | 408 | } |
---|