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
RevLine 
[9879]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> {
[9962]39    #region Parameter Names
[9879]40    private const string TankProgramParameterName = "TankProgram";
41    private const string MaxTankProgramLengthParameterName = "MaxProgramLength";
42    private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";
43    private const string TankGrammarParameterName = "Grammar";
[9926]44    private const string RobocodePathParamaterName = "RobocodePath";
45    private const string NrOfRoundsParameterName = "NrOfRounds";
[9947]46    private const string EnemiesParameterName = "Enemies";
[9879]47    #endregion
48
49    #region Parameters
50    public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
[9880]51      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
[9879]52    }
53    public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
[9880]54      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
[9879]55    }
56    public IValueParameter<Grammar> GrammarParameter {
[9880]57      get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; }
[9879]58    }
[9892]59    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
60      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
[9879]61    }
[9926]62    public IFixedValueParameter<IntValue> NrOfRoundsParameter {
63      get { return (IFixedValueParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
64    }
[9971]65    public IValueParameter<EnemyCollection> EnemiesParameter {
66      get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
[9947]67    }
[9879]68    #endregion
69
70    [StorableConstructor]
71    protected RobocodeProblem(bool deserializing)
72      : base(deserializing) {
[9971]73      if (deserializing) {
74        RegisterRobocodePathEvent();
75      }
[9879]76    }
77    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
78      : base(original, cloner) {
[9971]79      RegisterRobocodePathEvent();
[9879]80    }
81
82    public RobocodeProblem()
83      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
[9892]84      DirectoryValue robocodeDir = new DirectoryValue();
85      robocodeDir.Value = @"C:\robocode";
86
[9971]87      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
88      robotList.RobocodePath = robocodeDir.Value;
[9947]89
[9880]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()));
[9892]93      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
[9926]94      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent.", new IntValue(3)));
[9971]95      Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList));
[9879]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();
[9971]113      RegisterRobocodePathEvent();
[9879]114    }
115
[9971]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
[9879]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.