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;
19  
20  import java.util.Set;
21  import java.util.HashSet;
22  import java.util.List;
23  
24  import org.junit.Test;
25  import org.junit.Assert;
26  
27  import org.apache.commons.rng.UniformRandomProvider;
28  import org.apache.commons.rng.simple.RandomSource;
29  
30  import org.apache.commons.math4.neuralnet.oned.NeuronString;
31  
32  import static org.junit.jupiter.api.Assertions.assertThrows;
33  
34  /**
35   * Tests for {@link MapRanking} class.
36   */
37  public class MapRankingTest {
38  
39      /*
40       * Test assumes that the network is
41       *
42       *  0-----1-----2
43       */
44      @Test
45      public void testFindClosestNeuron() {
46          final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create();
47          final FeatureInitializer init
48              = new OffsetFeatureInitializer(FeatureInitializerFactory.uniform(rng, -0.1, 0.1));
49          final FeatureInitializer[] initArray = {init};
50  
51          final MapRanking ranking = new MapRanking(new NeuronString(3, false, initArray).getNetwork(),
52                                                    new EuclideanDistance());
53  
54          final Set<Neuron> allBest = new HashSet<>();
55          final Set<Neuron> best = new HashSet<>();
56          double[][] features;
57  
58          // The following tests ensures that
59          // 1. the same neuron is always selected when the input feature is
60          //    in the range of the initializer,
61          // 2. different network's neuron have been selected by inputs features
62          //    that belong to different ranges.
63  
64          best.clear();
65          features = new double[][] {
66              {-1 },
67              {0.4 },
68          };
69          for (double[] f : features) {
70              best.addAll(ranking.rank(f, 1));
71          }
72          Assert.assertEquals(1, best.size());
73          allBest.addAll(best);
74  
75          best.clear();
76          features = new double[][] {
77              {0.6 },
78              {1.4 },
79          };
80          for (double[] f : features) {
81              best.addAll(ranking.rank(f, 1));
82          }
83          Assert.assertEquals(1, best.size());
84          allBest.addAll(best);
85  
86          best.clear();
87          features = new double[][] {
88              {1.6 },
89              {3 },
90          };
91          for (double[] f : features) {
92              best.addAll(ranking.rank(f, 1));
93          }
94          Assert.assertEquals(1, best.size());
95          allBest.addAll(best);
96  
97          Assert.assertEquals(3, allBest.size());
98      }
99  
100     @Test
101     public void testRankPrecondition() {
102         final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create();
103         final FeatureInitializer init
104             = new OffsetFeatureInitializer(FeatureInitializerFactory.uniform(rng, -0.1, 0.1));
105         final FeatureInitializer[] initArray = {init};
106 
107         final EuclideanDistance distance = new EuclideanDistance();
108         final Network network = new NeuronString(3, false, initArray).getNetwork();
109         final MapRanking mapRanking = new MapRanking(network, distance);
110 
111         assertThrows(IllegalArgumentException.class, () ->
112                 mapRanking.rank(new double[]{-1}, 0)
113         );
114     }
115 
116     @Test
117     public void testSort() {
118         final Set<Neuron> list = new HashSet<>();
119 
120         for (int i = 0; i < 4; i++) {
121             list.add(new Neuron(i, new double[] {i - 0.5}));
122         }
123 
124         final MapRanking rank = new MapRanking(list, new EuclideanDistance());
125         final List<Neuron> sorted = rank.rank(new double[] {3.4});
126 
127         final long[] expected = new long[] {3, 2, 1, 0};
128         for (int i = 0; i < list.size(); i++) {
129             Assert.assertEquals(expected[i], sorted.get(i).getIdentifier());
130         }
131     }
132 
133 }