home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (LocationFinder.java)

This example Java source code file (LocationFinder.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

hashmap, location, locationfinder, long, map, mathillegalstateexception, util

The LocationFinder.java Java example source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.math3.ml.neuralnet.twod.util;

import java.util.Map;
import java.util.HashMap;
import org.apache.commons.math3.ml.neuralnet.Neuron;
import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
import org.apache.commons.math3.exception.MathIllegalStateException;

/**
 * Helper class to find the grid coordinates of a neuron.
 * @since 3.6
 */
public class LocationFinder {
    /** Identifier to location mapping. */
    private final Map<Long, Location> locations = new HashMap();

    /**
     * Container holding a (row, column) pair.
     */
    public static class Location {
        /** Row index. */
        private final int row;
        /** Column index. */
        private final int column;

        /**
         * @param row Row index.
         * @param column Column index.
         */
        public Location(int row,
                        int column) {
            this.row = row;
            this.column = column;
        }

        /**
         * @return the row index.
         */
        public int getRow() {
            return row;
        }

        /**
         * @return the column index.
         */
        public int getColumn() {
            return column;
        }
    }

    /**
     * Builds a finder to retrieve the locations of neurons that
     * belong to the given {@code map}.
     *
     * @param map Map.
     *
     * @throws MathIllegalStateException if the network contains non-unique
     * identifiers.  This indicates an inconsistent state due to a bug in
     * the construction code of the underlying
     * {@link org.apache.commons.math3.ml.neuralnet.Network network}.
     */
    public LocationFinder(NeuronSquareMesh2D map) {
        final int nR = map.getNumberOfRows();
        final int nC = map.getNumberOfColumns();

        for (int r = 0; r < nR; r++) {
            for (int c = 0; c < nC; c++) {
                final Long id = map.getNeuron(r, c).getIdentifier();
                if (locations.get(id) != null) {
                    throw new MathIllegalStateException();
                }
                locations.put(id, new Location(r, c));
            }
        }
    }

    /**
     * Retrieves a neuron's grid coordinates.
     *
     * @param n Neuron.
     * @return the (row, column) coordinates of {@code n}, or {@code null}
     * if no such neuron belongs to the {@link #LocationFinder(NeuronSquareMesh2D)
     * map used to build this instance}.
     */
    public Location getLocation(Neuron n) {
        return locations.get(n.getIdentifier());
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java LocationFinder.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.