#region License Information /* HeuristicLab * Copyright (C) 2002-2018 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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("RelocateEquipmentManipluator", "Relocates a random equipment from an overstuffed location to a random one with space or a random equipment if constraints are satisfied.")] [StorableClass] public class RelocateEquipmentManipluator : GQAPManipulator { [StorableConstructor] protected RelocateEquipmentManipluator(bool deserializing) : base(deserializing) { } protected RelocateEquipmentManipluator(RelocateEquipmentManipluator original, Cloner cloner) : base(original, cloner) { } public RelocateEquipmentManipluator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new RelocateEquipmentManipluator(this, cloner); } public static void Apply(IRandom random, IntegerVector assignment, int locations, double stopProb) { if (locations < 2) throw new InvalidOperationException("There are not enough locations for relocation operations."); var equipments = Enumerable.Range(0, assignment.Length).Shuffle(random); foreach (var equipment in equipments) { var oldLoc = assignment[equipment]; assignment[equipment] = (oldLoc + random.Next(1, locations)) % locations; if (random.NextDouble() < stopProb) break; } } protected override void Manipulate(IRandom random, IntegerVector vector, GQAPInstance problemInstance) { Apply(random, vector, problemInstance.Capacities.Length, 4.0 / problemInstance.Demands.Length); } } }