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