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.Collection;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.Iterator;
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * A no-frills implementation of an SVM classifier model.
034 *
035 * @author Matthew Pocock
036 */
037public class SimpleSVMClassifierModel extends AbstractSVMClassifierModel {
038  private SVMKernel kernel;
039  private double threshold;
040  
041  private Set itemAlphaSet;
042  private Map itemToItemAlpha;
043  
044  public SimpleSVMClassifierModel(SVMKernel kernel) {
045    this.kernel = kernel;
046    itemAlphaSet = new HashSet();
047    itemToItemAlpha = new HashMap();
048  }
049  
050  public SimpleSVMClassifierModel(SVMKernel kernel, Collection items) {
051    this(kernel);
052    for(Iterator i = items.iterator(); i.hasNext(); ) {
053      addItem(i.next());
054    }
055  }    
056
057  public SimpleSVMClassifierModel(SVMKernel kernel, SVMTarget target) {
058    this(kernel, target.items());
059  }
060  
061  public SVMKernel getKernel() {
062    return kernel;
063  }
064  
065  public void setThreshold(double threshold) {
066    this.threshold = threshold;
067  }
068  
069  public double getThreshold() {
070    return threshold;
071  }
072  
073  public Set items() {
074    return itemToItemAlpha.keySet();
075  }
076  
077  public Set itemAlphas() {
078    return itemAlphaSet;
079  }
080  
081  public double getAlpha(Object item) {
082    return ((ItemValue) itemToItemAlpha.get(item)).getValue();
083  }
084  
085  public void setAlpha(Object item, double alpha) {
086    ItemValue iv = (ItemValue) itemToItemAlpha.get(item);
087    iv.setValue(alpha);
088  }
089  
090  public void addItem(Object item) {
091    ItemValue iv = new SimpleItemValue(item, 0.0);
092    itemToItemAlpha.put(item, iv);
093    itemAlphaSet.add(iv);
094  }
095  
096  public void addItemAlpha(Object item, double alpha) {
097    ItemValue iv = new SimpleItemValue(item, alpha);
098    itemToItemAlpha.put(item, iv);
099    itemAlphaSet.add(iv);
100  }
101  
102  public void removeItem(Object item) {
103    itemToItemAlpha.remove(item);
104    itemAlphaSet.remove(item);
105  }
106  
107  public void clear() {
108    itemAlphaSet.clear();
109    itemToItemAlpha.clear();
110  }
111}