Create Selection Operator

From jecoliwiki
Jump to: navigation, search



The first step is to extend the Interface ISelectionOperator. This Interface contains the method selectSolutions that has as purpose to select a certain number of solutions based on specific algorithm. The method has the following parameters: the number of solutions to select, the solution set and the optimization type.

/**
 * The Interface ISelectionOperator.
 */
public interface ISelectionOperator<T extends IRepresentation> extends IOperator,IDeepCopy{

	/**
	 * Select solutions.
	 * 
	 * @param numberOfSolutionsToSelect the number of solutions to select
	 * @param solutionSet the solution set
	 * @param isMaximization the is maximization
	 * @param randomNumberGenerator TODO 
	 * @return the list< i solution> 
 	 *  
	 * @throws InvalidSelectionProcedureException the invalid selection procedure exception
  	 * @throws InvalidSelectionParameterException the invalid selection parameter exception
  	 * @throws Exception the exception
 	 */
 	List<ISolution<T>> selectSolutions(int numberOfSolutionsToSelect,ISolutionSet<T> solutionSet,
              boolean isMaximization, IRandomNumberGenerator randomNumberGenerator) 
       throws  InvalidSelectionProcedureException, InvalidSelectionParameterException, Exception;

	ISelectionOperator<T> deepCopy() throws Exception;
	
}

In the following box is shown how to implement the tourment selection operator:

 /**
 * The Class TournamentSelection.
 */
public class TournamentSelection<T extends IRepresentation> implements ISelectionOperator<T> {
	 
 	/** The k. */
 	protected int k;
	
 	/** The number of solutions per tournment. */
	protected int numberOfSolutionsPerTournment;

	/**
	 * Instantiates a new tournament selection.
	 * 
	 * @param k the k
	 * @param numberOfSolutionsPerTournment the number of solutions per tournment
	 * 
	 * @throws InvalidSelectionParameterException the invalid selection parameter exception
	 */
	public TournamentSelection(int k,int numberOfSolutionsPerTournment) 
              throws InvalidSelectionParameterException {

		if(k < 1)
			throw new InvalidSelectionParameterException("k < 0");

		if(numberOfSolutionsPerTournment < 1)
			throw new InvalidSelectionParameterException("numberOfSolutionsPerTournment < 1");

		if(k > numberOfSolutionsPerTournment)
			throw new InvalidSelectionParameterException("k > numberOfSolutionsPerTournment");

		
		this.k = k;
		this.numberOfSolutionsPerTournment = numberOfSolutionsPerTournment;
	}


 	/* (non-Javadoc)
	 * @see operators.ISelectionOperator#selectSolutions(int, core.interfaces.ISolutionSet, boolean)
	 */
	@Override
	public List<ISolution<T>> selectSolutions(int numberOfSolutionsToSelect,ISolutionSet<T> solutionSet, 
               boolean isMaximization, IRandomNumberGenerator randomNumberGenerator) 
          throws InvalidSelectionProcedureException, InvalidSelectionParameterException {
 		if(numberOfSolutionsToSelect < 0)
			throw new InvalidSelectionParameterException(" numberOfSolutionToSelect < 0");
 
		if(k > numberOfSolutionsToSelect)
			throw new InvalidSelectionParameterException("k > numberOfSolutionToSelect");
 
		int modNumberSolutions = numberOfSolutionsToSelect % k;

		if(modNumberSolutions != 0)
			throw new InvalidSelectionParameterException(" numberOfSolutionsToSelect mod k != 0");
 
		List<ISolution<T>> solutionList = new ArrayList<ISolution<T>>(numberOfSolutionsToSelect);

 		int currentNumberOfSolutions = 0;

		while(currentNumberOfSolutions < numberOfSolutionsToSelect){
			tournment(solutionList,solutionSet,isMaximization,randomNumberGenerator);
 			currentNumberOfSolutions += k;
		}


		return solutionList;
	}
 

	/**
	 * Tournment.
	 * 
	 * @param solutionList the solution list
 	 * @param solutionSet the solution set
	 * @param isMaximization the is maximization
	 * @param randomNumberGenerator 
	 */
 	protected void tournment(List<ISolution<T>> solutionList, ISolutionSet<T> solutionSet,boolean isMaximization, IRandomNumberGenerator randomNumberGenerator) {
		TreeSet<ISolution<T>> treeSet = selectRandomOrderedSet(solutionSet,randomNumberGenerator);
		ISolution<T> solution = null;
 		for(int i = 0; i < k;i++){
			if(isMaximization)
				solution = treeSet.pollLast();
 			else
				solution = treeSet.pollFirst();
			
			solutionList.add(solution);
		}
	}
 
	/**
	 * Select random ordered set.
	 * 
	 * @param solutionSet the solution set
	 * @param randomNumberGenerator 
	 * 
	 * @return the tree set< i solution>
	 */
	protected TreeSet<ISolution<T>> selectRandomOrderedSet(ISolutionSet<T> solutionSet, IRandomNumberGenerator randomNumberGenerator) {
		TreeSet<ISolution<T>>  orderedSolutionSet = new TreeSet<ISolution<T>>(solutionSet.getComparator());
		int numberOfSolutions = solutionSet.getNumberOfSolutions()-1;
 		for(int i = 0;i < numberOfSolutionsPerTournment;i++){
			int randomIndex = (int) (randomNumberGenerator.nextDouble()*numberOfSolutions);
			ISolution<T> solution = solutionSet.getSolution(randomIndex);
 			orderedSolutionSet.add(solution);
		}
		return orderedSolutionSet;
	}


	@Override
	public TournamentSelection<T> deepCopy() {
		try {
			return new TournamentSelection<T>(k, numberOfSolutionsPerTournment);
 		} catch (InvalidSelectionParameterException e) {
			e.printStackTrace();
		}
		return null;
	}

}
Personal tools