Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs @ 11559

Last change on this file since 11559 was 11559, checked in by mkommend, 10 years ago

#2174: Adapted IEncoding and Encoding base class.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.Programmable {
32  [Item("IntegerEncoding", "Describes an integer vector encoding.")]
33  [StorableClass]
34  public class IntegerEncoding : Encoding<IIntegerVectorCreator> {
35    [Storable]
36    private IntValue length;
37    public IntValue Length {
38      get { return length; }
39      set {
40        if (length == value) return;
41        length = value;
42        OnParameterConfigurationChanged();
43      }
44    }
45
46    [Storable]
47    private IntMatrix bounds;
48    public IntMatrix Bounds {
49      get { return bounds; }
50      set {
51        if (bounds == value) return;
52        bounds = value;
53        OnParameterConfigurationChanged();
54      }
55    }
56
57    [StorableConstructor]
58    protected IntegerEncoding(bool deserializing) : base(deserializing) { }
59    protected IntegerEncoding(IntegerEncoding original, Cloner cloner)
60      : base(original, cloner) {
61      length = cloner.Clone(original.length);
62      bounds = cloner.Clone(original.bounds);
63    }
64    public IntegerEncoding(string name, int length, int min, int max, int? step = null)
65      : base(name) {
66      if (min >= max) throw new ArgumentException("min must be less than max", "min");
67      if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step");
68      this.length = new IntValue(length);
69      bounds = new IntMatrix(1, step.HasValue ? 3 : 2);
70      bounds[0, 0] = min;
71      bounds[0, 1] = max;
72      if (step.HasValue) bounds[0, 2] = step.Value;
73    }
74    public IntegerEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)
75      : base(name) {
76      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters.");
77      if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
78      if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step");
79      if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min");
80      this.length = new IntValue(length);
81      bounds = new IntMatrix(min.Count, step != null ? 3 : 2);
82      for (int i = 0; i < min.Count; i++) {
83        bounds[i, 0] = min[i];
84        bounds[i, 1] = max[i];
85        if (step != null) bounds[i, 2] = step[i];
86      }
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new IntegerEncoding(this, cloner);
91    }
92
93  }
94}
Note: See TracBrowser for help on using the repository browser.