View Javadoc
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 org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.simple.RandomSource;
22  
23  import org.apache.commons.math4.neuralnet.Network;
24  import org.apache.commons.math4.neuralnet.FeatureInitializer;
25  import org.apache.commons.math4.neuralnet.FeatureInitializerFactory;
26  import org.apache.commons.math4.neuralnet.SquareNeighbourhood;
27  import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  /**
32   * Test for {@link LocationFinder}.
33   */
34  public class LocationFinderTest {
35      private final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create();
36      private final FeatureInitializer init = FeatureInitializerFactory.uniform(rng, 0, 2);
37  
38      /*
39       * Test assumes that the network is
40       *
41       *  0-----1
42       *  |     |
43       *  |     |
44       *  2-----3
45       */
46      @Test
47      public void test2x2Network() {
48          final FeatureInitializer[] initArray = {init};
49          final NeuronSquareMesh2D map = new NeuronSquareMesh2D(2, false,
50                                                                2, false,
51                                                                SquareNeighbourhood.VON_NEUMANN,
52                                                                initArray);
53          final LocationFinder finder = new LocationFinder(map);
54          final Network net = map.getNetwork();
55          LocationFinder.Location loc;
56  
57          loc = finder.getLocation(net.getNeuron(0));
58          Assert.assertEquals(0, loc.getRow());
59          Assert.assertEquals(0, loc.getColumn());
60  
61          loc = finder.getLocation(net.getNeuron(1));
62          Assert.assertEquals(0, loc.getRow());
63          Assert.assertEquals(1, loc.getColumn());
64  
65          loc = finder.getLocation(net.getNeuron(2));
66          Assert.assertEquals(1, loc.getRow());
67          Assert.assertEquals(0, loc.getColumn());
68  
69          loc = finder.getLocation(net.getNeuron(3));
70          Assert.assertEquals(1, loc.getRow());
71          Assert.assertEquals(1, loc.getColumn());
72      }
73  }