Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs @ 16751

Last change on this file since 16751 was 16751, checked in by mkommend, 5 years ago

#2521: Renamed Solution to EncodedSolution.

File size: 3.2 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HEAL.Attic;
28
29namespace HeuristicLab.Encodings.RealVectorEncoding {
30  [StorableType("C4213FB3-77C3-4814-A7A3-0151B0FF5270")]
31  [Item("RealVector", "Represents a vector of real values.")]
32  public class RealVector : DoubleArray, IEncodedSolution {
33    [StorableConstructor]
34    protected RealVector(StorableConstructorFlag _) : base(_) { }
35    protected RealVector(RealVector original, Cloner cloner) : base(original, cloner) { }
36    public RealVector() : base() { }
37    public RealVector(int length) : base(length) { }
38    public RealVector(int length, IRandom random, double min, double max)
39      : this(length) {
40      Randomize(random, min, max);
41    }
42    public RealVector(double[] elements) : base(elements) { }
43    public RealVector(DoubleArray elements)
44      : this(elements.Length) {
45      for (int i = 0; i < array.Length; i++)
46        array[i] = elements[i];
47    }
48    public RealVector(RealVector other) : this(other.array) { }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new RealVector(this, cloner);
52    }
53
54    public virtual void Randomize(IRandom random, int startIndex, int length, double min, double max) {
55      double delta = max - min;
56      if (length > 0) {
57        for (int i = 0; i < length; i++)
58          array[startIndex + i] = min + delta * random.NextDouble();
59        OnReset();
60      }
61    }
62
63    public virtual void Randomize(IRandom random, int startIndex, int length, DoubleMatrix bounds) {
64      if (length > 0) {
65        for (int i = startIndex; i < startIndex + length; i++) {
66          double min = bounds[i % bounds.Rows, 0];
67          double max = bounds[i % bounds.Rows, 1];
68          array[i] = min + (max - min) * random.NextDouble();
69        }
70        OnReset();
71      }
72    }
73
74    public void Randomize(IRandom random, double min, double max) {
75      Randomize(random, 0, Length, min, max);
76    }
77
78    public void Randomize(IRandom random, DoubleMatrix bounds) {
79      Randomize(random, 0, Length, bounds);
80    }
81
82    public double DotProduct(RealVector other) {
83      if (other.Length != Length) throw new ArgumentException("Vectors are of unequal length.");
84      var dotProd = 0.0;
85      for (var i = 0; i < Length; i++)
86        dotProd += this[i] * other[i];
87      return dotProd;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.