Free cookie consent management tool by TermsFeed Policy Generator

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