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 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 | }
|
---|