Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/RandomInitializer.cs @ 8471

Last change on this file since 8471 was 8471, checked in by abeham, 12 years ago

#1913: integrated branch into trunk

File size: 3.2 KB
RevLine 
[8425]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Random;
30
[8471]31namespace HeuristicLab.Algorithms.DataAnalysis {
[8425]32  [Item("Random", "Initializes the matrix randomly.")]
33  [StorableClass]
34  public class RandomInitializer : ParameterizedNamedItem, INCAInitializer {
35    private IValueParameter<IntValue> RandomParameter {
36      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
37    }
38    private IValueParameter<BoolValue> SetSeedRandomlyParameter {
39      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
40    }
41
42    public int Seed {
43      get { return RandomParameter.Value.Value; }
44      set { RandomParameter.Value.Value = value; }
45    }
46
47    public bool SetSeedRandomly {
48      get { return SetSeedRandomlyParameter.Value.Value; }
49      set { SetSeedRandomlyParameter.Value.Value = value; }
50    }
51
52    [StorableConstructor]
53    protected RandomInitializer(bool deserializing) : base(deserializing) { }
54    protected RandomInitializer(RandomInitializer original, Cloner cloner) : base(original, cloner) { }
55    public RandomInitializer()
56      : base() {
57      Parameters.Add(new ValueParameter<IntValue>("Seed", "The seed for the random number generator.", new IntValue(0)));
58      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "Whether the seed should be randomized for each call.", new BoolValue(true)));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new RandomInitializer(this, cloner);
63    }
64
65    public double[] Initialize(IClassificationProblemData data, int dimensions) {
66      var instances = data.TrainingIndices.Count();
67      var attributes = data.AllowedInputVariables.Count();
68
69      var random = new MersenneTwister();
70      if (SetSeedRandomly) Seed = random.Next();
71      random.Reset(Seed);
72
73      var range = data.AllowedInputVariables.Select(x => data.Dataset.GetDoubleValues(x).Max() - data.Dataset.GetDoubleValues(x).Min()).ToArray();
74      var matrix = new double[attributes * dimensions];
75      for (int i = 0; i < matrix.Length; i++)
[8437]76        matrix[i] = random.NextDouble() / range[i / dimensions];
[8425]77
78      return matrix;
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.