001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.libsvm;
018
019import com.oracle.labs.mlrg.olcut.config.Config;
020import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
021import com.oracle.labs.mlrg.olcut.provenance.impl.ConfiguredObjectProvenanceImpl;
022import org.tribuo.classification.Label;
023import org.tribuo.common.libsvm.SVMType;
024
025/**
026 * The carrier type for LibSVM classification modes.
027 * <p>
028 * Supports C_SVC and NU_SVC.
029 */
030public class SVMClassificationType implements SVMType<Label> {
031    private static final long serialVersionUID = 1L;
032
033    /**
034     * The classification model types.
035     */
036    public enum SVMMode {
037        /**
038         * Original SVM algorithm.
039         */
040        C_SVC(0),
041        /**
042         * Classification SVM, optimization in dual space.
043         */
044        NU_SVC(1);
045
046        final int nativeType;
047
048        SVMMode(int type) {
049            this.nativeType = type;
050        }
051    }
052
053    @Config(mandatory=true,description="The SVM classification algorithm to use.")
054    private SVMMode type;
055
056    /**
057     * for olcut.
058     */
059    private SVMClassificationType() {}
060
061    /**
062     * Constructs an SVMClassificationType using the supplied SVM algorithm.
063     * @param type The SVM algorithm.
064     */
065    public SVMClassificationType(SVMMode type) {
066        this.type = type;
067    }
068
069    @Override
070    public boolean isClassification() {
071        return true;
072    }
073
074    @Override
075    public boolean isRegression() {
076        return false;
077    }
078
079    @Override
080    public boolean isAnomaly() {
081        return false;
082    }
083
084    @Override
085    public boolean isNu() {
086        return type == SVMMode.NU_SVC;
087    }
088
089    @Override
090    public int getNativeType() {
091        return type.nativeType;
092    }
093
094    @Override
095    public ConfiguredObjectProvenance getProvenance() {
096        return new ConfiguredObjectProvenanceImpl(this,"SVMType");
097    }
098}