Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Manipulators/MultiVRPSolutionManipulator.cs @ 4347

Last change on this file since 4347 was 4347, checked in by svonolfe, 14 years ago

Sorted Operators alphabetically, disabled LS operators by default (#1039)

File size: 7.3 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.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
34  [Item("MultiVRPSolutionManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
35  [StorableClass]
36  public class MultiVRPSolutionManipulator : StochasticMultiBranch<IVRPManipulator>, IVRPManipulator, IStochasticOperator {
37    public override bool CanChangeName {
38      get { return false; }
39    }
40    protected override bool CreateChildOperation {
41      get { return true; }
42    }
43
44    public ILookupParameter<IVRPEncoding> VRPToursParameter {
45      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
46    }
47
48    public int Cities {
49      get { return CoordinatesParameter.ActualValue.Rows - 1; }
50    }
51    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
52      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
53    }
54    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
55      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
56    }
57    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
58      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
59    }
60    public ILookupParameter<IntValue> VehiclesParameter {
61      get { return (ILookupParameter<IntValue>)Parameters["Vehicles"]; }
62    }
63    public ILookupParameter<DoubleValue> CapacityParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters["Capacity"]; }
65    }
66    public ILookupParameter<DoubleArray> DemandParameter {
67      get { return (ILookupParameter<DoubleArray>)Parameters["Demand"]; }
68    }
69    public ILookupParameter<DoubleArray> ReadyTimeParameter {
70      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
71    }
72    public ILookupParameter<DoubleArray> DueTimeParameter {
73      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
74    }
75    public ILookupParameter<DoubleArray> ServiceTimeParameter {
76      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
77    }
78
79    [StorableConstructor]
80    private MultiVRPSolutionManipulator(bool deserializing) : base(deserializing) { }
81    public MultiVRPSolutionManipulator()
82      : base() {
83      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours to be manipulated."));
84
85      Parameters.Add(new ValueLookupParameter<IntValue>("Cities", "The city count."));
86      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates of the cities."));
87      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
88      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
89      Parameters.Add(new LookupParameter<IntValue>("Vehicles", "The number of vehicles."));
90      Parameters.Add(new LookupParameter<DoubleValue>("Capacity", "The capacity of each vehicle."));
91      Parameters.Add(new LookupParameter<DoubleArray>("Demand", "The demand of each customer."));
92      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
93      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
94      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
95
96      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IVRPManipulator)).OrderBy(op => op.Name)) {
97        if (!typeof(MultiOperator<IVRPManipulator>).IsAssignableFrom(type)) {
98          IVRPManipulator op = (IVRPManipulator)Activator.CreateInstance(type);
99          bool operatorChecked = true;
100          if(op is HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinLocalSearchManipulator ||
101            op is HeuristicLab.Problems.VehicleRouting.Encodings.Prins.PrinsLSManipulator)
102            operatorChecked = false;
103          Operators.Add(op, operatorChecked);
104        }
105      }
106    }
107
108    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
109      base.Operators_ItemsReplaced(sender, e);
110      ParameterizeManipulators();
111    }
112
113    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
114      base.Operators_ItemsAdded(sender, e);
115      ParameterizeManipulators();
116    }
117
118    private void ParameterizeManipulators() {
119      foreach (IVRPManipulator manipulator in Operators.OfType<IVRPManipulator>()) {
120        manipulator.VRPToursParameter.ActualName = VRPToursParameter.Name;
121
122        if (manipulator.CoordinatesParameter != null) manipulator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
123        if (manipulator.DistanceMatrixParameter != null) manipulator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
124        if (manipulator.UseDistanceMatrixParameter != null) manipulator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
125        if (manipulator.VehiclesParameter != null) manipulator.VehiclesParameter.ActualName = VehiclesParameter.Name;
126        if (manipulator.CapacityParameter != null) manipulator.CapacityParameter.ActualName = CapacityParameter.Name;
127        if (manipulator.DemandParameter != null) manipulator.DemandParameter.ActualName = DemandParameter.Name;
128        if (manipulator.ReadyTimeParameter != null) manipulator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
129        if (manipulator.DueTimeParameter != null) manipulator.DueTimeParameter.ActualName = DueTimeParameter.Name;
130        if (manipulator.ServiceTimeParameter != null) manipulator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
131      }
132      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
133        manipulator.RandomParameter.ActualName = RandomParameter.Name;
134      }
135    }
136
137    public override IOperation Apply() {
138      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation manipulator to choose from.");
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.