package net.sf.jclec.problem.funopt.binarray; import java.util.StringTokenizer; import javolution.xml.XmlFormat; import javolution.xml.XmlElement; import net.sf.jclec.IFitness; import net.sf.jclec.binarray.BinArrayIndividual; import net.sf.jclec.problem.funopt.IFuncOptIndividual; import org.apache.commons.lang.builder.ToStringBuilder; /** * BinArrayIndividual used in function optimization problems. * * @author Sebastián Ventura */ public class FuncOptBinArrayIndividual extends BinArrayIndividual implements IFuncOptIndividual { ///////////////////////////////////////////////////////////////// // ------------------------------------- Marshal/unmarshal format ///////////////////////////////////////////////////////////////// /** * Uses super.format(). Then marshal/unmarshal phenotype. */ protected static final XmlFormat<FuncOptBinArrayIndividual> XML = new XmlFormat<FuncOptBinArrayIndividual>(FuncOptBinArrayIndividual.class) { public void format(FuncOptBinArrayIndividual source, XmlElement xml) { // Call super.format() BinArrayIndividual.XML.format(source, xml); // Marshal phenotype int phenotypeLenght = source.phenotype.length; StringBuffer sb = new StringBuffer(); for (int i=0; i<phenotypeLenght; i++) { sb.append(source.phenotype[i]); if (i != phenotypeLenght-1) { sb.append(" "); } } xml.add(sb.toString(), "phenotype", STRING_XML); } public FuncOptBinArrayIndividual parse(XmlElement xml) { // Call super.parse() FuncOptBinArrayIndividual result = (FuncOptBinArrayIndividual) BinArrayIndividual.XML.parse(xml); // Unmarshal phenotype String phenotypeString = xml.<String> get("phenotype", STRING_XML); StringTokenizer st = new StringTokenizer(phenotypeString); result.phenotype = new double[st.countTokens()]; for (int i=0; st.hasMoreTokens(); i++) { result.phenotype[i] = Double.parseDouble(st.nextToken()); } // Return result return result; } public String defaultName() { return "funct-opt-bin-array-individual"; } }; ///////////////////////////////////////////////////////////////// // --------------------------------------- Serialization constant ///////////////////////////////////////////////////////////////// /** Generated by Eclipse */ private static final long serialVersionUID = -3981819616875133994L; ///////////////////////////////////////////////////////////////// // --------------------------------------------------- Attributes ///////////////////////////////////////////////////////////////// /** Individual phenotype */ protected double [] phenotype; ///////////////////////////////////////////////////////////////// // ------------------------------------------------- Constructors ///////////////////////////////////////////////////////////////// /** * Empty constructor. */ public FuncOptBinArrayIndividual() { super(); } /** * Constructor that sets individual genotype and phenotype. * * @param genotype Individual genotype * @param phenotype Individual phenotype */ public FuncOptBinArrayIndividual(byte[] genotype, double [] phenotype) { super(genotype); setPhenotype(phenotype); } /** * Constructor that sets individual genotype, phenotype and fitness. * * @param genotype Individual genotype * @param phenotype Individual phenotype * @param fitness Individual fitness */ public FuncOptBinArrayIndividual(byte[] genotype, double [] phenotype, IFitness fitness) { super(genotype, fitness); setPhenotype(phenotype); } ///////////////////////////////////////////////////////////////// // ------------------- Implementing IFunctOptIndividual interface ///////////////////////////////////////////////////////////////// /** * {@inheritDoc} */ public double[] getPhenotype() { return phenotype; } ///////////////////////////////////////////////////////////////// // ----------------------------------------------- Public methods ///////////////////////////////////////////////////////////////// /** * Set individual phenotype */ public void setPhenotype(double [] phenotype) { this.phenotype = phenotype; } ///////////////////////////////////////////////////////////////// // ------------------------- Overwriting java.lang.Object methods ///////////////////////////////////////////////////////////////// /** * Shows individual genotype, phenotype and fitness */ public String toString() { ToStringBuilder tsb = new ToStringBuilder(this); tsb.append("genotype", genotype); tsb.append("phenotype", phenotype); tsb.append("fitness", fitness); return tsb.toString(); } }