Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069

  • renamed path parameter to robocode path
  • made number of rounds configurable
File size: 7.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    #endregion
47
48    #region Parameters
49    public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
50      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
51    }
52    public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
53      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
54    }
55    public IValueParameter<Grammar> GrammarParameter {
56      get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; }
57    }
58    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
59      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
60    }
61    public IFixedValueParameter<IntValue> NrOfRoundsParameter {
62      get { return (IFixedValueParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected RobocodeProblem(bool deserializing)
68      : base(deserializing) {
69    }
70    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
71      : base(original, cloner) {
72    }
73
74    public RobocodeProblem()
75      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
76      DirectoryValue robocodeDir = new DirectoryValue();
77      robocodeDir.Value = @"C:\robocode";
78
79      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6)));
80      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
81      Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
82      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
83      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent.", new IntValue(3)));
84
85      Maximization.Value = true;
86      InitializeOperators();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new RobocodeProblem(this, cloner);
91    }
92
93    private void InitializeOperators() {
94      Operators.AddRange(
95        ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
96      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
97      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
98      Operators.Add(new BestSolutionAnalyzer());
99      ParameterizeOperators();
100      ParameterizeAnalyzers();
101    }
102
103    protected override void OnEvaluatorChanged() {
104      base.OnEvaluatorChanged();
105      Evaluator.TankProgramParameter.ActualName =
106        TankProgramParameterName;
107      ParameterizeAnalyzers();
108      ParameterizeOperators();
109    }
110
111    protected override void OnSolutionCreatorChanged() {
112      base.OnSolutionCreatorChanged();
113      SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
114        TankProgramParameterName;
115      ParameterizeAnalyzers();
116      ParameterizeOperators();
117    }
118
119    private void ParameterizeAnalyzers() {
120      var analyzers = Operators.OfType<IAnalyzer>();
121      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
122        o.SymbolicExpressionTreeParameter.ActualName =
123          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
124      }
125      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
126        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
127      }
128    }
129
130    private void ParameterizeOperators() {
131      var operators = Parameters
132        .OfType<IValueParameter>()
133        .Select(p => p.Value)
134        .OfType<IOperator>()
135        .Union(Operators);
136      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
137        o.SymbolicExpressionTreeGrammarParameter.ActualName =
138          TankGrammarParameterName;
139      }
140      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
141        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
142          MaxTankProgramDepthParameterName;
143        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
144          MaxTankProgramLengthParameterName;
145      }
146      foreach (var op in operators.OfType<RobocodeEvaluator>()) {
147        op.TankProgramParameter.ActualName =
148          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
149      }
150      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
151        op.ParentsParameter.ActualName =
152          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
153        op.ChildParameter.ActualName =
154          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
155      }
156      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
157        op.SymbolicExpressionTreeParameter.ActualName =
158          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
159      }
160      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
161        op.SymbolicExpressionTreeParameter.ActualName =
162          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.