1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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;
|
---|
23 | using System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.Modifiers {
|
---|
32 | [StorableType("28866F64-5BB4-41A0-95E9-7DF9A7C0B376")]
|
---|
33 | [Item("ClusteringTranscodingProblemModifier", "A problem modifier that groups input parameters into clusters and sets them to equal values. Values assigned to cluster -1 will be fixed to their default values")]
|
---|
34 | public class ClusteringTranscodingProblemModifier : TranscodingProblemModifier {
|
---|
35 | #region ParameterNames
|
---|
36 | private const string DefaultVectorParameterName = "DefaultVector";
|
---|
37 | private const string ClusterAssignmentParameterName = "ClusterAssignment";
|
---|
38 | #endregion
|
---|
39 |
|
---|
40 | #region Parameters
|
---|
41 | public IValueParameter<IntegerVector> DefaultVectorParameter => (IValueParameter<IntegerVector>)Parameters[DefaultVectorParameterName];
|
---|
42 | public IValueParameter<IntegerVector> ClusterAssignmentParameter => (IValueParameter<IntegerVector>)Parameters[ClusterAssignmentParameterName];
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region ParameterNames
|
---|
46 | public IntegerVector DefaultVector {
|
---|
47 | get => DefaultVectorParameter.Value;
|
---|
48 | set => DefaultVectorParameter.Value = value;
|
---|
49 | }
|
---|
50 | public IntegerVector ClusterAssignment {
|
---|
51 | get => ClusterAssignmentParameter.Value;
|
---|
52 | set => ClusterAssignmentParameter.Value = value;
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | #region Constructors & Cloning
|
---|
57 | [StorableConstructor]
|
---|
58 | protected ClusteringTranscodingProblemModifier(StorableConstructorFlag _) : base(_) { }
|
---|
59 | protected ClusteringTranscodingProblemModifier(ClusteringTranscodingProblemModifier original, Cloner cloner) : base(original, cloner) {
|
---|
60 | RegisterEvents();
|
---|
61 | }
|
---|
62 | public ClusteringTranscodingProblemModifier() {
|
---|
63 | Parameters.Add(new ValueParameter<IntegerVector>(DefaultVectorParameterName, "The default values for every dimension of the original encoding", new IntegerVector(2)));
|
---|
64 | Parameters.Add(new ValueParameter<IntegerVector>(ClusterAssignmentParameterName, "The cluster assignments for every dimension of the original encoding", new IntegerVector(2)));
|
---|
65 | RegisterEvents();
|
---|
66 | }
|
---|
67 | [StorableHook(HookType.AfterDeserialization)]
|
---|
68 | private void AfterDeserialization() { RegisterEvents(); }
|
---|
69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
70 | return new ClusteringTranscodingProblemModifier(this, cloner);
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region ParameterEvents
|
---|
75 | private void RegisterEvents() {
|
---|
76 | ClusterAssignmentParameter.ValueChanged -= ClusterAssignmentValueChanged;
|
---|
77 | ClusterAssignmentParameter.ValueChanged += ClusterAssignmentValueChanged;
|
---|
78 | ClusterAssignmentParameter.Value.ToStringChanged -= RedoEncoding;
|
---|
79 | ClusterAssignmentParameter.Value.ToStringChanged += RedoEncoding;
|
---|
80 | DefaultVectorParameter.ValueChanged -= DefaultVectorValueChanged;
|
---|
81 | DefaultVectorParameter.ValueChanged += DefaultVectorValueChanged;
|
---|
82 | DefaultVectorParameter.Value.ToStringChanged -= RedoEncoding;
|
---|
83 | DefaultVectorParameter.Value.ToStringChanged += RedoEncoding;
|
---|
84 | }
|
---|
85 | private void ClusterAssignmentValueChanged(object sender, EventArgs e) {
|
---|
86 | ClusterAssignmentParameter.Value.ToStringChanged -= RedoEncoding;
|
---|
87 | ClusterAssignmentParameter.Value.ToStringChanged += RedoEncoding;
|
---|
88 | RedoEncoding(sender, e);
|
---|
89 | }
|
---|
90 | private void DefaultVectorValueChanged(object sender, EventArgs e) {
|
---|
91 | DefaultVectorParameter.Value.ToStringChanged -= RedoEncoding;
|
---|
92 | DefaultVectorParameter.Value.ToStringChanged += RedoEncoding;
|
---|
93 | RedoEncoding(sender, e);
|
---|
94 | }
|
---|
95 | private void RedoEncoding(object sender, EventArgs e) {
|
---|
96 | InvokeEncodingChanged();
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region TranscodingProblemModifier
|
---|
101 |
|
---|
102 | protected override ISolutionCreator ChangeSolutionCreator(ISolutionCreator oldCreator) {
|
---|
103 | if (oldCreator == null || encoding == null) return null;
|
---|
104 |
|
---|
105 | var nc = Encoding.Operators.SingleOrDefault(o => o.GetType() == oldCreator.GetType());
|
---|
106 | return (ISolutionCreator)nc ?? throw new ArgumentException($"Could not transcode {oldCreator.GetType()}. Please extend {GetType()} to handle your specific solution creator.");
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override IEncoding ChangeEncoding(IEncoding oldEncoding) {
|
---|
111 | if (oldEncoding == null) {
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 | if (!(oldEncoding is IntegerVectorEncoding iEncoding)) throw new ArgumentException("This form of transcoding is only valid for IntegerVectorEncoding");
|
---|
115 | if (iEncoding.Length != ClusterAssignment.Length) throw new ArgumentException("ClusterAssignment and original Encoding need to have equal length");
|
---|
116 | if (iEncoding.Length != DefaultVector.Length) throw new ArgumentException("ClusterAssignment and default Vector need to have equal length");
|
---|
117 | var clusters = ClusterAssignment.Max() + 1;
|
---|
118 | var min = new int[clusters];
|
---|
119 | var max = new int[clusters];
|
---|
120 | var steps = new int[clusters];
|
---|
121 | var b = new bool[clusters];
|
---|
122 | for (var i = 0; i < iEncoding.Length; i++) {
|
---|
123 | var t = ClusterAssignment[i];
|
---|
124 | if (t < 0) continue;
|
---|
125 | var i2 = i % iEncoding.Length;
|
---|
126 | var iMin = iEncoding.Bounds[i2, 0];
|
---|
127 | var iMax = iEncoding.Bounds[i2, 1];
|
---|
128 | var iSteps = iEncoding.Bounds.Columns > 2 ? iEncoding.Bounds[i2, 2] : 1; //check
|
---|
129 | if (!b[t] || min[t] < iMin) min[t] = iMin; //biggest possible minimum
|
---|
130 | if (!b[t] || max[t] > iMax) max[t] = iMax; //smallest possible maximum
|
---|
131 | if (!b[t]) steps[t] = iSteps;
|
---|
132 | if (steps[t] != iSteps && b[t]) throw new ArgumentException("step sizes for variables within clusters must not differ");
|
---|
133 | b[t] = true;
|
---|
134 | }
|
---|
135 | if (!b.All(x => x)) throw new ArgumentException("At least one cluster is not mapped to any input variable");
|
---|
136 | return new IntegerVectorEncoding(iEncoding.Name, clusters, min, max, steps);
|
---|
137 | }
|
---|
138 | protected override Individual ChangeIndividual(Individual newIndividual) {
|
---|
139 | var intVec = newIndividual.IntegerVector();
|
---|
140 | var enc = newIndividual.GetEncoding<IntegerVectorEncoding>();
|
---|
141 | var individualScope = new Scope();
|
---|
142 | foreach (var v in newIndividual.Values) //intentional shallow copy
|
---|
143 | individualScope.Variables.Add(new Variable(v.Key, v.Value));
|
---|
144 | var oldIndividual = new SingleEncodingIndividual(enc, individualScope) { [enc.Name] = TransCode(intVec) };
|
---|
145 | return oldIndividual;
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Helpers
|
---|
150 | private IntegerVector TransCode(IntegerVector newVector) {
|
---|
151 | return new IntegerVector(ClusterAssignment.Select((v, i) => v >= 0 ? newVector[v] : DefaultVector[i]).ToArray());
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 | }
|
---|
155 | }
|
---|