Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Creators/MultiVRPSolutionCreator.cs @ 10834

Last change on this file since 10834 was 10295, checked in by mkommend, 11 years ago

#2119: Adapted all multi operators to subclass InstrumentedOperator.

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.VehicleRouting.Interfaces;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34
35namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
36  [Item("MultiVRPSolutionCreator", "Randomly selects and applies one of its creator every time it is called.")]
37  [StorableClass]
38  public class MultiVRPSolutionCreator : StochasticMultiBranch<IVRPCreator>, IVRPCreator, 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<IVRPProblemInstance> ProblemInstanceParameter {
47      get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
48    }
49
50    public ILookupParameter<IVRPEncoding> VRPToursParameter {
51      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
52    }
53
54    [StorableConstructor]
55    protected MultiVRPSolutionCreator(bool deserializing) : base(deserializing) { }
56    public MultiVRPSolutionCreator()
57      : base() {
58      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The new VRP tours."));
59
60      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new MultiVRPSolutionCreator(this, cloner);
65    }
66
67    protected MultiVRPSolutionCreator(MultiVRPSolutionCreator original, Cloner cloner)
68      : base(original, cloner) {
69    }
70
71    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
72      base.Operators_ItemsReplaced(sender, e);
73      ParameterizeCreators();
74    }
75
76    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
77      base.Operators_ItemsAdded(sender, e);
78      ParameterizeCreators();
79    }
80
81    public void SetOperators(IEnumerable<IOperator> operators) {
82      foreach (IOperator op in operators) {
83        if (op is IVRPCreator && !(op is MultiVRPSolutionCreator)) {
84          Operators.Add(op.Clone() as IVRPCreator, true);
85        }
86      }
87    }
88
89    private void ParameterizeCreators() {
90      foreach (IVRPCreator creator in Operators.OfType<IVRPCreator>()) {
91        creator.VRPToursParameter.ActualName = VRPToursParameter.Name;
92        creator.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
93      }
94      foreach (IStochasticOperator creator in Operators.OfType<IStochasticOperator>()) {
95        creator.RandomParameter.ActualName = RandomParameter.Name;
96      }
97    }
98
99    public override IOperation InstrumentedApply() {
100      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one VRP creator to choose from.");
101      return base.InstrumentedApply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.