Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Crossovers/BiasedMultiVRPSolutionCrossover.cs @ 10331

Last change on this file since 10331 was 10298, checked in by svonolfe, 11 years ago

Adapted all VRP related operators to subclass InstrumentedOperator (#2119)

File size: 7.3 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
34  [Item("BiasedMultiVRPSolutionCrossover", "Randomly selects and applies one of its crossovers every time it is called based on the success progress.")]
35  [StorableClass]
36  public class BiasedMultiVRPSolutionCrossover : MultiVRPSolutionCrossover {
37    public ValueLookupParameter<DoubleArray> ActualProbabilitiesParameter {
38      get { return (ValueLookupParameter<DoubleArray>)Parameters["ActualProbabilities"]; }
39    }
40
41    public ValueLookupParameter<StringValue> SuccessProgressAnalyisis {
42      get { return (ValueLookupParameter<StringValue>)Parameters["SuccessProgressAnalysis"]; }
43    }
44
45    public ValueLookupParameter<DoubleValue> Factor {
46      get { return (ValueLookupParameter<DoubleValue>)Parameters["Factor"]; }
47    }
48
49    public ValueParameter<DoubleValue> LowerBoundParameter {
50      get { return (ValueParameter<DoubleValue>)Parameters["LowerBound"]; }
51    }
52
53    public ValueParameter<IntValue> DepthParameter {
54      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
55    }
56
57    [StorableConstructor]
58    protected BiasedMultiVRPSolutionCrossover(bool deserializing) : base(deserializing) { }
59    protected BiasedMultiVRPSolutionCrossover(BiasedMultiVRPSolutionCrossover original, Cloner cloner) : base(original, cloner) { }
60    public BiasedMultiVRPSolutionCrossover()
61      : base() {
62      Parameters.Add(new ValueLookupParameter<DoubleArray>("ActualProbabilities", "The array of relative probabilities for each operator."));
63      Parameters.Add(new ValueLookupParameter<StringValue>("SuccessProgressAnalysis", "The success progress analyisis to be considered",
64        new StringValue("ExecutedCrossoverOperator")));
65
66      Parameters.Add(new ValueLookupParameter<DoubleValue>("Factor", "The factor with which the probabilities should be updated", new DoubleValue(0.2)));
67      Parameters.Add(new ValueParameter<DoubleValue>("LowerBound", "The depth of the individuals in the scope tree.", new DoubleValue(0.01)));
68      Parameters.Add(new ValueParameter<IntValue>("Depth", "The depth of the individuals in the scope tree.", new IntValue(1)));
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new BiasedMultiVRPSolutionCrossover(this, cloner);
73    }
74
75    public override void InitializeState() {
76      base.InitializeState();
77
78      ActualProbabilitiesParameter.Value = null;
79    }
80
81    public override IOperation InstrumentedApply() {
82      IOperator successor = null;
83
84      if (ActualProbabilitiesParameter.ActualValue == null) {
85        ActualProbabilitiesParameter.Value = ProbabilitiesParameter.ActualValue.Clone() as DoubleArray;
86      } else {
87        String key = "SuccessfulOffspringAnalyzer Results";
88
89        ResultCollection results = null;
90        IScope scope = ExecutionContext.Parent.Scope;
91        int depth = 1;
92        while (scope != null && depth < DepthParameter.Value.Value) {
93          scope = scope.Parent;
94          depth++;
95        }
96        if (scope != null)
97          results = scope.Variables["Results"].Value as ResultCollection;
98
99        if (results != null && results.ContainsKey(key)) {
100          ResultCollection successProgressAnalysisResult = results[key].Value as ResultCollection;
101          key = SuccessProgressAnalyisis.Value.Value;
102
103          if (successProgressAnalysisResult.ContainsKey(key)) {
104            DataTable successProgressAnalysis = successProgressAnalysisResult[key].Value as DataTable;
105
106            for (int i = 0; i < Operators.Count; i++) {
107              IOperator current = Operators[i];
108
109              if (successProgressAnalysis.Rows.ContainsKey(current.Name)) {
110                DataRow row = successProgressAnalysis.Rows[current.Name];
111
112                double sum = 0.0;
113                ObservableList<double> usages = row.Values;
114
115                sum += (double)usages.Last();
116
117                ActualProbabilitiesParameter.ActualValue[i] += (sum / ActualProbabilitiesParameter.ActualValue[i]) * Factor.Value.Value;
118              }
119            }
120          }
121        }
122
123        //normalize
124        double max = ActualProbabilitiesParameter.ActualValue.Max();
125        for (int i = 0; i < ActualProbabilitiesParameter.ActualValue.Length; i++) {
126          ActualProbabilitiesParameter.ActualValue[i] /= max;
127          ActualProbabilitiesParameter.ActualValue[i] =
128            Math.Max(LowerBoundParameter.Value.Value,
129            ActualProbabilitiesParameter.ActualValue[i]);
130        }
131      }
132
133      ////////////////
134      IRandom random = RandomParameter.ActualValue;
135      DoubleArray probabilities = ActualProbabilitiesParameter.ActualValue;
136      if (probabilities.Length != Operators.Count) {
137        throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
138      }
139      var checkedOperators = Operators.CheckedItems;
140      if (checkedOperators.Count() > 0) {
141        // select a random operator from the checked operators
142        double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
143        if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
144        double r = random.NextDouble() * sum;
145        sum = 0;
146        foreach (var indexedItem in checkedOperators) {
147          sum += probabilities[indexedItem.Index];
148          if (sum > r) {
149            successor = indexedItem.Value;
150            break;
151          }
152        }
153      }
154
155      IOperation successorOp = null;
156      if (Successor != null)
157        successorOp = ExecutionContext.CreateOperation(Successor);
158      OperationCollection next = new OperationCollection(successorOp);
159      if (successor != null) {
160        SelectedOperatorParameter.ActualValue = new StringValue(successor.Name);
161
162        if (CreateChildOperation)
163          next.Insert(0, ExecutionContext.CreateChildOperation(successor));
164        else next.Insert(0, ExecutionContext.CreateOperation(successor));
165      } else {
166        SelectedOperatorParameter.ActualValue = new StringValue("");
167      }
168
169      return next;
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.