#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.ALPS.SteadyState {
[Item("SteadyStateMatingPoolCreator", "")]
[StorableClass]
public class SteadyStateMatingPoolCreator : SingleSuccessorOperator {
private ILookupParameter WorkingScopeParameter {
get { return (ILookupParameter)Parameters["WorkingScope"]; }
}
private ILookupParameter LayersScopeParameter {
get { return (ILookupParameter)Parameters["LayersScope"]; }
}
private ILookupParameter LayerParameter {
get { return (ILookupParameter)Parameters["Layer"]; }
}
private ILookupParameter AgeLimitsParameter {
get { return (ILookupParameter)Parameters["AgeLimits"]; }
}
private ILookupParameter NumberOfLayersParameter {
get { return (ILookupParameter)Parameters["NumberOfLayers"]; }
}
private ILookupParameter EvaluatedSolutionsParameter {
get { return (ILookupParameter)Parameters["EvaluatedSolutions"]; }
}
private ILookupParameter PopulationSizeParameter {
get { return (ILookupParameter)Parameters["PopulationSize"]; }
}
[StorableConstructor]
private SteadyStateMatingPoolCreator(bool deserializing)
: base(deserializing) { }
private SteadyStateMatingPoolCreator(SteadyStateMatingPoolCreator original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SteadyStateMatingPoolCreator(this, cloner);
}
public SteadyStateMatingPoolCreator()
: base() {
Parameters.Add(new LookupParameter("WorkingScope"));
Parameters.Add(new LookupParameter("LayersScope"));
Parameters.Add(new LookupParameter("Layer"));
Parameters.Add(new LookupParameter("AgeLimits"));
Parameters.Add(new LookupParameter("NumberOfLayers"));
Parameters.Add(new LookupParameter("EvaluatedSolutions"));
Parameters.Add(new LookupParameter("PopulationSize"));
}
public override IOperation Apply() {
int layerNumber = LayerParameter.ActualValue.Value;
var workingScope = WorkingScopeParameter.ActualValue;
var layers = LayersScopeParameter.ActualValue;
var ageLimits = AgeLimitsParameter.ActualValue;
int numberOfLayers = NumberOfLayersParameter.ActualValue.Value;
int evaluations = EvaluatedSolutionsParameter.ActualValue.Value;
int popSize = PopulationSizeParameter.ActualValue.Value;
var currentLayer = layers.SubScopes[layerNumber].SubScopes;
var prevLayer = layerNumber > 0 ? layers.SubScopes[layerNumber - 1].SubScopes : Enumerable.Empty();
var ageLimit = layerNumber < numberOfLayers - 1 ? ageLimits[layerNumber] : int.MaxValue;
var matingPool =
from individual in currentLayer.Union(prevLayer)
let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value
let age = (evaluations - evalsCreated) / popSize
where age < ageLimit
select (IScope)individual/*.Clone()*/;
foreach (var individual in matingPool) {
workingScope.SubScopes.Add(individual);
}
return base.Apply();
}
}
}