#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.Optimization.Operators {
///
/// A shaking operator for VNS.
///
[Item("ShakingOperator", "A shaking operator for VNS that can be filled with arbitrary manipulation operators.")]
[StorableType("FE392285-B0A7-4559-A43B-920C47851500")]
public class ShakingOperator : CheckedMultiOperator, IMultiNeighborhoodShakingOperator where T : class, IManipulator {
public IValueLookupParameter CurrentNeighborhoodIndexParameter {
get { return (IValueLookupParameter)Parameters["CurrentNeighborhoodIndex"]; }
}
public ILookupParameter NeighborhoodCountParameter {
get { return (ILookupParameter)Parameters["NeighborhoodCount"]; }
}
[StorableConstructor]
protected ShakingOperator(bool deserializing) : base(deserializing) { }
protected ShakingOperator(ShakingOperator original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new ShakingOperator(this, cloner);
}
public ShakingOperator()
: base() {
Parameters.Add(new ValueLookupParameter("CurrentNeighborhoodIndex", "The index of the operator that should be applied (k)."));
Parameters.Add(new LookupParameter("NeighborhoodCount", "The number of operators that are available."));
}
public override IOperation InstrumentedApply() {
if (NeighborhoodCountParameter.ActualValue == null)
NeighborhoodCountParameter.ActualValue = new IntValue(Operators.CheckedItems.Count());
else NeighborhoodCountParameter.ActualValue.Value = Operators.CheckedItems.Count();
int index = CurrentNeighborhoodIndexParameter.ActualValue.Value;
var shaker = base.Operators.CheckedItems.SingleOrDefault(x => x.Index == index);
OperationCollection next = new OperationCollection(base.InstrumentedApply());
if (shaker.Value != null)
next.Insert(0, ExecutionContext.CreateChildOperation(shaker.Value));
return next;
}
}
}