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