1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
9 * Ported to Java June 2009 Alokika Dash
10 * University of California, Irvine
12 * =============================================================================
14 * For the license of bayes/sort.h and bayes/sort.c, please see the header
17 * ------------------------------------------------------------------------
19 * Unless otherwise noted, the following license applies to STAMP files:
21 * Copyright (c) 2007, Stanford University
22 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions are
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
31 * * Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in
33 * the documentation and/or other materials provided with the
36 * * Neither the name of Stanford University nor the names of its
37 * contributors may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
41 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
50 * THE POSSIBILITY OF SUCH DAMAGE.
52 * =============================================================================
55 #define DATA_PRECISION 100
56 #define DATA_INIT 2 /* not 0 or 1 */
61 byte[] records; /* coordination of all records */
67 /* =============================================================================
69 * =============================================================================
71 public static Data data_alloc (int numVar, int numRecord, Random randomPtr)
73 Data dataPtr = new Data();
75 if (dataPtr != null) {
76 int numDatum = numVar * numRecord;
77 dataPtr.records = new byte[numDatum];
78 for(int i = 0; i<numDatum; i++)
79 dataPtr.records[i] = (byte)DATA_INIT;
81 dataPtr.numVar = numVar;
82 dataPtr.numRecord = numRecord;
83 dataPtr.randomPtr = randomPtr;
90 /* =============================================================================
92 * =============================================================================
100 /* =============================================================================
102 * -- Binary variables of random PDFs
103 * -- If seed is <0, do not reseed
104 * -- Returns random network
105 * =============================================================================
107 public Net data_generate (int seed, int maxNumParent, int percentParent)
110 randomPtr.random_seed(seed);
114 * Generate random Bayesian network
117 Net netPtr = Net.net_alloc(numVar);
118 netPtr.net_generateRandomEdges(maxNumParent, percentParent, randomPtr);
121 * Create a threshold for each of the possible permutation of variable
125 int[][] thresholdsTable = new int[numVar][];
127 for (v = 0; v < numVar; v++) {
128 IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
129 int numThreshold = 1 << parentIdListPtr.list_getSize();
130 int[] thresholds = new int[numThreshold];
131 for (int t = 0; t < numThreshold; t++) {
132 int threshold = (int) (randomPtr.random_generate() % (DATA_PRECISION + 1));
133 thresholds[t] = threshold;
135 thresholdsTable[v] = thresholds;
139 * Create variable dependency ordering for record generation
142 int[] order = new int[numVar];
145 Queue workQueuePtr = Queue.queue_alloc(-1);
147 IntVector dependencyVectorPtr = IntVector.vector_alloc(1);
149 BitMap orderedBitmapPtr = BitMap.bitmap_alloc(numVar);
150 orderedBitmapPtr.bitmap_clearAll();
152 BitMap doneBitmapPtr = BitMap.bitmap_alloc(numVar);
153 doneBitmapPtr.bitmap_clearAll();
156 while ((v = doneBitmapPtr.bitmap_findClear(v + 1)) >= 0) {
157 IntList childIdListPtr = netPtr.net_getChildIdListPtr(v);
158 int numChild = childIdListPtr.list_getSize();
165 * Use breadth-first search to find net connected to this leaf
168 workQueuePtr.queue_clear();
169 if((status = workQueuePtr.queue_push(v)) != true) {
170 System.out.println("Assert failed: status= "+ status + "should be true");
174 while (!(workQueuePtr.queue_isEmpty())) {
175 int id = workQueuePtr.queue_pop();
176 if((status = doneBitmapPtr.bitmap_set(id)) != true) {
177 System.out.println("Assert failed: status= "+ status + "should be true");
181 if((status = dependencyVectorPtr.vector_pushBack(id)) == false) {
182 System.out.println("Assert failed: status= "+ status + "should be true");
186 IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
187 IntListNode it = parentIdListPtr.head;
189 while (it.nextPtr!=null) {
191 int parentId = it.dataPtr;
192 if((status = workQueuePtr.queue_push(parentId)) == false) {
193 System.out.println("Assert failed: status= "+ status + "should be true");
203 int n = dependencyVectorPtr.vector_getSize();
204 for (int i = 0; i < n; i++) {
205 int id = dependencyVectorPtr.vector_popBack();
206 if (!(orderedBitmapPtr.bitmap_isSet(id))) {
207 orderedBitmapPtr.bitmap_set(id);
208 order[numOrder++] = id;
214 if(numOrder != numVar) {
215 System.out.println("Assert failed: numVar should be equal to numOrder");
224 for (int r = 0; r < numRecord; r++) {
225 for (int o = 0; o < numOrder; o++) {
227 IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
229 IntListNode it = parentIdListPtr.head;
231 while (it.nextPtr!=null) {
233 int parentId = it.dataPtr;
234 int value = records[startindex + parentId];
235 if(value == DATA_INIT) {
236 System.out.println("Assert failed value should be != DATA_INIT");
240 index = (index << 1) + value;
242 int rnd = (int) (randomPtr.random_generate() % DATA_PRECISION);
243 int threshold = thresholdsTable[v][index];
244 records[startindex + v] = (byte) ((rnd < threshold) ? 1 : 0);
246 startindex += numVar;
247 if(startindex > numRecord * numVar) {
248 System.out.println("Assert failed: value should be != DATA_INIT in data_generate()");
257 doneBitmapPtr.bitmap_free();
258 orderedBitmapPtr.bitmap_free();
259 dependencyVectorPtr.vector_free();
260 workQueuePtr.queue_free();
263 for (v = 0; v < numVar; v++) {
264 thresholdsTable[v] = null;
267 thresholdsTable = null;
273 /* =============================================================================
275 * -- Returns false on failure
276 * =============================================================================
278 public static boolean
279 data_copy (Data dstPtr, Data srcPtr)
281 int numDstDatum = dstPtr.numVar * dstPtr.numRecord;
282 int numSrcDatum = srcPtr.numVar * srcPtr.numRecord;
283 if (numDstDatum != numSrcDatum) {
284 dstPtr.records = null;
285 dstPtr.records = new byte[numSrcDatum];
286 if (dstPtr.records == null) {
291 dstPtr.numVar = srcPtr.numVar;
292 dstPtr.numRecord = srcPtr.numRecord;
293 for(int i=0; i<numSrcDatum; i++)
294 dstPtr.records[i] = srcPtr.records[i];
299 /* =============================================================================
302 * =============================================================================
305 data_sort (int start,
309 if((start < 0) || (start > numRecord)) {
310 System.out.println("start: Assert failed in data_sort");
313 if((num < 0) || (num > numRecord)) {
314 System.out.println("num: Assert failed in data_sort");
317 if((start + num < 0) || (start + num > numRecord)) {
318 System.out.println("start + num: Assert failed in data_sort");
332 /* =============================================================================
334 * -- Call data_sort first with proper start, num, offset
335 * -- Returns number of zeros in offset column
336 * =============================================================================
339 data_findSplit (int start, int num, int offset)
342 int high = start + num - 1;
344 while (low <= high) {
345 int mid = (low + high) / 2;
346 if (records[numVar * mid + offset] == 0) {
353 return (low - start);
357 /* =============================================================================
361 * =============================================================================