1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.simulation.assignments.exhaustive;
17  
18  import junit.framework.TestCase;
19  import p3j.pppm.parameters.ParameterAssignment;
20  import p3j.pppm.sets.Set;
21  
22  /**
23   * Tests {@link SetTypeManager}. Adds a second set to the test Settype. This is
24   * more probable but just contains a single assignment combination.
25   * 
26   * @author Christina Bohk
27   * @author Roland Ewald
28   */
29  public class TestSetTypeManager extends TestCase {
30  
31    /** The Constant PRECISION. */
32    final static double PRECISION = 0.000001;
33  
34    /** The overall number of assignments. */
35    final static int NUM_ASSIGNMENTS = 5;
36  
37    /** The test for the set manager. */
38    protected final TestSetManager setManagerTest = new TestSetManager();
39  
40    /** The single assignment for the first instance. */
41    protected final ParameterAssignment assignmentInst1 = new ParameterAssignment(
42        TestSetManager.instance1MigF, "#1", "", 1.0, 0.0, null);
43  
44    /** The single assignment for the second instance. */
45    protected final ParameterAssignment assignmentInst2 = new ParameterAssignment(
46        TestSetManager.instance2MigM, "#2", "", 1.0, 0.0, null);
47  
48    /** The second set. */
49    Set secondSet = setManagerTest.setType.createSet("Second test set.",
50        "This is just another test set.", TestSetManager.probSecond / 2);
51    {
52      setManagerTest.set.setProbability(1 - (TestSetManager.probSecond / 2));
53      secondSet.addParameterAssignment(assignmentInst1);
54      secondSet.addParameterAssignment(assignmentInst2);
55    }
56  
57    /** The set manager. */
58    SetTypeManager setTypeManager;
59  
60    @Override
61    public void setUp() {
62      setTypeManager = new SetTypeManager(setManagerTest.setType);
63    }
64  
65    /**
66     * Tests the Settype manager.
67     */
68    public void testSetTypeManager() {
69      assertTrue(setTypeManager.createAssignments(NUM_ASSIGNMENTS));
70      assertFalse(setTypeManager.createAssignments(NUM_ASSIGNMENTS + 1));
71      assertFalse(setTypeManager.createAssignments(NUM_ASSIGNMENTS + 100));
72  
73      // Check most likely assignment
74      for (ParameterAssignment pa : setTypeManager.getAssignment(0).values()) {
75        assertEquals(TestSetManager.probFirst, pa.getProbability());
76      }
77      assertEquals((1.0 - (TestSetManager.probSecond / 2.0))
78          * (TestSetManager.probFirst * TestSetManager.probFirst),
79          setTypeManager.getProbability(0), PRECISION);
80  
81      // Check most unlikely assignment
82      for (ParameterAssignment pa : setTypeManager.getAssignment(
83          NUM_ASSIGNMENTS - 1).values()) {
84        assertEquals(TestSetManager.probSecond, pa.getProbability());
85      }
86      assertEquals((1 - TestSetManager.probSecond / 2.0)
87          * TestSetManager.probSecond * TestSetManager.probSecond,
88          setTypeManager.getProbability(NUM_ASSIGNMENTS - 1), PRECISION);
89    }
90  
91  }