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

weka.classifiers.immune.airs.AIRS2Parallel Maven / Gradle / Ivy

Go to download

Fork of the following defunct sourceforge.net project: https://sourceforge.net/projects/wekaclassalgos/

The newest version!
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 * Created on 30/12/2004
 *
 */
package weka.classifiers.immune.airs;

import weka.classifiers.AbstractClassifier;
import weka.classifiers.immune.airs.algorithm.AIRS2ParallelTrainer;
import weka.classifiers.immune.airs.algorithm.AISModelClassifier;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHelper;
import weka.core.SelectedTag;
import weka.core.Tag;
import weka.core.UnsupportedClassTypeException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * Type: AIRS1
 * File: AIRS1.java
 * Date: 07/01/2005
 * 

* Description: * * @author Jason Brownlee */ public class AIRS2Parallel extends AbstractClassifier implements AIRSParameterDocumentation { // paramters protected long seed; protected double affinityThresholdScalar; protected double clonalRate; protected double hypermutationRate; protected double totalResources; protected double stimulationValue; protected int numInstancesAffinityThreshold; protected int memInitialPoolSize; protected int knn; protected int numThreads; protected int mergeMode; protected String trainingSummary; protected String classifierSummary; private final static String[] PARAMETERS = { "S", // seed "F", // affinity threshold "C", // clonal rate "H", // hypermutation "R", // total resources "V", // stimulation value "A", // num affinity threshold instances "E", // mem pool size "K", // kNN "N", // num threads "M", // mege mode }; private final static String[] DESCRIPTIONS = { PARAM_SEED, PARAM_ATS, PARAM_CLONAL_RATE, PARAM_HMR, PARAM_RESOURCES, PARAM_STIMULATION, PARAM_AT_INSTANCES, PARAM_MEM_INSTANCES, PARAM_KNN, PARAM_THREADS, PARAM_MERGE }; public final static Tag[] TAGS_MERGE_MODE = { new Tag(1, "Concatenate"), new Tag(2, "Concatenate & Prune") }; /** * The model */ protected AISModelClassifier classifier; public AIRS2Parallel() { // set default values seed = 1; affinityThresholdScalar = 0.2; totalResources = 150; stimulationValue = 0.9; clonalRate = 10; hypermutationRate = 2.0; numInstancesAffinityThreshold = -1; memInitialPoolSize = 1; knn = 3; numThreads = 2; mergeMode = 1; } /** * @param data * @throws java.lang.Exception */ public void buildClassifier(Instances data) throws Exception { Instances trainingInstances = new Instances(data); // must have a class assigned if (trainingInstances.classIndex() < 0) { throw new Exception("No class attribute assigned to instances"); } // class must be nominal else if (!trainingInstances.classAttribute().isNominal()) { throw new UnsupportedClassTypeException("Class attribute must be nominal"); } // must have attributes besides the class attribute else if (trainingInstances.numAttributes() <= +1) { throw new Exception("Dataset contains no supported comparable attributes"); } // delete with missing class trainingInstances.deleteWithMissingClass(); for (int i = 0; i < trainingInstances.numAttributes(); i++) { trainingInstances.deleteWithMissing(i); } // must have some training instances if (trainingInstances.numInstances() == 0) { throw new Exception("No usable training instances!"); } // validate paramters validateParameters(trainingInstances); // determine merge mode AIRS2ParallelTrainer.MERGE_MODE theMergeMode = null; if (mergeMode == 1) { theMergeMode = AIRS2ParallelTrainer.MERGE_MODE.CONCATENATE; } else if (mergeMode == 2) { theMergeMode = AIRS2ParallelTrainer.MERGE_MODE.PRUNE; } // construct trainer Random rand = new Random(seed); AIRS2ParallelTrainer trainer = new AIRS2ParallelTrainer( affinityThresholdScalar, clonalRate, hypermutationRate, totalResources, stimulationValue, numInstancesAffinityThreshold, rand, memInitialPoolSize, knn, numThreads, theMergeMode); // prepare classifier classifier = trainer.train(trainingInstances); // get summaries trainingSummary = trainer.getTrainingSummary(); classifierSummary = classifier.getModelSummary(trainingInstances); } protected void validateParameters(Instances trainingInstances) throws Exception { int numInstances = trainingInstances.numInstances(); if (memInitialPoolSize > numInstances) { memInitialPoolSize = numInstances; } if (numThreads < 2) { throw new Exception("Number of threads must be more than 1."); } } public double classifyInstance(Instance instance) throws Exception { if (classifier == null) { throw new Exception("Algorithm has not been prepared."); } // TODO: validate of data provided matches training data specs return classifier.classifyInstance(instance); } public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append("AIRS2 Parallel - Parallel Artificial Immune Recognition System v2.0\n"); if (trainingSummary != null) { buffer.append(trainingSummary); buffer.append("\n"); } if (classifierSummary != null) { buffer.append(classifierSummary); } return buffer.toString(); } public String globalInfo() { StringBuilder buffer = new StringBuilder(); buffer.append(toString()); buffer.append("A parallel version of the AIRS algorithm. " + "Designed to run with multiple threads or on multiple machines."); buffer.append("\n\n"); buffer.append("Andrew Watkins and Jon Timmis, "); buffer.append("\"Exploiting Parallelism Inherent in AIRS, an Artificial Immune Classifier\", "); buffer.append("Proceedings of the 3rd International Conference on Artificial Immune Systems (ICARIS2004), "); buffer.append("Catania, Italy, pp. 427-438, 2004."); return buffer.toString(); } public Enumeration listOptions() { Vector





© 2015 - 2025 Weber Informatics LLC | Privacy Policy