- Timestamp:
- 11/19/15 21:16:55 (9 years ago)
- Location:
- stable
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13210,13225,13266,13309,13311
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.GeneticProgramming/3.3/robocode/Interpreter.cs
r13210 r13312 31 31 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 32 32 public static class Interpreter { 33 34 // necessary for synchronization to guarantee that only one robocode program is executed at the same time 35 // NOTE: this does not guarantee OS-wide mutual exclusion, but we ignore that for now 36 private static readonly object syncRoot = new object(); 37 33 38 // 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 34 39 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) { … … 56 61 javaCompileInfo.CreateNoWindow = true; 57 62 63 // it's ok to compile multiple robocode programs concurrently 58 64 using (Process javaCompile = new Process()) { 59 65 javaCompile.StartInfo = javaCompileInfo; … … 70 76 } 71 77 72 //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database73 var robotsDbFileName = Path.Combine(path, "robots", "robot.database");74 if (File.Exists(robotsDbFileName))75 File.Delete(robotsDbFileName);76 77 78 ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); 78 79 … … 89 90 evaluateCodeInfo.CreateNoWindow = true; 90 91 92 // the robocode framework writes state to a file therefore parallel evaluation of multiple robocode programs is not possible yet. 91 93 double evaluation; 92 using (Process evaluateCode = new Process()) { 93 evaluateCode.StartInfo = evaluateCodeInfo; 94 evaluateCode.Start(); 95 evaluateCode.WaitForExit(); 96 97 if (evaluateCode.ExitCode != 0) { 98 DeleteRobotFiles(path, robotName); 99 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd() + Environment.NewLine + 100 evaluateCode.StandardOutput.ReadToEnd()); 101 } 102 103 try { 104 string scoreString = 105 evaluateCode.StandardOutput.ReadToEnd() 106 .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) 107 .Last(); 108 evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture); 109 } 110 catch (Exception ex) { 111 throw new Exception("Error parsing score string: " + ex); 112 } 113 finally { 114 DeleteRobotFiles(path, robotName); 94 lock (syncRoot) { 95 using (Process evaluateCode = new Process()) { 96 evaluateCode.StartInfo = evaluateCodeInfo; 97 evaluateCode.Start(); 98 evaluateCode.WaitForExit(); 99 100 if (evaluateCode.ExitCode != 0) { 101 DeleteRobotFiles(path, robotName); 102 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd() + 103 Environment.NewLine + 104 evaluateCode.StandardOutput.ReadToEnd()); 105 } 106 107 try { 108 string scoreString = 109 evaluateCode.StandardOutput.ReadToEnd() 110 .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) 111 .Last(); 112 evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture); 113 } 114 catch (Exception ex) { 115 throw new Exception("Error parsing score string: " + ex); 116 } 117 finally { 118 DeleteRobotFiles(path, robotName); 119 } 115 120 } 116 121 } … … 222 227 223 228 private static string InterpretCodeSymbol(ISymbolicExpressionTreeNode node) { 224 var sy = node.Symbol as CodeSymbol; 225 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 226 return string.Format( 227 @"{0} 228 {1} 229 {2}", sy.Prefix, code, sy.Suffix); 229 var sy = (CodeSymbol)node.Symbol; 230 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 231 return sy.Prefix + Environment.NewLine + code + Environment.NewLine + sy.Suffix; 230 232 } 231 233 232 234 private static string InterpretBoolValue(ISymbolicExpressionTreeNode node) { 233 var boolNode = node as BooleanTreeNode;235 var boolNode = (BooleanTreeNode)node; 234 236 return string.Format(NumberFormatInfo.InvariantInfo, "{0}", boolNode.Value).ToLower(); 235 237 } 236 238 237 239 private static string InterpretNumber(ISymbolicExpressionTreeNode node) { 238 var numberNode = node as NumberTreeNode;240 var numberNode = (NumberTreeNode)node; 239 241 return string.Format(NumberFormatInfo.InvariantInfo, "{0}", numberNode.Value); 240 242 } 241 243 242 244 private static string InterpetShotPower(ISymbolicExpressionTreeNode node) { 243 var shotPowerNode = node as ShotPowerTreeNode;245 var shotPowerNode = (ShotPowerTreeNode)node; 244 246 return string.Format(NumberFormatInfo.InvariantInfo, "{0:E}", shotPowerNode.Value); 245 247 } … … 306 308 rhs = node.GetSubtree(1); 307 309 308 return Interpret(lhs) + " == "+ Interpret(rhs);310 return Interpret(lhs) + compSy + Interpret(rhs); 309 311 } 310 312 -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/robocode/Symbols/ShotPowerTreeNode.cs
r13210 r13312 61 61 // mutation 62 62 var d = random.NextDouble() * 2.0 - 1.0; 63 value = Math.Max(0.1, value + shakingFactor * d); 63 value += shakingFactor * d; 64 if (value < 0.1) value = 0.1; 65 if (value > 3) value = 3; 64 66 } 65 67 }
Note: See TracChangeset
for help on using the changeset viewer.