1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Analysis;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 | using HeuristicLab.Collections;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
|
---|
36 | [Item("BiasedMultiVRPSolutionManipulator", "Randomly selects and applies one of its crossovers every time it is called based on the success progress.")]
|
---|
37 | [StorableClass]
|
---|
38 | public class BiasedMultiVRPSolutionManipulator : MultiVRPSolutionManipulator {
|
---|
39 | public ValueLookupParameter<DoubleArray> ActualProbabilitiesParameter {
|
---|
40 | get { return (ValueLookupParameter<DoubleArray>)Parameters["ActualProbabilities"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public ValueLookupParameter<IntValue> LastUpdateParameter {
|
---|
44 | get { return (ValueLookupParameter<IntValue>)Parameters["LastUpdate"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
48 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ValueLookupParameter<StringValue> SuccessProgressAnalyisis {
|
---|
52 | get { return (ValueLookupParameter<StringValue>)Parameters["SuccessProgressAnalysis"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public ValueLookupParameter<IntValue> Interval {
|
---|
56 | get { return (ValueLookupParameter<IntValue>)Parameters["Interval"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ValueLookupParameter<DoubleValue> Factor {
|
---|
60 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Factor"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public LookupParameter<IntValue> Generations {
|
---|
64 | get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [StorableConstructor]
|
---|
68 | protected BiasedMultiVRPSolutionManipulator(bool deserializing) : base(deserializing) { }
|
---|
69 | protected BiasedMultiVRPSolutionManipulator(BiasedMultiVRPSolutionManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
70 | public BiasedMultiVRPSolutionManipulator()
|
---|
71 | : base() {
|
---|
72 | Parameters.Add(new ValueLookupParameter<DoubleArray>("ActualProbabilities", "The array of relative probabilities for each operator."));
|
---|
73 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the succedd progress analysis results should be stored."));
|
---|
74 | Parameters.Add(new ValueLookupParameter<StringValue>("SuccessProgressAnalysis", "The success progress analyisis to be considered",
|
---|
75 | new StringValue("Success Progress ExecutedMutationOperator")));
|
---|
76 | Parameters.Add(new ValueLookupParameter<IntValue>("Interval", "The interval the probabilities should be updated", new IntValue(1)));
|
---|
77 | Parameters.Add(new ValueLookupParameter<IntValue>("LastUpdate", "Specifies when the probabilities were last updated", new IntValue(0)));
|
---|
78 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Factor", "The factor with which the probabilities should be updated", new DoubleValue(35)));
|
---|
79 | Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
83 | return new BiasedMultiVRPSolutionManipulator(this, cloner);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override IOperation Apply() {
|
---|
87 | int generations = Generations.ActualValue.Value;
|
---|
88 | int interval = Interval.ActualValue.Value;
|
---|
89 |
|
---|
90 | if (generations == 0) {
|
---|
91 | ActualProbabilitiesParameter.Value = ProbabilitiesParameter.ActualValue.Clone() as DoubleArray;
|
---|
92 | LastUpdateParameter.Value = new IntValue(0);
|
---|
93 | } else if (generations % interval == 0 && LastUpdateParameter.Value.Value != generations) {
|
---|
94 | String key = SuccessProgressAnalyisis.ActualValue.Value;
|
---|
95 | if (ResultsParameter.ActualValue != null && ResultsParameter.ActualValue.ContainsKey(key)) {
|
---|
96 | DataTable successProgressAnalysis = ResultsParameter.ActualValue[key].Value as DataTable;
|
---|
97 |
|
---|
98 | for (int i = 0; i < Operators.Count; i++) {
|
---|
99 | IOperator current = Operators[i];
|
---|
100 |
|
---|
101 | if (successProgressAnalysis.Rows.ContainsKey(current.Name)) {
|
---|
102 | DataRow row = successProgressAnalysis.Rows[current.Name];
|
---|
103 |
|
---|
104 | double sum = 0.0;
|
---|
105 | ObservableList<double> usages = row.Values;
|
---|
106 |
|
---|
107 | int end = generations;
|
---|
108 | int start = generations - interval;
|
---|
109 |
|
---|
110 | if (end - start > 0) {
|
---|
111 | for (int j = start; j < end; j++) {
|
---|
112 | sum += (double)usages[j];
|
---|
113 | }
|
---|
114 |
|
---|
115 | ActualProbabilitiesParameter.Value[i] += ((sum / (end - start)) / ActualProbabilitiesParameter.Value[i]) * Factor.Value.Value;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | //normalize
|
---|
122 | double max = ActualProbabilitiesParameter.Value.Max();
|
---|
123 | for (int i = 0; i < ActualProbabilitiesParameter.Value.Length; i++) {
|
---|
124 | ActualProbabilitiesParameter.Value[i] /= max;
|
---|
125 | }
|
---|
126 |
|
---|
127 | LastUpdateParameter.Value.Value = generations;
|
---|
128 | }
|
---|
129 |
|
---|
130 | ////////////////
|
---|
131 | IRandom random = RandomParameter.ActualValue;
|
---|
132 | DoubleArray probabilities = ActualProbabilitiesParameter.ActualValue;
|
---|
133 | if (probabilities.Length != Operators.Count) {
|
---|
134 | throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
|
---|
135 | }
|
---|
136 | IOperator successor = null;
|
---|
137 | var checkedOperators = Operators.CheckedItems;
|
---|
138 | if (checkedOperators.Count() > 0) {
|
---|
139 | // select a random operator from the checked operators
|
---|
140 | double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
|
---|
141 | if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
|
---|
142 | double r = random.NextDouble() * sum;
|
---|
143 | sum = 0;
|
---|
144 | foreach (var indexedItem in checkedOperators) {
|
---|
145 | sum += probabilities[indexedItem.Index];
|
---|
146 | if (sum > r) {
|
---|
147 | successor = indexedItem.Value;
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | IOperation successorOp = null;
|
---|
154 | if (Successor != null)
|
---|
155 | successorOp = ExecutionContext.CreateOperation(Successor);
|
---|
156 | OperationCollection next = new OperationCollection(successorOp);
|
---|
157 | if (successor != null) {
|
---|
158 | ExecutedOperatorParameter.ActualValue = new StringValue(successor.GetType().Name);
|
---|
159 |
|
---|
160 | if (CreateChildOperation)
|
---|
161 | next.Insert(0, ExecutionContext.CreateChildOperation(successor));
|
---|
162 | else next.Insert(0, ExecutionContext.CreateOperation(successor));
|
---|
163 | } else {
|
---|
164 | ExecutedOperatorParameter.ActualValue = new StringValue("");
|
---|
165 | }
|
---|
166 |
|
---|
167 | return next;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|