Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 some minor fixes in the enemy detection

File size: 8.8 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 robotList = ReloadEnemies(robocodeDir.Value);
87
88      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6)));
89      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
90      Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
91      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
92      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent.", new IntValue(3)));
93      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>(EnemiesParameterName, "The enemies that should be battled.", robotList));
94
95      Maximization.Value = true;
96      InitializeOperators();
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new RobocodeProblem(this, cloner);
101    }
102
103    private ICheckedItemList<StringValue> ReloadEnemies(string robocodeDir) {
104      var robotsDir = new DirectoryInfo(Path.Combine(robocodeDir, "robots"));
105      var robotFiles = robotsDir.GetFiles("*", SearchOption.AllDirectories)
106                                .Where(x => x.Extension == ".class" || x.Extension == ".jar");
107      var robotSet = new Dictionary<string, StringValue>();
108      foreach (var robot in robotFiles) {
109        string robotName = Path.Combine(robot.DirectoryName, Path.GetFileNameWithoutExtension(robot.FullName));
110        robotName = robotName.Remove(0, robotsDir.FullName.Length);
111        string[] nameParts = robotName.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
112        robotName = string.Join(".", nameParts);
113        robotSet[robot.FullName] = new StringValue(robotName);
114      }
115      var robotList = new CheckedItemList<StringValue>(robotSet.Values);
116      foreach (var robot in robotList) {
117        robotList.SetItemCheckedState(robot, false);
118      }
119
120      return robotList;
121    }
122
123    private void InitializeOperators() {
124      Operators.AddRange(
125        ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
126      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
127      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
128      Operators.Add(new BestSolutionAnalyzer());
129      ParameterizeOperators();
130      ParameterizeAnalyzers();
131    }
132
133    protected override void OnEvaluatorChanged() {
134      base.OnEvaluatorChanged();
135      Evaluator.TankProgramParameter.ActualName =
136        TankProgramParameterName;
137      ParameterizeAnalyzers();
138      ParameterizeOperators();
139    }
140
141    protected override void OnSolutionCreatorChanged() {
142      base.OnSolutionCreatorChanged();
143      SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
144        TankProgramParameterName;
145      ParameterizeAnalyzers();
146      ParameterizeOperators();
147    }
148
149    private void ParameterizeAnalyzers() {
150      var analyzers = Operators.OfType<IAnalyzer>();
151      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
152        o.SymbolicExpressionTreeParameter.ActualName =
153          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
154      }
155      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
156        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
157      }
158    }
159
160    private void ParameterizeOperators() {
161      var operators = Parameters
162        .OfType<IValueParameter>()
163        .Select(p => p.Value)
164        .OfType<IOperator>()
165        .Union(Operators);
166      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
167        o.SymbolicExpressionTreeGrammarParameter.ActualName =
168          TankGrammarParameterName;
169      }
170      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
171        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
172          MaxTankProgramDepthParameterName;
173        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
174          MaxTankProgramLengthParameterName;
175      }
176      foreach (var op in operators.OfType<RobocodeEvaluator>()) {
177        op.TankProgramParameter.ActualName =
178          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
179      }
180      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
181        op.ParentsParameter.ActualName =
182          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
183        op.ChildParameter.ActualName =
184          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
185      }
186      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
187        op.SymbolicExpressionTreeParameter.ActualName =
188          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
189      }
190      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
191        op.SymbolicExpressionTreeParameter.ActualName =
192          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
193      }
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.