Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/Evaluator.cs @ 9567

Last change on this file since 9567 was 9567, checked in by melkaref, 11 years ago

Removed need to load xml file in Evaluator.cs;
Added BattleRunner.java

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