Changeset 9880 for branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs
- Timestamp:
- 08/13/13 12:11:18 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs
r9879 r9880 20 20 #endregion 21 21 22 23 using System;24 22 using System.Threading; 25 using System.Xml;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; … … 36 33 [StorableClass] 37 34 [Item("Robocode Evaluator", "Evaluator for the Robocode GP problem.")] 38 public class RobocodeEvaluator : SingleSuccessorOperator, 39 ISingleObjectiveEvaluator { 40 #region parameter names 35 public class RobocodeEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator { 41 36 private const string QualityParameterName = "Quality"; 42 37 private const string TankProgramParameterName = "TankProgram"; 43 38 private const string RobocodePathParamaterName = "Path"; 44 private const string CoevolutionParameterName = "Coevolution";45 39 40 //TODO: this should be removed 41 public static SemaphoreSlim semaphore = new SemaphoreSlim(10); 46 42 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 43 #region Parameters 60 44 public ILookupParameter<DoubleValue> QualityParameter { 61 get { 62 return (ILookupParameter<DoubleValue>) 63 Parameters[QualityParameterName]; 64 } 45 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 65 46 } 66 47 public ILookupParameter<ISymbolicExpressionTree> TankProgramParameter { 67 get { 68 return (ILookupParameter<ISymbolicExpressionTree>) 69 Parameters[TankProgramParameterName]; 70 } 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[TankProgramParameterName]; } 71 49 } 72 50 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 } 51 get { return (ILookupParameter<StringValue>)Parameters[RobocodePathParamaterName]; } 83 52 } 84 53 #endregion 85 86 54 87 55 [StorableConstructor] 88 56 protected RobocodeEvaluator(bool deserializing) : base(deserializing) { } 89 57 protected RobocodeEvaluator(RobocodeEvaluator original, Cloner cloner) 90 : base(original, cloner) { 58 : base(original, cloner) { } 59 60 public RobocodeEvaluator() { 61 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the Robocode tank program.")); 62 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(TankProgramParameterName, "The Robocode tank program to evaluate represented as a symbolic expression tree.")); 63 Parameters.Add(new LookupParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation.")); 91 64 } 92 65 93 // default constructor for the evaluator94 public RobocodeEvaluator() {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 operator117 // here we take trees as inputs and calculate qualities118 // (fitness evaluation)119 66 public override IOperation Apply() { 120 67 semaphore.Wait(); 121 68 122 69 ISymbolicExpressionTree tree = TankProgramParameter.ActualValue; 123 //XmlDocument doc = new XmlDocument();124 //doc.Load("../tank.xml");125 XmlNode node = null;126 70 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)); 71 QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, path)); 149 72 150 73 semaphore.Release(); 151 74 152 // important: return base.Apply() to make sure that the153 // next operator is queued for execution154 75 return base.Apply(); 155 76 } … … 158 79 return new RobocodeEvaluator(this, cloner); 159 80 } 160 161 public bool EnabledByDefault {162 get { return true; }163 }164 81 } 165 82 }
Note: See TracChangeset
for help on using the changeset viewer.