Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/CompoundMutator.cs @ 13071

Last change on this file since 13071 was 13071, checked in by gkronber, 9 years ago

#2499: added license headers and removed unused usings

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