Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/ShakingOperators/VehicleRoutingShakingOperator.cs @ 17717

Last change on this file since 17717 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

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