From efe490fec1c7a94f004b496c7c97c82083fe44ec Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 21 Apr 2014 00:09:55 +0100 Subject: Tooltips are looking strange, checking on a different machine --- .../modules/problem/DigitalCircuitProblem.java | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/jcgp/backend/modules/problem/DigitalCircuitProblem.java (limited to 'src/jcgp/backend/modules/problem/DigitalCircuitProblem.java') diff --git a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java new file mode 100644 index 0000000..e92989e --- /dev/null +++ b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java @@ -0,0 +1,85 @@ +package jcgp.backend.modules.problem; + +import jcgp.backend.function.BitwiseLogic; +import jcgp.backend.function.UnsignedInteger; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; + +public class DigitalCircuitProblem extends TestCaseProblem { + + public DigitalCircuitProblem(Resources resources) { + super(resources); + functionSet = new BitwiseLogic(); + } + + @Override + public void evaluate(Population population, Resources resources) { + // set fittest to 0, change it whenever a fitter one is found + population.setFittest(0); + + // for every chromosome in the population + for (int i = 0; i < resources.populationSize(); i++) { + // assume an initial fitness of 0 + int fitness = 0; + + // iterate over every test case + for (int t = 0; t < testCases.size(); t++) { + population.getChromosome(i).setInputs((Object[]) testCases.get(t).getInputs()); + // check each output + for (int o = 0; o < resources.outputs(); o++) { + Integer output = ((UnsignedInteger) population.getChromosome(i).getOutput(o).calculate()).get(); + Integer matches = ~(output ^ testCases.get(t).getOutput(o).get()); + // check only the relevant bits + int bits = (int) Math.pow(2.0, (double) resources.inputs()); + for (int b = 0; b < bits; b++) { + if (((matches >>> b) & 1) == 1) { + fitness++; + } + } + } + } + + // assign the resulting fitness to the respective individual + population.getChromosome(i).setFitness(fitness); + if (fitness >= population.getFittest().getFitness()) { + population.setFittest(i); + } + } + } + + @Override + protected int getMaxFitness() { + int maxFitness = (int) Math.pow(2.0, (double) resources.inputs()) * resources.outputs(); + return maxFitness; + } + + @Override + public String toString() { + return "Digital circuit"; + } + + @Override + public void addTestCase(String[] inputs, String[] outputs) { + UnsignedInteger[] inputCases = new UnsignedInteger[inputs.length]; + UnsignedInteger[] outputCases = new UnsignedInteger[outputs.length]; + for (int i = 0; i < inputCases.length; i++) { + inputCases[i] = new UnsignedInteger(inputs[i]); + } + for (int o = 0; o < outputCases.length; o++) { + outputCases[o] = new UnsignedInteger(outputs[o]); + } + + addTestCase(new TestCase(inputCases, outputCases)); + } + + @Override + public boolean isPerfectSolution(Chromosome fittest) { + return fittest.getFitness() >= maxFitness.get(); + } + + @Override + public String getFileExtension() { + return ".plu"; + } +} -- cgit v1.2.3