Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15711 was 13071, checked in by gkronber, 9 years ago

#2499: added license headers and removed unused usings

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