Changeset 13312 for stable/HeuristicLab.Problems.GeneticProgramming
- Timestamp:
- 11/19/15 21:16:55 (9 years ago)
- Location:
- stable
- Files:
-
- 5 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/HeuristicLab.Problems.GeneticProgramming-3.3.csproj
r13280 r13312 104 104 </Reference> 105 105 <Reference Include="System.Drawing" /> 106 <Reference Include="System.IO.Compression" /> 107 <Reference Include="System.IO.Compression.FileSystem" /> 106 108 </ItemGroup> 107 109 <ItemGroup> … … 117 119 <Compile Include="Plugin.cs" /> 118 120 <Compile Include="Properties\AssemblyInfo.cs" /> 121 <Compile Include="robocode\EnemyCollection.cs" /> 122 <Compile Include="robocode\Grammar.cs" /> 123 <Compile Include="robocode\Interpreter.cs" /> 124 <Compile Include="robocode\Problem.cs" /> 125 <Compile Include="robocode\Solution.cs" /> 126 <Compile Include="robocode\Symbols\BooleanTreeNode.cs" /> 127 <Compile Include="robocode\Symbols\BooleanValue.cs" /> 128 <Compile Include="robocode\Symbols\CodeSymbol.cs" /> 129 <Compile Include="robocode\Symbols\Number.cs" /> 130 <Compile Include="robocode\Symbols\NumberTreeNode.cs" /> 131 <Compile Include="robocode\Symbols\ShotPower.cs" /> 132 <Compile Include="robocode\Symbols\ShotPowerTreeNode.cs" /> 119 133 </ItemGroup> 120 134 <ItemGroup> … … 122 136 <None Include="Plugin.cs.frame" /> 123 137 <None Include="Properties\AssemblyInfo.cs.frame" /> 138 <None Include="robocode\BattleObserver.class"> 139 <CopyToOutputDirectory>Always</CopyToOutputDirectory> 140 </None> 141 <None Include="robocode\BattleRunner.class"> 142 <CopyToOutputDirectory>Always</CopyToOutputDirectory> 143 </None> 144 <None Include="robocode\robocode.zip"> 145 <CopyToOutputDirectory>Always</CopyToOutputDirectory> 146 </None> 124 147 </ItemGroup> 125 148 <ItemGroup> … … 211 234 <Install>true</Install> 212 235 </BootstrapperPackage> 236 </ItemGroup> 237 <ItemGroup> 238 <Content Include="robocode\BattleRunner.java" /> 213 239 </ItemGroup> 214 240 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/Plugin.cs.frame
r13280 r13312 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Text; 23 using System.IO; 24 using System.IO.Compression; 25 using System.Linq; 25 26 using HeuristicLab.PluginInfrastructure; 26 27 … … 28 29 [Plugin("HeuristicLab.Problems.GeneticProgramming","Provides implementations for genetic programming problems such as the artificial ant problem.", "3.3.0.$WCREV$")] 29 30 [PluginFile("HeuristicLab.Problems.GeneticProgramming-3.3.dll", PluginFileType.Assembly)] 31 [PluginFile("Robocode/robocode.zip", PluginFileType.Data)] 32 [PluginFile("Robocode/BattleRunner.class", PluginFileType.Data)] 33 [PluginFile("Robocode/BattleObserver.class", PluginFileType.Data)] 30 34 [PluginDependency("HeuristicLab.Collections", "3.3")] 31 35 [PluginDependency("HeuristicLab.Common", "3.3")] … … 41 45 [PluginDependency("HeuristicLab.Random", "3.3")] 42 46 public class HeuristicLabProblemsGeneticProgrammingPlugin : PluginBase { 47 public override void OnLoad() { 48 base.OnLoad(); 49 if (!Directory.EnumerateDirectories(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("Robocode", "libs"), SearchOption.TopDirectoryOnly).Any()) { 50 ZipFile.ExtractToDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("Robocode", "robocode.zip")), AppDomain.CurrentDomain.BaseDirectory); 51 } 52 } 43 53 } 44 54 } -
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.