#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ConditionActionEncoding { [Item("XCSDeletionOperator", "Description missing")] [StorableClass] public class XCSDeletionOperator : SingleSuccessorOperator, IStochasticOperator { #region Parameter Properties public ILookupParameter NumberToDeleteParameter { get { return (ILookupParameter)Parameters["NumberToDelete"]; } } public ILookupParameter> NumerositiesParameter { get { return (ILookupParameter>)Parameters["Numerosities"]; } } public ILookupParameter> AverageActionSetSizesParameter { get { return (ILookupParameter>)Parameters["AverageActionSetSizes"]; } } public ILookupParameter> FitnessesParameter { get { return (ILookupParameter>)Parameters["Fitnesses"]; } } public ILookupParameter> ExperiencesParameter { get { return (ILookupParameter>)Parameters["Experiences"]; } } public ILookupParameter ThetaDeletionParameter { get { return (ILookupParameter)Parameters["ThetaDeletion"]; } } public ILookupParameter DeltaParameter { get { return (ILookupParameter)Parameters["Delta"]; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public ILookupParameter CurrentPopulationSizeParameter { get { return (ILookupParameter)Parameters["CurrentPopulationSize"]; } } protected ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } public string HasToBeDeletedVariableName { get { return "HasToBeDeleted"; } } #endregion #region Parameter Properties private double Delta { get { return DeltaParameter.ActualValue.Value; } } private int ThetaDeletion { get { return ThetaDeletionParameter.ActualValue.Value; } } #endregion [StorableConstructor] protected XCSDeletionOperator(bool deserializing) : base(deserializing) { } protected XCSDeletionOperator(XCSDeletionOperator original, Cloner cloner) : base(original, cloner) { } public XCSDeletionOperator() : base() { Parameters.Add(new LookupParameter("NumberToDelete")); Parameters.Add(new ScopeTreeLookupParameter("Numerosities")); Parameters.Add(new ScopeTreeLookupParameter("AverageActionSetSizes")); Parameters.Add(new ScopeTreeLookupParameter("Fitnesses")); Parameters.Add(new ScopeTreeLookupParameter("Experiences")); Parameters.Add(new LookupParameter("ThetaDeletion")); Parameters.Add(new LookupParameter("Delta")); Parameters.Add(new LookupParameter("Random")); Parameters.Add(new ScopeParameter("CurrentScope")); Parameters.Add(new LookupParameter("CurrentPopulationSize")); } public override IDeepCloneable Clone(Cloner cloner) { return new XCSDeletionOperator(this, cloner); } public override IOperation Apply() { var fitnesses = FitnessesParameter.ActualValue; var numerosities = NumerositiesParameter.ActualValue; var experiences = ExperiencesParameter.ActualValue; var averageActionSetSizes = AverageActionSetSizesParameter.ActualValue; var subscopes = CurrentScope.SubScopes; double fitnessSum = fitnesses.Select(x => x.Value).Sum(); double numerositySum = numerosities.Select(x => x.Value).Sum(); if (fitnesses.Length != numerosities.Length && fitnesses.Length != experiences.Length && fitnesses.Length != subscopes.Count) { throw new ArgumentException("Different number of fitness, numerosity and experience values."); } for (int cl = 0; cl < fitnesses.Length; cl++) { if (subscopes[cl].Variables.ContainsKey(HasToBeDeletedVariableName)) { ((BoolValue)subscopes[cl].Variables[HasToBeDeletedVariableName].Value).Value = false; } else { subscopes[cl].Variables.Add(new Variable(HasToBeDeletedVariableName, new BoolValue(false))); } } for (int i = 0; i < NumberToDeleteParameter.ActualValue.Value; i++) { double voteSum = 0; double averageFitnessInPopulation = fitnessSum / numerositySum; for (int cl = 0; cl < fitnesses.Length; cl++) { if (numerosities[cl].Value > 0) { voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value); } } double choicePoint = RandomParameter.ActualValue.NextDouble() * voteSum; voteSum = 0; for (int cl = 0; cl < fitnesses.Length; cl++) { if (numerosities[cl].Value > 0) { voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value); if (voteSum > choicePoint) { if (numerosities[cl].Value <= 1) { ((BoolValue)subscopes[cl].Variables[HasToBeDeletedVariableName].Value).Value = true; fitnessSum -= fitnesses[cl].Value; } numerosities[cl].Value--; numerositySum--; break; } } } } CurrentPopulationSizeParameter.ActualValue.Value -= NumberToDeleteParameter.ActualValue.Value; return base.Apply(); } private double DeletionVote(double averageFitnessInPopulation, double ass, int num, double f, int exp) { double vote = ass * num; if (exp > ThetaDeletion && (f / num) < Delta * averageFitnessInPopulation) { vote *= averageFitnessInPopulation / (f / num); } return vote; } } }