Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Problem.cs @ 13083

Last change on this file since 13083 was 13017, checked in by gkronber, 9 years ago

#2069 refactored grammar, symbols, and interpreter

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