Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Problem.cs @ 9844

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

#2069

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