[9565] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Data;
|
---|
| 6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using HeuristicLab.Parameters;
|
---|
| 9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 10 | using HeuristicLab.PluginInfrastructure;
|
---|
| 11 |
|
---|
| 12 | namespace HeuristicLab.Problems.Robocode
|
---|
| 13 | {
|
---|
| 14 | [StorableClass]
|
---|
| 15 | [Creatable("Problems")]
|
---|
| 16 | [Item("Robocode Problem",
|
---|
| 17 | "The Robocode problem for genetic programming.")]
|
---|
| 18 | public class Problem : SingleObjectiveHeuristicOptimizationProblem<Evaluator,
|
---|
| 19 | ISymbolicExpressionTreeCreator>
|
---|
| 20 | {
|
---|
| 21 | #region parameter names
|
---|
| 22 | private const string TankProgramParameterName = "TankProgram";
|
---|
| 23 | private const string MaxTankProgramLengthParameterName =
|
---|
| 24 | "MaxProgramLength";
|
---|
| 25 | private const string MaxTankProgramDepthParameterName =
|
---|
| 26 | "MaxProgramDepth";
|
---|
| 27 | private const string TankGrammarParameterName = "Grammar";
|
---|
| 28 | private const string RobocodePathParamaterName = "Path";
|
---|
| 29 | private const string CoevolutionParameterName = "Coevolution";
|
---|
| 30 | #endregion
|
---|
| 31 |
|
---|
| 32 | #region parameters
|
---|
| 33 | public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter
|
---|
| 34 | {
|
---|
| 35 | get
|
---|
| 36 | {
|
---|
| 37 | return (IFixedValueParameter<IntValue>)
|
---|
| 38 | Parameters[MaxTankProgramLengthParameterName];
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter
|
---|
| 42 | {
|
---|
| 43 | get
|
---|
| 44 | {
|
---|
| 45 | return (IFixedValueParameter<IntValue>)
|
---|
| 46 | Parameters[MaxTankProgramDepthParameterName];
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | public IValueParameter<Grammar> GrammarParameter
|
---|
| 50 | {
|
---|
| 51 | get
|
---|
| 52 | {
|
---|
| 53 | return (IValueParameter<Grammar>)
|
---|
| 54 | Parameters[TankGrammarParameterName];
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | public IFixedValueParameter<StringValue> RobocodePathParameter
|
---|
| 58 | {
|
---|
| 59 | get
|
---|
| 60 | {
|
---|
| 61 | return (IFixedValueParameter<StringValue>)
|
---|
| 62 | Parameters[RobocodePathParamaterName];
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | public IFixedValueParameter<BoolValue> CoevolutionParameter
|
---|
| 66 | {
|
---|
| 67 | get
|
---|
| 68 | {
|
---|
| 69 | return (IFixedValueParameter<BoolValue>)
|
---|
| 70 | Parameters[CoevolutionParameterName];
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | #endregion
|
---|
| 74 |
|
---|
| 75 | [StorableConstructor]
|
---|
| 76 | protected Problem(bool deserializing)
|
---|
| 77 | : base(deserializing)
|
---|
| 78 | {
|
---|
| 79 | }
|
---|
| 80 | protected Problem(Problem original, Cloner cloner)
|
---|
| 81 | : base(original, cloner)
|
---|
| 82 | {
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // default constructor for the problem
|
---|
| 86 | // also creates the fitness evaluation operator (Evaluator),
|
---|
| 87 | // and the tree creation operator (RampedHalfAndHalfTreeCreator)
|
---|
| 88 | public Problem()
|
---|
| 89 | : base(new Evaluator(), new RampedHalfAndHalfTreeCreator())
|
---|
| 90 | {
|
---|
| 91 | Parameters.Add(
|
---|
| 92 | new FixedValueParameter<IntValue>(
|
---|
| 93 | MaxTankProgramDepthParameterName,
|
---|
| 94 | "Maximal depth of the Robocode tank program.",
|
---|
| 95 | new IntValue(6)));
|
---|
| 96 | Parameters.Add(
|
---|
| 97 | new FixedValueParameter<IntValue>(
|
---|
| 98 | MaxTankProgramLengthParameterName,
|
---|
| 99 | "Maximal length of the tank program.",
|
---|
| 100 | new IntValue(1000)));
|
---|
| 101 | Parameters.Add(
|
---|
| 102 | new ValueParameter<Grammar>(
|
---|
| 103 | TankGrammarParameterName,
|
---|
| 104 | "Grammar for the tank program.",
|
---|
| 105 | new Grammar()));
|
---|
| 106 | Parameters.Add(new FixedValueParameter<StringValue>(
|
---|
| 107 | RobocodePathParamaterName,
|
---|
| 108 | "Path of the Robocode installation.",
|
---|
| 109 | new StringValue("F:/robocode2")));
|
---|
| 110 | Parameters.Add(new FixedValueParameter<BoolValue>(
|
---|
| 111 | CoevolutionParameterName,
|
---|
| 112 | "Use Coevolution", new BoolValue(false)));
|
---|
| 113 |
|
---|
| 114 | Maximization.Value = true;
|
---|
| 115 | InitializeOperators();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 119 | {
|
---|
| 120 | return new Problem(this, cloner);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void InitializeOperators()
|
---|
| 124 | {
|
---|
| 125 | Operators.AddRange(
|
---|
| 126 | ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
|
---|
| 127 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
| 128 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
| 129 | Operators.Add(new BestSolutionAnalyzer());
|
---|
| 130 | ParameterizeOperators();
|
---|
| 131 | ParameterizeAnalyzers();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | protected override void OnEvaluatorChanged()
|
---|
| 135 | {
|
---|
| 136 | base.OnEvaluatorChanged();
|
---|
| 137 | Evaluator.TankProgramParameter.ActualName =
|
---|
| 138 | TankProgramParameterName;
|
---|
| 139 | ParameterizeAnalyzers();
|
---|
| 140 | ParameterizeOperators();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | protected override void OnSolutionCreatorChanged()
|
---|
| 144 | {
|
---|
| 145 | base.OnSolutionCreatorChanged();
|
---|
| 146 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
|
---|
| 147 | TankProgramParameterName;
|
---|
| 148 | ParameterizeAnalyzers();
|
---|
| 149 | ParameterizeOperators();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private void ParameterizeAnalyzers()
|
---|
| 153 | {
|
---|
| 154 | var analyzers = Operators.OfType<IAnalyzer>();
|
---|
| 155 | foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>())
|
---|
| 156 | {
|
---|
| 157 | o.SymbolicExpressionTreeParameter.ActualName =
|
---|
| 158 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 159 | }
|
---|
| 160 | foreach (var o in analyzers.OfType<BestSolutionAnalyzer>())
|
---|
| 161 | {
|
---|
| 162 | o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private void ParameterizeOperators()
|
---|
| 167 | {
|
---|
| 168 | var operators = Parameters
|
---|
| 169 | .OfType<IValueParameter>()
|
---|
| 170 | .Select(p => p.Value)
|
---|
| 171 | .OfType<IOperator>()
|
---|
| 172 | .Union(Operators);
|
---|
| 173 | foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>())
|
---|
| 174 | {
|
---|
| 175 | o.SymbolicExpressionTreeGrammarParameter.ActualName =
|
---|
| 176 | TankGrammarParameterName;
|
---|
| 177 | }
|
---|
| 178 | foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>())
|
---|
| 179 | {
|
---|
| 180 | o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
|
---|
| 181 | MaxTankProgramDepthParameterName;
|
---|
| 182 | o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
|
---|
| 183 | MaxTankProgramLengthParameterName;
|
---|
| 184 | }
|
---|
| 185 | foreach (var op in operators.OfType<Evaluator>())
|
---|
| 186 | {
|
---|
| 187 | op.TankProgramParameter.ActualName =
|
---|
| 188 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 189 | }
|
---|
| 190 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>())
|
---|
| 191 | {
|
---|
| 192 | op.ParentsParameter.ActualName =
|
---|
| 193 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 194 | op.ChildParameter.ActualName =
|
---|
| 195 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 196 | }
|
---|
| 197 | foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>())
|
---|
| 198 | {
|
---|
| 199 | op.SymbolicExpressionTreeParameter.ActualName =
|
---|
| 200 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 201 | }
|
---|
| 202 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>())
|
---|
| 203 | {
|
---|
| 204 | op.SymbolicExpressionTreeParameter.ActualName =
|
---|
| 205 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | } |
---|