1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Fossil;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization {
|
---|
32 | [StorableType("43619638-9D00-4951-8138-8CCD0786E784")]
|
---|
33 | public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
|
---|
34 | private List<IEncoding> encodings = new List<IEncoding>();
|
---|
35 | [Storable(Name = "Encodings")]
|
---|
36 | private IEnumerable<IEncoding> StorableEncodings {
|
---|
37 | get { return encodings; }
|
---|
38 | set { encodings = new List<IEncoding>(value); }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) {
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone));
|
---|
48 | foreach (var encoding in encodings)
|
---|
49 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected MultiEncodingOperator() : base() { }
|
---|
53 |
|
---|
54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
55 | private void AfterDeserialization() {
|
---|
56 | foreach (var encoding in encodings)
|
---|
57 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | public override IOperation Apply() {
|
---|
62 | var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
|
---|
63 | return new OperationCollection(operations);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public virtual void AddEncoding(IEncoding encoding) {
|
---|
67 | if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
|
---|
68 |
|
---|
69 | encodings.Add(encoding);
|
---|
70 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
71 |
|
---|
72 | var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
|
---|
73 | param.Value = param.ValidValues.First();
|
---|
74 | Parameters.Add(param);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public virtual bool RemoveEncoding(IEncoding encoding) {
|
---|
78 | if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
79 | encoding.OperatorsChanged -= Encoding_OperatorsChanged;
|
---|
80 | return Parameters.Remove(encoding.Name);
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
|
---|
84 | if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
85 |
|
---|
86 | return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void Encoding_OperatorsChanged(object sender, EventArgs e) {
|
---|
90 | var encoding = (IEncoding)sender;
|
---|
91 | var param = GetParameter(encoding);
|
---|
92 |
|
---|
93 | var oldParameterValue = param.Value;
|
---|
94 | param.ValidValues.Clear();
|
---|
95 | foreach (var op in encoding.Operators.OfType<T>())
|
---|
96 | param.ValidValues.Add(op);
|
---|
97 |
|
---|
98 | var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
|
---|
99 | if (newValue == null) newValue = param.ValidValues.First();
|
---|
100 | param.Value = newValue;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|