diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java')
-rw-r--r-- | src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java | 109 |
1 files changed, 64 insertions, 45 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java b/src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java index 9d866ecc..58d1c886 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/referencepoints/RandomGeneratedReferencePoints.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.utilities.referencepoints; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2013 + Copyright (C) 2014 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -29,86 +29,70 @@ import java.util.Collection; import de.lmu.ifi.dbs.elki.data.NumberVector; import de.lmu.ifi.dbs.elki.database.relation.Relation; import de.lmu.ifi.dbs.elki.database.relation.RelationUtil; -import de.lmu.ifi.dbs.elki.utilities.DatabaseUtil; +import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector; +import de.lmu.ifi.dbs.elki.math.random.RandomFactory; import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer; import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID; import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.CommonConstraints; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.DoubleParameter; import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.IntParameter; -import de.lmu.ifi.dbs.elki.utilities.pairs.Pair; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter; /** * Reference points generated randomly within the used data space. * * @author Erich Schubert - * - * @param <V> Object type */ -// TODO: Erich: use reproducible random -public class RandomGeneratedReferencePoints<V extends NumberVector<?>> implements ReferencePointsHeuristic<V> { - /** - * Parameter to specify the number of requested reference points. - * <p> - * Key: {@code -generate.n} - * </p> - */ - public static final OptionID N_ID = new OptionID("generate.n", "The number of reference points to be generated."); - +public class RandomGeneratedReferencePoints implements ReferencePointsHeuristic { /** - * Parameter for additional scaling of the space, to allow out-of-space - * reference points. - * <p> - * Key: {@code -generate.scale} - * </p> + * Holds the sample size. */ - public static final OptionID SCALE_ID = new OptionID("generate.scale", "Scale the grid by the given factor. This can be used to obtain reference points outside the used data space."); + protected int samplesize; /** - * Holds the value of {@link #N_ID}. + * Holds the scaling factor. */ - protected int samplesize; + protected double scale = 1.0; /** - * Holds the value of {@link #SCALE_ID}. + * Random generator. */ - protected double scale = 1.0; + protected RandomFactory rnd; /** * Constructor. * * @param samplesize Size of desired sample set * @param scale Scaling factor + * @param rnd Random generator */ - public RandomGeneratedReferencePoints(int samplesize, double scale) { + public RandomGeneratedReferencePoints(int samplesize, double scale, RandomFactory rnd) { super(); this.samplesize = samplesize; this.scale = scale; + this.rnd = rnd; } @Override - public <T extends V> Collection<V> getReferencePoints(Relation<T> db) { - Relation<V> database = DatabaseUtil.relationUglyVectorCast(db); - Pair<V, V> minmax = DatabaseUtil.computeMinMax(database); - NumberVector.Factory<V, ?> factory = RelationUtil.getNumberVectorFactory(database); - + public Collection<? extends NumberVector> getReferencePoints(Relation<? extends NumberVector> db) { + double[][] minmax = RelationUtil.computeMinMax(db); int dim = RelationUtil.dimensionality(db); - // Compute mean from minmax. - double[] mean = new double[dim]; - double[] delta = new double[dim]; + // Compute mean and extend from minmax. + double[] mean = minmax[0], delta = minmax[1]; for(int d = 0; d < dim; d++) { - mean[d] = (minmax.first.doubleValue(d + 1) + minmax.second.doubleValue(d + 1)) * .5; - delta[d] = (minmax.second.doubleValue(d + 1) - minmax.first.doubleValue(d + 1)); + delta[d] -= mean[d]; + mean[d] -= delta[d] * .5; } - ArrayList<V> result = new ArrayList<>(samplesize); + ArrayList<Vector> result = new ArrayList<>(samplesize); double[] vec = new double[dim]; for(int i = 0; i < samplesize; i++) { for(int d = 0; d < dim; d++) { vec[d] = mean[d] + (Math.random() - 0.5) * scale * delta[d]; } - V newp = factory.newNumberVector(vec); + Vector newp = new Vector(vec); // logger.debug("New reference point: " + FormatUtil.format(vec)); result.add(newp); } @@ -123,7 +107,32 @@ public class RandomGeneratedReferencePoints<V extends NumberVector<?>> implement * * @apiviz.exclude */ - public static class Parameterizer<V extends NumberVector<?>> extends AbstractParameterizer { + public static class Parameterizer extends AbstractParameterizer { + /** + * Parameter to specify the number of requested reference points. + * <p> + * Key: {@code -generate.n} + * </p> + */ + public static final OptionID N_ID = new OptionID("generate.n", "The number of reference points to be generated."); + + /** + * Parameter for additional scaling of the space, to allow out-of-space + * reference points. + * <p> + * Key: {@code -generate.scale} + * </p> + */ + public static final OptionID SCALE_ID = new OptionID("generate.scale", "Scale the grid by the given factor. This can be used to obtain reference points outside the used data space."); + + /** + * Parameter to specify the sample size. + * <p> + * Key: {@code -generate.random} + * </p> + */ + public static final OptionID RANDOM_ID = new OptionID("generate.random", "Random generator seed."); + /** * Holds the value of {@link #N_ID}. */ @@ -134,26 +143,36 @@ public class RandomGeneratedReferencePoints<V extends NumberVector<?>> implement */ protected double scale = 1.0; + /** + * Random generator. + */ + protected RandomFactory rnd; + @Override protected void makeOptions(Parameterization config) { super.makeOptions(config); - IntParameter samplesizeP = new IntParameter(N_ID); - samplesizeP.addConstraint(CommonConstraints.GREATER_EQUAL_ONE_INT); + IntParameter samplesizeP = new IntParameter(N_ID) // + .addConstraint(CommonConstraints.GREATER_EQUAL_ONE_INT); if(config.grab(samplesizeP)) { samplesize = samplesizeP.getValue(); } - DoubleParameter scaleP = new DoubleParameter(SCALE_ID, 1.0); - scaleP.addConstraint(CommonConstraints.GREATER_THAN_ZERO_DOUBLE); + DoubleParameter scaleP = new DoubleParameter(SCALE_ID, 1.0) // + .addConstraint(CommonConstraints.GREATER_THAN_ZERO_DOUBLE); if(config.grab(scaleP)) { scale = scaleP.getValue(); } + + RandomParameter randomP = new RandomParameter(RANDOM_ID); + if(config.grab(randomP)) { + rnd = randomP.getValue(); + } } @Override - protected RandomGeneratedReferencePoints<V> makeInstance() { - return new RandomGeneratedReferencePoints<>(samplesize, scale); + protected RandomGeneratedReferencePoints makeInstance() { + return new RandomGeneratedReferencePoints(samplesize, scale, rnd); } } } |