001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.ml.clustering.rforest; 031 032import java.io.DataInput; 033import java.io.DataOutput; 034import java.io.IOException; 035import java.io.PrintWriter; 036import java.util.Random; 037 038/** 039 * A single decision node of a RandomForest tree. This decision holds the feature index and the 040 * threshold for that index. 041 * 042 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 043 * 044 */ 045public class RandomDecision { 046 047 /** 048 * Feature threshold 049 */ 050 public int threshold; 051 /** 052 * Feature index 053 */ 054 public int feature; 055 private int randomSeed = -1; 056 private Random random = new Random(); 057 058 /** 059 * @param featureLength The number of entries in this featurevector 060 * @param minVal the min values of each featurevector entry 061 * @param maxVal the max values of each featurevector entry 062 */ 063 public RandomDecision(int featureLength, int[] minVal, int[] maxVal) { 064 setFeatureDecision(featureLength,minVal,maxVal); 065 } 066 067 private void setFeatureDecision(int featureLength, int[] minVal,int[] maxVal) { 068 this.feature = this.random.nextInt(featureLength); 069 if(maxVal[this.feature]-minVal[this.feature] == 0) 070 this.threshold = minVal[this.feature]; 071 else 072 this.threshold = this.random.nextInt(maxVal[this.feature]-minVal[this.feature]) + minVal[this.feature]; 073 } 074 075 /** 076 * @param featureLength The number of entries in this featurevector 077 * @param minVal the min values of each featurevector entry 078 * @param maxVal the max values of each featurevector entry 079 * @param r random seed to set before construction 080 */ 081 public RandomDecision(int featureLength, int[] minVal, int[] maxVal, Random r) { 082 this.random = r; 083 setFeatureDecision(featureLength,minVal, maxVal); 084 } 085 086 /** 087 * Emtpy contructor provided to allow reading of the decision 088 */ 089 public RandomDecision() { 090 } 091 092 093 094 /** 095 * Write decision to a binary stream, threshold followed by feature. 096 * @param o 097 * @throws IOException 098 */ 099 public void write(DataOutput o) throws IOException { 100 o.writeInt(threshold); 101 o.writeInt(feature); 102 } 103 104 /** 105 * write decision in a human readable form 106 * @param writer 107 */ 108 public void writeASCII(PrintWriter writer) { 109 writer.print(threshold + "," + feature); 110 } 111 112 /** 113 * Read decision 114 * @param dis 115 * @return A decision 116 * @throws IOException 117 */ 118 public RandomDecision readBinary(DataInput dis) throws IOException { 119 threshold = dis.readInt(); 120 feature = dis.readInt(); 121 return this; 122 } 123 124 /** 125 * Read decision from a string 126 * @param line 127 * @return a decision 128 */ 129 public RandomDecision readString(String line) { 130 String[] bits = line.split(","); 131 threshold = Integer.parseInt(bits[0]); 132 feature = Integer.parseInt(bits[1]); 133 return this; 134 } 135 136 @Override 137 public String toString() 138 { 139 String s = "(" + this.feature + "," + this.threshold + ")"; 140 return s; 141 } 142 143 /** 144 * Random seed upon which a java {@link Random} object is seeded and used to choose 145 * random indecies and thresholds. 146 * @param randomSeed 147 */ 148 public void setRandomSeed(int randomSeed) { 149 this.randomSeed = randomSeed; 150 this.random = new Random(this.randomSeed); 151 } 152}