From jecoliwiki

The definition of the evaluation function, i.e. the way fitness is calculated for each problem, is of foremost importance for solving a specific problem. This is the glue between the problem and JECoLi.

To implement this, we need to implement a Java class that extends the class AbstractEvaluationFunction<R> or the class AbstractMultiobjectiveEvaluationFunction<R> depending if we will use a multiobjective or singleobjective algorithm, where R stands for the class of the representation used. Therefore, an evaluation function is tied to a representation type.

The class must redefine the method :

public double evaluate(R solutionRepresentation)

If we extended the AbstractEvaluationFunction<R>, or if the extended class is AbstractMultiobjectiveEvaluationFunction<R>:

public Double[] evaluateMO(R solutionRepresentation)

This method creates a double or an array of Double with the numerical fitness value(s), in a singleobjective cases a single double is returned, in other hand an array of Double is returned in multiobjective cases.

The evaluation function typically has a constructor that calls the superclass constructor, defining the type of optimization (true, for maximization; false, for minimization).

It is common that the evaluation functions for typical problems include attributes that represent the problem-specific knowledge, needed to evaluate the solution.

In the following box we show how to create a evaluation function for the Count ones problem using a binary representation:

/** * 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 { } }

Check for other more realistic examples in the packages test/knapsacking or test/tsp, for example.