Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: working on refactoring

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