Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Evaluator.cs @ 9844

Last change on this file since 9844 was 9790, checked in by ascheibe, 11 years ago

#2069

  • added license headers
  • corrected version information
  • fixed formatting
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22
23using System;
24using System.Threading;
25using System.Xml;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.Problems.Robocode {
36  [StorableClass]
37  [Item("Robocode Evaluator", "Evaluator for the Robocode GP problem.")]
38  public class Evaluator : SingleSuccessorOperator,
39    ISingleObjectiveEvaluator {
40    #region parameter names
41    private const string QualityParameterName = "Quality";
42    private const string TankProgramParameterName = "TankProgram";
43    private const string RobocodePathParamaterName = "Path";
44    private const string CoevolutionParameterName = "Coevolution";
45
46
47    private static string solutionTemplatePath = "";
48    public static string SolutionTemplate {
49      get { return solutionTemplatePath; }
50      set {
51        solutionTemplatePath = value;
52      }
53    }
54
55    public static SemaphoreSlim semaphore = new SemaphoreSlim(10);
56    #endregion
57
58
59    #region parameters
60    public ILookupParameter<DoubleValue> QualityParameter {
61      get {
62        return (ILookupParameter<DoubleValue>)
63                 Parameters[QualityParameterName];
64      }
65    }
66    public ILookupParameter<ISymbolicExpressionTree> TankProgramParameter {
67      get {
68        return (ILookupParameter<ISymbolicExpressionTree>)
69                 Parameters[TankProgramParameterName];
70      }
71    }
72    public ILookupParameter<StringValue> RobocodePathParameter {
73      get {
74        return (ILookupParameter<StringValue>)
75            Parameters[RobocodePathParamaterName];
76      }
77    }
78    public ILookupParameter<BoolValue> CoevolutionParameter {
79      get {
80        return (ILookupParameter<BoolValue>)
81            Parameters[CoevolutionParameterName];
82      }
83    }
84    #endregion
85
86
87    [StorableConstructor]
88    protected Evaluator(bool deserializing) : base(deserializing) { }
89    protected Evaluator(Evaluator original, Cloner cloner)
90      : base(original, cloner) {
91    }
92
93    // default constructor for the evaluator
94    public Evaluator() {
95      Parameters.Add(
96        new LookupParameter<DoubleValue>(
97          QualityParameterName,
98          "The solution quality of the Robocode tank program."));
99      Parameters.Add(
100        new LookupParameter<ISymbolicExpressionTree>(
101          TankProgramParameterName,
102          "The Robocode tank program to evaluate represented as a " +
103          "symbolic expression tree."));
104      Parameters.Add(
105        new LookupParameter<StringValue>(
106          RobocodePathParamaterName,
107          "Path of the Robocode installation."));
108      Parameters.Add(
109          new LookupParameter<BoolValue>(
110              CoevolutionParameterName,
111              "Use Coevolution"));
112
113      SolutionTemplate = "../tank.xml";
114    }
115
116    // override the apply method to change the behaviour of the operator
117    // here we take trees as inputs and calculate qualities
118    // (fitness evaluation)
119    public override IOperation Apply() {
120      semaphore.Wait();
121
122      ISymbolicExpressionTree tree = TankProgramParameter.ActualValue;
123      //XmlDocument doc = new XmlDocument();
124      //doc.Load("../tank.xml");
125      XmlNode node = null;
126      string path = RobocodePathParameter.ActualValue.Value;
127
128      if (CoevolutionParameter.ActualValue.Value) {
129        var trees = ExecutionContext.Parent.Scope.SubScopes;//SymbolicExpressionTreeParameter.ActualValue;
130        if (trees.Count == 2) {
131          trees = trees[0].SubScopes;
132          //treeCount = trees[0].SubScopes.Count;
133          //int selTreeCount = trees[1].SubScopes.Count;
134        }
135
136        Random random = new Random();
137        ScopeList chosenScopes = new ScopeList(5);
138        for (int i = 0; i < 5; i++) {
139          var newScope = trees.ToArray()[random.Next(0, trees.Count - 1)];
140          chosenScopes.Add(newScope);
141        }
142
143        //if (evaluations % 99 == 0)
144        //    evaluations = evaluations;
145
146        QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, chosenScopes, node, path));
147      } else
148        QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, node, path));
149
150      semaphore.Release();
151
152      // important: return base.Apply() to make sure that the
153      // next operator is queued for execution
154      return base.Apply();
155    }
156
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new Evaluator(this, cloner);
159    }
160
161    public bool EnabledByDefault {
162      get { return true; }
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.