All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enterprisemath.math.nn.ThresholdNeuron Maven / Gradle / Ivy

The newest version!
package com.enterprisemath.math.nn;

import com.enterprisemath.utils.ValidationUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * Neuron which uses threshold function. Returns 1 if input is greater than zero and 0 otherwise.
 *
 * @author radek.hecl
 */
public class ThresholdNeuron implements Neuron {

    /**
     * Builder object.
     */
    public static class Builder {

        /**
         * Identification.
         */
        private String id;

        /**
         * Sets identification.
         *
         * @param id identification
         * @return this instance
         */
        public Builder setId(String id) {
            this.id = id;
            return this;
        }

        /**
         * Builds the result object.
         *
         * @return created object
         */
        public ThresholdNeuron build() {
            return new ThresholdNeuron(this);
        }
    }

    /**
     * Identification.
     */
    private String id;

    /**
     * Creates new instance.
     */
    private ThresholdNeuron() {
    }

    /**
     * Creates new instance.
     *
     * @param builder builder object
     */
    public ThresholdNeuron(Builder builder) {
        id = builder.id;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotEmpty(id, "id cannot be empty");
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public double getOutput(double input) {
        return input > 0 ? 1 : 0;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * Creates new instance.
     *
     * @param id identification
     * @return created neuron
     */
    public static ThresholdNeuron create(String id) {
        ThresholdNeuron res = new ThresholdNeuron();
        res.id = id;
        res.guardInvariants();
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy