Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/TargetMergingIntegerVectorMutator.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: 5.2 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 HeuristicLab.Random;
30using System;
31using System.Linq;
32
33namespace HeuristicLab.BioBoost.Operators.Mutation {
34  [StorableClass]
35  public class TargetMergingIntegerVectorManipulator : BoundedIntegerVectorManipulator {
36
37    #region Parameters
38    public LookupParameter<DistanceMatrix> DistanceMatrixParameter {
39      get { return (LookupParameter<DistanceMatrix>) Parameters["DistanceMatrix"]; }
40    }
41    public ValueParameter<BoolValue> DistanceMatrixInProblemDataParameter {
42      get { return (ValueParameter<BoolValue>) Parameters["DistanceMatrixInProblemData"]; }
43    }
44    public ILookupParameter<BioBoostProblemData> ProblemDataParameter {
45      get { return (ILookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
46    }
47    public ValueLookupParameter<DoubleValue> AvgBundleMutationRatioParameter {
48      get { return (ValueLookupParameter<DoubleValue>) Parameters["AvgBundleMutationRatio"]; }
49    }
50    #endregion
51
52    #region Parameter Values
53    public DistanceMatrix DistanceMatrix {
54      get {
55        if (DistanceMatrixInProblemData) {
56          return ((IValueParameter<DistanceMatrix>) ProblemData.Parameters[DistanceMatrixParameter.ActualName]).Value;
57        } else {
58          return DistanceMatrixParameter.ActualValue;
59        }
60      }
61    }
62    public bool DistanceMatrixInProblemData { get { return DistanceMatrixInProblemDataParameter.Value.Value; } }
63    public BioBoostProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
64    public double AvgBundleMutationRatio { get { return AvgBundleMutationRatioParameter.ActualValue.Value; } }
65    #endregion
66
67    #region Construction & Cloning
68    [StorableConstructor]
69    protected TargetMergingIntegerVectorManipulator(bool isDeserializing) : base(isDeserializing) {}
70    protected TargetMergingIntegerVectorManipulator(TargetMergingIntegerVectorManipulator orig, Cloner cloner) : base(orig, cloner) {}
71    public TargetMergingIntegerVectorManipulator() {
72      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The distance matrix to use", "StreetDistanceMatrix"));
73      Parameters.Add(new ValueParameter<BoolValue>("DistanceMatrixInProblemData", "Whether to look for the distance matrix in problem data or in scope.", new BoolValue(true)));
74      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The problem instance description container."));
75      Parameters.Add(new ValueLookupParameter<DoubleValue>("AvgBundleMutationRatio", "The average ratio of modified targets in a bundle.", new PercentValue(1)));
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new TargetMergingIntegerVectorManipulator(this, cloner);
79    }
80    #endregion
81
82    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
83      var ratio =
84        new NormalDistributedRandom(random, Math.Min(1, Math.Max(0, AvgBundleMutationRatio)), 0.5).NextDouble();
85      var targetGroups = integerVector
86        .Select((t, i) => new {t, i})
87        .GroupBy(x => x.t, x => x.i)
88        .Shuffle(random)
89        .Take(2)
90        .ToList();
91      if (targetGroups.Count == 2) {
92        var dm = DistanceMatrix;
93        var key1 = targetGroups[0].Key;
94        var key2 = targetGroups[1].Key;
95        var targetList1 = targetGroups[0].Shuffle(random).ToList();
96        var targetList2 = targetGroups[1].Shuffle(random).ToList();
97        var center = Enumerable
98          .Range(0, integerVector.Length)
99          .Select(i => new {idx = i, dist = dm[key1, i] + dm[key2, i]})
100          .MinItems(d => d.dist)
101          .Shuffle(random)
102          .First();
103        foreach (var i in targetList1.Take(Math.Max(1, (int)Math.Round(ratio*targetList1.Count)))) {
104          integerVector[i] = center.idx;
105        }
106        foreach (var i in targetList2.Take(Math.Max(1, (int)Math.Round(ratio*targetList2.Count)))) {
107          integerVector[i] = center.idx;
108        }
109      } else if (targetGroups.Count == 1) {
110        integerVector[random.Next(integerVector.Length)] = random.Next(integerVector.Length);
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.