Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Creators/MultiVRPSolutionCreator.cs @ 4383

Last change on this file since 4383 was 4365, checked in by svonolfe, 15 years ago

Worked on VRP operators (WIP) (#1177)

File size: 3.8 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Data;
32using HeuristicLab.Problems.VehicleRouting.Interfaces;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using System.Collections.Generic;
35
36namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
37  [Item("MultiVRPSolutionCreator", "Randomly selects and applies one of its creator every time it is called.")]
38  [StorableClass]
39  public class MultiVRPSolutionCreator : StochasticMultiBranch<IVRPCreator>, IVRPCreator, IGeneralVRPOperator, IMultiVRPOperator, IStochasticOperator {
40    public override bool CanChangeName {
41      get { return false; }
42    }
43    protected override bool CreateChildOperation {
44      get { return true; }
45    }
46
47    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
48      get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
49    }
50
51    public ILookupParameter<IVRPEncoding> VRPToursParameter {
52      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
53    }
54
55    [StorableConstructor]
56    private MultiVRPSolutionCreator(bool deserializing) : base(deserializing) { }
57    public MultiVRPSolutionCreator()
58      : base() {
59      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The new VRP tours."));
60
61      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
62    }
63
64    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
65      base.Operators_ItemsReplaced(sender, e);
66      ParameterizeCreators();
67    }
68
69    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IVRPCreator>> e) {
70      base.Operators_ItemsAdded(sender, e);
71      ParameterizeCreators();
72    }
73
74    public void SetOperators(IEnumerable<IOperator> operators) {
75      foreach (IOperator op in operators) {
76        if (op is IVRPCreator && !(op is MultiVRPSolutionCreator)) {
77          Operators.Add(op.Clone() as IVRPCreator, true);
78        }
79      }
80    }
81
82    private void ParameterizeCreators() {
83      foreach (IVRPCreator creator in Operators.OfType<IVRPCreator>()) {
84        creator.VRPToursParameter.ActualName = VRPToursParameter.Name;
85        creator.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
86      }
87      foreach (IStochasticOperator creator in Operators.OfType<IStochasticOperator>()) {
88        creator.RandomParameter.ActualName = RandomParameter.Name;
89      }
90    }
91
92    public override IOperation Apply() {
93      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one VRP creator to choose from.");
94      return base.Apply();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.