1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.math4.neuralnet.twod.util; 19 20 import java.util.Map; 21 import java.util.concurrent.ConcurrentHashMap; 22 import org.apache.commons.math4.neuralnet.Neuron; 23 import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D; 24 25 /** 26 * Helper class to find the grid coordinates of a neuron. 27 * @since 3.6 28 */ 29 public class LocationFinder { 30 /** Identifier to location mapping. */ 31 private final Map<Long, Location> locations = new ConcurrentHashMap<>(); 32 33 /** 34 * Container holding a (row, column) pair. 35 */ 36 public static class Location { 37 /** Row index. */ 38 private final int row; 39 /** Column index. */ 40 private final int column; 41 42 /** 43 * @param row Row index. 44 * @param column Column index. 45 */ 46 public Location(int row, 47 int column) { 48 this.row = row; 49 this.column = column; 50 } 51 52 /** 53 * @return the row index. 54 */ 55 public int getRow() { 56 return row; 57 } 58 59 /** 60 * @return the column index. 61 */ 62 public int getColumn() { 63 return column; 64 } 65 } 66 67 /** 68 * Builds a finder to retrieve the locations of neurons that 69 * belong to the given {@code map}. 70 * 71 * @param map Map. 72 * 73 * @throws IllegalStateException if the network contains non-unique 74 * identifiers. This indicates an inconsistent state due to a bug in 75 * the construction code of the underlying 76 * {@link org.apache.commons.math4.neuralnet.Network network}. 77 */ 78 public LocationFinder(NeuronSquareMesh2D map) { 79 final int nR = map.getNumberOfRows(); 80 final int nC = map.getNumberOfColumns(); 81 82 for (int r = 0; r < nR; r++) { 83 for (int c = 0; c < nC; c++) { 84 final Long id = map.getNeuron(r, c).getIdentifier(); 85 if (locations.get(id) != null) { 86 throw new IllegalStateException(); 87 } 88 locations.put(id, new Location(r, c)); 89 } 90 } 91 } 92 93 /** 94 * Retrieves a neuron's grid coordinates. 95 * 96 * @param n Neuron. 97 * @return the (row, column) coordinates of {@code n}, or {@code null} 98 * if no such neuron belongs to the {@link #LocationFinder(NeuronSquareMesh2D) 99 * map used to build this instance}. 100 */ 101 public Location getLocation(Neuron n) { 102 return locations.get(n.getIdentifier()); 103 } 104 }