source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/CompoundMutator.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: 11.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.BioBoost.Operators.Transformation;
4using HeuristicLab.BioBoost.ProblemDescription;
5using HeuristicLab.BioBoost.Utils;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.IntegerVectorEncoding;
10using HeuristicLab.Encodings.RealVectorEncoding;
11using HeuristicLab.Operators;
12using HeuristicLab.Optimization;
13using HeuristicLab.Parameters;
14using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
15using HeuristicLab.PluginInfrastructure;
16using System.Linq;
17using HeuristicLab.BioBoost.Representation;
18using UniformOnePositionManipulator = HeuristicLab.Encodings.IntegerVectorEncoding.UniformOnePositionManipulator;
19using UniformOnePositionRealVectorManipulator = HeuristicLab.Encodings.RealVectorEncoding.UniformOnePositionManipulator;
20
21namespace HeuristicLab.BioBoost.Operators.Mutation {
22  [StorableClass]
23  public class CompoundMutator : SingleSuccessorOperator, IManipulator, IStochasticOperator {
24
25
26    #region Parameters
27    public ValueLookupParameter<IntMatrix> RegionBoundsParameter { get { return (ValueLookupParameter<IntMatrix>) Parameters["RegionBounds"]; } }
28    public LookupParameter<BioBoostProblemData> ProblemDataParameter { get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; } }
29    public OptionalConstrainedValueParameter<IIntegerVectorManipulator> TransportTargetManipulatorParameter { get { return (OptionalConstrainedValueParameter<IIntegerVectorManipulator>) Parameters["TransportTargetManipulator"]; } }
30    public OptionalConstrainedValueParameter<IRealVectorManipulator> FeedstockUtilizationManipulatorParameter { get { return (OptionalConstrainedValueParameter<IRealVectorManipulator>) Parameters["FeedstockUtilizationManipulator"]; } }
31    public ILookupParameter<IRandom> RandomParameter { get { return (ILookupParameter<IRandom>) Parameters["Random"]; } }
32    public ILookupParameter<IntValue> MaximumGenerationsParameter { get { return (ILookupParameter<IntValue>) Parameters["MaximumGenerations"]; } }
33    public ILookupParameter<IntValue> GenerationsParameter { get { return (ILookupParameter<IntValue>) Parameters["Generations"]; } }
34    public ILookupParameter<PercentValue> MinGenerationPercentageForUtilzationMutationParameter { get { return (ILookupParameter<PercentValue>) Parameters["MinGenerationPercentageForUtilzationMutation"]; } }
35    public ILookupParameter<PercentValue> ProbabilityOfCombinedMutationParameter { get { return (ILookupParameter<PercentValue>) Parameters["ProbabilityOfCombinedMutation"]; } }
36    #endregion
37
38    #region Parameter Values
39    public IIntegerVectorManipulator TransportTargetManipulator { get { return (IIntegerVectorManipulator) TransportTargetManipulatorParameter.ActualValue; } }
40    public IRealVectorManipulator FeedstockUtilizationManipulator { get { return (IRealVectorManipulator) FeedstockUtilizationManipulatorParameter.ActualValue; } }
41    public BioBoostProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
42    public IRandom Random { get { return RandomParameter.ActualValue; } }
43    public int MaximumGenerations { get { return MaximumGenerationsParameter.ActualValue.Value; } }
44    public int Generations { get { return GenerationsParameter.ActualValue.Value; } }
45    public double MinGenerationPercentageForUtilzationMutation { get { return MinGenerationPercentageForUtilzationMutationParameter.ActualValue.Value; } }
46    public double ProbabilityOfCombinedMutation { get { return ProbabilityOfCombinedMutationParameter.ActualValue.Value; } }
47    #endregion
48
49    #region Construction & Cloning
50    [StorableConstructor]
51    protected CompoundMutator(bool isDeserializing) : base(isDeserializing) {}
52    protected CompoundMutator(CompoundMutator orig, Cloner cloner) : base(orig, cloner) { }
53    public CompoundMutator() {
54      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The data store of the detailed problem description."));
55      Parameters.Add(new ValueLookupParameter<IntMatrix>("RegionBounds", "The limits of valid region ids."));
56      Parameters.Add(new OptionalConstrainedValueParameter<IIntegerVectorManipulator>("TransportTargetManipulator", "The manipulator to apply to the transport target vectors."));
57      Parameters.Add(new OptionalConstrainedValueParameter<IRealVectorManipulator>("FeedstockUtilizationManipulator", "The manipulator for the feedstock utilization vectors."));
58      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "The random number generator used."));
59      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations."));
60      Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
61      Parameters.Add(new ValueLookupParameter<PercentValue>("MinGenerationPercentageForUtilzationMutation", "The minimum percentage of generations (Generations/MaxGenerations) until mutation of utilizations become enabled.", new PercentValue(0.5)));
62      Parameters.Add(new ValueLookupParameter<PercentValue>("ProbabilityOfCombinedMutation", "Probability of mutating both utilization and transport. Don't set to 100% otherwise intermediates are not touched anymore.", new PercentValue(0.3)));
63      foreach (var op in ApplicationManager.Manager.GetInstances<IRealVectorManipulator>()) {
64        op.BoundsParameter.Value = new DoubleMatrix(new double[,] {{0, 1}});
65        FeedstockUtilizationManipulatorParameter.ValidValues.Add(op);
66      }
67      foreach (var op in ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>()) {
68        var bop = op as BoundedIntegerVectorManipulator;
69        if (bop != null)
70          bop.BoundsParameter.ActualName = RegionBoundsParameter.ActualName;
71        if (op.Parameters.ContainsKey("Bounds") && op.Parameters["Bounds"] is ILookupParameter<IntMatrix>) {
72          ((ILookupParameter<IntMatrix>) op.Parameters["Bounds"]).ActualName = RegionBoundsParameter.ActualName;
73        }
74        TransportTargetManipulatorParameter.ValidValues.Add(op);
75      }
76      FeedstockUtilizationManipulatorParameter.Value =
77        FeedstockUtilizationManipulatorParameter.ValidValues.FirstOrDefault(
78          c => c.GetType() == typeof (MultiRealVectorManipulator));
79      TransportTargetManipulatorParameter.Value =
80        TransportTargetManipulatorParameter.ValidValues.FirstOrDefault(
81          c => c.GetType() == typeof (MultiIntegerVectorManipulator));
82      var um = (MultiRealVectorManipulator) FeedstockUtilizationManipulator;
83      foreach (var op in um.Operators) {
84        if (op is PolynomialAllPositionManipulator) {
85          ((PolynomialAllPositionManipulator) op).ContiguityParameter.Value = new DoubleValue(20);
86        } else if (op is PolynomialOnePositionManipulator) {
87          ((PolynomialOnePositionManipulator) op).ContiguityParameter.Value = new DoubleValue(8);
88        } else if (op is UniformOnePositionRealVectorManipulator) {
89          // nothing to do
90        } else {
91          um.Operators.SetItemCheckedState(op, false);
92        }
93      }
94      var tm = (MultiIntegerVectorManipulator) TransportTargetManipulator;
95      foreach (var op in tm.Operators) {
96        if (op is UniformSomePositionsManipulator ||
97            op is UniformOnePositionManipulator ||
98            op is CommonTargetIntegerVectorMutator ||
99            op is CommonTargetSwappingIntegerVectorMutator ||
100            op is TargetMergingIntegerVectorManipulator ||
101            op is DistanceBasedIntegerVectorMutator) {
102          // accept
103        } else {
104          tm.Operators.SetItemCheckedState(op, false);
105        }
106      }
107    }
108
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new CompoundMutator(this, cloner);
111    }
112
113    [StorableHook(HookType.AfterDeserialization)]
114    private void AfterDeserialization() {
115      if (!Parameters.ContainsKey("MaximumGenerations"))
116        Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations."));
117      if (!Parameters.ContainsKey("Generations"))
118        Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
119      if (!Parameters.ContainsKey("MinGenerationPercentageForUtilzationMutation"))
120        Parameters.Add(new ValueLookupParameter<PercentValue>("MinGenerationPercentageForUtilzationMutation", "The minimum percentage of generations (Generations/MaxGenerations) until mutation of utilizations become enabled.", new PercentValue(0.5)));
121      if (!Parameters.ContainsKey("ProbabilityOfCombinedMutation"))
122        Parameters.Add(new ValueLookupParameter<PercentValue>("ProbabilityOfCombinedMutation", "Probability of mutating both utilization and transport. Don't set to 100% otherwise intermediates are not touched anymore.", new PercentValue(0.3)));
123    }
124    #endregion
125
126    public override IOperation Apply() {
127      var ops = new OperationCollection(base.Apply());
128      var targets = ProblemData.TransportTargets.CheckedItems.Select(t => t.Value.Value).ToArray();
129      var utilizations = ProblemData.Utilizations.CheckedItems.Select(t => t.Value.Value).ToArray();
130      var feedstockUtilizationManipulator = FeedstockUtilizationManipulator;
131      if (1.0*Generations/MaximumGenerations < MinGenerationPercentageForUtilzationMutation && TransportTargetManipulator != null) {
132        feedstockUtilizationManipulator = null;
133      }
134      if (feedstockUtilizationManipulator == null && TransportTargetManipulator == null) {
135        // do nothing
136      } else if (feedstockUtilizationManipulator == null) {
137        ops.AddRange(CreateTransportMutation(targets[Random.Next(targets.Length)]));
138      } else if (TransportTargetManipulator == null) {
139        ops.AddRange(CreateUtilizationMutation(utilizations[Random.Next(utilizations.Length)]));
140      } else {
141        if (Random.Next() > ProbabilityOfCombinedMutation) {
142          int n = utilizations.Length;
143          int r = Random.Next(n + targets.Length);
144          ops.AddRange(
145            r < n
146              ? CreateUtilizationMutation(utilizations[r])
147              : CreateTransportMutation(targets[r - n]));
148        } else {
149          var feedstocks = new HashSet<string>(utilizations.Concat(targets)).ToArray();
150          var feedstock = feedstocks[Random.Next(feedstocks.Length)];
151          ops.AddRange(CreateUtilizationMutation(feedstock));
152          ops.AddRange(CreateTransportMutation(feedstock));
153        }
154      }
155      return ops;
156    }
157
158    private IEnumerable<IOperation> CreateUtilizationMutation(string name) {
159      yield return ExecutionContext.CreateChildOperation(new FeedstockDeflater {ProductName = name});
160      var u = (IRealVectorManipulator) FeedstockUtilizationManipulatorParameter.ActualValue.Clone();
161      u.RealVectorParameter.ActualName = LayerDescriptor.Utilizations.NameWithPrefix(name);
162      yield return ExecutionContext.CreateChildOperation(u);
163      yield return ExecutionContext.CreateChildOperation(new FeedstockInflater {ProductName = name});
164    }
165
166    private IEnumerable<IOperation> CreateTransportMutation(string name) {
167      yield return ExecutionContext.CreateChildOperation(new FeedstockDeflater {ProductName = name});
168      var t = (IIntegerVectorManipulator)TransportTargetManipulator.Clone();
169      t.IntegerVectorParameter.ActualName = LayerDescriptor.TransportTargets.NameWithPrefix(name);
170      yield return ExecutionContext.CreateChildOperation(t);
171      yield return ExecutionContext.CreateChildOperation(new FeedstockInflater {ProductName = name});
172    }
173
174  }
175}
Note: See TracBrowser for help on using the repository browser.