using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Robocode { [StorableClass] [Item("Best Tank program Analyzer", "Analyzer that stores the best tank program.")] public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer { #region parameter names private const string QualityParameterName = "Quality"; private const string SymbolicExpressionTreeParameterName = "TankProgram"; private const string MovesParameterName = "Moves"; private const string ShotsParameterName = "Shots"; private const string BestSolutionParameterName = "Best solution"; private const string ResultsParameterName = "Results"; private const string RobocodePathParamaterName = "Path"; private const string CoevolutionParameterName = "Coevolution"; #endregion #region parameters public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter) Parameters[QualityParameterName]; } } public IScopeTreeLookupParameter SymbolicExpressionTreeParameter { get { return (IScopeTreeLookupParameter) Parameters[SymbolicExpressionTreeParameterName]; } } //public ILookupParameter MovesParameter //{ // get // { // return (ILookupParameter) // Parameters[MovesParameterName]; // } //} //public ILookupParameter ShotsParameter //{ // get // { // return (ILookupParameter) // Parameters[ShotsParameterName]; // } //} public ILookupParameter BestSolutionParameter { get { return (ILookupParameter) Parameters[BestSolutionParameterName]; } } public ILookupParameter ResultParameter { get { return (ILookupParameter) Parameters[ResultsParameterName]; } } public ILookupParameter RobocodePathParameter { get { return (ILookupParameter) Parameters[RobocodePathParamaterName]; } } public ILookupParameter CoevolutionParameter { get { return (ILookupParameter) Parameters[CoevolutionParameterName]; } } #endregion [StorableConstructor] protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { } protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } public BestSolutionAnalyzer() { Parameters.Add( new ScopeTreeLookupParameter( QualityParameterName, "The solution quality of the tank program.")); Parameters.Add( new ScopeTreeLookupParameter( SymbolicExpressionTreeParameterName, "The tank program to evaluate represented " + "as symbolic expression tree.")); Parameters.Add( new LookupParameter( BestSolutionParameterName, "The best tank program.")); //Parameters.Add( // new LookupParameter( // MovesParameterName, "The number of moves made.")); //Parameters.Add( // new LookupParameter( // ShotsParameterName, "The shots made.")); Parameters.Add( new LookupParameter( ResultsParameterName, "The result collection of the algorithm.")); Parameters.Add( new LookupParameter( RobocodePathParamaterName, "Path of the Robocode installation.")); Parameters.Add( new LookupParameter( CoevolutionParameterName, "Use Coevolution")); } public override IOperation Apply() { // get an array of all trees // and an array of all qualities var trees = SymbolicExpressionTreeParameter.ActualValue; var qualities = QualityParameter.ActualValue; // find the tree with the best quality double maxQuality = double.NegativeInfinity; ISymbolicExpressionTree bestTree = null; for (int i = 0; i < qualities.Length; i++) { if (qualities[i].Value > maxQuality) { maxQuality = qualities[i].Value; bestTree = trees[i]; } } var coevolution = CoevolutionParameter.ActualValue.Value; double actualQuality = 0; if (coevolution) actualQuality = Interpreter.EvaluateTankProgram(bestTree, null, RobocodePathParameter.ActualValue.Value); // create a solution instance //int shots = ShotsParameter.ActualValue.Value; //int moves = MovesParameter.ActualValue.Value; var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value);//, moves, shots); // store the new solution in the best solution parameter BestSolutionParameter.ActualValue = bestSolution; // also add the best solution as a result to the result collection // or alternatively update the existing result var resultCollection = ResultParameter.ActualValue; if (!resultCollection.ContainsKey(BestSolutionParameterName)) { resultCollection.Add( new Result(BestSolutionParameterName, "The best tank program", bestSolution)); //if(coevolution) // resultCollection.Add( // new Result("Actual Quality", // "The actual quality of the best program", new DoubleValue(actualQuality))); } else { resultCollection[BestSolutionParameterName].Value = bestSolution; } if (coevolution) { if (!resultCollection.ContainsKey("Actual Quality")) { resultCollection.Add( new Result("Actual Quality", "The actual quality of the best program", new DoubleValue(actualQuality))); } else { resultCollection["Actual Quality"].Value = new DoubleValue(actualQuality); } } // important return base.Apply() to make sure the // next operator is queued for execution return base.Apply(); } public override IDeepCloneable Clone(Cloner cloner) { return new BestSolutionAnalyzer(this, cloner); } // override this property to indicate that this analyzer // should be enabled by default in the algorithm public bool EnabledByDefault { get { return true; } } } }