001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022
023package org.biojava.stats.svm;
024
025import java.util.Iterator;
026
027/**
028 * <p>
029 * Abstract implementation of SVMClassifierModel.
030 * </p>
031 *
032 * <p>
033 * To implement a read-only implementation, you need only implement
034 * getThreshold and getAlpha.
035 * </p>
036 *
037 * @author Matthew Pocock
038 */
039public abstract class AbstractSVMClassifierModel implements SVMClassifierModel {
040  public void setThreshold()
041  throws UnsupportedOperationException {
042    throw new UnsupportedOperationException(
043      "setThreshold not supported by " + getClass()
044    );
045  }
046  
047  public void setAlpha(Object item, double alpha)
048  throws UnsupportedOperationException {
049    throw new UnsupportedOperationException(
050      "setAlpha not supported by " + getClass()
051    );
052  }
053  
054  public void addItem(Object item)
055  throws UnsupportedOperationException {
056    throw new UnsupportedOperationException(
057      "addItem not supported by " + getClass()
058    );
059  }
060  
061  public void addItemAlpha(Object item, double alpha)
062  throws UnsupportedOperationException {
063    throw new UnsupportedOperationException(
064      "addItemAlpha not supported by " + getClass()
065    );
066  }
067  
068  public void removeItem(Object item)
069  throws UnsupportedOperationException {
070    throw new UnsupportedOperationException(
071      "removeItem not supported by " + getClass()
072     );
073  }
074
075  
076  public void clear()
077  throws UnsupportedOperationException {
078    throw new UnsupportedOperationException(
079      "clear not supported by " + getClass()
080    );
081  }
082  
083  public double classify(Object item) {
084    double delta=0;
085    SVMKernel kernel = getKernel();
086    double threshold = getThreshold();
087    for(Iterator i = itemAlphas().iterator(); i.hasNext(); ) {
088      ItemValue itemValue = (ItemValue) i.next();
089      double alpha = itemValue.getValue();
090            if (alpha != 0)
091      delta += alpha * kernel.evaluate(itemValue.getItem(), item);
092    }
093    return delta - threshold;
094  }
095}