View Javadoc

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.random;
17  
18  import james.core.math.random.generators.IRandom;
19  import james.core.util.misc.Pair;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import p3j.misc.errors.GeneratorError;
28  import p3j.misc.math.RandomNumberChecks;
29  import p3j.pppm.IProjectionModel;
30  import p3j.pppm.parameters.ParameterAssignment;
31  import p3j.pppm.parameters.ParameterAssignmentSet;
32  import p3j.pppm.parameters.ParameterInstance;
33  import p3j.pppm.sets.Set;
34  import p3j.pppm.sets.SetType;
35  import p3j.simulation.assignments.plugintype.IParamAssignmentGenerator;
36  
37  /**
38   * Simple parameter assignment generator that just employs random sampling.
39   * 
40   * Created: August 17, 2008
41   * 
42   * @author Christina Bohk
43   * @author Roland Ewald
44   * 
45   */
46  public class RandomParamAssignmentGenerator implements
47      IParamAssignmentGenerator {
48  
49  	/** The projection for which random assignments shall be generated. */
50  	private IProjectionModel projection;
51  
52  	@Override
53  	public void init(IProjectionModel proj) {
54  		projection = proj;
55  	}
56  
57  	@Override
58  	public Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>> chooseParamAssignments(
59  	    IRandom random) {
60  
61  		Map<ParameterInstance, ParameterAssignment> assignments = new HashMap<ParameterInstance, ParameterAssignment>();
62  		RandomNumberChecks rnc = RandomNumberChecks.getInstance();
63  		List<GeneratorError> errors = new ArrayList<GeneratorError>();
64  
65  		// Get all Settypes
66  		List<SetType> setTypes = projection.getAllSetTypes();
67  
68  		// Choose a set from each type randomly
69  		List<Set> sets = new ArrayList<Set>();
70  		for (SetType setType : setTypes) {
71  			sets.add(chooseSetRandomly(setType, rnc, random, errors));
72  		}
73  
74  		// Choose parameter from each set randomly
75  		for (Set set : sets) {
76  			assignments.putAll(chooseParameterAssignmentsRandomly(set, rnc, random,
77  			    errors));
78  		}
79  
80  		return new Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>>(
81  		    assignments, errors);
82  	}
83  
84  	/**
85  	 * Chooses a set randomly.
86  	 * 
87  	 * @param setType
88  	 *          the Settype from which to choose a set at random
89  	 * @param rnc
90  	 *          the {@link RandomNumberChecks} instance for validity checks
91  	 * @param rand
92  	 *          the random number generator
93  	 * @param errorLog
94  	 *          the errors that might occur due erroneous specification of
95  	 *          scenario (to be filled, see {@link RandomNumberChecks})
96  	 * @return a randomly chosen set
97  	 */
98  	public Set chooseSetRandomly(SetType setType, RandomNumberChecks rnc,
99  	    IRandom rand, List<GeneratorError> errorLog) {
100 		rnc.checkProbabilitySetting("Sets of Settype '" + setType.getName() + "'",
101 		    setType.getSets(), errorLog);
102 		return rnc.chooseNormalizedRandomObject(setType.getSets(), rand);
103 	}
104 
105 	/**
106 	 * Executes second level of Monte-Carlo simulation. For each
107 	 * {@link ParameterInstance}, one {@link ParameterAssignment} is chosen.
108 	 * 
109 	 * @param set
110 	 *          the set from which the mapping should be chosen
111 	 * @param rnc
112 	 *          the checker for probability validity
113 	 * @param rand
114 	 *          the randomiser to be used
115 	 * @param errorLog
116 	 *          the error log
117 	 * @return maps that gives one assignment for each instance defined by the
118 	 *         {@link SetType}
119 	 */
120 	public Map<ParameterInstance, ParameterAssignment> chooseParameterAssignmentsRandomly(
121 	    Set set, RandomNumberChecks rnc, IRandom rand,
122 	    List<GeneratorError> errorLog) {
123 		Map<ParameterInstance, ParameterAssignment> result = new HashMap<ParameterInstance, ParameterAssignment>();
124 		Map<ParameterInstance, ParameterAssignmentSet> setData = set.getSetData();
125 		for (Entry<ParameterInstance, ParameterAssignmentSet> instEntry : setData
126 		    .entrySet()) {
127 			ParameterInstance instance = instEntry.getKey();
128 			ParameterAssignmentSet assignments = instEntry.getValue();
129 			rnc.checkProbabilitySetting("matrices of set '" + set.getName()
130 			    + "' for parameter instance '" + instance + "'",
131 			    assignments.getAssignments(), errorLog);
132 			result.put(instance,
133 			    rnc.chooseNormalizedRandomObject(assignments.getAssignments(), rand));
134 		}
135 		return result;
136 	}
137 
138 	/**
139 	 * The number of assignments is practically unlimited in all realistic
140 	 * situations.
141 	 * 
142 	 * TODO: Retrieve calculation of exact number
143 	 * 
144 	 * @return Long.MAX_VALUE
145 	 */
146 	@Override
147 	public long assignmentsLeft() {
148 		return Long.MAX_VALUE;
149 	}
150 
151 }