#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.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Random;
namespace HeuristicLab.Algorithms.ALPS.SteadyState {
[Item("AlpsSsMover", "")]
[StorableClass]
public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator {
private ILookupParameter LayerParameter {
get { return (ILookupParameter)Parameters["Layer"]; }
}
private ILookupParameter TargetIndexParameter {
get { return (ILookupParameter)Parameters["TargetIndex"]; }
}
private ILookupParameter PopulationSizeParameter {
get { return (ILookupParameter)Parameters["PopulationSize"]; }
}
private ILookupParameter NumberOfLayersParameter {
get { return (ILookupParameter)Parameters["NumberOfLayers"]; }
}
private ILookupParameter WorkingScopeParameter {
get { return (ILookupParameter)Parameters["WorkingScope"]; }
}
private ILookupParameter LayersParameter {
get { return (ILookupParameter)Parameters["Layers"]; }
}
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
[StorableConstructor]
private AlpsSsMover(bool deserializing)
: base(deserializing) { }
private AlpsSsMover(AlpsSsMover original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new AlpsSsMover(this, cloner);
}
public AlpsSsMover()
: base() {
Parameters.Add(new LookupParameter("Layer"));
Parameters.Add(new LookupParameter("TargetIndex"));
Parameters.Add(new LookupParameter("PopulationSize"));
Parameters.Add(new LookupParameter("NumberOfLayers"));
Parameters.Add(new LookupParameter("WorkingScope"));
Parameters.Add(new LookupParameter("Layers"));
Parameters.Add(new LookupParameter("Random", "The random number generator to use."));
}
private int n;
private int m;
private IScope layers;
private IRandom rand;
public override IOperation Apply() {
int i = LayerParameter.ActualValue.Value;
int j = TargetIndexParameter.ActualValue.Value;
n = NumberOfLayersParameter.ActualValue.Value;
m = PopulationSizeParameter.ActualValue.Value;
rand = RandomParameter.ActualValue;
layers = LayersParameter.ActualValue;
var newIndividual = (IScope)WorkingScopeParameter.ActualValue.Clone();
newIndividual.Name = j.ToString();
TryMoveUp(i, j);
var currentLayer = layers.SubScopes[i];
currentLayer.SubScopes[j] = newIndividual;
return base.Apply();
}
private void TryMoveUp(int i, int j) {
var currentLayer = layers.SubScopes[i];
var currentIndividual = currentLayer.SubScopes[j];
if (i < n - 1) {
var nextLayer = layers.SubScopes[i + 1];
int? k = FindReplaceable(nextLayer, currentIndividual);
if (k.HasValue) {
TryMoveUp(i + 1, k.Value);
nextLayer.SubScopes[k.Value] = currentIndividual;
}
}
}
private int? FindReplaceable(IScope layer, IScope individual) {
return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand);
}
}
}