Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 don't use architecture operators as the Robocode problem does not support ADFs

File size: 8.2 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.Robocode {
33  [StorableClass]
34  [Creatable("Problems")]
35  [Item("Robocode Problem",
36        "The Robocode problem for genetic programming.")]
37  public class RobocodeProblem : SingleObjectiveHeuristicOptimizationProblem<RobocodeEvaluator,
38    ISymbolicExpressionTreeCreator> {
39    #region Parameter Names
40    private const string TankProgramParameterName = "TankProgram";
41    private const string MaxTankProgramLengthParameterName = "MaxProgramLength";
42    private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";
43    private const string TankGrammarParameterName = "Grammar";
44    private const string RobocodePathParamaterName = "RobocodePath";
45    private const string NrOfRoundsParameterName = "NrOfRounds";
46    private const string EnemiesParameterName = "Enemies";
47    #endregion
48
49    #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    }
59    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
60      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
61    }
62    public IFixedValueParameter<IntValue> NrOfRoundsParameter {
63      get { return (IFixedValueParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
64    }
65    public IValueParameter<EnemyCollection> EnemiesParameter {
66      get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
67    }
68    #endregion
69
70    [StorableConstructor]
71    protected RobocodeProblem(bool deserializing) : base(deserializing) { }
72    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
73      : base(original, cloner) {
74      RegisterEventHandlers();
75    }
76
77    public RobocodeProblem()
78      : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) {
79      DirectoryValue robocodeDir = new DirectoryValue();
80      robocodeDir.Value = @"C:\robocode";
81
82      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
83      robotList.RobocodePath = robocodeDir.Value;
84
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()));
88      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
89      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of rounds a robot has to fight against each opponent.", new IntValue(3)));
90      Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList));
91
92      Maximization.Value = true;
93      InitializeOperators();
94      RegisterEventHandlers();
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new RobocodeProblem(this, cloner);
99    }
100
101    [StorableHook(HookType.AfterDeserialization)]
102    private void AfterDeserialization() {
103      RegisterEventHandlers();
104    }
105
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();
114    }
115
116    private void RegisterEventHandlers() {
117      RobocodePathParameter.Value.StringValue.ValueChanged += RobocodePathParameter_ValueChanged;
118    }
119
120    void RobocodePathParameter_ValueChanged(object sender, System.EventArgs e) {
121      EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value;
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.