#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;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
[Item("MultiVRPSolutionCreator", "Randomly selects and applies one of its creator every time it is called.")]
[StorableClass]
public class MultiVRPSolutionCreator : StochasticMultiBranch, IVRPCreator, IStochasticOperator {
public override bool CanChangeName {
get { return false; }
}
protected override bool CreateChildOperation {
get { return true; }
}
public ILookupParameter VRPToursParameter {
get { return (ILookupParameter)Parameters["VRPTours"]; }
}
public int Cities {
get { return CoordinatesParameter.ActualValue.Rows - 1; }
}
public ILookupParameter CoordinatesParameter {
get { return (ILookupParameter)Parameters["Coordinates"]; }
}
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"]; }
}
[StorableConstructor]
protected MultiVRPSolutionCreator(bool deserializing) : base(deserializing) { }
protected MultiVRPSolutionCreator(MultiVRPSolutionCreator original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new MultiVRPSolutionCreator(this, cloner);
}
public MultiVRPSolutionCreator()
: base() {
Parameters.Add(new LookupParameter("VRPTours", "The new VRP tours."));
Parameters.Add(new ValueLookupParameter("Cities", "The city count."));
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 (Type type in ApplicationManager.Manager.GetTypes(typeof(IVRPCreator)).OrderBy(op => op.Name)) {
if (!typeof(MultiOperator).IsAssignableFrom(type))
Operators.Add((IVRPCreator)Activator.CreateInstance(type), true);
}
}
protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
base.Operators_ItemsReplaced(sender, e);
ParameterizeManipulators();
}
protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
base.Operators_ItemsAdded(sender, e);
ParameterizeManipulators();
}
private void ParameterizeManipulators() {
foreach (IVRPCreator creator in Operators.OfType()) {
creator.VRPToursParameter.ActualName = VRPToursParameter.Name;
if (creator.CoordinatesParameter != null) creator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
if (creator.DistanceMatrixParameter != null) creator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
if (creator.UseDistanceMatrixParameter != null) creator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
if (creator.VehiclesParameter != null) creator.VehiclesParameter.ActualName = VehiclesParameter.Name;
if (creator.CapacityParameter != null) creator.CapacityParameter.ActualName = CapacityParameter.Name;
if (creator.DemandParameter != null) creator.DemandParameter.ActualName = DemandParameter.Name;
if (creator.ReadyTimeParameter != null) creator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
if (creator.DueTimeParameter != null) creator.DueTimeParameter.ActualName = DueTimeParameter.Name;
if (creator.ServiceTimeParameter != null) creator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
}
foreach (IStochasticOperator manipulator in Operators.OfType()) {
manipulator.RandomParameter.ActualName = RandomParameter.Name;
}
}
public override IOperation Apply() {
if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one VRP creator to choose from.");
return base.Apply();
}
}
}