Java List Random Item softMaxIndex(Random rand, int listSize, double temperature)

Here you can find the source of softMaxIndex(Random rand, int listSize, double temperature)

Description

soft Max Index

License

Open Source License

Declaration

public static int softMaxIndex(Random rand, int listSize,
            double temperature) 

Method Source Code

//package com.java2s;
/*//from w  w w . j ava2  s  .c o  m
 * Copyright (C) 2009 Swedish Institute of Computer Science (SICS) Copyright (C)
 * Copyright (C) 2009 Royal Institute of Technology (KTH)
 *
 * Croupier 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.Random;

public class Main {
    public static int softMaxIndex(Random rand, int listSize,
            double temperature) {
        double rnd = rand.nextDouble();
        double total = 0.0d;
        double[] values = new double[listSize];
        int j = listSize + 1;
        for (int i = 0; i < listSize; i++) {
            // get inverse of values - lowest have highest value.
            double val = j;
            j--;
            values[i] = Math.exp(val / temperature);
            total += values[i];
        }

        for (int i = 0; i < values.length; i++) {
            if (i != 0) {
                values[i] += values[i - 1];
            }
            // normalise the probability
            double normalisedReward = values[i] / total;
            if (normalisedReward >= rnd) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. sample(Random random, List list)
  2. sampleRandomSublist(List list, int sampleSize)
  3. sampleWithoutReplacement(List items, int sampleSize)
  4. sampleWithReplacement(final Random random, final List data, final int sampleSize)
  5. sampleWithReplacementInto(final Random random, final List data, final int sampleSize, final Collection result)