Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17655 was 17655, checked in by abeham, 4 years ago

#2521: adapted readonly of reference parameters

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