Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/MultiVRPSolutionCreator.cs @ 6448

Last change on this file since 6448 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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("MultiVRPSolutionCreator", "Randomly selects and applies one of its creator every time it is called.")]
36  [StorableClass]
37  public class MultiVRPSolutionCreator : StochasticMultiBranch<IVRPCreator>, IVRPCreator, 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 MultiVRPSolutionCreator(bool deserializing) : base(deserializing) { }
82    protected MultiVRPSolutionCreator(MultiVRPSolutionCreator original, Cloner cloner) : base(original, cloner) { }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new MultiVRPSolutionCreator(this, cloner);
85    }
86
87    public MultiVRPSolutionCreator()
88      : base() {
89      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The new VRP tours."));
90
91      Parameters.Add(new ValueLookupParameter<IntValue>("Cities", "The city count."));
92      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates of the cities."));
93      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
94      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
95      Parameters.Add(new LookupParameter<IntValue>("Vehicles", "The number of vehicles."));
96      Parameters.Add(new LookupParameter<DoubleValue>("Capacity", "The capacity of each vehicle."));
97      Parameters.Add(new LookupParameter<DoubleArray>("Demand", "The demand of each customer."));
98      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
99      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
100      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
101
102      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IVRPCreator)).OrderBy(op => op.Name)) {
103        if (!typeof(MultiOperator<IVRPCreator>).IsAssignableFrom(type))
104          Operators.Add((IVRPCreator)Activator.CreateInstance(type), true);
105      }
106    }
107
108    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
109      base.Operators_ItemsReplaced(sender, e);
110      ParameterizeManipulators();
111    }
112
113    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
114      base.Operators_ItemsAdded(sender, e);
115      ParameterizeManipulators();
116    }
117
118    private void ParameterizeManipulators() {
119      foreach (IVRPCreator creator in Operators.OfType<IVRPCreator>()) {
120        creator.VRPToursParameter.ActualName = VRPToursParameter.Name;
121
122        if (creator.CoordinatesParameter != null) creator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
123        if (creator.DistanceMatrixParameter != null) creator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
124        if (creator.UseDistanceMatrixParameter != null) creator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
125        if (creator.VehiclesParameter != null) creator.VehiclesParameter.ActualName = VehiclesParameter.Name;
126        if (creator.CapacityParameter != null) creator.CapacityParameter.ActualName = CapacityParameter.Name;
127        if (creator.DemandParameter != null) creator.DemandParameter.ActualName = DemandParameter.Name;
128        if (creator.ReadyTimeParameter != null) creator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
129        if (creator.DueTimeParameter != null) creator.DueTimeParameter.ActualName = DueTimeParameter.Name;
130        if (creator.ServiceTimeParameter != null) creator.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 VRP creator to choose from.");
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.