source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.GeneticProgramming/3.3/robocode/Problem.cs @ 17270

Last change on this file since 17270 was 17270, checked in by abeham, 3 years ago

#2521: worked on removing virtual from Maximization for single-objective problems

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
31  [StorableType("0B4FE44B-3044-4531-8CA9-3C4D3BB3A4BB")]
32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 360)]
33  [Item("Robocode Problem", "Evolution of a robocode program in java using genetic programming.")]
34  public class Problem : SymbolicExpressionTreeProblem {
35    #region Parameter Names
36    private const string RobocodePathParamaterName = "RobocodePath";
37    private const string NrOfRoundsParameterName = "NrOfRounds";
38    private const string EnemiesParameterName = "Enemies";
39    #endregion
40
41    #region Parameters
42    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
43      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
44    }
45    public IFixedValueParameter<IntValue> NrOfRoundsParameter {
46      get { return (IFixedValueParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
47    }
48    public IValueParameter<EnemyCollection> EnemiesParameter {
49      get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
50    }
51
52    public string RobocodePath {
53      get { return RobocodePathParameter.Value.Value; }
54      set { RobocodePathParameter.Value.Value = value; }
55    }
56
57    public int NrOfRounds {
58      get { return NrOfRoundsParameter.Value.Value; }
59      set { NrOfRoundsParameter.Value.Value = value; }
60    }
61
62    public EnemyCollection Enemies {
63      get { return EnemiesParameter.Value; }
64      set { EnemiesParameter.Value = value; }
65    }
66    #endregion
67
68    [StorableConstructor]
69    protected Problem(StorableConstructorFlag _) : base(_) { }
70    protected Problem(Problem original, Cloner cloner)
71      : base(original, cloner) {
72      RegisterEventHandlers();
73    }
74
75    public Problem() : base(new SymbolicExpressionTreeEncoding(new Grammar(), maximumLength: 1000, maximumDepth: 10)) {
76      Maximization = true;
77      DirectoryValue robocodeDir = new DirectoryValue { Value = @"robocode" };
78
79      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
80      robotList.RobocodePath = robocodeDir.Value;
81
82
83      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
84      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Number of rounds a robot has to fight against each opponent.", new IntValue(3)));
85      Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList));
86
87      Encoding.FunctionArguments = 0;
88      Encoding.FunctionDefinitions = 0;
89
90      RegisterEventHandlers();
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new Problem(this, cloner);
95    }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() { RegisterEventHandlers(); }
99
100    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
101      return Interpreter.EvaluateTankProgram(tree, RobocodePath, Enemies, null, false, NrOfRounds);
102    }
103
104    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
105      // find the tree with the best quality
106      double maxQuality = double.NegativeInfinity;
107      ISymbolicExpressionTree bestTree = null;
108      for (int i = 0; i < qualities.Length; i++) {
109        if (qualities[i] > maxQuality) {
110          maxQuality = qualities[i];
111          bestTree = trees[i];
112        }
113      }
114
115      // create a solution instance
116      var bestSolution = new Solution(bestTree, RobocodePath, NrOfRounds, Enemies);
117
118      // also add the best solution as a result to the result collection
119      // or alternatively update the existing result
120      if (!results.ContainsKey("BestSolution")) {
121        results.Add(new Result("BestSolution", "The best tank program", bestSolution));
122      } else {
123        results["BestSolution"].Value = bestSolution;
124      }
125    }
126
127    private void RegisterEventHandlers() {
128      RobocodePathParameter.Value.StringValue.ValueChanged += RobocodePathParameter_ValueChanged;
129    }
130
131    private void RobocodePathParameter_ValueChanged(object sender, System.EventArgs e) {
132      EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value;
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.