1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
|
---|
34 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
35 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
36 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
|
---|
39 | [Item("MultiVRPMoveGenerator", "Randomly selects and applies its move generators.")]
|
---|
40 | [StorableClass]
|
---|
41 | public class MultiVRPMoveGenerator : CheckedMultiOperator<IMultiVRPMoveGenerator>, IMultiVRPMoveOperator,
|
---|
42 | IStochasticOperator, IMoveGenerator, IGeneralVRPOperator, IMultiVRPOperator {
|
---|
43 | public override bool CanChangeName {
|
---|
44 | get { return false; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IValueLookupParameter<IntValue> SelectedOperatorsParameter {
|
---|
48 | get { return (IValueLookupParameter<IntValue>)Parameters["SelectedOperators"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ILookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
52 | get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public ILookupParameter VRPMoveParameter {
|
---|
56 | get { return (ILookupParameter)Parameters["VRPMove"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
60 | get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IVRPProblemInstance ProblemInstance {
|
---|
64 | get { return ProblemInstanceParameter.ActualValue; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public ValueLookupParameter<DoubleArray> ProbabilitiesParameter {
|
---|
68 | get { return (ValueLookupParameter<DoubleArray>)Parameters["Probabilities"]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<IRandom> RandomParameter {
|
---|
71 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public DoubleArray Probabilities {
|
---|
75 | get { return ProbabilitiesParameter.Value; }
|
---|
76 | set { ProbabilitiesParameter.Value = value; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected MultiVRPMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
81 | public MultiVRPMoveGenerator()
|
---|
82 | : base() {
|
---|
83 | Parameters.Add(new ValueLookupParameter<IntValue>("SelectedOperators", "The number of selected operators.", new IntValue(1)));
|
---|
84 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
85 | Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray()));
|
---|
86 | Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
87 |
|
---|
88 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours."));
|
---|
89 | Parameters.Add(new LookupParameter<IVRPMove>("VRPMove", "The generated moves."));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
93 | return new MultiVRPMoveGenerator(this, cloner);
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected MultiVRPMoveGenerator(MultiVRPMoveGenerator original, Cloner cloner)
|
---|
97 | : base(original, cloner) {
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void SetOperators(IEnumerable<IOperator> operators) {
|
---|
101 | foreach (IOperator op in operators) {
|
---|
102 | if (op is IMultiVRPMoveGenerator && !(op is MultiOperator<IMultiVRPMoveGenerator>)) {
|
---|
103 | Operators.Add(op.Clone() as IMultiVRPMoveGenerator, !(op is IAlbaOperator || op is PotvinVehicleAssignmentMultiMoveGenerator));
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMultiVRPMoveGenerator>> e) {
|
---|
109 | base.Operators_ItemsRemoved(sender, e);
|
---|
110 | if (Probabilities != null && Probabilities.Length > Operators.Count) {
|
---|
111 | List<double> probs = new List<double>(Probabilities.Cast<double>());
|
---|
112 | var sorted = e.Items.OrderByDescending(x => x.Index);
|
---|
113 | foreach (IndexedItem<IMultiVRPMoveGenerator> item in sorted)
|
---|
114 | if (probs.Count > item.Index) probs.RemoveAt(item.Index);
|
---|
115 | Probabilities = new DoubleArray(probs.ToArray());
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMultiVRPMoveGenerator>> e) {
|
---|
120 | base.Operators_ItemsReplaced(sender, e);
|
---|
121 | ParameterizeMoveGenerators();
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMultiVRPMoveGenerator>> e) {
|
---|
125 | base.Operators_ItemsAdded(sender, e);
|
---|
126 | ParameterizeMoveGenerators();
|
---|
127 |
|
---|
128 | if (Probabilities != null && Probabilities.Length < Operators.Count) {
|
---|
129 | double avg = (Probabilities.Where(x => x > 0).Count() > 0) ? (Probabilities.Where(x => x > 0).Average()) : (1);
|
---|
130 | // add the average of all probabilities in the respective places (the new operators)
|
---|
131 | var added = e.Items.OrderBy(x => x.Index).ToList();
|
---|
132 | int insertCount = 0;
|
---|
133 | DoubleArray probs = new DoubleArray(Operators.Count);
|
---|
134 | for (int i = 0; i < Operators.Count; i++) {
|
---|
135 | if (insertCount < added.Count && i == added[insertCount].Index) {
|
---|
136 | probs[i] = avg;
|
---|
137 | insertCount++;
|
---|
138 | } else if (i - insertCount < Probabilities.Length) {
|
---|
139 | probs[i] = Probabilities[i - insertCount];
|
---|
140 | } else probs[i] = avg;
|
---|
141 | }
|
---|
142 | Probabilities = probs;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void ParameterizeMoveGenerators() {
|
---|
147 | foreach (IMultiVRPMoveOperator moveGenerator in Operators.OfType<IMultiVRPMoveOperator>()) {
|
---|
148 | moveGenerator.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
|
---|
149 | moveGenerator.VRPToursParameter.ActualName = VRPToursParameter.Name;
|
---|
150 | moveGenerator.VRPMoveParameter.ActualName = VRPMoveParameter.Name;
|
---|
151 | }
|
---|
152 | foreach (IStochasticOperator moveGenerator in Operators.OfType<IStochasticOperator>()) {
|
---|
153 | moveGenerator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override IOperation InstrumentedApply() {
|
---|
158 | if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one VRP move generator choose from.");
|
---|
159 | OperationCollection next = new OperationCollection(base.InstrumentedApply());
|
---|
160 |
|
---|
161 | for (int i = 0; i < SelectedOperatorsParameter.ActualValue.Value; i++) {
|
---|
162 | IRandom random = RandomParameter.ActualValue;
|
---|
163 | DoubleArray probabilities = ProbabilitiesParameter.ActualValue;
|
---|
164 | if (probabilities.Length != Operators.Count) {
|
---|
165 | throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
|
---|
166 | }
|
---|
167 | IOperator successor = null;
|
---|
168 | var checkedOperators = Operators.CheckedItems;
|
---|
169 | if (checkedOperators.Count() > 0) {
|
---|
170 | // select a random operator from the checked operators
|
---|
171 | double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
|
---|
172 | if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
|
---|
173 | double r = random.NextDouble() * sum;
|
---|
174 | sum = 0;
|
---|
175 | foreach (var indexedItem in checkedOperators) {
|
---|
176 | sum += probabilities[indexedItem.Index];
|
---|
177 | if (sum > r) {
|
---|
178 | successor = indexedItem.Value;
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (successor != null) {
|
---|
185 | next.Insert(0, ExecutionContext.CreateChildOperation(successor));
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return next;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|