#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("GridRealVectorsCreator", "An operator that creates solutions on a grid accross the whole solution space.")]
[StorableClass]
public sealed class GridRealVectorCreator : SingleSuccessorOperator {
#region Parameters
public ValueLookupParameter NumberOfSolutionsParameter {
get { return (ValueLookupParameter)Parameters["NumberOfSolutions"]; }
}
public LookupParameter ProblemSizeParameter {
get { return (LookupParameter)Parameters["ProblemSize"]; }
}
public ValueLookupParameter BoundsParameter {
get { return (ValueLookupParameter)Parameters["Bounds"]; }
}
private ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
public LookupParameter RandomParameter {
get { return (LookupParameter)Parameters["Random"]; }
}
public LookupParameter RealVectorParameter {
get { return (LookupParameter)Parameters["RealVector"]; }
}
#endregion
#region Parameter Values
public IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
public int NumberOfSolutions {
get { return NumberOfSolutionsParameter.ActualValue.Value; }
}
public DoubleMatrix Bounds {
get { return BoundsParameter.ActualValue; }
}
public IRandom Random {
get { return RandomParameter.ActualValue; }
}
public int ProblemSize {
get { return ProblemSizeParameter.ActualValue.Value; }
}
#endregion
#region Construction & Cloning
[StorableConstructor]
private GridRealVectorCreator(bool deserializing) : base(deserializing) { }
private GridRealVectorCreator(GridRealVectorCreator original, Cloner cloner) : base(original, cloner) { }
public GridRealVectorCreator()
: base() {
Parameters.Add(new ValueLookupParameter("NumberOfSolutions", "The number of solutions that should be created."));
Parameters.Add(new ValueLookupParameter("Bounds", "The minimum and maximum values in each dimension."));
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
Parameters.Add(new LookupParameter("Random", "Random number generator for sampling large grids."));
Parameters.Add(new LookupParameter("ProblemSize", "The number of dimensions"));
Parameters.Add(new LookupParameter("RealVector", "The real vector to be created in every scope."));
RealVectorParameter.ActualName = "Point";
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GridRealVectorCreator(this, cloner);
}
#endregion
public override IOperation Apply() {
string realVectorName = RealVectorParameter.TranslatedName;
int current = CurrentScope.SubScopes.Count;
for (int i = 0; i < NumberOfSolutions; i++)
CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
double[] ranges = Enumerable.Range(0, Bounds.Rows).Select(i => Math.Abs(Bounds[i, 0]-Bounds[i, 1])).ToArray();
double[] offsets = Enumerable.Range(0, Bounds.Rows).Select(i => Math.Min(Bounds[i, 0], Bounds[i, 1])).ToArray();
foreach (RealVector vector in GenerateGridSample(ProblemSize, NumberOfSolutions)) {
for (int i = 0; i GenerateGridSample(int nDim, int nVectors) {
int nPoints = (int)Math.Max(Math.Ceiling(Math.Pow(nVectors, 1.0/nDim)), 3);
double nPossibleSamples = Math.Pow(nPoints, nDim);
if (nVectors > nPossibleSamples)
throw new InvalidOperationException(string.Format("Number of samples ({0}) exceeds number of possible samples ({1})", nVectors, nPossibleSamples));
if (nPossibleSamples < nVectors * 2) {
if (nPossibleSamples > (double)int.MaxValue)
throw new InvalidOperationException("Cannot exhaustivly generate more than int.MaxValue samples");
List vectors = new List();
for (int i = 0; i nVectors) {
vectors.RemoveAt(Random.Next(vectors.Count));
}
return vectors;
} else {
HashSet vectors = new HashSet();
while (vectors.Count < nVectors) {
RealVector newVector = new RealVector(nDim);
for (int i = 0; i