Changeset 13015


Ignore:
Timestamp:
10/15/15 17:31:02 (5 years ago)
Author:
gkronber
Message:

#2069: refactored Robocode problem and derived from SymbolicExpressionTreeProblem

Location:
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
Files:
2 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/HeuristicLab.Problems.Robocode-3.3.csproj

    r11628 r13015  
    9292  </ItemGroup>
    9393  <ItemGroup>
    94     <Compile Include="BestSolutionAnalyzer.cs" />
    9594    <None Include="BattleObserver.class">
    9695      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     
    102101    <None Include="Plugin.cs.frame" />
    103102    <Compile Include="EnemyCollection.cs" />
    104     <Compile Include="RobocodeEvaluator.cs" />
    105103    <Compile Include="Grammar.cs" />
    106104    <Compile Include="Interpreter.cs" />
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs

    r13013 r13015  
    2020#endregion
    2121
    22 using System.Linq;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    2827using HeuristicLab.Parameters;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    30 using HeuristicLab.PluginInfrastructure;
    3129
    3230namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    3331  [StorableClass]
    34   [Creatable("Problems")]
    35   [Item("Robocode Problem",
    36         "The Robocode problem for genetic programming.")]
    37   public class RobocodeProblem : SingleObjectiveHeuristicOptimizationProblem<RobocodeEvaluator,
    38     ISymbolicExpressionTreeCreator> {
     32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 360)]
     33  [Item("Robocode Problem", "Evolution of a robocode program in java using genetic programming.")]
     34  public class RobocodeProblem : SymbolicExpressionTreeProblem {
    3935    #region Parameter Names
    40     private const string TankProgramParameterName = "TankProgram";
    4136    private const string MaxTankProgramLengthParameterName = "MaxProgramLength";
    4237    private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";
    43     private const string TankGrammarParameterName = "Grammar";
    4438    private const string RobocodePathParamaterName = "RobocodePath";
    4539    private const string NrOfRoundsParameterName = "NrOfRounds";
     
    4842
    4943    #region Parameters
    50     public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
    51       get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
    52     }
    53     public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
    54       get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
    55     }
    56     public IValueParameter<Grammar> GrammarParameter {
    57       get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; }
    58     }
     44    //public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
     45    //  get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
     46    //}
     47    //public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
     48    //  get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
     49    //}
    5950    public IFixedValueParameter<DirectoryValue> RobocodePathParameter {
    6051      get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
     
    6556    public IValueParameter<EnemyCollection> EnemiesParameter {
    6657      get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
     58    }
     59
     60    //public int MaxTankProgramDepth {
     61    //  get { return MaxTankProgramDepthParameter.Value.Value; }
     62    //  set { MaxTankProgramDepthParameter.Value.Value = value; }
     63    //}
     64    //
     65    //public int MaxTankProgramLength {
     66    //  get { return MaxTankProgramLengthParameter.Value.Value; }
     67    //  set { MaxTankProgramLengthParameter.Value.Value = value; }
     68    //}
     69
     70    public string RobocodePath {
     71      get { return RobocodePathParameter.Value.Value; }
     72      set { RobocodePathParameter.Value.Value = value; }
     73    }
     74
     75    public int NrOfRounds {
     76      get { return NrOfRoundsParameter.Value.Value; }
     77      set { NrOfRoundsParameter.Value.Value = value; }
     78    }
     79
     80    public EnemyCollection Enemies {
     81      get { return EnemiesParameter.Value; }
     82      set { EnemiesParameter.Value = value; }
    6783    }
    6884    #endregion
     
    7692
    7793    public RobocodeProblem()
    78       : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) {
    79       DirectoryValue robocodeDir = new DirectoryValue();
    80       robocodeDir.Value = @"C:\robocode";
     94      : base() {
     95      DirectoryValue robocodeDir = new DirectoryValue { Value = @"C:\robocode" };
    8196
    8297      var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value);
    8398      robotList.RobocodePath = robocodeDir.Value;
    8499
    85       Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(10)));
    86       Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
    87       Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
     100
    88101      Parameters.Add(new FixedValueParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir));
    89       Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Nr. of rounds a robot has to fight against each opponent.", new IntValue(3)));
     102      Parameters.Add(new FixedValueParameter<IntValue>(NrOfRoundsParameterName, "Number of rounds a robot has to fight against each opponent.", new IntValue(3)));
    90103      Parameters.Add(new ValueParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled.", robotList));
    91104
    92       Maximization.Value = true;
    93       InitializeOperators();
     105      Encoding = new SymbolicExpressionTreeEncoding(new Grammar(), 1000, 10);
     106      Encoding.FunctionArguments = 0;
     107      Encoding.FunctionDefinitions = 0;
     108
    94109      RegisterEventHandlers();
    95110    }
     
    100115
    101116    [StorableHook(HookType.AfterDeserialization)]
    102     private void AfterDeserialization() {
    103       RegisterEventHandlers();
     117    private void AfterDeserialization() { RegisterEventHandlers(); }
     118
     119    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
     120      return Interpreter.EvaluateTankProgram(tree, RobocodePath, Enemies, null, false, NrOfRounds);
    104121    }
    105122
    106     private void InitializeOperators() {
    107       Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>()
    108         .Where(x => !(x is ISymbolicExpressionTreeArchitectureAlteringOperator)));
    109       Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
    110       Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    111       Operators.Add(new BestSolutionAnalyzer());
    112       ParameterizeOperators();
    113       ParameterizeAnalyzers();
     123    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
     124      // find the tree with the best quality
     125      double maxQuality = double.NegativeInfinity;
     126      ISymbolicExpressionTree bestTree = null;
     127      for (int i = 0; i < qualities.Length; i++) {
     128        if (qualities[i] > maxQuality) {
     129          maxQuality = qualities[i];
     130          bestTree = trees[i];
     131        }
     132      }
     133
     134      // create a solution instance
     135      var bestSolution = new Solution(bestTree, RobocodePath, NrOfRounds, Enemies);
     136
     137      // also add the best solution as a result to the result collection
     138      // or alternatively update the existing result
     139      if (!results.ContainsKey("BestSolution")) {
     140        results.Add(new Result("BestSolution", "The best tank program", bestSolution));
     141      } else {
     142        results["BestSolution"].Value = bestSolution;
     143      }
     144    }
     145
     146    public override bool Maximization {
     147      get { return true; }
    114148    }
    115149
     
    121155      EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value;
    122156    }
    123 
    124     protected override void OnEvaluatorChanged() {
    125       base.OnEvaluatorChanged();
    126       Evaluator.TankProgramParameter.ActualName =
    127         TankProgramParameterName;
    128       ParameterizeAnalyzers();
    129       ParameterizeOperators();
    130     }
    131 
    132     protected override void OnSolutionCreatorChanged() {
    133       base.OnSolutionCreatorChanged();
    134       SolutionCreator.SymbolicExpressionTreeParameter.ActualName =
    135         TankProgramParameterName;
    136       ParameterizeAnalyzers();
    137       ParameterizeOperators();
    138     }
    139 
    140     private void ParameterizeAnalyzers() {
    141       var analyzers = Operators.OfType<IAnalyzer>();
    142       foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
    143         o.SymbolicExpressionTreeParameter.ActualName =
    144           SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    145       }
    146       foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
    147         o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    148       }
    149     }
    150 
    151     private void ParameterizeOperators() {
    152       var operators = Parameters
    153         .OfType<IValueParameter>()
    154         .Select(p => p.Value)
    155         .OfType<IOperator>()
    156         .Union(Operators);
    157       foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
    158         o.SymbolicExpressionTreeGrammarParameter.ActualName =
    159           TankGrammarParameterName;
    160       }
    161       foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
    162         o.MaximumSymbolicExpressionTreeDepthParameter.ActualName =
    163           MaxTankProgramDepthParameterName;
    164         o.MaximumSymbolicExpressionTreeLengthParameter.ActualName =
    165           MaxTankProgramLengthParameterName;
    166       }
    167       foreach (var op in operators.OfType<RobocodeEvaluator>()) {
    168         op.TankProgramParameter.ActualName =
    169           SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    170       }
    171       foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
    172         op.ParentsParameter.ActualName =
    173           SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    174         // op.ChildParameter.ActualName =
    175         //   SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    176       }
    177       foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
    178         op.SymbolicExpressionTreeParameter.ActualName =
    179           SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    180       }
    181       foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
    182         op.SymbolicExpressionTreeParameter.ActualName =
    183           SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    184       }
    185     }
    186157  }
    187158}
Note: See TracChangeset for help on using the changeset viewer.