Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 use directory values instead of string values for the robocode path

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