diff --git a/Classification/FeatureSelection/pom.xml b/Classification/FeatureSelection/pom.xml index bc1e72b94..c59237139 100644 --- a/Classification/FeatureSelection/pom.xml +++ b/Classification/FeatureSelection/pom.xml @@ -32,6 +32,11 @@ + + org.ojalgo + ojalgo + 53.0.0 + ${project.groupId} tribuo-core diff --git a/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Discreeting/TransferFunction.java b/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Discreeting/TransferFunction.java new file mode 100644 index 000000000..de75c2dba --- /dev/null +++ b/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Discreeting/TransferFunction.java @@ -0,0 +1,32 @@ +package FS_Wrapper_Approaches.Discreeting; + +import org.ojalgo.function.special.ErrorFunction; + +import java.util.function.DoubleUnaryOperator; + +/** + * Enumeration that contains the types of transfer functions in which they are used to define the type of transfer function + */ +public enum TransferFunction implements DoubleUnaryOperator { + V1, V2, V3, V4, S1, S2, S3, S4; + + /** + * Applies this operator to the given value. + * + * @param value the operand as continuous value to be converted to either 1 or 0 + * @return the operator result that is a d + */ + @Override + public double applyAsDouble(double value) { + return switch (this) { + case V1 -> Math.abs(ErrorFunction.erf(Math.sqrt(Math.PI) / 2 * value)) >= 0.5 ? 1 : 0; + case V2 -> Math.abs(Math.tan(value)) >= 0.5 ? 1 : 0; + case V3 -> Math.abs(value / Math.abs(1 + Math.pow(value, 2))) >= 0.5 ? 1 : 0; + case V4 -> Math.abs(2 / Math.PI * Math.atan(Math.PI / 2 * value)) >= 0.5 ? 1 : 0; + case S1 -> 1 / (1 + Math.pow(Math.E, - 2 * value)) >= 0.5 ? 1 : 0; + case S2 -> 1 / (1 + Math.pow(Math.E, - value)) >= 0.5 ? 1 : 0; + case S3 -> 1 / (1 + Math.pow(Math.E, - value / 2)) >= 0.5 ? 1 : 0; + case S4 -> 1 / (1 + Math.pow(Math.E, - value / 3)) >= 0.5 ? 1 : 0; + }; + } +} diff --git a/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Optimizers/CuckooSearchOptimizer.java b/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Optimizers/CuckooSearchOptimizer.java new file mode 100644 index 000000000..2c6a80492 --- /dev/null +++ b/Classification/FeatureSelection/src/main/java/org/tribuo/classification/fs/FS_Wrapper_Approaches/Optimizers/CuckooSearchOptimizer.java @@ -0,0 +1,350 @@ +package FS_Wrapper_Approaches.Optimizers; + +import FS.Discreeting.TransferFunction; +import com.oracle.labs.mlrg.olcut.util.Pair; +import org.tribuo.Dataset; +import org.tribuo.FeatureSelector; +import org.tribuo.ImmutableFeatureMap; +import org.tribuo.Model; +import org.tribuo.SelectedFeatureSet; +import org.tribuo.classification.Label; +import org.tribuo.classification.ensemble.VotingCombiner; +import org.tribuo.classification.evaluation.LabelEvaluation; +import org.tribuo.classification.evaluation.LabelEvaluator; +import org.tribuo.common.nearest.KNNModel; +import org.tribuo.common.nearest.KNNTrainer; +import org.tribuo.dataset.SelectedFeatureDataset; +import org.tribuo.evaluation.CrossValidation; +import org.tribuo.math.distance.L1Distance; +import org.tribuo.math.neighbour.NeighboursQueryFactoryType; +import org.tribuo.provenance.FeatureSelectorProvenance; +import org.tribuo.provenance.FeatureSetProvenance; +import org.tribuo.provenance.impl.FeatureSelectorProvenanceImpl; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Random; +import java.util.SplittableRandom; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Select features based on Cuckoo Search algorithm with binary transfer functions, KNN classifier and 10-fold cross validation + *

+ * see: + *

+ * Xin-She Yang and Suash Deb.
+ * "Cuckoo Search via L´evy Flights", 2010.
+ *
+ * L. A. M. Pereira et al.
+ * "A Binary Cuckoo Search and its Application for Feature Selection", 2014.
+ * 
+ */ +public final class CuckooSearchOptimizer implements FeatureSelector