Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/MultiTrajectory/GridRealVectorsCreator.cs @ 15071

Last change on this file since 15071 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis.FitnessLandscape {
34
35  [Item("GridRealVectorsCreator", "An operator that creates solutions on a grid accross the whole solution space.")]
36  [StorableClass]
37  public sealed class GridRealVectorCreator : SingleSuccessorOperator {
38
39    #region Parameters
40    public ValueLookupParameter<IntValue> NumberOfSolutionsParameter {
41      get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfSolutions"]; }
42    }
43    public LookupParameter<IntValue> ProblemSizeParameter {
44      get { return (LookupParameter<IntValue>)Parameters["ProblemSize"]; }
45    }
46    public ValueLookupParameter<DoubleMatrix> BoundsParameter {
47      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
48    }
49    private ScopeParameter CurrentScopeParameter {
50      get { return (ScopeParameter)Parameters["CurrentScope"]; }
51    }
52    public LookupParameter<IRandom> RandomParameter {
53      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
54    }
55    public LookupParameter<RealVector> RealVectorParameter {
56      get { return (LookupParameter<RealVector>)Parameters["RealVector"]; }
57    }
58    #endregion
59
60    #region Parameter Values
61    public IScope CurrentScope {
62      get { return CurrentScopeParameter.ActualValue; }
63    }
64    public int NumberOfSolutions {
65      get { return NumberOfSolutionsParameter.ActualValue.Value; }
66    }
67    public DoubleMatrix Bounds {
68      get { return BoundsParameter.ActualValue; }
69    }
70    public IRandom Random {
71      get { return RandomParameter.ActualValue; }
72    }
73    public int ProblemSize {
74      get { return ProblemSizeParameter.ActualValue.Value; }
75    }
76    #endregion
77
78    #region Construction & Cloning
79    [StorableConstructor]
80    private GridRealVectorCreator(bool deserializing) : base(deserializing) { }
81    private GridRealVectorCreator(GridRealVectorCreator original, Cloner cloner) : base(original, cloner) { }
82    public GridRealVectorCreator()
83      : base() {
84      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSolutions", "The number of solutions that should be created."));
85      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The minimum and maximum values in each dimension."));
86      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
87      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for sampling large grids."));
88      Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The number of dimensions"));
89      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real vector to be created in every scope."));
90      RealVectorParameter.ActualName = "Point";
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new GridRealVectorCreator(this, cloner);
95    }
96    #endregion
97
98    public override IOperation Apply() {
99      string realVectorName = RealVectorParameter.TranslatedName;
100      int current = CurrentScope.SubScopes.Count;
101      for (int i = 0; i < NumberOfSolutions; i++)
102        CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
103      double[] ranges = Enumerable.Range(0, Bounds.Rows).Select(i => Math.Abs(Bounds[i, 0]-Bounds[i, 1])).ToArray();
104      double[] offsets = Enumerable.Range(0, Bounds.Rows).Select(i => Math.Min(Bounds[i, 0], Bounds[i, 1])).ToArray();
105      foreach (RealVector vector in GenerateGridSample(ProblemSize, NumberOfSolutions)) {
106        for (int i = 0; i<ProblemSize; i++) {
107          vector[i] = vector[i] * ranges[i % ranges.Length] + offsets[i % ranges.Length];
108        }
109        CurrentScope.SubScopes[current].Variables.Add(new Variable(realVectorName, vector));
110        current++;
111      }
112      return base.Apply();
113    }
114
115    public IEnumerable<RealVector> GenerateGridSample(int nDim, int nVectors) {
116      int nPoints = (int)Math.Max(Math.Ceiling(Math.Pow(nVectors, 1.0/nDim)), 3);
117      double nPossibleSamples = Math.Pow(nPoints, nDim);
118      if (nVectors > nPossibleSamples)
119        throw new InvalidOperationException(string.Format("Number of samples ({0}) exceeds number of possible samples ({1})", nVectors, nPossibleSamples));
120      if (nPossibleSamples < nVectors * 2) {
121        if (nPossibleSamples > (double)int.MaxValue)
122          throw new InvalidOperationException("Cannot exhaustivly generate more than int.MaxValue samples");
123        List<RealVector> vectors = new List<RealVector>();
124        for (int i = 0; i<nPossibleSamples; i++) {
125          RealVector newVector = new RealVector(nDim);
126          int index = i;
127          for (int j = 0; j<nDim; j++) {
128            newVector[j] = 1.0 * (index % nPoints) / (nPoints-1);
129            index /= nPoints;
130          }
131          vectors.Add(newVector);
132        }
133        while (vectors.Count > nVectors) {
134          vectors.RemoveAt(Random.Next(vectors.Count));
135        }
136        return vectors;
137      } else {
138        HashSet<RealVector> vectors = new HashSet<RealVector>();
139        while (vectors.Count < nVectors) {
140          RealVector newVector = new RealVector(nDim);
141          for (int i = 0; i<nDim; i++) {
142            newVector[i] = 1.0 * Random.Next(nPoints) / (nPoints-1);
143          }
144          vectors.Add(newVector);
145        }
146        return vectors;
147      }
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.