Counting Ones

From jecoliwiki
Jump to: navigation, search

The counting ones task is here used as a toy problem to show how to address the resolution of a problem using JECoLi.

Problem definition

In this problem, there are N binary variables and the objective function simply counts how many of these variables are equal to 1.

Therefore, the optimal solution has fitness N, where all variables have value equal to 1.

Resolution steps

To solve any given problem with JECoLi, the first step is always to decide on a suitable representation, which in this particular case is a straightforward task since the binary encoding is an obvious choice.

Next, we need to create a Java class with an appropriate evaluation (or fitness) function for the problem (see How To: creating evaluation functions). This function provides the decoding of the genome into a solution to the problem and also the process of assigning this solution an appropriate fitness value (numerical).

The code for the evaluation function in this case is given below:

/**
* The Class CountOnesEvaluationFunction.
* Defines the evaluation function for the toy problem Count Ones.
*/
public class CountOnesEvaluationFunction extends AbstractEvaluationFunction<ILinearRepresentation<Boolean>> {

     
     /**
      * Instantiates a new count ones evaluation function.
     */
	public CountOnesEvaluationFunction(){
        super(true);
    }
	

    @Override
    public double evaluate(ILinearRepresentation<Boolean> solution) {
		int countOnesValue = countOnes(solution);

		return countOnesValue;
	}

	/**
	 * Count ones.
	 * 
	 * @param genomeRepresentation the genome representation
	 * 
	 * @return the int
	 */
	protected int countOnes(ILinearRepresentation<Boolean> genomeRepresentation){
		int countOneValues = 0;
		for(int i = 0;i < genomeRepresentation.getNumberOfElements();i++)
			if(genomeRepresentation.getElement(i))	countOneValues++;

		return countOneValues;
	}


	@Override
	public IEvaluationFunction<ILinearRepresentation<Boolean>> deepCopy() {
		return new CountOnesEvaluationFunction();
	}


	@Override
	public void verifyInputData()
			throws InvalidEvaluationFunctionInputDataException {
		// TODO Auto-generated method stub
		
	}
} 

The next step will be to choose one of the available algorithms.

Let us start by illustrating the use of Evolutionary Algorithms.

The next segment of code addresses the tasks of configuring and running an EA (the detailed explanation on the configuration of the several components is addressed in the How To: configuring EA).

  /**
 * Class to solve the count ones toy problem using Evolutionary Algorithms.
 */
public class CountOnesEATest {


	/**
 	 * The main method.
	 * 
	 * @param args the arguments
	 */
 
	public static void main(String[] args) {
		try {
			
 			EvolutionaryConfiguration<ILinearRepresentation<Boolean>,BinaryRepresentationFactory> configuration = new  EvolutionaryConfiguration<ILinearRepresentation<Boolean>,BinaryRepresentationFactory>();
 			
			IEvaluationFunction<ILinearRepresentation<Boolean>> evaluationFunction = new CountOnesEvaluationFunction();
  			configuration.setEvaluationFunction(evaluationFunction);
			
 			int solutionSize = 100;
			BinaryRepresentationFactory solutionFactory = new BinaryRepresentationFactory(solutionSize);
 			configuration.setSolutionFactory(solutionFactory);
			
			int populationSize = 100;
			configuration.setPopulationSize(populationSize);
			
			int numberGenerations = 200;
  			ITerminationCriteria terminationCriteria = new IterationTerminationCriteria(numberGenerations);
			configuration.setTerminationCriteria(terminationCriteria);
 			
			RecombinationParameters recombinationParameters = new RecombinationParameters(populationSize);
 			configuration.setRecombinationParameters(recombinationParameters);
  			
			configuration.setSelectionOperators(new TournamentSelection<ILinearRepresentation<Boolean>>(1,4));
			
			configuration.setRandomNumberGenerator(new DefaultRandomNumberGenerator());
 			configuration.setProblemBaseDirectory("nullDirectory");
  			configuration.setAlgorithmStateFile("nullFile");
			configuration.setSaveAlgorithmStateDirectoryPath("nullDirectory");
 			configuration.setPopulationSize(1000);
 			configuration.setAlgorithmResultWriterList(new ArrayList<IAlgorithmResultWriter<ILinearRepresentation<Boolean>>>());
			configuration.setStatisticsConfiguration(new StatisticsConfiguration());
			
			ReproductionOperatorContainer operatorContainer = new ReproductionOperatorContainer();
 			operatorContainer.addOperator(0.5, new UniformCrossover<Boolean>());
			operatorContainer.addOperator(0.5, new BitFlipMutation(2));
 			configuration.setReproductionOperatorContainer(operatorContainer);
			
			IAlgorithm<ILinearRepresentation<Boolean>> algorithm = new EvolutionaryAlgorithm<ILinearRepresentation<Boolean>,BinaryRepresentationFactory>(configuration);
			//algorithm.newInstance().run();
			algorithm.run();
 

		} catch (InvalidNumberOfIterationsException e) {
			e.printStackTrace();
 		} 
		catch (InvalidConfigurationException e) {
			e.printStackTrace();
 		}catch (Exception e) {
			e.printStackTrace();
		} 

	}

}
Personal tools