Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Crossovers/BiasedMultiVRPSolutionCrossover.cs @ 10743

Last change on this file since 10743 was 10743, checked in by abeham, 10 years ago

#2146: merged r10407,r10465,r10466,r10474,r10503,r10504,r10646 into stable

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