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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
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++)
|
---|
76 | matrix[i] = random.NextDouble() / range[i / dimensions];
|
---|
77 |
|
---|
78 | return matrix;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|