/* * File: SigmaPiFitness.java * Author: Justin Basilico * Course: PO CS 152: Neural Networks * Assignment: Final Project * Updated: 2001.12.16 * Created: 2001.12.06 * * Description: * This file contains the SigmaPiFitnes class, which implements a * FitnessFunction that tests the fitness of SigmaPiChromosomes by testing to * see how well they do on a specified dataset. The fitness value returned * for a SigmaPiChromosome is the average mean squared error across all of the * inputs in the dataset specified when the SigmaPiFitness object was * constructed. * * Copyright: Justin Basilico (2001). */ import java.util.Random; /** * SigmaPiFitness class * * This class implements a FitnessFunction for testing the fitness of * SigmaPiChromosomes. It calculates the fitness of such a chromosome by * loading it into a SigmaPiNetwork that this SigmaPiFitness was created with * and then testing the network against a dataset of NetworkInputs. The * fitness value returned is the average mean squared error across all of the * inputs in the dataset. Thus, a better chromosome has a smaller fitness * value. It is assumed that the SigmaPiNetwork, the NetworkInputs in the * dataset, and the SigmaPiChromosomes will all work together in harmony. If * they do not, the SigmaPiFitness may not work properly. * * @author Justin Basilico * @version 2001.12.16 */ public class SigmaPiFitness extends Object implements FitnessFunction { /** network * This is the SigmaPiNetwork that SigmaPiChromosomes will be loaded into * in order to see how well the Chromosome does. All tested Chromosomes * will be loaded into the same network and tested to save space. Each * SigmaPiFitness function is defined in terms of a specific network * architecture that it is loading Chromosomes into. */ protected SigmaPiNetwork network = null; /** dataset * This is the array of NetworkInputs that make up the dataset the * SigmaPiFitness function is using to test the SigmaPiChromosomes. It * is assumed be a dataset that works with the SigmaPiNetwork that the * fitness object is created for, in that the input and output * UnitEncoders for the network will properly handle the NetworkInput. */ protected NetworkInput[] dataset = null; /** * SigmaPiFitness basic constructor * * This constructor takes a SigmaPiNetwork and an array of NetworkInputs. * The network is the network that all SigmaPiChromosomes will be loaded * into for testing and the array of NetworkInputs is the dataset of * NetworkInputs that the chromosomes will be tested against. It is * assumed that the network will work properly with any Chromosomes tested * with this FitnessFunction. It also assumes that the network's input * and output UnitEncoders will work properly with the dataset of * NetworkInputs. * * @param network The SigmaPiNetwork to load SigmaPiChromosomes into for * testing their fitness. * @param dataset The array of NetworkInputs that make up the dataset for * which SigmaPiChromosomes will be tested against when loaded * into a SigmaPiNetwork. */ public SigmaPiFitness( SigmaPiNetwork network, // Network for chromosomes. NetworkInput[] dataset) // Dataset to test with. { this.network = network; this.dataset = dataset; } /** * calculateFitness * * This method takes a Chromosome, which assumes that it is a * SigmaPiChromosome, and returns the fitness of that SigmaPiChromosome. * The fitness is calculated by loading the SigmaPiChromosome into the * SigmaPiNetwork that this SigmaPiFitness was created for. It then tests * the network against all of the NetworkInputs in the dataset that the * SigmaPiFitness was created for and returns the average mean squared * error of the network across all inputs as the fitness value. Thus, a * better Chromosome has a smaller fitness value.. * * @param chromosome The Chromosome to calculate the fitness of. It is * assumed to be a SigmaPiChromosome that can be loaded into the * SigmaPiNetwork that this SigmaPiFitness is using for testing. * @return The fitness value of the SigmaPiChromosome, which is the * average mean squared error that the SigmaPiNetwork defined by * the SigmaPiChromosome gets over all of the NetworkInputs in * the dataset for this SigmaPiFitness object. The fitness value * is a double and smaller fitness values are better. */ public double calculateFitness( Chromosome chromosome) // Chromosome to calculate. { // Convert the Chromosome to a SigmaPiChromosome. SigmaPiChromosome chrom = (SigmaPiChromosome) chromosome; // Load the SigmaPiChromosome into the SigmaPiNetwork. chrom.loadIntoNetwork(network); // Calculate the sum of errors. double errorSum = 0.0; // Figure out the error for each NetworkInput. for (int i = 0; i < dataset.length; i++) // Test the network on the ith input. errorSum += network.testOnInput(dataset[i]); // Return the average mean squared error. return errorSum / (double) dataset.length; } }