#region License Information /* HeuristicLab * Copyright (C) 2002-2017 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.Encodings.IntegerVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("Stochastic N-Move SingleMoveGenerator", "Randomly samples a single N-Move.")] [StorableClass] public class StochasticNMoveSingleMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, ISingleMoveGenerator { public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } [StorableConstructor] protected StochasticNMoveSingleMoveGenerator(bool deserializing) : base(deserializing) { } protected StochasticNMoveSingleMoveGenerator(StochasticNMoveSingleMoveGenerator original, Cloner cloner) : base(original, cloner) { } public StochasticNMoveSingleMoveGenerator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator that should be used.")); } public override IDeepCloneable Clone(Cloner cloner) { return new StochasticNMoveSingleMoveGenerator(this, cloner); } public static NMove GenerateUpToN(IRandom random, IntegerVector assignment, int n, DoubleArray capacities) { return GenerateExactlyN(random, assignment, random.Next(n) + 1, capacities); } public static NMove GenerateExactlyN(IRandom random, IntegerVector assignment, int n, DoubleArray capacities) { if (capacities.Length <= 1) throw new ArgumentException("There must be at least two locations."); var dim = assignment.Length; var reassignment = new int[dim]; var equipments = Enumerable.Range(0, dim).SampleRandomWithoutRepetition(random, n, dim).ToList(); for (var i = 0; i < n; i++) { var equip = equipments[i]; do { reassignment[equip] = random.Next(capacities.Length) + 1; } while (reassignment[equip] == assignment[equip] + 1); } return new NMove(reassignment, equipments); } public override IEnumerable GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) { yield return GenerateUpToN(RandomParameter.ActualValue, assignment, n, problemInstance.Capacities); } } }