Free cookie consent management tool by TermsFeed Policy Generator

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