// Path: src/com/ibm/exception/DataGenerationException.java
// public class DataGenerationException extends Exception {
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/ibm/utils/Tuple.java
// public class Tuple<X, Y> { 
// 	
// 	public X x; 
// 	public Y y; 
// 	
// 	public Tuple(X x, Y y) { 
// 	    this.x = x; 
// 	    this.y = y; 
// 	} 
// } 	


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.ibm.exception.DataGenerationException;
import com.ibm.utils.Tuple;
/*
 * Copyright (C) 2014 Raul Gracia-Tinedo
 * 
 * This program 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.
 * 
 * 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, see http://www.gnu.org/licenses/.
 */
package com.ibm.generation;



/**
 * This class is implements the Fitness Proportional Selection algorithm. In 
 * summary, given a map of elements and their frequencies, this class computes
 * a vector of ranges according to the relative weight of each element's 
 * frequency compared with the total frequencies. Furthermore, on this vector 
 * is computed, this class also retrieves elements of the map based on the 
 * distribution of their weights.
 * 
 * @author Raul Gracia-Tinedo (raulgraciatinedo@gmail.com)
 *
 */
public class FitnessProportionateSelection <K> implements Serializable{

	private static final long serialVersionUID = 1L;

	private Map<K, Long> histogram = null;
	
	private BiMap<K, Tuple<Double, Double>> proportionalityMap;
// Path: src/com/ibm/exception/DataGenerationException.java
// public class DataGenerationException extends Exception {
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/ibm/utils/Tuple.java
// public class Tuple<X, Y> { 
// 	
// 	public X x; 
// 	public Y y; 
// 	
// 	public Tuple(X x, Y y) { 
// 	    this.x = x; 
// 	    this.y = y; 
// 	} 
// } 	


// Path: src/com/ibm/generation/FitnessProportionateSelection.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.ibm.exception.DataGenerationException;
import com.ibm.utils.Tuple;

/*
 * Copyright (C) 2014 Raul Gracia-Tinedo
 * 
 * This program 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.
 * 
 * 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, see http://www.gnu.org/licenses/.
 */
package com.ibm.generation;



/**
 * This class is implements the Fitness Proportional Selection algorithm. In 
 * summary, given a map of elements and their frequencies, this class computes
 * a vector of ranges according to the relative weight of each element's 
 * frequency compared with the total frequencies. Furthermore, on this vector 
 * is computed, this class also retrieves elements of the map based on the 
 * distribution of their weights.
 * 
 * @author Raul Gracia-Tinedo (raulgraciatinedo@gmail.com)
 *
 */
public class FitnessProportionateSelection <K> implements Serializable{

	private static final long serialVersionUID = 1L;

	private Map<K, Long> histogram = null;
	
	private BiMap<K, Tuple<Double, Double>> proportionalityMap;
	private List<Tuple<Double, Double>> sortedProbabilityBins;