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  import static org.junit.Assert.fail;
18  
19  import java.text.DecimalFormat;
20  
21  import org.apache.commons.functor.generator.range.DoubleRange;
22  import org.junit.Test;
23  
24  import fuzzy.mf.MembershipFunction;
25  import fuzzy.mf.SigmoidalMembershipFunction;
26  
27  
28  /**
29   * Tests for Centroid Defuzzification Function.
30   *
31   * @since 0.2
32   * @see CentroidDefuzzificationFunction
33   */
34  public class TestCentroidDefuzzificationFunction extends BaseDefuzzificationFunctionTest<CentroidDefuzzificationFunction<Double>>{
35  
36  	@Override
37  	protected CentroidDefuzzificationFunction<Double> makeDefuzzificationFunction() {
38  		final CentroidDefuzzificationFunction<Double> df = new CentroidDefuzzificationFunction<Double>();
39  		return df;
40  	}
41  
42  	@Test
43  	public void testDefuzzification() {
44  		CentroidDefuzzificationFunction<Double> df = makeDefuzzificationFunction();
45  		DoubleRange range = new DoubleRange(-10.0, 10.0, 0.1);
46  		MembershipFunction<Double> mf = new SigmoidalMembershipFunction(-10.0, 10.0);
47  		Double d = df.apply(range, mf);
48  		assertEquals(Integer.valueOf(0).toString(), new DecimalFormat("#.#").format(Math.abs(d)));
49  	}
50  
51  	@Test(expected=IllegalArgumentException.class)
52  	public void testDefuzzificationEmptySet() {
53  		CentroidDefuzzificationFunction<Double> df = makeDefuzzificationFunction();
54  		DoubleRange range = new DoubleRange(0.0, 0.0);
55  		MembershipFunction<Double> mf = new SigmoidalMembershipFunction(-10.0, 10.0);
56  		df.apply(range, mf);
57  		fail("Not supposed to get here");
58  	}
59  
60  }