#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Problems.Robocode { [StorableClass] [Creatable("Problems")] [Item("Robocode Problem", "The Robocode problem for genetic programming.")] public class RobocodeProblem : SingleObjectiveHeuristicOptimizationProblem { #region Parameter Names private const string TankProgramParameterName = "TankProgram"; private const string MaxTankProgramLengthParameterName = "MaxProgramLength"; private const string MaxTankProgramDepthParameterName = "MaxProgramDepth"; private const string TankGrammarParameterName = "Grammar"; private const string RobocodePathParamaterName = "RobocodePath"; private const string NrOfRoundsParameterName = "NrOfRounds"; private const string EnemiesParameterName = "Enemies"; #endregion #region Parameters public IFixedValueParameter MaxTankProgramLengthParameter { get { return (IFixedValueParameter)Parameters[MaxTankProgramLengthParameterName]; } } public IFixedValueParameter MaxTankProgramDepthParameter { get { return (IFixedValueParameter)Parameters[MaxTankProgramDepthParameterName]; } } public IValueParameter GrammarParameter { get { return (IValueParameter)Parameters[TankGrammarParameterName]; } } public IFixedValueParameter RobocodePathParameter { get { return (IFixedValueParameter)Parameters[RobocodePathParamaterName]; } } public IFixedValueParameter NrOfRoundsParameter { get { return (IFixedValueParameter)Parameters[NrOfRoundsParameterName]; } } public IValueParameter EnemiesParameter { get { return (IValueParameter)Parameters[EnemiesParameterName]; } } #endregion [StorableConstructor] protected RobocodeProblem(bool deserializing) : base(deserializing) { } protected RobocodeProblem(RobocodeProblem original, Cloner cloner) : base(original, cloner) { RegisterEventHandlers(); } public RobocodeProblem() : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) { DirectoryValue robocodeDir = new DirectoryValue(); robocodeDir.Value = @"C:\robocode"; var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value); robotList.RobocodePath = robocodeDir.Value; Parameters.Add(new FixedValueParameter(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(10))); Parameters.Add(new FixedValueParameter(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000))); Parameters.Add(new ValueParameter(TankGrammarParameterName, "Grammar for the tank program.", new Grammar())); Parameters.Add(new FixedValueParameter(RobocodePathParamaterName, "Path of the Robocode installation.", robocodeDir)); Parameters.Add(new FixedValueParameter(NrOfRoundsParameterName, "Nr. of rounds a robot has to fight against each opponent.", new IntValue(3))); Parameters.Add(new ValueParameter(EnemiesParameterName, "The enemies that should be battled.", robotList)); Maximization.Value = true; InitializeOperators(); RegisterEventHandlers(); } public override IDeepCloneable Clone(Cloner cloner) { return new RobocodeProblem(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEventHandlers(); } private void InitializeOperators() { Operators.AddRange(ApplicationManager.Manager.GetInstances() .Where(x => !(x is ISymbolicExpressionTreeArchitectureAlteringOperator))); Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer()); Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer()); Operators.Add(new BestSolutionAnalyzer()); ParameterizeOperators(); ParameterizeAnalyzers(); } private void RegisterEventHandlers() { RobocodePathParameter.Value.StringValue.ValueChanged += RobocodePathParameter_ValueChanged; } void RobocodePathParameter_ValueChanged(object sender, System.EventArgs e) { EnemiesParameter.Value.RobocodePath = RobocodePathParameter.Value.Value; } protected override void OnEvaluatorChanged() { base.OnEvaluatorChanged(); Evaluator.TankProgramParameter.ActualName = TankProgramParameterName; ParameterizeAnalyzers(); ParameterizeOperators(); } protected override void OnSolutionCreatorChanged() { base.OnSolutionCreatorChanged(); SolutionCreator.SymbolicExpressionTreeParameter.ActualName = TankProgramParameterName; ParameterizeAnalyzers(); ParameterizeOperators(); } private void ParameterizeAnalyzers() { var analyzers = Operators.OfType(); foreach (var o in analyzers.OfType()) { o.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; } foreach (var o in analyzers.OfType()) { o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; } } private void ParameterizeOperators() { var operators = Parameters .OfType() .Select(p => p.Value) .OfType() .Union(Operators); foreach (var o in operators.OfType()) { o.SymbolicExpressionTreeGrammarParameter.ActualName = TankGrammarParameterName; } foreach (var o in operators.OfType()) { o.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxTankProgramDepthParameterName; o.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxTankProgramLengthParameterName; } foreach (var op in operators.OfType()) { op.TankProgramParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; } foreach (var op in operators.OfType()) { op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; } foreach (var op in operators.OfType()) { op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; } foreach (var op in operators.OfType()) { op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; } } } }