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(){

    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;

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

	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();
 			int solutionSize = 100;
			BinaryRepresentationFactory solutionFactory = new BinaryRepresentationFactory(solutionSize);
			int populationSize = 100;
			int numberGenerations = 200;
  			ITerminationCriteria terminationCriteria = new IterationTerminationCriteria(numberGenerations);
			RecombinationParameters recombinationParameters = new RecombinationParameters(populationSize);
			configuration.setSelectionOperators(new TournamentSelection<ILinearRepresentation<Boolean>>(1,4));
			configuration.setRandomNumberGenerator(new DefaultRandomNumberGenerator());
 			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));
			IAlgorithm<ILinearRepresentation<Boolean>> algorithm = new EvolutionaryAlgorithm<ILinearRepresentation<Boolean>,BinaryRepresentationFactory>(configuration);

		} catch (InvalidNumberOfIterationsException e) {
		catch (InvalidConfigurationException e) {
 		}catch (Exception e) {


Personal tools