#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.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; using HEAL.Attic; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("Exhaustive 1-Move MoveGenerator", "Exhaustively generates all possible 1-moves.")] [StorableType("A200C3BE-D761-4F82-9CA9-44A7BB2AB0DD")] public class ExhaustiveOneMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IExhaustiveMoveGenerator { public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } [StorableConstructor] protected ExhaustiveOneMoveGenerator(StorableConstructorFlag _) : base(_) { } protected ExhaustiveOneMoveGenerator(ExhaustiveOneMoveGenerator original, Cloner cloner) : base(original, cloner) { } public ExhaustiveOneMoveGenerator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator that should be used.")); NParameter.Value.Value = 1; NParameter.Hidden = true; } public override IDeepCloneable Clone(Cloner cloner) { return new ExhaustiveOneMoveGenerator(this, cloner); } public static IEnumerable Generate(IntegerVector assignment, GQAPInstance problemInstance) { var equipments = problemInstance.Demands.Length; var locations = problemInstance.Capacities.Length; var tmp = new int[equipments]; for (var e = 0; e < equipments; e++) { var indices = new List { e }; for (var l = 0; l < locations; l++) { if (assignment[e] == l) continue; var reassign = (int[])tmp.Clone(); reassign[e] = l + 1; yield return new NMove(reassign, indices); } } } public static IEnumerable GenerateAllNxM(GQAPInstance problemInstance) { var equipments = problemInstance.Demands.Length; var locations = problemInstance.Capacities.Length; var tmp = new int[equipments]; for (var e = 0; e < equipments; e++) { var indices = new List { e }; for (var l = 0; l < locations; l++) { var reassign = (int[])tmp.Clone(); reassign[e] = l + 1; yield return new NMove(reassign, indices); } } } public override IEnumerable GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) { if (n != 1) throw new ArgumentException("N must be equal to 1 for the exhaustive 1-move generator."); var random = RandomParameter.ActualValue; return Generate(assignment, problemInstance).Shuffle(random); } } }