Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Manipulators/MultiVRPSolutionManipulator.cs

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

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

File size: 4.3 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;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Problems.VehicleRouting.Interfaces;
33
34namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
35  [Item("MultiVRPSolutionManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
36  [StorableType("9EE8B958-A511-40EC-A3DA-0F0CE0BF6524")]
37  public class MultiVRPSolutionManipulator : StochasticMultiBranch<IVRPManipulator>, IVRPManipulator, IGeneralVRPOperator, IMultiVRPOperator, 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<IVRPEncodedSolution> VRPToursParameter {
46      get { return (ILookupParameter<IVRPEncodedSolution>)Parameters["VRPTours"]; }
47    }
48
49
50    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
51      get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
52    }
53
54    [StorableConstructor]
55    protected MultiVRPSolutionManipulator(StorableConstructorFlag _) : base(_) { }
56    public MultiVRPSolutionManipulator()
57      : base() {
58      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
59      Parameters.Add(new LookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP tours to be manipulated."));
60
61      SelectedOperatorParameter.ActualName = "SelectedManipulationOperator";
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new MultiVRPSolutionManipulator(this, cloner);
66    }
67
68    protected MultiVRPSolutionManipulator(MultiVRPSolutionManipulator original, Cloner cloner)
69      : base(original, cloner) {
70    }
71
72    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
73      base.Operators_ItemsReplaced(sender, e);
74      ParameterizeManipulators();
75    }
76
77    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPManipulator>> e) {
78      base.Operators_ItemsAdded(sender, e);
79      ParameterizeManipulators();
80    }
81
82    public void SetOperators(IEnumerable<IOperator> operators) {
83      foreach (IOperator op in operators) {
84        if (op is IVRPManipulator && !(op is MultiVRPSolutionManipulator)) {
85          Operators.Add(op.Clone() as IVRPManipulator, !(op is IVRPLocalSearchManipulator));
86        }
87      }
88    }
89
90    private void ParameterizeManipulators() {
91      foreach (IVRPManipulator manipulator in Operators.OfType<IVRPManipulator>()) {
92        manipulator.VRPToursParameter.ActualName = VRPToursParameter.Name;
93        manipulator.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
94      }
95      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
96        manipulator.RandomParameter.ActualName = RandomParameter.Name;
97      }
98    }
99
100    public override IOperation InstrumentedApply() {
101      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation manipulator to choose from.");
102      return base.InstrumentedApply();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.