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