1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an
10   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11   * either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package fuzzy.df;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.text.DecimalFormat;
19  
20  import org.apache.commons.functor.generator.range.DoubleRange;
21  import org.junit.Test;
22  
23  import fuzzy.mf.MembershipFunction;
24  import fuzzy.mf.SigmoidalMembershipFunction;
25  import fuzzy.mf.TrapezoidalMembershipFunction;
26  
27  
28  /**
29   * Tests for Smallest Of Maxima Defuzzification Function.
30   *
31   * @since 0.2
32   * @see LargestOfMaximaDefuzzificationFunction
33   */
34  public class TestLargestOfMaximaDefuzzificationFunction extends BaseDefuzzificationFunctionTest<LargestOfMaximaDefuzzificationFunction<Double>>{
35  
36  	@Override
37  	protected LargestOfMaximaDefuzzificationFunction<Double> makeDefuzzificationFunction() {
38  		final LargestOfMaximaDefuzzificationFunction<Double> df = new LargestOfMaximaDefuzzificationFunction<Double>();
39  		return df;
40  	}
41  
42  	@Test
43  	public void testDefuzzification() {
44  	    LargestOfMaximaDefuzzificationFunction<Double> df = makeDefuzzificationFunction();
45  		DoubleRange range = new DoubleRange(-10.0, 10.0, 0.1);
46  		MembershipFunction<Double> mf = new TrapezoidalMembershipFunction(-10.0, -8.0, -4.0, 7.0);
47  		Double d = df.apply(range, mf);
48  		assertEquals(Integer.valueOf(-8).toString(), new DecimalFormat("#.#").format(d));
49  	}
50  
51  	@Test()
52  	public void testDefuzzificationEmptySet() {
53  	    LargestOfMaximaDefuzzificationFunction<Double> df = makeDefuzzificationFunction();
54  		DoubleRange range = new DoubleRange(0.0, 0.0);
55  		MembershipFunction<Double> mf = new SigmoidalMembershipFunction(-10.0, 10.0);
56  		Double d = df.apply(range, mf);
57  		assertEquals(Double.valueOf(0.0), d);
58  	}
59  
60  }