package jhpro.sregression;

import org.jgap.InvalidConfigurationException;
import org.jgap.gp.impl.GPConfiguration;
import org.jgap.gp.impl.GPGenotype;
import org.jgap.gp.*;
import org.jgap.gp.terminal.*;


/**
 * Initialize the problem.
 * @author sergei
 *
 */
public class SymProblem  extends GPProblem  {

	
	protected SymProblem(GPConfiguration a_conf)
			throws InvalidConfigurationException {
		super(a_conf);
	}

	
	
	 
		/**
		 * This method is used for setting up the commands and terminals that can be
		 * used to solve the problem.
		 * 
		 * @return GPGenotype
		 * @throws InvalidConfigurationException
		 */
		public GPGenotype create() throws InvalidConfigurationException {
			GPConfiguration conf = getGPConfiguration();

			// At first, we define the return type of the GP program.
			// ------------------------------------------------------
			// Then, we define the arguments of the GP parts. Normally, only for
			// ADF's
			// there is a specification here, otherwise it is empty as in first
			// case.
			// -----------------------------------------------------------------------
			Class[] types;
			Class[][] argTypes;
			if (SymRegression.useADF) {
				if ("boolean".equals(SymRegression.adfType)) {
					types = new Class[] { CommandGene.DoubleClass,
							CommandGene.BooleanClass };
				} else if ("integer".equals(SymRegression.adfType)) {
					types = new Class[] { CommandGene.DoubleClass,
							CommandGene.IntegerClass };
				} else {
					types = new Class[] { CommandGene.DoubleClass,
							CommandGene.DoubleClass };
				}

				Class[] adfs = new Class[SymRegression.adfArity];
				for (int i = 0; i < SymRegression.adfArity; i++) {
					if ("boolean".equals(SymRegression.adfType)) {
						adfs[i] = CommandGene.BooleanClass;
					} else if ("integer".equals(SymRegression.adfType)) {
						adfs[i] = CommandGene.IntegerClass;
					} else {
						adfs[i] = CommandGene.DoubleClass;
					}
				}

				argTypes = new Class[][] { {}, adfs };

			} else {
				types = new Class[] { CommandGene.DoubleClass };
				argTypes = new Class[][] { {} };
			}

			// Configure desired minimum number of nodes per sub program.
			// Same as with types: First entry here corresponds with first entry in
			// nodeSets.
			// Configure desired maximum number of nodes per sub program.
			// First entry here corresponds with first entry in nodeSets.
			//
			// This is experimental!
			int[] minDepths;
			int[] maxDepths;
			if (SymRegression.useADF) {
				minDepths = new int[] { 1, 1 };
				maxDepths = new int[] { 9, 9 };
			} else {
				minDepths = new int[] { 1 };
				maxDepths = new int[] { 9 };
			}

			// Next, we define the set of available GP commands and terminals to
			// use.
			// Please see package org.jgap.gp.function and org.jgap.gp.terminal
			// You can easily add commands and terminals of your own.
			// ----------------------------------------------------------------------
			CommandGene[] commands = SymRegression.makeCommands(conf, SymRegression.functions, SymRegression.lowerRange,
					SymRegression.upperRange, "plain");

			// Create the node sets
			int command_len = commands.length;
			CommandGene[][] nodeSets = new CommandGene[2][SymRegression.numInputVariables
					+ command_len];

			// the variables:
			// 1) in the nodeSets matrix
			// 2) as variables (to be used for fitness checking)
			// --------------------------------------------------
			SymRegression.variables = new Variable[SymRegression.numInputVariables];
			int variableIndex = 0;
			for (int i = 0; i < SymRegression.numInputVariables + 1; i++) {
				String variableName = SymRegression.variableNames[i];
				if (i != SymRegression.outputVariable) {
					if (SymRegression.variableNames != null && SymRegression.variableNames.length > 0) {
						variableName = SymRegression.variableNames[i];
					}
					Variable[] variables;
					SymRegression.variables[variableIndex] = Variable.create(conf, variableName,
							CommandGene.DoubleClass);
					nodeSets[0][variableIndex] = SymRegression.variables[variableIndex];
					System.out.println("input variable: "
							+ SymRegression.variables[variableIndex]);
					variableIndex++;
				}
			}

			// assign the functions/terminals
			// ------------------------------
			for (int i = 0; i < command_len; i++) {
				System.out.println("function1: " + commands[i]);
				nodeSets[0][i + SymRegression.numInputVariables] = commands[i];
			}

			// ADF functions in the second array in nodeSets
			if (SymRegression.useADF) {
				CommandGene[] adfCommands = SymRegression.makeCommands(conf, SymRegression.adfFunctions,
						SymRegression.lowerRange, SymRegression.upperRange, "ADF");
				int adfLength = adfCommands.length;
				nodeSets[1] = new CommandGene[adfLength];
				for (int i = 0; i < adfLength; i++) {
					System.out.println("ADF function: " + adfCommands[i]);
					nodeSets[1][i] = adfCommands[i];
				}
			}

			// this is experimental.
			boolean[] full;
			if (SymRegression.useADF) {
				full = new boolean[] { true, true };
			} else {
				full = new boolean[] { true };
			}
			boolean[] fullModeAllowed = full;

			// Create genotype with initial population. Here, we use the
			// declarations made above:
			// ----------------------------------------------------------
			return GPGenotype.randomInitialGenotype(conf, types, argTypes,
					nodeSets, SymRegression.maxNodes, SymRegression.verboseOutput);

			// this is experimental
			// return GPGenotype.randomInitialGenotype(conf, types, argTypes,
			// nodeSets,
			// minDepths,maxDepths, maxNodes, fullModeAllowed,verboseOutput);

		}
	
}

 

Ads help maintain this website.