1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization {
|
---|
30 | [Item("VectorEncoding", "Represents a vector-based encoding.")]
|
---|
31 | [StorableType("fed1e058-6e47-4e0f-af0a-ff646a9a778b")]
|
---|
32 | public abstract class VectorEncoding : Encoding {
|
---|
33 | [Storable] public IFixedValueParameter<IntValue> LengthParameter { get; private set; }
|
---|
34 |
|
---|
35 | public int Length {
|
---|
36 | get { return LengthParameter.Value.Value; }
|
---|
37 | set {
|
---|
38 | if (Length == value) return;
|
---|
39 | if (Length <= 0) throw new ArgumentException("Length must be > 0", "value");
|
---|
40 | LengthParameter.Value.Value = value;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected VectorEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
46 | [StorableHook(HookType.AfterDeserialization)]
|
---|
47 | private void AfterDeserialization() {
|
---|
48 | RegisterEventHandlers();
|
---|
49 | }
|
---|
50 | protected VectorEncoding(VectorEncoding original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | LengthParameter = cloner.Clone(original.LengthParameter);
|
---|
53 | RegisterEventHandlers();
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected VectorEncoding() : this("Vector", 10) { }
|
---|
57 | protected VectorEncoding(string name) : this(name, 10) { }
|
---|
58 | protected VectorEncoding(int length) : this("Vector", length) { }
|
---|
59 | protected VectorEncoding(string name, int length)
|
---|
60 | : base(name) {
|
---|
61 | Parameters.Add(LengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length)));
|
---|
62 | RegisterEventHandlers();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void RegisterEventHandlers() {
|
---|
66 | IntValueParameterChangeHandler.Create(LengthParameter, OnLengthChanged);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public event EventHandler LengthChanged;
|
---|
70 | protected virtual void OnLengthChanged() {
|
---|
71 | LengthChanged?.Invoke(this, EventArgs.Empty);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|