#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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 System.Linq; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Problems.VehicleRouting.Encodings.General; namespace HeuristicLab.Problems.VehicleRouting { [Item("VRPShakingOperator", "A shaking operator for VNS that applies available mutation operators.")] [StorableClass] public class VehicleRoutingShakingOperator : ShakingOperator, IVRPMultiNeighborhoodShakingOperator, IStochasticOperator { #region Parameters public ILookupParameter VRPToursParameter { get { return (ILookupParameter)Parameters["VRPTours"]; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public ILookupParameter CoordinatesParameter { get { return (ILookupParameter)Parameters["Coordinates"]; } } public int Cities { get { if (CoordinatesParameter.ActualValue != null) return CoordinatesParameter.ActualValue.Rows; else return 0; } } public ILookupParameter DistanceMatrixParameter { get { return (ILookupParameter)Parameters["DistanceMatrix"]; } } public ILookupParameter UseDistanceMatrixParameter { get { return (ILookupParameter)Parameters["UseDistanceMatrix"]; } } public ILookupParameter VehiclesParameter { get { return (ILookupParameter)Parameters["Vehicles"]; } } public ILookupParameter CapacityParameter { get { return (ILookupParameter)Parameters["Capacity"]; } } public ILookupParameter DemandParameter { get { return (ILookupParameter)Parameters["Demand"]; } } public ILookupParameter ReadyTimeParameter { get { return (ILookupParameter)Parameters["ReadyTime"]; } } public ILookupParameter DueTimeParameter { get { return (ILookupParameter)Parameters["DueTime"]; } } public ILookupParameter ServiceTimeParameter { get { return (ILookupParameter)Parameters["ServiceTime"]; } } #endregion [StorableConstructor] protected VehicleRoutingShakingOperator(bool deserializing) : base(deserializing) { } protected VehicleRoutingShakingOperator(VehicleRoutingShakingOperator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new VehicleRoutingShakingOperator(this, cloner); } public VehicleRoutingShakingOperator() : base() { Parameters.Add(new LookupParameter("VRPTours", "The vrp tour encoding to shake.")); Parameters.Add(new LookupParameter("Random", "The random number generator that will be used for stochastic shaking operators.")); Parameters.Add(new LookupParameter("Coordinates", "The coordinates of the cities.")); Parameters.Add(new LookupParameter("DistanceMatrix", "The matrix which contains the distances between the cities.")); Parameters.Add(new LookupParameter("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.")); Parameters.Add(new LookupParameter("Vehicles", "The number of vehicles.")); Parameters.Add(new LookupParameter("Capacity", "The capacity of each vehicle.")); Parameters.Add(new LookupParameter("Demand", "The demand of each customer.")); Parameters.Add(new LookupParameter("ReadyTime", "The ready time of each customer.")); Parameters.Add(new LookupParameter("DueTime", "The due time of each customer.")); Parameters.Add(new LookupParameter("ServiceTime", "The service time of each customer.")); foreach (IVRPManipulator shaker in ApplicationManager.Manager.GetInstances().OrderBy(x => x.Name)) if (!(shaker is MultiVRPSolutionManipulator)) Operators.Add(shaker); } #region Wiring of some parameters protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { base.Operators_ItemsAdded(sender, e); ParameterizeOperators(e.Items); } protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) { base.Operators_ItemsReplaced(sender, e); ParameterizeOperators(e.Items); } private void ParameterizeOperators(IEnumerable> items) { if (items.Any()) { foreach (IStochasticOperator op in items.Select(x => x.Value).OfType()) op.RandomParameter.ActualName = RandomParameter.Name; foreach (IVRPManipulator op in items.Select(x => x.Value).OfType()) { op.VRPToursParameter.ActualName = VRPToursParameter.Name; if (op.CoordinatesParameter != null) op.CoordinatesParameter.ActualName = CoordinatesParameter.Name; if (op.DistanceMatrixParameter != null) op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; if (op.UseDistanceMatrixParameter != null) op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; if (op.VehiclesParameter != null) op.VehiclesParameter.ActualName = VehiclesParameter.Name; if (op.CapacityParameter != null) op.CapacityParameter.ActualName = CapacityParameter.Name; if (op.DemandParameter != null) op.DemandParameter.ActualName = DemandParameter.Name; if (op.ReadyTimeParameter != null) op.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name; if (op.DueTimeParameter != null) op.DueTimeParameter.ActualName = DueTimeParameter.Name; if (op.ServiceTimeParameter != null) op.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name; } } } #endregion } }