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 java.util.ArrayList;
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.jamesii.core.util.misc.Pair;
24  import org.jamesii.core.util.misc.Strings;
25  
26  import p3j.misc.MatrixDimension;
27  import p3j.pppm.PPPModelFactory;
28  import p3j.pppm.ProjectionModel;
29  import p3j.pppm.SubPopulation;
30  import p3j.pppm.parameters.Parameter;
31  import p3j.pppm.parameters.ParameterAssignment;
32  import p3j.pppm.parameters.ParameterInstance;
33  import p3j.pppm.parameters.ParameterType;
34  import p3j.pppm.parameters.Population;
35  import p3j.pppm.sets.Set;
36  import p3j.pppm.sets.SetType;
37  
38  /**
39   * Tests {@link ExhaustiveAssignmentGenerator}.
40   * 
41   * TODO: Move general set-up to super class, add test for randomised assignment
42   * generator.
43   * 
44   * @author Christina Bohk
45   * @author Roland Ewald
46   */
47  public class TestExhaustiveAssignmentGenerator extends TestCase {
48  
49    /** The high probability constant. */
50    final static double PROB_HIGH = 0.8;
51  
52    /** The low probability constant, sums with high probability to 1. */
53    final static double PROB_LOW = 1 - PROB_HIGH;
54  
55    /** The even probability. */
56    final static double PROB_EVEN = 0.5;
57  
58    /** The overall number of combinations. */
59    final int NUM_COMBINATIONS = 64;
60  
61    /** The parameter instances. */
62    final ParameterInstance[] instances = new ParameterInstance[] {
63        new ParameterInstance(0, TestSetManager.instance1MigF.getParameter()),
64        new ParameterInstance(1, TestSetManager.instance2MigM.getParameter()),
65        new ParameterInstance(2, new Parameter(2, false,
66            ParameterType.MORTALITY.getMaleLabelFor(new SubPopulation()),
67            MatrixDimension.AGES, MatrixDimension.YEARS, Population.CUSTOM)),
68        new ParameterInstance(3, new Parameter(3, false,
69            ParameterType.MORTALITY.getFemaleLabelFor(new SubPopulation()),
70            MatrixDimension.AGES, MatrixDimension.YEARS, Population.CUSTOM)) };
71  
72    /** The projection model. */
73    final ProjectionModel projectionModel = new ProjectionModel(
74        "Test Projection", "Just a test projection", 1, 2, 100,
75        PPPModelFactory.DEFAULT_JUMP_OFF_YEAR,
76        PPPModelFactory.DEFAULT_SUBPOPULATION_MODEL);
77  
78    /** The available Settypes. */
79    SetType setType;
80  
81    /** The second Settype. */
82    SetType secondSetType;
83  
84    /** The array of test sets. */
85    final Set[] sets = new Set[4];
86  
87    /** The array of test parameter assignments. */
88    ParameterAssignment[] assignments;
89  
90    /** The assignment generator. */
91    ExhaustiveAssignmentGenerator aag;
92  
93    /*
94     * (non-Javadoc)
95     * 
96     * @see junit.framework.TestCase#setUp()
97     */
98    @Override
99    public void setUp() {
100 
101     List<ParameterInstance> instanceList = new ArrayList<ParameterInstance>();
102     for (ParameterInstance instance : instances) {
103       instanceList.add(instance);
104     }
105     projectionModel.setAllParameterInstances(instanceList);
106     projectionModel.init();
107 
108     setUpSets();
109     setUpAssignments();
110 
111     aag = new ExhaustiveAssignmentGenerator();
112     ExhaustiveSimParameters asp = new ExhaustiveSimParameters();
113     aag.setParameters(asp);
114     aag.init(projectionModel);
115   }
116 
117   /**
118    * Sets the up sets.
119    */
120   private void setUpSets() {
121     setType = projectionModel.createSetType("Test Settype",
122         "This is just a test Settype");
123     projectionModel.assignParameterInstance(instances[0], setType, false);
124     projectionModel.assignParameterInstance(instances[1], setType, false);
125 
126     secondSetType = projectionModel.createSetType("Test Second Settype",
127         "This is a second Settype.");
128     projectionModel.assignParameterInstance(instances[2], secondSetType, false);
129 
130     sets[0] = setType.createSet("1-1", "", PROB_HIGH);
131     sets[1] = setType.createSet("1-2", "", PROB_LOW);
132     sets[2] = secondSetType.createSet("2-1", "", PROB_EVEN);
133     sets[3] = secondSetType.createSet("2-2", "", PROB_EVEN);
134   }
135 
136   /**
137    * Sets the up assignments.
138    */
139   private void setUpAssignments() {
140 
141     // @formatter:off
142     assignments = new ParameterAssignment[] {
143         new ParameterAssignment(instances[0], "1-1 1-1", "", PROB_HIGH, 0.0,
144             null),
145         new ParameterAssignment(instances[0], "1-1 1-2", "", PROB_LOW, 0.0,
146             null),
147         new ParameterAssignment(instances[1], "1-1 2-1", "", PROB_EVEN, 0.0,
148             null),
149         new ParameterAssignment(instances[1], "1-1 2-2", "", PROB_EVEN, 0.0,
150             null),
151         new ParameterAssignment(instances[0], "1-2 1-1", "", PROB_EVEN, 0.0,
152             null),
153         new ParameterAssignment(instances[0], "1-2 1-2", "", PROB_EVEN, 0.0,
154             null),
155         new ParameterAssignment(instances[1], "1-2 2-1", "", PROB_EVEN, 0.0,
156             null),
157         new ParameterAssignment(instances[1], "1-2 2-2", "", PROB_EVEN, 0.0,
158             null),
159         new ParameterAssignment(instances[2], "2-1 1-1", "", PROB_HIGH, 0.0,
160             null),
161         new ParameterAssignment(instances[2], "2-1 1-2", "", PROB_LOW, 0.0,
162             null),
163         new ParameterAssignment(instances[2], "2-2 1-1", "", PROB_EVEN, 0.0,
164             null),
165         new ParameterAssignment(instances[2], "2-2 1-2", "", PROB_EVEN, 0.0,
166             null),
167         new ParameterAssignment(instances[3], "Def 1", "", PROB_HIGH, 0.0, null),
168         new ParameterAssignment(instances[3], "Def 2", "", PROB_LOW, 0.0, null) };
169     // @formatter:on
170     sets[0].addParameterAssignment(assignments[0]);
171     sets[0].addParameterAssignment(assignments[1]);
172     sets[0].addParameterAssignment(assignments[2]);
173     sets[0].addParameterAssignment(assignments[3]);
174 
175     sets[1].addParameterAssignment(assignments[4]);
176     sets[1].addParameterAssignment(assignments[5]);
177     sets[1].addParameterAssignment(assignments[6]);
178     sets[1].addParameterAssignment(assignments[7]);
179 
180     sets[2].addParameterAssignment(assignments[8]);
181     sets[2].addParameterAssignment(assignments[9]);
182 
183     sets[3].addParameterAssignment(assignments[10]);
184     sets[3].addParameterAssignment(assignments[11]);
185 
186     projectionModel.getDefaultSet().addParameterAssignment(assignments[12]);
187     projectionModel.getDefaultSet().addParameterAssignment(assignments[13]);
188   }
189 
190   /**
191    * Tests the overall set up.
192    */
193   public void testTheSetUp() {
194     assertEquals(1, projectionModel.getDefaultSetType().getDefinedParameters()
195         .size());
196     assertEquals(3, projectionModel.getAllSetTypes().size());
197   }
198 
199   /**
200    * Test assignment generator.
201    */
202   public void testAssignmentGenerator() {
203 
204     assertEquals(NUM_COMBINATIONS,
205         aag.calculateNumOfCombinations(projectionModel));
206     Pair<?, ?>[] pairs = new Pair<?, ?>[NUM_COMBINATIONS];
207     for (int i = 0; i < NUM_COMBINATIONS; i++) {
208       Assignment assignment = aag.peek();
209       if (assignment != null) {
210         System.out.println(Strings.dispArray(assignment.getAssignmentIndices()
211             .toArray()) + " / prob:" + assignment.getProbability());
212       }
213       pairs[i] = aag.nextAssignment();
214       assertNotNull(i + "-th assignment does exist.", pairs[i]);
215     }
216     assertNull(aag.nextAssignment());
217   }
218 }