| 1 | /* | 
|---|
| 2 | *    This program is free software; you can redistribute it and/or modify | 
|---|
| 3 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 4 | *    the Free Software Foundation; either version 2 of the License, or | 
|---|
| 5 | *    (at your option) any later version. | 
|---|
| 6 | * | 
|---|
| 7 | *    This program is distributed in the hope that it will be useful, | 
|---|
| 8 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 9 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 10 | *    GNU General Public License for more details. | 
|---|
| 11 | * | 
|---|
| 12 | *    You should have received a copy of the GNU General Public License | 
|---|
| 13 | *    along with this program; if not, write to the Free Software | 
|---|
| 14 | *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 | /* | 
|---|
| 18 | * MIPolyKernel.java | 
|---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand | 
|---|
| 20 | * | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | package weka.classifiers.mi.supportVector; | 
|---|
| 24 |  | 
|---|
| 25 | import weka.classifiers.functions.supportVector.PolyKernel; | 
|---|
| 26 | import weka.core.Capabilities; | 
|---|
| 27 | import weka.core.Instance; | 
|---|
| 28 | import weka.core.Instances; | 
|---|
| 29 | import weka.core.MultiInstanceCapabilitiesHandler; | 
|---|
| 30 | import weka.core.RevisionUtils; | 
|---|
| 31 | import weka.core.Capabilities.Capability; | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 | <!-- globalinfo-start --> | 
|---|
| 35 | * The polynomial kernel : K(x, y) = <x, y>^p or K(x, y) = (<x, y>+1)^p | 
|---|
| 36 | * <p/> | 
|---|
| 37 | <!-- globalinfo-end --> | 
|---|
| 38 | * | 
|---|
| 39 | <!-- options-start --> | 
|---|
| 40 | * Valid options are: <p/> | 
|---|
| 41 | * | 
|---|
| 42 | * <pre> -D | 
|---|
| 43 | *  Enables debugging output (if available) to be printed. | 
|---|
| 44 | *  (default: off)</pre> | 
|---|
| 45 | * | 
|---|
| 46 | * <pre> -no-checks | 
|---|
| 47 | *  Turns off all checks - use with caution! | 
|---|
| 48 | *  (default: checks on)</pre> | 
|---|
| 49 | * | 
|---|
| 50 | * <pre> -C <num> | 
|---|
| 51 | *  The size of the cache (a prime number), 0 for full cache and | 
|---|
| 52 | *  -1 to turn it off. | 
|---|
| 53 | *  (default: 250007)</pre> | 
|---|
| 54 | * | 
|---|
| 55 | * <pre> -E <num> | 
|---|
| 56 | *  The Exponent to use. | 
|---|
| 57 | *  (default: 1.0)</pre> | 
|---|
| 58 | * | 
|---|
| 59 | * <pre> -L | 
|---|
| 60 | *  Use lower-order terms. | 
|---|
| 61 | *  (default: no)</pre> | 
|---|
| 62 | * | 
|---|
| 63 | <!-- options-end --> | 
|---|
| 64 | * | 
|---|
| 65 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) | 
|---|
| 66 | * @author Shane Legg (shane@intelligenesis.net) (sparse vector code) | 
|---|
| 67 | * @author Stuart Inglis (stuart@reeltwo.com) (sparse vector code) | 
|---|
| 68 | * @author Lin Dong (ld21@cs.waikato.ac.nz) (MIkernel) | 
|---|
| 69 | * @version $Revision: 5154 $ | 
|---|
| 70 | */ | 
|---|
| 71 | public class MIPolyKernel | 
|---|
| 72 | extends PolyKernel | 
|---|
| 73 | implements MultiInstanceCapabilitiesHandler { | 
|---|
| 74 |  | 
|---|
| 75 | /** for serialiation */ | 
|---|
| 76 | private static final long serialVersionUID = 7926421479341051777L; | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | * default constructor - does nothing. | 
|---|
| 80 | */ | 
|---|
| 81 | public MIPolyKernel() { | 
|---|
| 82 | super(); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 | * Creates a new <code>MIPolyKernel</code> instance. | 
|---|
| 87 | * | 
|---|
| 88 | * @param data        the training dataset used. | 
|---|
| 89 | * @param cacheSize   the size of the cache (a prime number) | 
|---|
| 90 | * @param exponent    the exponent to use | 
|---|
| 91 | * @param lowerOrder  whether to use lower-order terms | 
|---|
| 92 | * @throws Exception  if something goes wrong | 
|---|
| 93 | */ | 
|---|
| 94 | public MIPolyKernel(Instances data, int cacheSize, double exponent, | 
|---|
| 95 | boolean lowerOrder) throws Exception { | 
|---|
| 96 |  | 
|---|
| 97 | super(data, cacheSize, exponent, lowerOrder); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * | 
|---|
| 102 | * @param id1         the index of instance 1 | 
|---|
| 103 | * @param id2         the index of instance 2 | 
|---|
| 104 | * @param inst1       the instance 1 object | 
|---|
| 105 | * @return            the dot product | 
|---|
| 106 | * @throws Exception  if something goes wrong | 
|---|
| 107 | */ | 
|---|
| 108 | protected double evaluate(int id1, int id2, Instance inst1) | 
|---|
| 109 | throws Exception { | 
|---|
| 110 |  | 
|---|
| 111 | double result, res; | 
|---|
| 112 | Instances data1= new Instances(inst1.relationalValue(1)); | 
|---|
| 113 | Instances data2; | 
|---|
| 114 | if(id1==id2) | 
|---|
| 115 | data2= new Instances(data1); | 
|---|
| 116 | else | 
|---|
| 117 | data2 = new Instances (m_data.instance(id2).relationalValue(1)); | 
|---|
| 118 |  | 
|---|
| 119 | res=0; | 
|---|
| 120 | for(int i=0; i<data1.numInstances();i++){ | 
|---|
| 121 | for (int j=0; j<data2.numInstances(); j++){ | 
|---|
| 122 | result = dotProd(data1.instance(i), data2.instance(j)); | 
|---|
| 123 |  | 
|---|
| 124 | // Use lower order terms? | 
|---|
| 125 | if (getUseLowerOrder()) { | 
|---|
| 126 | result += 1.0; | 
|---|
| 127 | } | 
|---|
| 128 | if (getExponent() != 1.0) { | 
|---|
| 129 | result = Math.pow(result, getExponent()); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | res += result; | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | return res; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | /** | 
|---|
| 140 | * Returns the Capabilities of this kernel. | 
|---|
| 141 | * | 
|---|
| 142 | * @return            the capabilities of this object | 
|---|
| 143 | * @see               Capabilities | 
|---|
| 144 | */ | 
|---|
| 145 | public Capabilities getCapabilities() { | 
|---|
| 146 | Capabilities result = super.getCapabilities(); | 
|---|
| 147 |  | 
|---|
| 148 | // attributes | 
|---|
| 149 | result.enable(Capability.NOMINAL_ATTRIBUTES); | 
|---|
| 150 | result.enable(Capability.RELATIONAL_ATTRIBUTES); | 
|---|
| 151 | result.enable(Capability.MISSING_VALUES); | 
|---|
| 152 |  | 
|---|
| 153 | // class | 
|---|
| 154 | result.enableAllClasses(); | 
|---|
| 155 |  | 
|---|
| 156 | // other | 
|---|
| 157 | result.enable(Capability.ONLY_MULTIINSTANCE); | 
|---|
| 158 |  | 
|---|
| 159 | return result; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /** | 
|---|
| 163 | * Returns the capabilities of this multi-instance kernel for the | 
|---|
| 164 | * relational data. | 
|---|
| 165 | * | 
|---|
| 166 | * @return            the capabilities of this object | 
|---|
| 167 | * @see               Capabilities | 
|---|
| 168 | */ | 
|---|
| 169 | public Capabilities getMultiInstanceCapabilities() { | 
|---|
| 170 | Capabilities result = super.getCapabilities(); | 
|---|
| 171 |  | 
|---|
| 172 | // class | 
|---|
| 173 | result.disableAllClasses(); | 
|---|
| 174 | result.enable(Capability.NO_CLASS); | 
|---|
| 175 |  | 
|---|
| 176 | return result; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 | * Frees the cache used by the kernel. | 
|---|
| 181 | */ | 
|---|
| 182 | public void clean() { | 
|---|
| 183 | m_storage = null; | 
|---|
| 184 | m_keys = null; | 
|---|
| 185 | m_kernelMatrix = null; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /** | 
|---|
| 189 | * Returns the revision string. | 
|---|
| 190 | * | 
|---|
| 191 | * @return            the revision | 
|---|
| 192 | */ | 
|---|
| 193 | public String getRevision() { | 
|---|
| 194 | return RevisionUtils.extract("$Revision: 5154 $"); | 
|---|
| 195 | } | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|