diff options
Diffstat (limited to 'src/jcgp/backend/modules/problem')
5 files changed, 40 insertions, 29 deletions
| diff --git a/src/jcgp/backend/modules/problem/BestFitness.java b/src/jcgp/backend/modules/problem/BestFitness.java new file mode 100644 index 0000000..dce9ecd --- /dev/null +++ b/src/jcgp/backend/modules/problem/BestFitness.java @@ -0,0 +1,18 @@ +package jcgp.backend.modules.problem; + +/** + * Enum type to allow problems to indicate their fitness + * orientation. + * <br><br> + * {@code BestFitness.HIGH} means high fitness values are + * better than low. Conversely, {@code BestFitness.LOW} + * signals that low fitness values indicate better fitness + * than high values.  + *  + *  + * @author Eduardo Pedroni + * + */ +public enum BestFitness { +	HIGH, LOW; +} diff --git a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java index 0071ed5..b615675 100644 --- a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java +++ b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java @@ -80,7 +80,7 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> {  	}  	@Override -	public int perfectSolutionFound(Population population) { +	public int hasPerfectSolution(Population population) {  		// higher fitness is better  		for (int i = 0; i < getResources().populationSize(); i++) {  			if (population.get(i).getFitness() >= maxFitness.get()) { @@ -89,16 +89,4 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> {  		}  		return -1;  	} - -	@Override -	public int hasImprovement(Population population) { -		// higher fitness is better -		for (int i = 0; i < getResources().populationSize(); i++) { -			if (population.get(i).getFitness() > bestFitness.get()) { -				bestFitness.set(population.get(i).getFitness()); -				return i; -			} -		} -		return -1; -	}  } diff --git a/src/jcgp/backend/modules/problem/Problem.java b/src/jcgp/backend/modules/problem/Problem.java index 5f194b5..2af2373 100644 --- a/src/jcgp/backend/modules/problem/Problem.java +++ b/src/jcgp/backend/modules/problem/Problem.java @@ -85,7 +85,7 @@ public abstract class Problem extends Module {  	 * @param population the population to search through for a perfect chromosome.  	 * @return the perfect solution index, if one exits, -1 if no perfect solution was found.  	 */ -	public abstract int perfectSolutionFound(Population population); +	public abstract int hasPerfectSolution(Population population);  	/**  	 * Used to assert whether a given population has a chromosome that is an improvement over  diff --git a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java index 24c61d6..3b5f539 100644 --- a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java +++ b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java @@ -126,7 +126,7 @@ public class SymbolicRegressionProblem extends TestCaseProblem<Double> {  	}  	@Override -	public int perfectSolutionFound(Population population) { +	public int hasPerfectSolution(Population population) {  		// higher fitness is better  		for (int i = 0; i < getResources().populationSize(); i++) {  			if (population.get(i).getFitness() >= maxFitness.get() - perfectionThreshold.get()) { @@ -135,18 +135,4 @@ public class SymbolicRegressionProblem extends TestCaseProblem<Double> {  		}  		return -1;  	} -	 -	@Override -	public int hasImprovement(Population population) { -		// higher fitness is better -		for (int i = 0; i < getResources().populationSize(); i++) { -			System.out.println("checking for improvement"); -			if (population.get(i).getFitness() > bestFitness.get()) { -				System.out.println("found a better chr, " + i); -				bestFitness.set(population.get(i).getFitness()); -				return i; -			} -		} -		return -1; -	}  } diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java index 69c078d..188e236 100644 --- a/src/jcgp/backend/modules/problem/TestCaseProblem.java +++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java @@ -5,6 +5,7 @@ import java.io.File;  import javafx.collections.FXCollections;  import javafx.collections.ObservableList;  import jcgp.backend.parsers.TestCaseParser; +import jcgp.backend.population.Population;  import jcgp.backend.resources.ModifiableResources;  import jcgp.backend.resources.Resources; @@ -162,6 +163,24 @@ public abstract class TestCaseProblem<T> extends Problem {  		// use standard test case parser for this  		TestCaseParser.parse(file, this, resources);  	} +	 +	@Override +	public int hasImprovement(Population population) { +		for (int i = 0; i < getResources().populationSize(); i++) { +			if (getFitnessOrientation() == BestFitness.HIGH) { +				if (population.get(i).getFitness() > bestFitness.get()) { +					bestFitness.set(population.get(i).getFitness()); +					return i; +				} +			} else { +				if (population.get(i).getFitness() < bestFitness.get()) { +					bestFitness.set(population.get(i).getFitness()); +					return i; +				} +			} +		} +		return -1; +	}  } | 
