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.common.libsvm;
018
019import java.io.Serializable;
020
021/**
022 * Kernel types from libsvm.
023 */
024public enum KernelType implements Serializable {
025    /**
026     * A linear kernel function (i.e., a dot product).
027     */
028    LINEAR(0),
029    /**
030     * A polynomial kernel of the form (gamma*u'*v + coef0)^degree
031     */
032    POLY(1),
033    /**
034     * An RBF kernel of the form exp(-gamma*|u-v|^2)
035     */
036    RBF(2),
037    /**
038     * A sigmoid kernel of the form tanh(gamma*u'*v + coef0)
039     */
040    SIGMOID(3);
041
042    final int nativeType;
043
044    KernelType(int nativeType) {
045        this.nativeType = nativeType;
046    }
047
048    /**
049     * Gets LibSVM's int id.
050     * @return The int id.
051     */
052    public int getNativeType() {
053        return nativeType;
054    }
055
056    /**
057     * Converts the LibSVM int id into the enum value.
058     * @param nativeType The LibSVM id.
059     * @return The corresponding enum.
060     */
061    public static KernelType getKernelType(int nativeType) {
062        switch (nativeType) {
063            case 0:
064                return LINEAR;
065            case 1:
066                return POLY;
067            case 2:
068                return RBF;
069            case 3:
070                return SIGMOID;
071            default:
072                throw new IllegalArgumentException("Unknown native type " + nativeType);
073        }
074    }
075}