All Downloads are FREE. Search and download functionalities are using the official Maven repository.

model.util.RegionConsoleViewer Maven / Gradle / Ivy

There is a newer version: 2.3.3
Show newest version
package model.util;

import model.MARK_II.Cell;
import model.MARK_II.Column;
import model.MARK_II.Region;
import model.MARK_II.Synapse;

import java.awt.*;
import java.util.Set;

/**
 * @author Quinn Liu ([email protected])
 * @version June 18, 2013
 */
public class RegionConsoleViewer {
    /**
     * Returns a 2-D array of chars representing each Column's activeState
     * within a Region inside of a SpatialPooler object. 'a' represents an
     * active Column while 'i' represents an inactive Column for the current
     * time step.
     *
     * @param region
     * @return A 2-D char array of Columns' overlapScores.
     */
    public static char[][] getColumnActiveStatesCharArray(Region region) {
        char[][] columnActiveStates = new char[region.getNumberOfRowsAlongRegionYAxis()][region
                .getNumberOfColumnsAlongRegionXAxis()];
        Column[][] columns = region.getColumns();
        for (int row = 0; row < columnActiveStates.length; row++) {
            for (int column = 0; column < columnActiveStates[row].length; column++) {
                if (columns[row][column].getActiveState()) {
                    // 'a' represents an active Column at a specific time step
                    columnActiveStates[row][column] = 'a';
                } else {
                    // 'i' represents an inactive Column at a specific time step
                    columnActiveStates[row][column] = 'i';
                }
            }
        }
        return columnActiveStates;
    }

    /**
     * Returns a 2-D array of integers representing each Column's overlapScore
     * within a Region inside of a SpatialPooler object.
     *
     * @param region
     * @return A 2-D integer array of Columns' overlapScores.
     */
    public static int[][] getColumnOverlapScoresIntArray(Region region) {
        int[][] columnOverlapScores = new int[region.getNumberOfRowsAlongRegionYAxis()][region
                .getNumberOfColumnsAlongRegionXAxis()];
        Column[][] columns = region.getColumns();
        for (int row = 0; row < columnOverlapScores.length; row++) {
            for (int column = 0; column < columnOverlapScores[row].length; column++) {
                columnOverlapScores[row][column] = columns[row][column].getOverlapScore();
            }
        }
        return columnOverlapScores;
    }

    /**
     * 0 means the SensorCell is disconnected from the Synapse at that location.
     * Any number 2-9 represents the permanenceValue of the Synapse at that
     * location rounded down in the tenth decimal place.
     *  
     * For example, a Synapse with a permanenceValue of 0.36 will be represented
     * as a 3 in the int[][] array being returned.
     */
    public static int[][] getSynapsePermanencesIntArray(Region region) {
        Dimension bottomLayerDimensions = region.getBottomLayerXYAxisLength();
        int[][] synapsePermanences = new int[bottomLayerDimensions.width][bottomLayerDimensions.height];

        Column[][] columns = region.getColumns();
        for (int row = 0; row < columns.length; row++) {
            for (int column = 0; column < columns[row].length; column++) {
                Set> synapses = columns[row][column]
                        .getProximalSegment().getSynapses();

                for (Synapse synapse : synapses) {
                    if (synapse.getPermanenceValue() < 0.2) {
                        synapsePermanences[synapse.getCellColumn()][synapse
                                .getCellRow()] = 0;
                    } else {
                        // permanenceTimesTen = round((0.9999 - 0.05555) * 10)
                        int permanenceTimesTen = (int) Math.round(((synapse
                                .getPermanenceValue() - 0.055555) * 10));
                        synapsePermanences[synapse.getCellColumn()][synapse
                                .getCellRow()] = permanenceTimesTen;
                    }
                }
            }
        }
        return synapsePermanences;
    }

    /**
     * Prints a byte 2-D array to the console.
     *
     * @param doubleByteArray The 2-D byte array to be printed.
     */
    public static void printDoubleByteArray(byte[][] doubleByteArray) {
        for (int row = 0; row < doubleByteArray.length; row++) {
            System.out.println();
            for (int column = 0; column < doubleByteArray[row].length; column++) {
                System.out.print(doubleByteArray[row][column]);
            }
        }
    }

    /**
     * Prints a int 2-D array to the console.
     *
     * @param doubleIntArray The 2-D int array to be printed.
     */
    public static void printDoubleIntArray(int[][] doubleIntArray) {
        for (int row = 0; row < doubleIntArray.length; row++) {
            System.out.println();
            for (int column = 0; column < doubleIntArray[row].length; column++) {
                System.out.printf("%1d", doubleIntArray[row][column]);
            }
        }
    }

    /**
     * Prints a char 2-D array to the console.
     *
     * @param doubleCharArray The 2-D char array to be printed.
     * @return The printed 2-D char array as a String.
     */
    public static String doubleCharArrayAsString(char[][] doubleCharArray) {
        String doubleCharArrayAsString = "";
        for (int row = 0; row < doubleCharArray.length; row++) {
            boolean isAtBeginning = (row == 0);
            boolean isAtEnd = (row == doubleCharArray.length);
            if (isAtBeginning || isAtEnd) {
                // don't add anything
            } else {
                doubleCharArrayAsString += "\n";
            }

            for (int column = 0; column < doubleCharArray[row].length; column++) {
                doubleCharArrayAsString += doubleCharArray[row][column];
            }
        }
        return doubleCharArrayAsString;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy