source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/CommonTargetSwappingIntegerVectorMutator.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 7 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 3.0 KB
Line 
1using HeuristicLab.BioBoost.ProblemDescription;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.IntegerVectorEncoding;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Random;
9using System;
10using System.Linq;
11
12namespace HeuristicLab.BioBoost.Operators.Mutation {
13  [StorableClass]
14  public class CommonTargetSwappingIntegerVectorMutator : BoundedIntegerVectorManipulator {
15
16    public LookupParameter<BioBoostProblemData> ProblemDataParameter {
17      get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
18    }
19
20    public ValueLookupParameter<DoubleValue> AvgBundleMutationRatioParameter {
21      get { return (ValueLookupParameter<DoubleValue>) Parameters["AvgBundleMutationRatio"]; }
22    }
23
24    public double AvgBundleMutationRatio {
25      get { return AvgBundleMutationRatioParameter.ActualValue.Value; }
26    }
27
28
29    public BioBoostProblemData ProblemData {
30      get { return ProblemDataParameter.ActualValue; }
31    }
32
33    #region Construction & Cloning
34    [StorableConstructor]
35    protected CommonTargetSwappingIntegerVectorMutator(bool isDeserializing) : base(isDeserializing) {}
36    protected CommonTargetSwappingIntegerVectorMutator(CommonTargetSwappingIntegerVectorMutator orig, Cloner cloner) : base(orig, cloner) {}
37    public CommonTargetSwappingIntegerVectorMutator() {
38      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The problem instance description container."));
39      Parameters.Add(new ValueLookupParameter<DoubleValue>("AvgBundleMutationRatio", "The average ratio of modified targets in a bundle.", new PercentValue(0.25)));
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new CommonTargetSwappingIntegerVectorMutator(this, cloner);
43    }
44    #endregion
45
46    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
47      var ratio =
48        new NormalDistributedRandom(random, Math.Min(1, Math.Max(0, AvgBundleMutationRatio)), 0.25).NextDouble();
49      var targetGroups = integerVector
50        .Select((t, i) => new {t, i})
51        .GroupBy(x => x.t, x => x.i)
52        .Shuffle(random)
53        .Take(2)
54        .ToList();
55      if (targetGroups.Count == 2) {
56        var targetList1 = targetGroups[0].Shuffle(random).ToList();
57        var targetList2 = targetGroups[1].Shuffle(random).ToList();
58        foreach (var i in targetList1.Take(Math.Max(1, (int)Math.Round(ratio*targetList1.Count)))) {
59          integerVector[i] = targetGroups[1].Key;
60        }
61        foreach (var i in targetList2.Take(Math.Max(1, (int)Math.Round(ratio*targetList2.Count)))) {
62          integerVector[i] = targetGroups[0].Key;
63        }
64      } else if (targetGroups.Count == 1) {
65        integerVector[random.Next(integerVector.Length)] = random.Next(integerVector.Length);
66      }
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.