Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Crossovers/MultiVRPSolutionCrossover.cs @ 4696

Last change on this file since 4696 was 4696, checked in by abeham, 13 years ago

#922

  • Fixed most of the errors that surfaced in the test cases
  • Removed storable attribute from SymbolicExpressionTreeStringFormatter
  • Modified StorableConstructorTest that the StorableConstructor may also be public (such as in BoolValue)
File size: 7.7 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("MultiVRPSolutionCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
36  [StorableClass]
37  public class MultiVRPSolutionCrossover : StochasticMultiBranch<IVRPCrossover>, IVRPCrossover, 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<ItemArray<IVRPEncoding>> ParentsParameter {
46      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["Parents"]; }
47    }
48
49    public ILookupParameter<IVRPEncoding> ChildParameter {
50      get { return (ILookupParameter<IVRPEncoding>)Parameters["Child"]; }
51    }
52
53    public int Cities {
54      get { return CoordinatesParameter.ActualValue.Rows - 1; }
55    }
56    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
57      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
58    }
59    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
60      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
61    }
62    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
63      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
64    }
65    public ILookupParameter<IntValue> VehiclesParameter {
66      get { return (ILookupParameter<IntValue>)Parameters["Vehicles"]; }
67    }
68    public ILookupParameter<DoubleValue> CapacityParameter {
69      get { return (ILookupParameter<DoubleValue>)Parameters["Capacity"]; }
70    }
71    public ILookupParameter<DoubleArray> DemandParameter {
72      get { return (ILookupParameter<DoubleArray>)Parameters["Demand"]; }
73    }
74    public ILookupParameter<DoubleArray> ReadyTimeParameter {
75      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
76    }
77    public ILookupParameter<DoubleArray> DueTimeParameter {
78      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
79    }
80    public ILookupParameter<DoubleArray> ServiceTimeParameter {
81      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
82    }
83
84    [StorableConstructor]
85    protected MultiVRPSolutionCrossover(bool deserializing) : base(deserializing) { }
86    protected MultiVRPSolutionCrossover(MultiVRPSolutionCrossover original, Cloner cloner) : base(original, cloner) { }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new MultiVRPSolutionCrossover(this, cloner);
89    }
90    public MultiVRPSolutionCrossover()
91      : base() {
92      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("Parents", "The parent permutations which should be crossed."));
93      ParentsParameter.ActualName = "VRPTours";
94      Parameters.Add(new LookupParameter<IVRPEncoding>("Child", "The child permutation resulting from the crossover."));
95      ChildParameter.ActualName = "VRPTours";
96
97      Parameters.Add(new ValueLookupParameter<IntValue>("Cities", "The city count."));
98      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates of the cities."));
99      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
100      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
101      Parameters.Add(new LookupParameter<IntValue>("Vehicles", "The number of vehicles."));
102      Parameters.Add(new LookupParameter<DoubleValue>("Capacity", "The capacity of each vehicle."));
103      Parameters.Add(new LookupParameter<DoubleArray>("Demand", "The demand of each customer."));
104      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
105      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
106      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
107
108      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IVRPCrossover)).OrderBy(op => op.Name)) {
109        if (!typeof(MultiOperator<IVRPCrossover>).IsAssignableFrom(type))
110          Operators.Add((IVRPCrossover)Activator.CreateInstance(type), true);
111      }
112    }
113
114    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCrossover>> e) {
115      base.Operators_ItemsReplaced(sender, e);
116      ParameterizeCrossovers();
117    }
118
119    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCrossover>> e) {
120      base.Operators_ItemsAdded(sender, e);
121      ParameterizeCrossovers();
122    }
123
124    private void ParameterizeCrossovers() {
125      foreach (IVRPCrossover crossover in Operators.OfType<IVRPCrossover>()) {
126        crossover.ChildParameter.ActualName = ChildParameter.Name;
127        crossover.ParentsParameter.ActualName = ParentsParameter.Name;
128
129        if (crossover.CoordinatesParameter != null) crossover.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
130        if (crossover.DistanceMatrixParameter != null) crossover.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
131        if (crossover.UseDistanceMatrixParameter != null) crossover.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
132        if (crossover.VehiclesParameter != null) crossover.VehiclesParameter.ActualName = VehiclesParameter.Name;
133        if (crossover.CapacityParameter != null) crossover.CapacityParameter.ActualName = CapacityParameter.Name;
134        if (crossover.DemandParameter != null) crossover.DemandParameter.ActualName = DemandParameter.Name;
135        if (crossover.ReadyTimeParameter != null) crossover.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
136        if (crossover.DueTimeParameter != null) crossover.DueTimeParameter.ActualName = DueTimeParameter.Name;
137        if (crossover.ServiceTimeParameter != null) crossover.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
138      }
139      foreach (IStochasticOperator crossover in Operators.OfType<IStochasticOperator>()) {
140        crossover.RandomParameter.ActualName = RandomParameter.Name;
141      }
142    }
143
144    public override IOperation Apply() {
145      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation crossover to choose from.");
146      return base.Apply();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.