#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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 alglib; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using System.Collections.Generic; using HeuristicLab.Problems.DataAnalysis.Evaluators; using HeuristicLab.Analysis; namespace HeuristicLab.Problems.DataAnalysis.Operators { [Item("Covariant Parsimony Pressure Adder", "Covariant Parsimony Pressure Adder.")] [StorableClass] public class CovariantParsimonyPressureAdder : SingleSuccessorOperator { public IScopeTreeLookupParameter ParentsParameter { get { return (IScopeTreeLookupParameter)Parameters["Parents"]; } } public IScopeTreeLookupParameter ParentQualityParameter { get { return (IScopeTreeLookupParameter)Parameters["ParentQuality"]; } } public IScopeTreeLookupParameter ParentAdjustedQualityParameter { get { return (IScopeTreeLookupParameter)Parameters["ParentAdjustedQuality"]; } } public ILookupParameter OffspringParameter { get { return (ILookupParameter)Parameters["Offspring"]; } } public ILookupParameter OffspringQualityParameter { get { return (ILookupParameter)Parameters["OffspringQuality"]; } } public ILookupParameter OffspringAdjustedQualityParameter { get { return (ILookupParameter)Parameters["OffspringAdjustedQuality"]; } } public IValueLookupParameter KParameter { get { return (IValueLookupParameter)Parameters["K"]; } } public IValueLookupParameter ApplyParsimonyPressureParameter { get { return (IValueLookupParameter)Parameters["ApplyParsimonyPressure"]; } } public ILookupParameter CParameter { get { return (ILookupParameter)Parameters["C"]; } } [StorableConstructor] public CovariantParsimonyPressureAdder(bool deserializing) : base(deserializing) { } public CovariantParsimonyPressureAdder() : base() { Parameters.Add(new ScopeTreeLookupParameter("Parents")); Parameters.Add(new ScopeTreeLookupParameter("ParentQuality")); Parameters.Add(new ScopeTreeLookupParameter("ParentAdjustedQuality")); Parameters.Add(new LookupParameter("Offspring")); Parameters.Add(new LookupParameter("OffspringQuality")); Parameters.Add(new LookupParameter("OffspringAdjustedQuality")); Parameters.Add(new ValueLookupParameter("K", new DoubleValue(1))); Parameters.Add(new LookupParameter("C")); Parameters.Add(new ValueLookupParameter("ApplyParsimonyPressure")); } [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)] private void AfterDeserialization() { } public override IOperation Apply() { if (ApplyParsimonyPressureParameter.ActualValue.Value) { double k = KParameter.ActualValue.Value; double c = CParameter.ActualValue.Value; // handle offspring var offspringLength = OffspringParameter.ActualValue.Size; OffspringAdjustedQualityParameter.ActualValue = new DoubleValue(OffspringQualityParameter.ActualValue.Value - c * Math.Pow(offspringLength, k)); // handle parents ItemArray parents = ParentsParameter.ActualValue; ItemArray parentQualities = ParentQualityParameter.ActualValue; ItemArray adjQualities = new ItemArray(parentQualities.Length); for (int i = 0; i < adjQualities.Length; i++) { adjQualities[i] = new DoubleValue(parentQualities[i].Value - c * Math.Pow(parents[i].Size, k)); } ParentAdjustedQualityParameter.ActualValue = adjQualities; } else { // adjusted fitness is equal to fitness ParentAdjustedQualityParameter.ActualValue = (ItemArray)ParentQualityParameter.ActualValue.Clone(); OffspringAdjustedQualityParameter.ActualValue = (DoubleValue)OffspringQualityParameter.ActualValue.Clone(); } return base.Apply(); } } }