package jcgp.backend.resources; import java.util.Random; import jcgp.backend.function.Function; import jcgp.backend.function.FunctionSet; import jcgp.backend.resources.parameters.IntegerParameter; /** * * The resources class encapsulates all of the resources based on which the program operates. * Each instance of JCGP contains a single instance of Resources, which gets passed to the selected * modules as the program executes. * * @author Eduardo Pedroni * */ public class Resources { protected IntegerParameter rows, columns, inputs, outputs, populationSize, levelsBack, currentGeneration, generations, currentRun, runs, arity, seed, reportInterval; protected Random numberGenerator = new Random(); protected FunctionSet functionSet; // GUI console protected Console console; /** * @return the rows */ public int rows() { return rows.get(); } /** * @return the columns */ public int columns() { return columns.get(); } /** * @return the inputs */ public int inputs() { return inputs.get(); } /** * @return the outputs */ public int outputs() { return outputs.get(); } /** * @return the populationSize */ public int populationSize() { return populationSize.get(); } /** * @return the levelsBack */ public int levelsBack() { return levelsBack.get(); } /** * @return the nodes */ public int nodes() { return columns.get() * rows.get(); } /** * @return the currentGeneration */ public int currentGeneration() { return currentGeneration.get(); } /** * @return the generations */ public int generations() { return generations.get(); } /** * @return the currentRun */ public int currentRun() { return currentRun.get(); } /** * @return the runs */ public int runs() { return runs.get(); } /** * @return the arity */ public int arity() { return arity.get(); } /** * @return the seed */ public int seed() { return seed.get(); } /** * @return the report interval */ public int reportInterval() { return reportInterval.get(); } /* * Utility functions */ public int getRandomInt(int limit) { return numberGenerator.nextInt(limit); } public double getRandomDouble(int limit) { return numberGenerator.nextDouble() * limit; } public double getRandomDouble() { return numberGenerator.nextDouble(); } /* * FunctionSet functions */ public Function getRandomFunction() { Function f = functionSet.getAllowedFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount())); return f; } public Function getFunction(int index) { return functionSet.getAllowedFunction(index); } /** * @return the functionSet */ public FunctionSet getFunctionSet() { return functionSet; } public int getFunctionIndex(Function function) { for (int i = 0; i < functionSet.getTotalFunctionCount(); i++) { if (function == functionSet.getFunction(i)) { return i; } } // not found, default to 0 return 0; } /* * Console functionality * These are affected by parameter report interval */ public void reportln(String s) { if (reportInterval.get() > 0) { if (currentGeneration.get() % reportInterval.get() == 0) { System.out.println(s); if (console != null) { console.println(s); } } } } public void report(String s) { if (reportInterval.get() > 0) { if (currentGeneration.get() % reportInterval.get() == 0) { System.out.print(s); if (console != null) { console.print(s); } } } } /* * Console functionality * These are not affected by parameter report interval */ public void println(String s) { System.out.println(s); if (console != null) { console.println(s); } } public void print(String s) { System.out.print(s); if (console != null) { console.print(s); } } }