List of usage examples for org.apache.commons.math.random MersenneTwister MersenneTwister
public MersenneTwister(long seed)
From source file:br.upe.ecomp.doss.algorithm.abc.ABC.java
/** * Each food source are initialized.//from w w w .j a va 2s. c o m */ private void initializeFoodSource(int index) { MersenneTwister mt = new MersenneTwister(System.nanoTime()); double randNumber; double fitness; double[] position = new double[dimensions]; for (int i = 0; i < dimensions; i++) { randNumber = mt.nextDouble() * 32767 / ((double) 32767 + (double) 1); position[i] = randNumber * (getProblem().getUpperBound(dimensions) - getProblem().getLowerBound(dimensions)) + getProblem().getLowerBound(dimensions); } fitness = getProblem().getFitness(position); foodSources[index] = new FoodSource(dimensions); foodSources[index].updateCurrentPosition(position, fitness); foodSources[index].updateBestPosition(position, fitness); foodSources[index].setCountStagnation(0); }
From source file:br.upe.ecomp.doss.algorithm.volitiveapso.WeightedAPSOParticle.java
/** * Updates the current position of the particle and the fitness variation. * //from w w w . j av a 2 s .c o m * @param problem The problem that we are trying to solve. */ public void updateCurrentPosition(Problem problem) { MersenneTwister random = new MersenneTwister(System.nanoTime()); double[] position = getCurrentPosition(); double[] nextPosition = new double[getDimensions()]; double newPosition; for (int i = 0; i < getDimensions(); i++) { newPosition = position[i] + getVelocity()[i]; // if a particle exceeds the search space limit, so inverts the particle velocity on // that dimension which the search space limit was exceeded if (newPosition >= problem.getUpperBound(i)) { position[i] = problem.getUpperBound(i) - getVelocity()[i] * random.nextDouble(); } else if (newPosition <= problem.getLowerBound(i)) { position[i] = problem.getLowerBound(i) - getVelocity()[i] * random.nextDouble(); } else { position[i] = newPosition; } nextPosition[i] = position[i]; } deltaFitness = problem.getFitness(nextPosition) - getCurrentFitness(); if (problem.isFitnessBetterThan(getCurrentFitness(), problem.getFitness(nextPosition)) && deltaFitness < 0) { deltaFitness *= -1; } // The particle never reduces its weight if (!problem.isFitnessBetterThan(getCurrentFitness(), problem.getFitness(nextPosition))) { deltaFitness = 0; } previousPosition = getCurrentPosition().clone(); updateCurrentPosition(nextPosition, problem.getFitness(nextPosition)); if (problem.isFitnessBetterThan(getBestFitness(), getCurrentFitness())) { updateBestPosition(getCurrentPosition(), getCurrentFitness()); } }
From source file:dr.evomodel.branchmodel.RandomBranchModel.java
public RandomBranchModel(TreeModel treeModel, // GY94CodonModel baseSubstitutionModel, // double rate, // boolean hasSeed, long seed) { super(RANDOM_BRANCH_MODEL); this.treeModel = treeModel; this.baseSubstitutionModel = baseSubstitutionModel; this.rate = rate; if (hasSeed) { // use fixed seed for e_i random = new MersenneTwister(seed); } else {//from w ww . j a v a 2 s.c o m //use BEAST seed random = new MersenneTwister(MathUtils.nextLong()); } //END: seed check setup(); }
From source file:br.upe.ecomp.doss.algorithm.abc.ABC.java
private void sendEmployedBees() { MersenneTwister random = new MersenneTwister(System.nanoTime()); double[] tempFood; double tempFitness; for (int i = 0; i < foodCount; i++) { double rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); int param2change = (int) (rand * dimensions); rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); int neighbour = (int) (rand * foodCount); tempFood = new double[dimensions]; for (int j = 0; j < dimensions; j++) { tempFood[j] = foodSources[i].getCurrentPosition()[j]; }// ww w . j av a 2s . c o m rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); tempFood[param2change] = foodSources[i].getCurrentPosition()[param2change] + (foodSources[i].getCurrentPosition()[param2change] - foodSources[neighbour].getCurrentPosition()[param2change]) * (rand - 0.5) * 2; if (tempFood[param2change] < getProblem().getLowerBound(dimensions)) tempFood[param2change] = getProblem().getLowerBound(dimensions); if (tempFood[param2change] > getProblem().getUpperBound(dimensions)) tempFood[param2change] = getProblem().getUpperBound(dimensions); tempFitness = getProblem().getFitness(tempFood); if (getProblem().isFitnessBetterThan(foodSources[i].getCurrentFitness(), tempFitness)) { foodSources[i].updateCurrentPosition(tempFood, tempFitness); foodSources[i].updateBestPosition(tempFood, tempFitness); foodSources[i].setCountStagnation(0); double bestSolutionFitness = getProblem().getFitness(solution); if (getProblem().isFitnessBetterThan(bestSolutionFitness, tempFitness)) { solution = tempFood; } } else { foodSources[i].setCountStagnation(foodSources[i].getCountStagnation() + 1); } } }
From source file:br.upe.ecomp.doss.algorithm.pso.PSOParticle.java
/** * Updates the current position of the particle. * //from ww w .j a va 2s. co m * @param problem The problem that we are trying to solve. */ public void updateCurrentPosition(Problem problem) { MersenneTwister random = new MersenneTwister(System.nanoTime()); double[] position = getCurrentPosition().clone(); double nextPosition; double positionLimiter = 0.5; for (int i = 0; i < getDimensions(); i++) { nextPosition = position[i] + velocity[i]; // if a particle exceeds the search space limit, so inverts the particle velocity on // that dimension which the search space limit was exceeded if (nextPosition >= problem.getUpperBound(i)) { position[i] = problem.getUpperBound(i) - velocity[i] * random.nextDouble() * positionLimiter; } else if (nextPosition <= problem.getLowerBound(i)) { position[i] = problem.getLowerBound(i) - velocity[i] * random.nextDouble() * positionLimiter; } else { position[i] = nextPosition; } } updateCurrentPosition(position, problem.getFitness(position)); }
From source file:br.upe.ecomp.doss.algorithm.bso.BSO.java
private void initializationPosition(int bee) { double[] position = new double[dimensions]; MersenneTwister random = new MersenneTwister(System.nanoTime()); double fitness; for (int i = 0; i < dimensions; i++) { double value = random.nextDouble(); position[i] = (getProblem().getUpperBound(i) - getProblem().getLowerBound(i)) * value + getProblem().getLowerBound(i); if (position[i] > getProblem().getUpperBound(i)) { position[i] = getProblem().getUpperBound(i); } else if (position[i] < getProblem().getLowerBound(i)) { position[i] = getProblem().getLowerBound(i); }/*from w w w.j av a2 s . c o m*/ } fitness = getProblem().getFitness(position); System.out.println("Current fitness in position: " + fitness); Bee beeAux = new Bee(dimensions); bees.add(bee, beeAux); bees.get(bee).updateCurrentPosition(position, fitness); bees.get(bee).updateBestPosition(position, fitness); }
From source file:br.upe.ecomp.doss.problem.movingpeaks.MovingPeaks.java
private double[] getMovingVector() { MersenneTwister random = new MersenneTwister(System.nanoTime()); double[] vector = new double[dimensions]; double movingLengthFraction = movingLength / dimensions; List<Integer> indexes = new ArrayList<Integer>(); for (int i = 0; i < dimensions; i++) { indexes.add(i);/*from ww w.ja v a 2 s . c o m*/ } int index; while (indexes.size() > 1) { index = indexes.remove(random.nextInt(indexes.size() - 1)); vector[index] = movingLengthFraction * random.nextDouble(); } vector[indexes.remove(0)] = movingLength - getLength(vector); return vector; }
From source file:dr.app.beagle.tools.Partition.java
public Partition(TreeModel treeModel, // BranchModel branchModel, // GammaSiteRateModel siteModel, // BranchRateModel branchRateModel, // FrequencyModel freqModel, // int from, // int to, // int every // ) {/*ww w. j av a2 s . co m*/ this.treeModel = treeModel; this.siteRateModel = siteModel; this.freqModel = freqModel; this.branchModel = branchModel; this.branchRateModel = branchRateModel; this.from = from; this.to = to; this.every = every; dataType = freqModel.getDataType(); partitionSiteCount = getPartitionSiteCount(); setBufferHelpers(); setSubstitutionModelDelegate(); loadBeagleInstance(); alignmentMap = new LinkedHashMap<Taxon, int[]>(); random = new MersenneTwister(MathUtils.nextLong()); }
From source file:br.upe.ecomp.doss.algorithm.abc.ABC.java
private void sendOnlookerBees() { MersenneTwister random = new MersenneTwister(System.nanoTime()); double[] tempFood; double tempFitness; int count = 0; int i = 0;//from www .ja v a 2s. com while (count < foodCount) { double rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); if (rand < foodSources[i].getProbabilitySelection()) { count++; rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); int param2change = (int) (rand * dimensions); rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); int neighbour = (int) (rand * foodCount); while (neighbour == i) { rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); neighbour = (int) (rand * foodCount); } tempFood = new double[dimensions]; for (int j = 0; j < dimensions; j++) { tempFood[j] = foodSources[i].getCurrentPosition()[j]; } rand = random.nextDouble() * 32767 / ((double) (32767) + (double) (1)); tempFood[param2change] = foodSources[i].getCurrentPosition()[param2change] + (foodSources[i].getCurrentPosition()[param2change] - foodSources[neighbour].getCurrentPosition()[param2change]) * (rand - 0.5) * 2; if (tempFood[param2change] < getProblem().getLowerBound(dimensions)) tempFood[param2change] = getProblem().getLowerBound(dimensions); if (tempFood[param2change] > getProblem().getUpperBound(dimensions)) tempFood[param2change] = getProblem().getUpperBound(dimensions); tempFitness = getProblem().getFitness(tempFood); if (getProblem().isFitnessBetterThan(foodSources[i].getCurrentFitness(), tempFitness)) { foodSources[i].updateCurrentPosition(tempFood, tempFitness); foodSources[i].updateBestPosition(tempFood, tempFitness); foodSources[i].setCountStagnation(0); double bestSolutionFitness = getProblem().getFitness(solution); if (getProblem().isFitnessBetterThan(bestSolutionFitness, tempFitness)) { solution = tempFood; } } else { foodSources[i].setCountStagnation(foodSources[i].getCountStagnation() + 1); } } i++; if (i == foodCount) i = 0; } }
From source file:edu.cornell.med.icb.goby.modes.ReadQualityStatsMode.java
/** * Display the alignments as text files. * * @throws java.io.IOException error reading / writing */// ww w. ja v a 2 s. c om @Override public void execute() throws IOException { PrintStream writer = null; try { numberOfSkippedReads = 0; numberOfObservedReads = 0; writer = outputFile == null ? System.out : new PrintStream(new FileOutputStream(outputFile)); final Int2ObjectMap<ReadQualityStats> qualityStats = new Int2ObjectOpenHashMap<ReadQualityStats>(); final ProgressLogger progress = new ProgressLogger(LOG); progress.start(); final MersenneTwister random = new MersenneTwister(37); final boolean doSample = sampleFraction < 1.0d; writer.println("basename\treadIndex\t25%-percentile\tmedian\taverageQuality\t75%-percentile"); for (final File filename : inputFiles) { final ReadsReader reader = new ReadsReader(filename); final String basename = ReadsReader.getBasename(filename.toString()); for (final Reads.ReadEntry entry : reader) { if (!doSample || random.nextDouble() < sampleFraction) { final ByteString qualityScores = entry.getQualityScores(); final int size = qualityScores.size(); for (int readIndex = 0; readIndex < size; readIndex++) { final byte code = qualityScores.byteAt(readIndex); ReadQualityStats stats = qualityStats.get(readIndex); if (stats == null) { stats = new ReadQualityStats(1.0d); qualityStats.put(readIndex, stats); stats.readIndex = readIndex; } stats.observe(code); } numberOfObservedReads++; } else { numberOfSkippedReads++; } progress.lightUpdate(); } for (final ReadQualityStats stat : qualityStats.values()) { if (!stat.sampleIsEmpty()) { stat.evaluatePercentiles(); writer.printf("%s\t%d\t%d\t%d\t%f\t%d%n", basename, stat.readIndex, stat.percentile(25), stat.percentile(50), stat.averageQuality / stat.observedCount, stat.percentile(75)); } } } progress.updateAndDisplay(); progress.stop(); } finally { if (writer != System.out) { IOUtils.closeQuietly(writer); } } }