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