Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs @ 9880

Last change on this file since 9880 was 9880, checked in by ascheibe, 11 years ago

#2069 removed dead code

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Diagnostics;
24using System.IO;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.Robocode {
30  public class Interpreter {
31
32    static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33
34    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
35      //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null);
36      //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves;
37      string interpretedProgram = InterpretProgramTree(tree.Root);
38      Random random = new Random();
39      string formattedPath = path.Replace("/", "\\");
40      string outputname = "";
41      string output = "";
42      bool noError = true;
43      do {
44        try {
45          outputname = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
46          output = /*solutionCandidateRoot.InnerText*/interpretedProgram.Replace("class output", "class " + outputname);
47          File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);
48          noError = true;
49        }
50        catch (Exception e) { noError = false; }
51      } while (!noError);
52
53      //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
54      //path.Replace("\\", "\\\\");
55      //path.Replace(" ", "\\ ");
56      #region Compile code file
57
58      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
59      javaCompileInfo.FileName = "cmd.exe";
60      javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java";
61      javaCompileInfo.RedirectStandardOutput = true;
62      javaCompileInfo.RedirectStandardError = true;
63      javaCompileInfo.UseShellExecute = false;
64      javaCompileInfo.CreateNoWindow = true;
65
66      Process javaCompile = new Process();
67      javaCompile.StartInfo = javaCompileInfo;
68      javaCompile.Start();
69
70      /*Evaluator.tankStatLog = "=====================================================\r\n";
71      Evaluator.tankStatLog += "Tree Compilation Output: \r\n";
72      Evaluator.tankStatLog += "=====================================================\r\n";*/
73
74      string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
75      cmdOutput += javaCompile.StandardError.ReadToEnd();
76
77      //Evaluator.tankStatLog += cmdOutput;
78      //Console.WriteLine(cmdOutput);
79      //Console.ReadLine();
80
81      /*Evaluator.tankStatLog = "=====================================================\r\n";
82      Evaluator.tankStatLog += "End of Tree Compilation Output: \r\n";
83      Evaluator.tankStatLog += "=====================================================\r\n";*/
84
85      javaCompile.WaitForExit();
86      if (javaCompile.ExitCode != 0) {
87        return -1000.0;
88      }
89      #endregion
90
91
92      try { File.Delete(path + "/robots/robot.database"); }
93      catch (Exception e) { }
94
95      #region evaluate code
96
97      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
98      evaluateCodeInfo.FileName = "cmd.exe";
99      //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";
100      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";
101      //Console.WriteLine(javaCompileInfo.Arguments);
102      evaluateCodeInfo.RedirectStandardOutput = true;
103      evaluateCodeInfo.RedirectStandardError = true;
104      evaluateCodeInfo.UseShellExecute = false;
105      evaluateCodeInfo.CreateNoWindow = true;
106
107      Process evaluateCode = new Process();
108      evaluateCode.StartInfo = evaluateCodeInfo;
109      evaluateCode.Start();
110
111      StringBuilder q = new StringBuilder();
112      string scoreString = "";
113      evaluateCode.WaitForExit();
114
115      try {
116        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(
117        new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
118        q.Append("Output: " + scoreString + "\r\n");
119        q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");
120        cmdOutput += q.ToString();
121      }
122      catch {
123        return -1000.0;
124      }
125
126      #endregion
127
128      double evaluation = -900;
129      try {
130        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
131        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
132        evaluation = Double.Parse(scoreString);
133      }
134      catch (Exception e) { evaluation = -900; }
135      Random rand = new Random();
136      evaluation += rand.NextDouble() / 100;
137
138      return evaluation;
139    }
140
141    public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) {
142      var tankNode = node;
143      while (!(tankNode.Symbol is Tank))
144        tankNode = tankNode.GetSubtree(0);
145      return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.