#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 GenerateOneMove(IRandom random, IntegerVector assignment, DoubleArray capacities) { var locations = capacities.Length; if (locations <= 1) throw new ArgumentException("There must be at least two locations."); var dim = assignment.Length; var equip = random.Next(dim); var equipments = new List(1) { equip }; var reassignment = new int[dim]; reassignment[equip] = 1 + (assignment[equip] + random.Next(1, locations)) % locations; return new NMove(reassignment, equipments); } public static NMove GenerateTwoMove(IRandom random, IntegerVector assignment, DoubleArray capacities) { var locations = capacities.Length; if (locations <= 1) throw new ArgumentException("There must be at least two locations."); var dim = assignment.Length; var equipments = new List(2) { random.Next(dim) }; equipments.Add((equipments[0] + random.Next(1, dim)) % dim); var reassignment = new int[dim]; for (var i = 0; i < 2; i++) { var equip = equipments[i]; reassignment[equip] = 1 + (assignment[equip] + random.Next(1, locations)) % locations; } return new NMove(reassignment, equipments); } public static NMove GenerateExactlyN(IRandom random, IntegerVector assignment, int n, DoubleArray capacities) { if (n == 1) return GenerateOneMove(random, assignment, capacities); if (n == 2) return GenerateTwoMove(random, assignment, capacities); var locations = capacities.Length; if (locations <= 1) throw new ArgumentException("There must be at least two locations."); var dim = assignment.Length; var equipments = Enumerable.Range(0, dim).SampleRandomWithoutRepetition(random, n, dim).ToList(); var reassignment = new int[dim]; for (var i = 0; i < n; i++) { var equip = equipments[i]; reassignment[equip] = 1 + (assignment[equip] + random.Next(1, locations)) % locations; } return new NMove(reassignment, equipments); } public override IEnumerable GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) { yield return GenerateUpToN(RandomParameter.ActualValue, assignment, n, problemInstance.Capacities); } } }