1 | using HeuristicLab.BioBoost.ProblemDescription;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
6 | using HeuristicLab.Parameters;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Random;
|
---|
9 | using System;
|
---|
10 | using System.Linq;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.BioBoost.Operators.Mutation {
|
---|
13 | [StorableClass]
|
---|
14 | public class TargetMergingIntegerVectorManipulator : BoundedIntegerVectorManipulator {
|
---|
15 |
|
---|
16 | #region Parameters
|
---|
17 | public LookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
18 | get { return (LookupParameter<DistanceMatrix>) Parameters["DistanceMatrix"]; }
|
---|
19 | }
|
---|
20 | public ValueParameter<BoolValue> DistanceMatrixInProblemDataParameter {
|
---|
21 | get { return (ValueParameter<BoolValue>) Parameters["DistanceMatrixInProblemData"]; }
|
---|
22 | }
|
---|
23 | public ILookupParameter<BioBoostProblemData> ProblemDataParameter {
|
---|
24 | get { return (ILookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
|
---|
25 | }
|
---|
26 | public ValueLookupParameter<DoubleValue> AvgBundleMutationRatioParameter {
|
---|
27 | get { return (ValueLookupParameter<DoubleValue>) Parameters["AvgBundleMutationRatio"]; }
|
---|
28 | }
|
---|
29 | #endregion
|
---|
30 |
|
---|
31 | #region Parameter Values
|
---|
32 | public DistanceMatrix DistanceMatrix {
|
---|
33 | get {
|
---|
34 | if (DistanceMatrixInProblemData) {
|
---|
35 | return ((IValueParameter<DistanceMatrix>) ProblemData.Parameters[DistanceMatrixParameter.ActualName]).Value;
|
---|
36 | } else {
|
---|
37 | return DistanceMatrixParameter.ActualValue;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 | public bool DistanceMatrixInProblemData { get { return DistanceMatrixInProblemDataParameter.Value.Value; } }
|
---|
42 | public BioBoostProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
|
---|
43 | public double AvgBundleMutationRatio { get { return AvgBundleMutationRatioParameter.ActualValue.Value; } }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region Construction & Cloning
|
---|
47 | [StorableConstructor]
|
---|
48 | protected TargetMergingIntegerVectorManipulator(bool isDeserializing) : base(isDeserializing) {}
|
---|
49 | protected TargetMergingIntegerVectorManipulator(TargetMergingIntegerVectorManipulator orig, Cloner cloner) : base(orig, cloner) {}
|
---|
50 | public TargetMergingIntegerVectorManipulator() {
|
---|
51 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The distance matrix to use", "StreetDistanceMatrix"));
|
---|
52 | Parameters.Add(new ValueParameter<BoolValue>("DistanceMatrixInProblemData", "Whether to look for the distance matrix in problem data or in scope.", new BoolValue(true)));
|
---|
53 | Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The problem instance description container."));
|
---|
54 | Parameters.Add(new ValueLookupParameter<DoubleValue>("AvgBundleMutationRatio", "The average ratio of modified targets in a bundle.", new PercentValue(1)));
|
---|
55 | }
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new TargetMergingIntegerVectorManipulator(this, cloner);
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
|
---|
62 | var ratio =
|
---|
63 | new NormalDistributedRandom(random, Math.Min(1, Math.Max(0, AvgBundleMutationRatio)), 0.5).NextDouble();
|
---|
64 | var targetGroups = integerVector
|
---|
65 | .Select((t, i) => new {t, i})
|
---|
66 | .GroupBy(x => x.t, x => x.i)
|
---|
67 | .Shuffle(random)
|
---|
68 | .Take(2)
|
---|
69 | .ToList();
|
---|
70 | if (targetGroups.Count == 2) {
|
---|
71 | var dm = DistanceMatrix;
|
---|
72 | var key1 = targetGroups[0].Key;
|
---|
73 | var key2 = targetGroups[1].Key;
|
---|
74 | var targetList1 = targetGroups[0].Shuffle(random).ToList();
|
---|
75 | var targetList2 = targetGroups[1].Shuffle(random).ToList();
|
---|
76 | var center = Enumerable
|
---|
77 | .Range(0, integerVector.Length)
|
---|
78 | .Select(i => new {idx = i, dist = dm[key1, i] + dm[key2, i]})
|
---|
79 | .MinItems(d => d.dist)
|
---|
80 | .Shuffle(random)
|
---|
81 | .First();
|
---|
82 | foreach (var i in targetList1.Take(Math.Max(1, (int)Math.Round(ratio*targetList1.Count)))) {
|
---|
83 | integerVector[i] = center.idx;
|
---|
84 | }
|
---|
85 | foreach (var i in targetList2.Take(Math.Max(1, (int)Math.Round(ratio*targetList2.Count)))) {
|
---|
86 | integerVector[i] = center.idx;
|
---|
87 | }
|
---|
88 | } else if (targetGroups.Count == 1) {
|
---|
89 | integerVector[random.Next(integerVector.Length)] = random.Next(integerVector.Length);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|