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