Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 removed unused symbols and cleaned up code

File size: 7.5 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 = "Path";
45    private const string CoevolutionParameterName = "Coevolution";
46    #endregion
47
48    #region Parameters
49    public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
50      get {
51        return (IFixedValueParameter<IntValue>)
52          Parameters[MaxTankProgramLengthParameterName];
53      }
54    }
55    public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
56      get {
57        return (IFixedValueParameter<IntValue>)
58          Parameters[MaxTankProgramDepthParameterName];
59      }
60    }
61    public IValueParameter<Grammar> GrammarParameter {
62      get {
63        return (IValueParameter<Grammar>)
64          Parameters[TankGrammarParameterName];
65      }
66    }
67    public IFixedValueParameter<StringValue> RobocodePathParameter {
68      get {
69        return (IFixedValueParameter<StringValue>)
70            Parameters[RobocodePathParamaterName];
71      }
72    }
73    public IFixedValueParameter<BoolValue> CoevolutionParameter {
74      get {
75        return (IFixedValueParameter<BoolValue>)
76            Parameters[CoevolutionParameterName];
77      }
78    }
79    #endregion
80
81    [StorableConstructor]
82    protected RobocodeProblem(bool deserializing)
83      : base(deserializing) {
84    }
85    protected RobocodeProblem(RobocodeProblem original, Cloner cloner)
86      : base(original, cloner) {
87    }
88
89    // default constructor for the problem
90    // also creates the fitness evaluation operator (Evaluator),
91    // and the tree creation operator (RampedHalfAndHalfTreeCreator)
92    public RobocodeProblem()
93      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
94      Parameters.Add(
95         new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName,
96           "Maximal depth of the Robocode tank program.",
97           new IntValue(6)));
98      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName,
99          "Maximal length of the tank program.", new IntValue(1000)));
100      Parameters.Add(
101        new ValueParameter<Grammar>(TankGrammarParameterName,
102          "Grammar for the tank program.",
103          new Grammar()));
104      Parameters.Add(new FixedValueParameter<StringValue>(RobocodePathParamaterName,
105          "Path of the Robocode installation.",
106          new StringValue("F:/robocode2")));
107      Parameters.Add(new FixedValueParameter<BoolValue>(CoevolutionParameterName,
108          "Use Coevolution", new BoolValue(false)));
109
110      Maximization.Value = true;
111      InitializeOperators();
112    }
113
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new RobocodeProblem(this, cloner);
116    }
117
118    private void InitializeOperators() {
119      Operators.AddRange(
120        ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
121      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
122      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
123      Operators.Add(new BestSolutionAnalyzer());
124      ParameterizeOperators();
125      ParameterizeAnalyzers();
126    }
127
128    protected override void OnEvaluatorChanged() {
129      base.OnEvaluatorChanged();
130      Evaluator.TankProgramParameter.ActualName =
131        TankProgramParameterName;
132      ParameterizeAnalyzers();
133      ParameterizeOperators();
134    }
135
136    protected override void OnSolutionCreatorChanged() {
137      base.OnSolutionCreatorChanged();
138      SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
139        TankProgramParameterName;
140      ParameterizeAnalyzers();
141      ParameterizeOperators();
142    }
143
144    private void ParameterizeAnalyzers() {
145      var analyzers = Operators.OfType<IAnalyzer>();
146      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
147        o.SymbolicExpressionTreeParameter.ActualName =
148          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
149      }
150      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
151        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
152      }
153    }
154
155    private void ParameterizeOperators() {
156      var operators = Parameters
157        .OfType<IValueParameter>()
158        .Select(p => p.Value)
159        .OfType<IOperator>()
160        .Union(Operators);
161      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
162        o.SymbolicExpressionTreeGrammarParameter.ActualName =
163          TankGrammarParameterName;
164      }
165      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
166        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
167          MaxTankProgramDepthParameterName;
168        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
169          MaxTankProgramLengthParameterName;
170      }
171      foreach (var op in operators.OfType<RobocodeEvaluator>()) {
172        op.TankProgramParameter.ActualName =
173          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
174      }
175      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
176        op.ParentsParameter.ActualName =
177          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
178        op.ChildParameter.ActualName =
179          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
180      }
181      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
182        op.SymbolicExpressionTreeParameter.ActualName =
183          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
184      }
185      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
186        op.SymbolicExpressionTreeParameter.ActualName =
187          SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
188      }
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.