source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs @ 9947

Last change on this file since 9947 was 9947, checked in by jkarder, 7 years ago

#2069:

  • modified the RobocodeProblem to extract all robots in robocode directory
  • modified the battle runner to take a number of enemies that should be battled
File size: 8.5 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.Collections.Generic;
24using System.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.Robocode {
36  [StorableClass]
37  [Creatable("Problems")]
38  [Item("Robocode Problem",
39        "The Robocode problem for genetic programming.")]
40  public class RobocodeProblem : SingleObjectiveHeuristicOptimizationProblem<RobocodeEvaluator,
41    ISymbolicExpressionTreeCreator> {
42    #region parameter names
43    private const string TankProgramParameterName = "TankProgram";
44    private const string MaxTankProgramLengthParameterName = "MaxProgramLength";
45    private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";
46    private const string TankGrammarParameterName = "Grammar";
47    private const string RobocodePathParamaterName = "RobocodePath";
48    private const string NrOfRoundsParameterName = "NrOfRounds";
49    private const string EnemiesParameterName = "Enemies";
50    #endregion
51
52    #region Parameters
53    public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
54      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
55    }
56    public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
57      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
58    }
59    public IValueParameter<Grammar> GrammarParameter {
60      get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; }
61    }
62    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
63      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
64    }
65    public IFixedValueParameter<IntValue> NrOfRoundsParameter {
66      get { return (IFixedValueParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
67    }
68    public IValueParameter<ICheckedItemList<StringValue>> EnemiesParameter {
69      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters[EnemiesParameterName]; }
70    }
71    #endregion
72
73    [StorableConstructor]
74    protected RobocodeProblem(bool deserializing)
75      : base(deserializing) {
76    }
77    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
78      : base(original, cloner) {
79    }
80
81    public RobocodeProblem()
82      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
83      DirectoryValue robocodeDir = new DirectoryValue();
84      robocodeDir.Value = @"C:\robocode";
85
86      var robotsDir = new DirectoryInfo(Path.Combine(robocodeDir.Value, "robots"));
87      var robotFiles = robotsDir.GetFiles("*", SearchOption.AllDirectories)
88                                .Where(x => x.Extension == ".class" || x.Extension == ".jar");
89      var robotSet = new Dictionary<string, StringValue>();
90      foreach (var robot in robotFiles) {
91        string robotName = Path.Combine(robot.DirectoryName, Path.GetFileNameWithoutExtension(robot.FullName));
92        robotName = robotName.Remove(0, robotsDir.FullName.Length);
93        string[] nameParts = robotName.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
94        robotName = string.Join(".", nameParts);
95        robotSet[robot.FullName] = new StringValue(robotName);
96      }
97      var robotList = new CheckedItemList<StringValue>(robotSet.Values);
98
99      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6)));
100      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
101      Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
102      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
103      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent.", new IntValue(3)));
104      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>(EnemiesParameterName, "The enemies that should be battled.", robotList));
105
106      Maximization.Value = true;
107      InitializeOperators();
108    }
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new RobocodeProblem(this, cloner);
112    }
113
114    private void InitializeOperators() {
115      Operators.AddRange(
116        ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
117      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
118      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
119      Operators.Add(new BestSolutionAnalyzer());
120      ParameterizeOperators();
121      ParameterizeAnalyzers();
122    }
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 = Parameters
153        .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  }
187}
Note: See TracBrowser for help on using the repository browser.