// tjhGene.java import java.util.*; import tjhNeuron; /** A tree structure of genes, each with a simple type (RED/GREEN) and a neuron. When a creature is born, a tree of tjhCell's is created that mirrors this tree representation of genes. */ public class tjhGene extends Object { //------------------------------------------------------------------------ // static constants //------------------------------------------------------------------------ /** fixed enum type value for a green cell (root cell is drawn in pink but always behaves like a green) */ protected final static int GREEN=0; /** fixed enum type value for a red cell */ protected final static int RED=1; /** the probability of a physical mutation */ protected final static float P_PHYSICAL_MUTATION=0.03F; /** the probability of a mental mutation */ protected final static float P_MENTAL_MUTATION=0.03F; /** the maximum number of allowed daughter cells per cell */ protected final static int MAX_SUBNODES=3; //------------------------------------------------------------------------ // data //------------------------------------------------------------------------ /** the type of the cell (RED/GREEN) */ public int type; /** the neuron associated with the cell */ public tjhNeuron neuron; /** does this gene have a parent? */ protected boolean has_parent; /** a reference to the gene's parent if it has one */ protected tjhGene parent; /** list of the subnodes of this gene */ public Vector subnodes; //------------------------------------------------------------------------ // public methods //------------------------------------------------------------------------ /** default constructor */ public tjhGene() { subnodes = new Vector(); type = GREEN; has_parent=false; neuron = new tjhNeuron(); } /** simple initialization from scratch, creates a creature with a red arm and a green arm */ public void InitSimple() { type=GREEN; tjhGene sub = new tjhGene(); sub.type=RED; sub.has_parent=true; sub.parent=this; subnodes.addElement(sub); sub = new tjhGene(); sub.type=GREEN; sub.has_parent=true; sub.parent=this; subnodes.addElement(sub); } /** gene replication routine */ public void Copy(tjhGene other) { // copy all the subnodes for(int i=0;i0.5?RED:GREEN; } else new_gene.type = source.type; // add the reference to the new gene to our list of daughter genes subnodes.addElement(new_gene); // recurse down to copy the genes lower in the tree new_gene.Copy(source); } } // slight chance of adding an extra cell if(PhysicalMutationCalledFor() && subnodes.size()0.5?RED:GREEN; new_gene.has_parent=true; new_gene.parent=this; subnodes.addElement(new_gene); } // copy the other's neuron across neuron.Copy(other.neuron); // mutate an entry if called for if(MentalMutationCalledFor()) { neuron.matrix[(int)(Math.random()*tjhNeuron.N_INPUTS)] = tjhNeuron.RandomEntry(); } } //------------------------------------------------------------------------ // protected methods //------------------------------------------------------------------------ /** decides whether a physical mutation should happen this time */ protected boolean PhysicalMutationCalledFor() { return Math.random()