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("MultiVRPSolutionCreator", "Randomly selects and applies one of its creator every time it is called.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class MultiVRPSolutionCreator : StochasticMultiBranch<IVRPCreator>, IVRPCreator, 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 MultiVRPSolutionCreator(bool deserializing) : base(deserializing) { }
|
---|
81 | public MultiVRPSolutionCreator()
|
---|
82 | : base() {
|
---|
83 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The new VRP tours."));
|
---|
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(IVRPCreator)).OrderBy(op => op.Name)) {
|
---|
97 | if (!typeof(MultiOperator<IVRPCreator>).IsAssignableFrom(type))
|
---|
98 | Operators.Add((IVRPCreator)Activator.CreateInstance(type), true);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
|
---|
103 | base.Operators_ItemsReplaced(sender, e);
|
---|
104 | ParameterizeManipulators();
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
|
---|
108 | base.Operators_ItemsAdded(sender, e);
|
---|
109 | ParameterizeManipulators();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void ParameterizeManipulators() {
|
---|
113 | foreach (IVRPCreator creator in Operators.OfType<IVRPCreator>()) {
|
---|
114 | creator.VRPToursParameter.ActualName = VRPToursParameter.Name;
|
---|
115 |
|
---|
116 | if (creator.CoordinatesParameter != null) creator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
117 | if (creator.DistanceMatrixParameter != null) creator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
118 | if (creator.UseDistanceMatrixParameter != null) creator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
119 | if (creator.VehiclesParameter != null) creator.VehiclesParameter.ActualName = VehiclesParameter.Name;
|
---|
120 | if (creator.CapacityParameter != null) creator.CapacityParameter.ActualName = CapacityParameter.Name;
|
---|
121 | if (creator.DemandParameter != null) creator.DemandParameter.ActualName = DemandParameter.Name;
|
---|
122 | if (creator.ReadyTimeParameter != null) creator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
|
---|
123 | if (creator.DueTimeParameter != null) creator.DueTimeParameter.ActualName = DueTimeParameter.Name;
|
---|
124 | if (creator.ServiceTimeParameter != null) creator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
|
---|
125 | }
|
---|
126 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
127 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override IOperation Apply() {
|
---|
132 | if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one VRP creator to choose from.");
|
---|
133 | return base.Apply();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|