001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * MIPolyKernel.java
019: * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.classifiers.mi.supportVector;
024:
025: import weka.classifiers.functions.supportVector.PolyKernel;
026: import weka.core.Capabilities;
027: import weka.core.Instance;
028: import weka.core.Instances;
029: import weka.core.MultiInstanceCapabilitiesHandler;
030: import weka.core.Capabilities.Capability;
031:
032: /**
033: <!-- globalinfo-start -->
034: * The polynomial kernel : K(x, y) = <x, y>^p or K(x, y) = (<x, y>+1)^p
035: * <p/>
036: <!-- globalinfo-end -->
037: *
038: <!-- options-start -->
039: * Valid options are: <p/>
040: *
041: * <pre> -D
042: * Enables debugging output (if available) to be printed.
043: * (default: off)</pre>
044: *
045: * <pre> -no-checks
046: * Turns off all checks - use with caution!
047: * (default: checks on)</pre>
048: *
049: * <pre> -C <num>
050: * The size of the cache (a prime number).
051: * (default: 250007)</pre>
052: *
053: * <pre> -E <num>
054: * The Exponent to use.
055: * (default: 1.0)</pre>
056: *
057: * <pre> -L
058: * Use lower-order terms.
059: * (default: no)</pre>
060: *
061: <!-- options-end -->
062: *
063: * @author Eibe Frank (eibe@cs.waikato.ac.nz)
064: * @author Shane Legg (shane@intelligenesis.net) (sparse vector code)
065: * @author Stuart Inglis (stuart@reeltwo.com) (sparse vector code)
066: * @author Lin Dong (ld21@cs.waikato.ac.nz) (MIkernel)
067: * @version $Revision: 1.2 $
068: */
069: public class MIPolyKernel extends PolyKernel implements
070: MultiInstanceCapabilitiesHandler {
071:
072: /** for serialiation */
073: private static final long serialVersionUID = 7926421479341051777L;
074:
075: /**
076: * default constructor - does nothing.
077: */
078: public MIPolyKernel() {
079: super ();
080: }
081:
082: /**
083: * Creates a new <code>MIPolyKernel</code> instance.
084: *
085: * @param data the training dataset used.
086: * @param cacheSize the size of the cache (a prime number)
087: * @param exponent the exponent to use
088: * @param lowerOrder whether to use lower-order terms
089: * @throws Exception if something goes wrong
090: */
091: public MIPolyKernel(Instances data, int cacheSize, double exponent,
092: boolean lowerOrder) throws Exception {
093:
094: super (data, cacheSize, exponent, lowerOrder);
095: }
096:
097: /**
098: *
099: * @param id1 the index of instance 1
100: * @param id2 the index of instance 2
101: * @param inst1 the instance 1 object
102: * @return the dot product
103: * @throws Exception if something goes wrong
104: */
105: protected double evaluate(int id1, int id2, Instance inst1)
106: throws Exception {
107:
108: double result, res;
109: Instances data1 = new Instances(inst1.relationalValue(1));
110: Instances data2;
111: if (id1 == id2)
112: data2 = new Instances(data1);
113: else
114: data2 = new Instances(m_data.instance(id2).relationalValue(
115: 1));
116:
117: res = 0;
118: for (int i = 0; i < data1.numInstances(); i++) {
119: for (int j = 0; j < data2.numInstances(); j++) {
120: result = dotProd(data1.instance(i), data2.instance(j));
121:
122: // Use lower order terms?
123: if (getUseLowerOrder()) {
124: result += 1.0;
125: }
126: if (getExponent() != 1.0) {
127: result = Math.pow(result, getExponent());
128: }
129:
130: res += result;
131: }
132: }
133:
134: return res;
135: }
136:
137: /**
138: * Returns the Capabilities of this kernel.
139: *
140: * @return the capabilities of this object
141: * @see Capabilities
142: */
143: public Capabilities getCapabilities() {
144: Capabilities result = super .getCapabilities();
145:
146: // attributes
147: result.enable(Capability.NOMINAL_ATTRIBUTES);
148: result.enable(Capability.RELATIONAL_ATTRIBUTES);
149: result.enable(Capability.MISSING_VALUES);
150:
151: // class
152: result.enableAllClasses();
153:
154: // other
155: result.enable(Capability.ONLY_MULTIINSTANCE);
156:
157: return result;
158: }
159:
160: /**
161: * Returns the capabilities of this multi-instance kernel for the
162: * relational data.
163: *
164: * @return the capabilities of this object
165: * @see Capabilities
166: */
167: public Capabilities getMultiInstanceCapabilities() {
168: Capabilities result = super .getCapabilities();
169:
170: // class
171: result.disableAllClasses();
172: result.enable(Capability.NO_CLASS);
173:
174: return result;
175: }
176: }
|