Changeset 13015
- Timestamp:
- 10/15/15 17:31:02 (9 years ago)
- Location:
- branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
- Files:
-
- 2 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/HeuristicLab.Problems.Robocode-3.3.csproj
r11628 r13015 92 92 </ItemGroup> 93 93 <ItemGroup> 94 <Compile Include="BestSolutionAnalyzer.cs" />95 94 <None Include="BattleObserver.class"> 96 95 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> … … 102 101 <None Include="Plugin.cs.frame" /> 103 102 <Compile Include="EnemyCollection.cs" /> 104 <Compile Include="RobocodeEvaluator.cs" />105 103 <Compile Include="Grammar.cs" /> 106 104 <Compile Include="Interpreter.cs" /> -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs
r13013 r13015 20 20 #endregion 21 21 22 using System.Linq;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure;31 29 32 30 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 33 31 [StorableClass] 34 [Creatable("Problems")] 35 [Item("Robocode Problem", 36 "The Robocode problem for genetic programming.")] 37 public class RobocodeProblem : SingleObjectiveHeuristicOptimizationProblem<RobocodeEvaluator, 38 ISymbolicExpressionTreeCreator> { 32 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 360)] 33 [Item("Robocode Problem", "Evolution of a robocode program in java using genetic programming.")] 34 public class RobocodeProblem : SymbolicExpressionTreeProblem { 39 35 #region Parameter Names 40 private const string TankProgramParameterName = "TankProgram";41 36 private const string MaxTankProgramLengthParameterName = "MaxProgramLength"; 42 37 private const string MaxTankProgramDepthParameterName = "MaxProgramDepth"; 43 private const string TankGrammarParameterName = "Grammar";44 38 private const string RobocodePathParamaterName = "RobocodePath"; 45 39 private const string NrOfRoundsParameterName = "NrOfRounds"; … … 48 42 49 43 #region Parameters 50 public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter { 51 get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; } 52 } 53 public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter { 54 get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; } 55 } 56 public IValueParameter<Grammar> GrammarParameter { 57 get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; } 58 } 44 //public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter { 45 // get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; } 46 //} 47 //public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter { 48 // get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; } 49 //} 59 50 public IFixedValueParameter<DirectoryValue> RobocodePathParameter { 60 51 get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; } … … 65 56 public IValueParameter<EnemyCollection> EnemiesParameter { 66 57 get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; } 58 } 59 60 //public int MaxTankProgramDepth { 61 // get { return MaxTankProgramDepthParameter.Value.Value; } 62 // set { MaxTankProgramDepthParameter.Value.Value = value; } 63 //} 64 // 65 //public int MaxTankProgramLength { 66 // get { return MaxTankProgramLengthParameter.Value.Value; } 67 // set { MaxTankProgramLengthParameter.Value.Value = value; } 68 //} 69 70 public string RobocodePath { 71 get { return RobocodePathParameter.Value.Value; } 72 set { RobocodePathParameter.Value.Value = value; } 73 } 74 75 public int NrOfRounds { 76 get { return NrOfRoundsParameter.Value.Value; } 77 set { NrOfRoundsParameter.Value.Value = value; } 78 } 79 80 public EnemyCollection Enemies { 81 get { return EnemiesParameter.Value; } 82 set { EnemiesParameter.Value = value; } 67 83 } 68 84 #endregion … … 76 92 77 93 public RobocodeProblem() 78 : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) { 79 DirectoryValue robocodeDir = new DirectoryValue(); 80 robocodeDir.Value = @"C:\robocode"; 94 : base() { 95 DirectoryValue robocodeDir = new DirectoryValue { Value = @"C:\robocode" }; 81 96 82 97 var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value); 83 98 robotList.RobocodePath = robocodeDir.Value; 84 99 85 Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(10))); 86 Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000))); 87 Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar())); 100 88 101 Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir)); 89 Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "N r.of rounds a robot has to fight against each opponent.", new IntValue(3)));102 Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Number of rounds a robot has to fight against each opponent.", new IntValue(3))); 90 103 Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList)); 91 104 92 Maximization.Value = true; 93 InitializeOperators(); 105 Encoding = new SymbolicExpressionTreeEncoding(new Grammar(), 1000, 10); 106 Encoding.FunctionArguments = 0; 107 Encoding.FunctionDefinitions = 0; 108 94 109 RegisterEventHandlers(); 95 110 } … … 100 115 101 116 [StorableHook(HookType.AfterDeserialization)] 102 private void AfterDeserialization() { 103 RegisterEventHandlers(); 117 private void AfterDeserialization() { RegisterEventHandlers(); } 118 119 public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { 120 return Interpreter.EvaluateTankProgram(tree, RobocodePath, Enemies, null, false, NrOfRounds); 104 121 } 105 122 106 private void InitializeOperators() { 107 Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>() 108 .Where(x => !(x is ISymbolicExpressionTreeArchitectureAlteringOperator))); 109 Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer()); 110 Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer()); 111 Operators.Add(new BestSolutionAnalyzer()); 112 ParameterizeOperators(); 113 ParameterizeAnalyzers(); 123 public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) { 124 // find the tree with the best quality 125 double maxQuality = double.NegativeInfinity; 126 ISymbolicExpressionTree bestTree = null; 127 for (int i = 0; i < qualities.Length; i++) { 128 if (qualities[i] > maxQuality) { 129 maxQuality = qualities[i]; 130 bestTree = trees[i]; 131 } 132 } 133 134 // create a solution instance 135 var bestSolution = new Solution(bestTree, RobocodePath, NrOfRounds, Enemies); 136 137 // also add the best solution as a result to the result collection 138 // or alternatively update the existing result 139 if (!results.ContainsKey("BestSolution")) { 140 results.Add(new Result("BestSolution", "The best tank program", bestSolution)); 141 } else { 142 results["BestSolution"].Value = bestSolution; 143 } 144 } 145 146 public override bool Maximization { 147 get { return true; } 114 148 } 115 149 … … 121 155 EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value; 122 156 } 123 124 protected override void OnEvaluatorChanged() {125 base.OnEvaluatorChanged();126 Evaluator.TankProgramParameter.ActualName =127 TankProgramParameterName;128 ParameterizeAnalyzers();129 ParameterizeOperators();130 }131 132 protected override void OnSolutionCreatorChanged() {133 base.OnSolutionCreatorChanged();134 SolutionCreator.SymbolicExpressionTreeParameter.ActualName =135 TankProgramParameterName;136 ParameterizeAnalyzers();137 ParameterizeOperators();138 }139 140 private void ParameterizeAnalyzers() {141 var analyzers = Operators.OfType<IAnalyzer>();142 foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {143 o.SymbolicExpressionTreeParameter.ActualName =144 SolutionCreator.SymbolicExpressionTreeParameter.ActualName;145 }146 foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {147 o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;148 }149 }150 151 private void ParameterizeOperators() {152 var operators = Parameters153 .OfType<IValueParameter>()154 .Select(p => p.Value)155 .OfType<IOperator>()156 .Union(Operators);157 foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {158 o.SymbolicExpressionTreeGrammarParameter.ActualName =159 TankGrammarParameterName;160 }161 foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {162 o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =163 MaxTankProgramDepthParameterName;164 o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =165 MaxTankProgramLengthParameterName;166 }167 foreach (var op in operators.OfType<RobocodeEvaluator>()) {168 op.TankProgramParameter.ActualName =169 SolutionCreator.SymbolicExpressionTreeParameter.ActualName;170 }171 foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {172 op.ParentsParameter.ActualName =173 SolutionCreator.SymbolicExpressionTreeParameter.ActualName;174 // op.ChildParameter.ActualName =175 // SolutionCreator.SymbolicExpressionTreeParameter.ActualName;176 }177 foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {178 op.SymbolicExpressionTreeParameter.ActualName =179 SolutionCreator.SymbolicExpressionTreeParameter.ActualName;180 }181 foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {182 op.SymbolicExpressionTreeParameter.ActualName =183 SolutionCreator.SymbolicExpressionTreeParameter.ActualName;184 }185 }186 157 } 187 158 }
Note: See TracChangeset
for help on using the changeset viewer.