Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10011 was 10011, checked in by jkarder, 11 years ago

#2069:

  • refactored grammar and symbols
  • fixed cloning and storable ctors
  • fixed plugin dependencies
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 RampedHalfAndHalfTreeCreator()) {
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      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
109      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
110      Operators.Add(new BestSolutionAnalyzer());
111      ParameterizeOperators();
112      ParameterizeAnalyzers();
113    }
114
115    private void RegisterEventHandlers() {
116      RobocodePathParameter.Value.StringValue.ValueChanged += RobocodePathParameter_ValueChanged;
117    }
118
119    void RobocodePathParameter_ValueChanged(object sender, System.EventArgs e) {
120      EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value;
121    }
122
123    protected override void OnEvaluatorChanged() {
124      base.OnEvaluatorChanged();
125      Evaluator.TankProgramParameter.ActualName =
126        TankProgramParameterName;
127      ParameterizeAnalyzers();
128      ParameterizeOperators();
129    }
130
131    protected override void OnSolutionCreatorChanged() {
132      base.OnSolutionCreatorChanged();
133      SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
134        TankProgramParameterName;
135      ParameterizeAnalyzers();
136      ParameterizeOperators();
137    }
138
139    private void ParameterizeAnalyzers() {
140      var analyzers = Operators.OfType<IAnalyzer>();
141      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
142        o.SymbolicExpressionTreeParameter.ActualName =
143          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
144      }
145      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
146        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
147      }
148    }
149
150    private void ParameterizeOperators() {
151      var operators = Parameters
152        .OfType<IValueParameter>()
153        .Select(p => p.Value)
154        .OfType<IOperator>()
155        .Union(Operators);
156      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
157        o.SymbolicExpressionTreeGrammarParameter.ActualName =
158          TankGrammarParameterName;
159      }
160      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
161        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
162          MaxTankProgramDepthParameterName;
163        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
164          MaxTankProgramLengthParameterName;
165      }
166      foreach (var op in operators.OfType<RobocodeEvaluator>()) {
167        op.TankProgramParameter.ActualName =
168          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
169      }
170      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
171        op.ParentsParameter.ActualName =
172          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
173        op.ChildParameter.ActualName =
174          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
175      }
176      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
177        op.SymbolicExpressionTreeParameter.ActualName =
178          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
179      }
180      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
181        op.SymbolicExpressionTreeParameter.ActualName =
182          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
183      }
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.