Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 removed dead code

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