Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/DistanceBasedIntegerVectorMutator.cs @ 16575

Last change on this file since 16575 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.BioBoost.ProblemDescription;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System;
30using System.Linq;
31using HEAL.Attic;
32
33namespace HeuristicLab.BioBoost.Operators.Mutation {
34  [StorableType("37633B84-8BEF-410F-891B-953AD45342AE")]
35  public class DistanceBasedIntegerVectorMutator : IntegerVectorManipulator {
36
37
38    #region Parameters
39    public LookupParameter<DistanceMatrix> DistanceMatrixParameter { get { return (LookupParameter<DistanceMatrix>) Parameters["DistanceMatrix"]; } }
40    public ValueParameter<BoolValue> DistanceMatrixInProblemDataParameter { get { return (ValueParameter<BoolValue>) Parameters["DistanceMatrixInProblemData"]; } }
41    public ILookupParameter<BioBoostProblemData> ProblemDataParameter { get { return (ILookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; } }
42    public ILookupParameter<IntArray> ViableSourcesParameter { get { return (ILookupParameter<IntArray>) Parameters["ViableSources"]; } }
43    public ILookupParameter<IntArray> ViableTargetsParameter { get { return (ILookupParameter<IntArray>) Parameters["ViableTargets"]; } }
44    public IValueLookupParameter<DoubleValue> MaximumDistanceParameter { get { return (IValueLookupParameter<DoubleValue>) Parameters["MaximumDistance"]; } }
45
46    #endregion
47
48    #region Parameter Values
49    public DistanceMatrix DistanceMatrix {
50      get {
51        if (DistanceMatrixInProblemData) {
52          return ((IValueParameter<DistanceMatrix>) ProblemData.Parameters[DistanceMatrixParameter.ActualName]).Value;
53        } else {
54          return DistanceMatrixParameter.ActualValue;
55        }
56      }
57    }
58    public bool DistanceMatrixInProblemData { get { return DistanceMatrixInProblemDataParameter.Value.Value; } }
59    public BioBoostProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
60    public IntArray ViableSources { get { return ViableSourcesParameter.ActualValue; } }
61    public IntArray ViableTargets { get { return ViableTargetsParameter.ActualValue; } }
62    public double MaximumDistance { get { return MaximumDistanceParameter.ActualValue.Value;  } }
63    #endregion
64
65    #region Construction & Cloning
66    [StorableConstructor]
67    protected DistanceBasedIntegerVectorMutator(StorableConstructorFlag _) : base(_) { }
68    protected DistanceBasedIntegerVectorMutator(DistanceBasedIntegerVectorMutator orig, Cloner cloner) : base(orig, cloner) {}
69    public DistanceBasedIntegerVectorMutator() {
70      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The distance matrix to use", "StreetDistanceMatrix")); // TODO: check this
71      Parameters.Add(new ValueParameter<BoolValue>("DistanceMatrixInProblemData", "Whether to look for the distance matrix in problem data or in scope.", new BoolValue(true)));
72      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "Contains fixed values describing the problem."));
73      Parameters.Add(new LookupParameter<IntArray>("ViableSources", "A list with all indices of viable source regions."));
74      Parameters.Add(new LookupParameter<IntArray>("ViableTargets", "A list with all indices of viable target regions."));
75      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumDistance", "the maximum distance from source to target allowed for mutation", new DoubleValue(Double.MaxValue)));
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new DistanceBasedIntegerVectorMutator(this, cloner);
79    }
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      if (!Parameters.ContainsKey("ViableSources"))
84        Parameters.Add(new LookupParameter<IntArray>("ViableSources", "A list with all indices of viable source regions."));
85      if (!Parameters.ContainsKey("ViableTargets"))
86        Parameters.Add(new LookupParameter<IntArray>("ViableTargets", "A list with all indices of viable target regions."));
87      if (!Parameters.ContainsKey("MaximumDistance"))
88        Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumDistance", "the maximum distance from source to target allowed for mutation", new DoubleValue(Double.MaxValue)));
89    }
90    #endregion
91
92    protected override void Manipulate(IRandom random, IntegerVector integerVector) {
93      var sources = ViableSources ?? new IntArray(Enumerable.Range(0, integerVector.Length).ToArray());
94      var targets = ViableTargets ?? new IntArray(Enumerable.Range(0, integerVector.Length).ToArray());
95      var source = random.Next(sources.Length);
96      var target = integerVector[sources[source]];
97      var neighborDistances = new double[0].Select((d, idx) => new {index = idx, distance = d}).ToList();
98      var distances = DistanceMatrix;
99      var maxDistance = 0d;
100      for (int j = 0; j < targets.Length; j++) {
101        if (distances[source, targets[j]] > MaximumDistance) continue;
102        var dist = distances[target, targets[j]];
103        neighborDistances.Add(new {index = targets[j], distance = dist});
104        maxDistance = Math.Max(dist, maxDistance);
105      }
106      neighborDistances = neighborDistances.Select(p => new {p.index, distance = maxDistance - p.distance}).ToList();
107      neighborDistances.Sort((a, b) => b.distance.CompareTo(a.distance));
108      var totalDistance = neighborDistances.Sum(p => p.distance);
109      var threshold = random.NextDouble()*totalDistance;
110      var sum = 0d;
111      var index = 0;
112      while (index < neighborDistances.Count && sum < threshold) {
113        sum += neighborDistances[index].distance;
114        index++;
115      }
116      if (index < 0 || index >= neighborDistances.Count) {
117        integerVector[sources[source]] = sources[source];  // reset to local transport if no choices exist
118      } else {
119        integerVector[sources[source]] = neighborDistances[index].index;
120      }
121    }
122
123  }
124}
Note: See TracBrowser for help on using the repository browser.