Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 added a view for displaying/reloading enemies

File size: 8.1 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)
72      : base(deserializing) {
73      if (deserializing) {
74        RegisterRobocodePathEvent();
75      }
76    }
77    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
78      : base(original, cloner) {
79      RegisterRobocodePathEvent();
80    }
81
82    public RobocodeProblem()
83      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
84      DirectoryValue robocodeDir = new DirectoryValue();
85      robocodeDir.Value = @"C:\robocode";
86
87      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
88      robotList.RobocodePath = robocodeDir.Value;
89
90      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6)));
91      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
92      Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
93      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
94      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent.", new IntValue(3)));
95      Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList));
96
97      Maximization.Value = true;
98      InitializeOperators();
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new RobocodeProblem(this, cloner);
103    }
104
105    private void InitializeOperators() {
106      Operators.AddRange(
107        ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
108      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
109      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
110      Operators.Add(new BestSolutionAnalyzer());
111      ParameterizeOperators();
112      ParameterizeAnalyzers();
113      RegisterRobocodePathEvent();
114    }
115
116    private void RegisterRobocodePathEvent() {
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.