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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
34 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
35 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
38 | [Item("VRPShakingOperator", "A shaking operator for VNS that applies available mutation operators.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class VehicleRoutingShakingOperator : ShakingOperator<IVRPManipulator>, IVRPMultiNeighborhoodShakingOperator, IGeneralVRPOperator, IStochasticOperator {
|
---|
41 | #region Parameters
|
---|
42 | public ILookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
43 | get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ILookupParameter<IRandom> RandomParameter {
|
---|
47 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
51 | get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public IVRPProblemInstance ProblemInstance {
|
---|
55 | get { return ProblemInstanceParameter.ActualValue; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | protected VehicleRoutingShakingOperator(bool deserializing) : base(deserializing) { }
|
---|
62 | protected VehicleRoutingShakingOperator(VehicleRoutingShakingOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
64 | return new VehicleRoutingShakingOperator(this, cloner);
|
---|
65 | }
|
---|
66 | public VehicleRoutingShakingOperator()
|
---|
67 | : base() {
|
---|
68 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The vrp tour encoding to shake."));
|
---|
69 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that will be used for stochastic shaking operators."));
|
---|
70 | Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
71 |
|
---|
72 | foreach (IVRPManipulator shaker in ApplicationManager.Manager.GetInstances<IVRPManipulator>().OrderBy(x => x.Name))
|
---|
73 | if (!(shaker is MultiVRPSolutionManipulator)) Operators.Add(shaker);
|
---|
74 | }
|
---|
75 |
|
---|
76 | #region Wiring of some parameters
|
---|
77 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
|
---|
78 | base.Operators_ItemsAdded(sender, e);
|
---|
79 | ParameterizeOperators(e.Items);
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
|
---|
83 | base.Operators_ItemsReplaced(sender, e);
|
---|
84 | ParameterizeOperators(e.Items);
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void ParameterizeOperators(IEnumerable<IndexedItem<IVRPManipulator>> items) {
|
---|
88 | if (items.Any()) {
|
---|
89 | foreach (IStochasticOperator op in items.Select(x => x.Value).OfType<IStochasticOperator>())
|
---|
90 | op.RandomParameter.ActualName = RandomParameter.Name;
|
---|
91 | foreach (IVRPManipulator op in items.Select(x => x.Value).OfType<IVRPManipulator>()) {
|
---|
92 | op.VRPToursParameter.ActualName = VRPToursParameter.Name;
|
---|
93 | if (op.ProblemInstanceParameter != null) op.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | #endregion
|
---|
98 | }
|
---|
99 | }
|
---|