[5200] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
| 32 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 33 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
| 34 | using System.Collections.Generic;
|
---|
| 35 | using HeuristicLab.Common;
|
---|
| 36 |
|
---|
| 37 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
|
---|
| 38 | [Item("MultiVRPSolutionCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public class MultiVRPSolutionCrossover : StochasticMultiBranch<IVRPCrossover>, IVRPCrossover, IGeneralVRPOperator, IMultiVRPOperator, IStochasticOperator {
|
---|
| 41 | public override bool CanChangeName {
|
---|
| 42 | get { return false; }
|
---|
| 43 | }
|
---|
| 44 | protected override bool CreateChildOperation {
|
---|
| 45 | get { return true; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
| 49 | get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public ILookupParameter<ItemArray<IVRPEncoding>> ParentsParameter {
|
---|
| 53 | get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["Parents"]; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public ILookupParameter<IVRPEncoding> ChildParameter {
|
---|
| 57 | get { return (ILookupParameter<IVRPEncoding>)Parameters["Child"]; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | [StorableConstructor]
|
---|
[6716] | 62 | protected MultiVRPSolutionCrossover(bool deserializing) : base(deserializing) { }
|
---|
[5200] | 63 | public MultiVRPSolutionCrossover()
|
---|
| 64 | : base() {
|
---|
| 65 | Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
| 66 |
|
---|
| 67 | Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("Parents", "The parent permutations which should be crossed."));
|
---|
| 68 | ParentsParameter.ActualName = "VRPTours";
|
---|
| 69 | Parameters.Add(new LookupParameter<IVRPEncoding>("Child", "The child permutation resulting from the crossover."));
|
---|
| 70 | ChildParameter.ActualName = "VRPTours";
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new MultiVRPSolutionCrossover(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected MultiVRPSolutionCrossover(MultiVRPSolutionCrossover original, Cloner cloner)
|
---|
| 78 | : base(original, cloner) {
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCrossover>> e) {
|
---|
| 82 | base.Operators_ItemsReplaced(sender, e);
|
---|
| 83 | ParameterizeCrossovers();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCrossover>> e) {
|
---|
| 87 | base.Operators_ItemsAdded(sender, e);
|
---|
| 88 | ParameterizeCrossovers();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void SetOperators(IEnumerable<IOperator> operators) {
|
---|
| 92 | foreach(IOperator op in operators) {
|
---|
| 93 | if (op is IVRPCrossover && !(op is MultiVRPSolutionCrossover)) {
|
---|
| 94 | Operators.Add(op.Clone() as IVRPCrossover, true);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void ParameterizeCrossovers() {
|
---|
| 100 | foreach (IVRPCrossover crossover in Operators.OfType<IVRPCrossover>()) {
|
---|
| 101 | crossover.ChildParameter.ActualName = ChildParameter.Name;
|
---|
| 102 | crossover.ParentsParameter.ActualName = ParentsParameter.Name;
|
---|
| 103 | crossover.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
|
---|
| 104 | }
|
---|
| 105 | foreach (IStochasticOperator crossover in Operators.OfType<IStochasticOperator>()) {
|
---|
| 106 | crossover.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public override IOperation Apply() {
|
---|
| 111 | if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation crossover to choose from.");
|
---|
| 112 | return base.Apply();
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|