Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/ShakingOperators/VehicleRoutingShakingOperator.cs @ 17590

Last change on this file since 17590 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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