#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Optimization.Operators { /// /// FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, "A Fast and Elitist Multiobjective /// Genetic Algorithm: NSGA-II", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002 /// [Item("FastNonDominatedSort", @"FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002")] [StorableClass] public class FastNonDominatedSort : SingleSuccessorOperator, IMultiObjectiveOperator { #region Parameter properties public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } public IValueLookupParameter DominateOnEqualQualitiesParameter { get { return (ValueLookupParameter)Parameters["DominateOnEqualQualities"]; } } public IScopeTreeLookupParameter QualitiesParameter { get { return (IScopeTreeLookupParameter)Parameters["Qualities"]; } } public IScopeTreeLookupParameter RankParameter { get { return (IScopeTreeLookupParameter)Parameters["Rank"]; } } #endregion [StorableConstructor] protected FastNonDominatedSort(bool deserializing) : base(deserializing) { } protected FastNonDominatedSort(FastNonDominatedSort original, Cloner cloner) : base(original, cloner) { } public FastNonDominatedSort() { Parameters.Add(new ValueLookupParameter("Maximization", "Whether each objective is maximization or minimization.")); Parameters.Add(new ValueLookupParameter("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated.")); Parameters.Add(new ScopeTreeLookupParameter("Qualities", "The qualities of a solution.", 1)); Parameters.Add(new ScopeTreeLookupParameter("Rank", "The rank of a solution.", 1)); } public override IOperation Apply() { bool dominateOnEqualQualities = DominateOnEqualQualitiesParameter.ActualValue.Value; bool[] maximization = MaximizationParameter.ActualValue.ToArray(); double[][] qualities = QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(); if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found."); IScope scope = ExecutionContext.Scope; int populationSize = scope.SubScopes.Count; int[] rank; var fronts = DominationCalculator.CalculateAllParetoFronts(scope.SubScopes.ToArray(), qualities, maximization, out rank, dominateOnEqualQualities); RankParameter.ActualValue = new ItemArray(rank.Select(x => new IntValue(x))); scope.SubScopes.Clear(); for (var i = 0; i < fronts.Count; i++) { Scope frontScope = new Scope("Front " + i); foreach (var p in fronts[i]) frontScope.SubScopes.Add(p.Item1); if (frontScope.SubScopes.Count > 0) scope.SubScopes.Add(frontScope); } return base.Apply(); } public override IDeepCloneable Clone(Cloner cloner) { return new FastNonDominatedSort(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { // BackwardsCompatibility3.3 #region Backwards compatible code, remove with 3.4 if (!Parameters.ContainsKey("DominateOnEqualQualities")) Parameters.Add(new ValueLookupParameter("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated.")); #endregion } } }