/* * File: DoubleArrayEncoder.java * Author: Justin Basilico * Course: PO CS 152: Neural Networks * Assignment: Final Project * Updated: 2001.12.18 * Created: 2001.12.03 * * Description: * This file contains the DoubleArrayEncoder class, which is a simple * UnitEncoder that encodes a given property, which is an array of doubles, by * just copying the values into the given array of units. * * This is pretty much the same as the DoubleArrayEncoder used in Project 4. * * Copyright: Justin Basilico (2001). */ /** * DoubleArrayEncoder * * This UnitEncoder uses an integer property number that should be an array of * doubles. If it is, it encodes into the given array of network units by * copying the values from the array between the two indices the UnitEncoder * was created for. Thus, its encoding is just doing a copy of double values. * * @author Justin Basilico * @version 2001.12.18 */ public class DoubleArrayEncoder extends UnitEncoder { /** propertyNum * The integer property number to encode. */ private int propertyNum = 0; /** arrayStart * The integer starting index to start copying at. */ private int arrayStart = 0; /** arrayEnd * The integer ending index to copy up to (exclusive). */ private int arrayEnd = 0; /** copyLength * The size of area the encoder is copying. */ private int copyLength = 0; /** * DoubleArrayEncoder basic constructor * * This constructor takes the integer property number of the double[] * property the encoder is encoding for, the integer index to start the * copying at (inclusive), and the integer index to stop the copying at * (exclusive). It calls the default constructor for the UnitEncoder class * and then sets the internal data based on the given values. * * @param propertyName The integer property number of the double[] * property of a NetworkInput to encode. * @param arrayStart The integer index to start the encoding/copying at. * @param arrayEnd The integer index to stop the encoding/copying at. * This means that the element at the index of arrayEnd will no * be copied. */ public DoubleArrayEncoder( int propertyNum, // Property number to copy. int arrayStart, // Index to start copying at. int arrayEnd) // Index to stop copying at. { super(); this.propertyNum = propertyNum; this.arrayStart = arrayStart; this.arrayEnd = arrayEnd; this.copyLength = Math.max(arrayEnd - arrayStart, 0); } /** * DoubleArrayEncoder basic constructor * * This constructor takes the integer property number of the double[] * property the encoder is encoding for, the integer index to start the * copying at (inclusive), the integer index to stop the copying at * (exclusive), and the integer offset to start the encoding at. It calls * the constructor for the UnitEncoder class and passes the offset to it, * then sets the internal data based on the given values. * * @param propertyName The integer property number of the double[] * property of a NetworkInput to encode. * @param arrayStart The integer index to start the encoding/copying at. * @param arrayEnd The integer index to stop the encoding/copying at. * This means that the element at the index of arrayEnd will no * be copied. * @param offset The integer offset to start the encoding at. */ public DoubleArrayEncoder( int propertyNum, // Property number to copy. int arrayStart, // Index to start copying at. int arrayEnd, // Index to stop copying at. int offset) // Offset to start encoding. { super(offset); this.propertyNum = propertyNum; this.arrayStart = arrayStart; this.arrayEnd = arrayEnd; this.copyLength = Math.max(arrayEnd - arrayStart, 0); } /** * getDimensionality * * This method returns the integer dimensionality of the encoding, which * is just the length of the part of the array that the DoubleArrayEncoder * is copying. * * @return The integer dimensionality of the encoding, which is the length * of the array that is being copied/encoded. */ public int getDimensionality() { return copyLength; } /** * encode * * This method takes a NetworkInput and the array of doubles to encode the * double[] property of the NetworkInput. What the encoding does is call * the getProperty(int) method on the NetworkInput with the property * that this DoubleArrayEncoder was created for. If it returns a double[], * it copies the values of that double[] from the starting index to the * ending index that the DoubleArrayEncoder was created with into the * target array, starting at the encoding offset. If the value returned * was null or not a double[], it fills the array with 0.0 for the range * the encoder is encoding for. * * @param input The NetworkInput to encode. * @param target The array of doubles that are the units to encode into. */ public void encode( NetworkInput input, // Input to encode. double[] units) // Network units to encode. { if ( (units.length - offset) < copyLength ) { // Error: Target doesn't have proper dimensionality. System.err.println("DoubleArrayEncoder.encode(): Target does " + "not have the correct dimensionality."); return; } // Get the Object that we are going to encode. Object propertyObject = input.getProperty(propertyNum); if ( propertyObject != null && propertyObject instanceof double[] ) { // The property was an array of doubles, copy over the data. double[] array = (double[]) propertyObject; for (int i = arrayStart; i < arrayEnd && i < array.length; i++) // Copy the ith array entry. units[offset + i - arrayStart] = array[i]; } else { // The property was null or wasn't an array of doubles. Make the // proper range all 0.0. for (int i = arrayStart; i < arrayEnd; i++) units[offset + i - arrayStart] = 0.0; } } }