Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Manipulators/MultiVRPSolutionManipulator.cs @ 5370

Last change on this file since 5370 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
35  [Item("MultiVRPSolutionManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
36  [StorableClass]
37  public class MultiVRPSolutionManipulator : StochasticMultiBranch<IVRPManipulator>, IVRPManipulator, IStochasticOperator {
38    public override bool CanChangeName {
39      get { return false; }
40    }
41    protected override bool CreateChildOperation {
42      get { return true; }
43    }
44
45    public ILookupParameter<IVRPEncoding> VRPToursParameter {
46      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
47    }
48
49    public int Cities {
50      get { return CoordinatesParameter.ActualValue.Rows - 1; }
51    }
52    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
53      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
54    }
55    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
57    }
58    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
59      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
60    }
61    public ILookupParameter<IntValue> VehiclesParameter {
62      get { return (ILookupParameter<IntValue>)Parameters["Vehicles"]; }
63    }
64    public ILookupParameter<DoubleValue> CapacityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["Capacity"]; }
66    }
67    public ILookupParameter<DoubleArray> DemandParameter {
68      get { return (ILookupParameter<DoubleArray>)Parameters["Demand"]; }
69    }
70    public ILookupParameter<DoubleArray> ReadyTimeParameter {
71      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
72    }
73    public ILookupParameter<DoubleArray> DueTimeParameter {
74      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
75    }
76    public ILookupParameter<DoubleArray> ServiceTimeParameter {
77      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
78    }
79
80    [StorableConstructor]
81    protected MultiVRPSolutionManipulator(bool deserializing) : base(deserializing) { }
82    protected MultiVRPSolutionManipulator(MultiVRPSolutionManipulator original, Cloner cloner) : base(original, cloner) { }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new MultiVRPSolutionManipulator(this, cloner);
85    }
86    public MultiVRPSolutionManipulator()
87      : base() {
88      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours to be manipulated."));
89
90      Parameters.Add(new ValueLookupParameter<IntValue>("Cities", "The city count."));
91      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates of the cities."));
92      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
93      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
94      Parameters.Add(new LookupParameter<IntValue>("Vehicles", "The number of vehicles."));
95      Parameters.Add(new LookupParameter<DoubleValue>("Capacity", "The capacity of each vehicle."));
96      Parameters.Add(new LookupParameter<DoubleArray>("Demand", "The demand of each customer."));
97      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
98      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
99      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
100
101      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IVRPManipulator)).OrderBy(op => op.Name)) {
102        if (!typeof(MultiOperator<IVRPManipulator>).IsAssignableFrom(type)) {
103          IVRPManipulator op = (IVRPManipulator)Activator.CreateInstance(type);
104          bool operatorChecked = true;
105          if (op is HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinLocalSearchManipulator ||
106            op is HeuristicLab.Problems.VehicleRouting.Encodings.Prins.PrinsLSManipulator)
107            operatorChecked = false;
108          Operators.Add(op, operatorChecked);
109        }
110      }
111    }
112
113    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
114      base.Operators_ItemsReplaced(sender, e);
115      ParameterizeManipulators();
116    }
117
118    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
119      base.Operators_ItemsAdded(sender, e);
120      ParameterizeManipulators();
121    }
122
123    private void ParameterizeManipulators() {
124      foreach (IVRPManipulator manipulator in Operators.OfType<IVRPManipulator>()) {
125        manipulator.VRPToursParameter.ActualName = VRPToursParameter.Name;
126
127        if (manipulator.CoordinatesParameter != null) manipulator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
128        if (manipulator.DistanceMatrixParameter != null) manipulator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
129        if (manipulator.UseDistanceMatrixParameter != null) manipulator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
130        if (manipulator.VehiclesParameter != null) manipulator.VehiclesParameter.ActualName = VehiclesParameter.Name;
131        if (manipulator.CapacityParameter != null) manipulator.CapacityParameter.ActualName = CapacityParameter.Name;
132        if (manipulator.DemandParameter != null) manipulator.DemandParameter.ActualName = DemandParameter.Name;
133        if (manipulator.ReadyTimeParameter != null) manipulator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
134        if (manipulator.DueTimeParameter != null) manipulator.DueTimeParameter.ActualName = DueTimeParameter.Name;
135        if (manipulator.ServiceTimeParameter != null) manipulator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
136      }
137      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
138        manipulator.RandomParameter.ActualName = RandomParameter.Name;
139      }
140    }
141
142    public override IOperation Apply() {
143      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation manipulator to choose from.");
144      return base.Apply();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.