#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Collections.Generic; 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; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("Stochastic N-Move SingleMoveGenerator", "Randomly samples a single N-Move.")] [StorableClass] public class StochasticNMoveSingleMoveGenerator : GQAPNMoveGenerator, ICapacitiesAwareGQAPOperator, IStochasticOperator, ISingleMoveGenerator { public ILookupParameter CapacitiesParameter { get { return (ILookupParameter)Parameters["Capacities"]; } } 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.")); Parameters.Add(new LookupParameter("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription)); } 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) { int[] equipments = new int[n], locations = new int[n]; HashSet chosenEquipments = new HashSet(); for (int i = 0; i < n; i++) { do { equipments[i] = random.Next(assignment.Length); } while (chosenEquipments.Contains(equipments[i])); chosenEquipments.Add(equipments[i]); do { locations[i] = random.Next(capacities.Length); } while (locations[i] == assignment[equipments[i]]); } return new NMove(equipments, locations); } public override IEnumerable GenerateMoves(IntegerVector assignment, int n) { yield return GenerateUpToN(RandomParameter.ActualValue, assignment, n, CapacitiesParameter.ActualValue); } } }