Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Mutation/MultiMutator.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: 8.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.Reflection;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using System;
34using System.Collections.Generic;
35using System.Linq;
36
37namespace HeuristicLab.BioBoost.Operators.Mutation {
38  [StorableClass]
39  public class MultiMutator : StochasticMultiBranch<IManipulator>, IManipulator, IStochasticOperator {
40
41    public override bool CanChangeName { get { return false; } }
42
43    protected override bool CreateChildOperation { get { return true; } }
44
45    public IValueLookupParameter<IntValue> SeriesLengthParameter { get { return (IValueLookupParameter<IntValue>)Parameters["SeriesLength"]; } }
46    public ValueParameter<PercentArray> MinGenerationPercentagesParameter { get { return (ValueParameter<PercentArray>)Parameters["MinGenerationPercentages"]; } }
47    public ILookupParameter<IntValue> GenerationsParameter { get { return (ILookupParameter<IntValue>)Parameters["Generations"]; } }
48    public ILookupParameter<IntValue> MaxGenerationsParameter { get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; } }
49    public PercentArray MinGenerationPercentages {
50      get { return MinGenerationPercentagesParameter.Value; }
51      set { MinGenerationPercentagesParameter.Value = value; }
52    }
53
54    public int SeriesLength { get { return SeriesLengthParameter.ActualValue.Value; } }
55
56    #region Construction & Cloning
57    [StorableConstructor]
58    protected MultiMutator(bool isDeserializing) : base(isDeserializing) { }
59    protected MultiMutator(MultiMutator orig, Cloner cloner) : base(orig, cloner) { }
60    public MultiMutator() {
61      Parameters.Add(new ValueLookupParameter<IntValue>("SeriesLength", "The number of manipulations to perform at once", new IntValue(1)));
62      Parameters.Add(new ValueParameter<PercentArray>("MinGenerationPercentages", "Relative number of generations before the respetive mutation becomes active.", new PercentArray(0)));
63      Parameters.Add(new LookupParameter<IntValue>("Generations", "Current number of generations/iterations."));
64      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations/iterations."));
65      foreach (var t in ApplicationManager.Manager.GetTypes(typeof(IManipulator), Assembly.GetExecutingAssembly())) {
66        if (t == this.GetType() || typeof(IIntegerVectorManipulator).IsAssignableFrom(t)) continue;
67        IManipulator instance = null;
68        try {
69          instance = Activator.CreateInstance(t) as IManipulator;
70        } catch { }
71        if (instance != null) Operators.Add(instance);
72      }
73      for (int i = 0; i < Operators.Count; i++) {
74        if (Operators[i] is CompoundMutator ||
75            Operators[i] is PlantMerger ||
76            Operators[i] is PlantSplitter ||
77            Operators[i] is PlantMover) {
78          // everything is fine
79        } else if (Operators[i] is PlantSupplierToggler ||
80                   Operators[i] is PlantSupplierUtilizationExchanger ||
81                   Operators[i] is PlantSupplierEqualizer ||
82                   Operators[i] is PlantSupplierRandomizer ||
83                   Operators[i] is PlantScalingMutator) {
84          MinGenerationPercentages[i] = 0.5;
85        } else if (Operators[i] is EmptyMutator) {
86          MinGenerationPercentages[i] = 0.8;
87        } else if (Operators[i] is PlantKiller) {
88          MinGenerationPercentages[i] = 0.9;
89        } else {
90          Operators.SetItemCheckedState(Operators[i], false);
91        }
92      }
93    }
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new MultiMutator(this, cloner);
96    }
97
98    [StorableHook(HookType.AfterDeserialization)]
99    private void AfterDeserialization() {
100      if (!Parameters.ContainsKey("MinGenerationPercentages")) {
101        Parameters.Add(new ValueParameter<PercentArray>("MinGenerationPercentages", "Relative number of generations before the respetive mutation becomes active.", new PercentArray(Operators.Count)));
102      }
103      if (!Parameters.ContainsKey("Generations"))
104        Parameters.Add(new LookupParameter<IntValue>("Generations", "Current number of generations/iterations."));
105      if (!Parameters.ContainsKey("MaximumGenerations"))
106        Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations/iterations."));
107      if (!Parameters.ContainsKey("SeriesLength"))
108        Parameters.Add(new ValueLookupParameter<IntValue>("SeriesLength", "The number of manipulations to perform at once", new IntValue(1)));
109    }
110    #endregion
111
112    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IManipulator>> e) {
113      base.Operators_ItemsAdded(sender, e);
114      if (MinGenerationPercentages.Length >= Operators.Count) return;
115      var added = e.Items.OrderBy(x => x.Index).ToList();
116      int insertCount = 0;
117      var percentages = new PercentArray(Operators.Count);
118      for (int i = 0; i < Operators.Count; i++) {
119        if (insertCount < added.Count && i == added[insertCount].Index) {
120          percentages[i] = 0;
121          insertCount++;
122        } else if (i - insertCount < MinGenerationPercentages.Length) {
123          percentages[i] = MinGenerationPercentages[i - insertCount];
124        } else percentages[i] = 0;
125      }
126      MinGenerationPercentages = percentages;
127    }
128
129    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IManipulator>> e) {
130      base.Operators_ItemsRemoved(sender, e);
131      if (MinGenerationPercentages.Length <= Operators.Count) return;
132      var percentages = new List<double>(MinGenerationPercentages);
133      var sorted = e.Items.OrderByDescending(x => x.Index);
134      foreach (var item in sorted)
135        if (percentages.Count > item.Index) percentages.RemoveAt(item.Index);
136      MinGenerationPercentages = new PercentArray(percentages.ToArray());
137    }
138
139    public override IOperation InstrumentedApply() {
140      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one manipulator to choose from.");
141      if (MaxGenerationsParameter.ActualValue == null || GenerationsParameter.ActualValue == null || MinGenerationPercentages.Max() == 0)
142        return base.InstrumentedApply();
143      DoubleArray probability = null;
144      try {
145        var generationPercentage = 1.0 * GenerationsParameter.ActualValue.Value / MaxGenerationsParameter.ActualValue.Value;
146        for (int i = 0; i < Probabilities.Length; i++) {
147          if (MinGenerationPercentages[i] > generationPercentage) {
148            if (probability == null) probability = (DoubleArray)Probabilities.Clone(); // backup old probabilities
149            Probabilities[i] = 0;
150          }
151        }
152        if (SeriesLength == 1) {
153          return base.InstrumentedApply();
154        } else {
155          var operations = new OperationCollection();
156          for (var i = 0; i < SeriesLength; i++) {
157            operations.Add(base.InstrumentedApply());
158          }
159          return operations;
160        }
161      } finally {
162        // restore old probabilities
163        if (probability != null)
164          Probabilities = probability;
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.