/* * File: SigmaPiChromosome.java * Author: Justin Basilico * Course: PO CS 152: Neural Networks * Assignment: Final Project * Updated: 2001.12.18 * Created: 2001.12.06 * * Description: * This file contains the SigmaPiChromosome class, which is a BinaryChromosome * that is used for creating Chromosomes that represent SigmaPiNetworks. In * addition to the normal binary string, it also keeps an array that stores * the indices of the level, in-node, and out-node that identify the weight * that each bit in the binary string correspond to in the SigmaPiNetwork, * since the Chromosome encodes the connectivity of a SigmaPiNetwork. * * Copyright: Justin Basilico (2001). */ import java.util.Random; /** * SigmaPiChromosome class * * This class implements a BinaryChromosome that is used for encoding the * connectivity of a SigmaPiNetwork. In addition to using the binary string * for encoding the connectivity, it keeps an array with the index of the * weight in the network that each bit in the chromosome is encoding the * connectivity of. It implements constructors for creating these Chromosomes * and for loading them into SigmaPiNetworks. * * @author Justin Basilico * @version 2001.12.18 */ public class SigmaPiChromosome extends BinaryChromosome { /** weightIndex * This array is the length of the chromosome by 3 of integer values that * are the indices corresponding to the layer index, in-node index, and * out-node index that each bit in the binary string corresponds to for * the connectivity of the SigmaPiNetwork. */ protected int[][] weightIndex = null; /** * SigmaPiChromosome length, Random constructor * * This constructor takes the length of a SigmaPiChromosome to create, the * array that specifies the weight values that each bit applies to, and a * Random object and creates a chromosome string of that length, randomly * setting each bit to true with a probability of 0.5 by using the given * Random object. It just uses the given weight indices and does not copy * them. * * @param length The integer length of the BinaryChromosome to create. * @param weightIndex The array of array of integers of size three that * specifies the index of the weight that each bit in the binary * code for the chromosome applies to. * @param random The Random object for randomly deciding which bits * should be true and false, which it does with a probability 0.5. */ public SigmaPiChromosome( int length, // Chromosome length. int[][] weightIndex, // The weight index. Random random) // Random number generator. { super(length, random); this.weightIndex = weightIndex; } /** * SigmaPiChromosome array constructor * * This method takes an array of booleans and just uses that as the string * for the SigmaPiChromosome by just setting it directly as the internal * data. It also takes the weight index matrix that specify where each of * the bits in the chromosome apply to in the SigmaPiNetwork. It does not * copy the data and assumes that both will work together properly. * * @param chromosome The boolean array that contains the genetic code for * the SigmaPiChromosome. * @param weightIndex The array of array of integers of size three that * specifies the index of the weight that each bit in the binary * code for the chromosome applies to. */ public SigmaPiChromosome( boolean[] chromosome, // Chromosome data. int[][] weightIndex) // The weight index. { super(chromosome); this.weightIndex = weightIndex; } /** * SigmaPiChromosome copy constructor * * This constructor takes another SigmaPiChromosome and initializes this * SigmaPiChromosome to be a copy of the given one, which does a full copy * of the given one's code, except it does not copy the weight indices. * * @param copy The SigmaPiChromosome to copy. */ public SigmaPiChromosome( SigmaPiChromosome copy) // BinaryChromosome to copy. { super(copy); this.weightIndex = copy.weightIndex; } /** * loadIntoNetwork * * This method takes a SigmaPiNetwork and loads this SigmaPiChromosome * into that network by encoding its connectivity by setting those weight * values to 1.0 where the bit is true in the binary code and the weight * to 0.0 where the bit is false. The weight is determined by the values * described in the weight indices for the SigmaPiChromosome. It is * assumed that the SigmaPiChromosome corresponds properly to the given * SigmaPiNetwork. * * @param network The SigmaPiNetwork to load this SigmaPiChromosome into. * It is assumed that the SigmaPiChromosome will properly go into * that network. */ public void loadIntoNetwork( SigmaPiNetwork network) // Network to load into. { // This is a pointer for the index of a specific bit in the // chromosome. int[] index = null; for (int i = 0; i < length; i++) { // Get the index of the layer, in-unit, and out-unit for this // bit. index = weightIndex[i]; // Set the weight for that bit based on its value. if ( chromosome[i] ) // Make a connection between the units. network.setWeight(index[0], index[1], index[2], 1.0); else // Make it so the units are not connected. network.setWeight(index[0], index[1], index[2], 0.0); } } /** * getSizeFor * * This method takes a SigmaPiNetwork and returns the size of the * SigmaPiChromosome that is needed to fully encode the connectivity of * that network. * * @param network The SigmaPiNetwork to get the SigmaPiChromosome size * for. * @return The integer size of the SigmaPiChromosome needed to fully * encode the connectivity of that network. */ public static int getSizeFor( SigmaPiNetwork network) // Network to get size for. { // Figure out the size. int size = 0; // Go through each layer of the network and add the size of the fully // connected network. int numLayers = network.getNumLayers(); for (int i = 1; i < numLayers; i++) { // Add to the size the number of weights in this fully-connected // layer with a bias node on the previous layer. size += network.getLayerSize(i) * (network.getLayerSize(i - 1) + 1); } // Return the size of the Chromosome. return size; } /** * clone * * This method returns a deep-copy of the SigmaPiChromosome it is called * on. * * @return A new BinaryChromosome is a deep-copy of this * SigmaPiChromosome. * @see java.lang.Cloneable */ public Object clone() { return new SigmaPiChromosome(this); } }