Class AbstractGeneticAlgorithm

    • Field Detail

      • PROPS_RELATION

        public static final String PROPS_RELATION
        the key for the relation name in the generated properties file.
        See Also:
        Constant Field Values
      • PROPS_FILTER

        public static final String PROPS_FILTER
        the key for a filter setup in the setup properties.
        See Also:
        Constant Field Values
      • m_NumGenes

        protected int m_NumGenes
        number of genes per chromosome. NB: must be initialized by the algorithm!
      • m_NumChrom

        protected int m_NumChrom
        number of chromosomes.
      • m_Genes

        protected BitSet[] m_Genes
        the genes.
      • m_Fitness

        protected double[] m_Fitness
        the fitness of the genes.
      • m_BestRange

        protected Range m_BestRange
        stores the best range of attribtues.
      • m_FavorZeroes

        protected boolean m_FavorZeroes
        whether to favor 0s instead of 1s.
      • m_Seed

        protected int m_Seed
        the seed value.
      • m_Random

        protected Random m_Random
        the random number generator.
      • m_Running

        protected boolean m_Running
        whether the algorithm is still running.
      • m_MaxTrainTime

        protected int m_MaxTrainTime
        the maximum number of seconds to train.
      • m_TrainStart

        protected long m_TrainStart
        the time when training commenced.
      • m_Iterations

        protected int m_Iterations
        number of iterations.
    • Constructor Detail

      • AbstractGeneticAlgorithm

        public AbstractGeneticAlgorithm()
    • Method Detail

      • getInitialSetups

        public abstract Vector<int[]> getInitialSetups()
      • reset

        public void reset()
        Resets the genetic algorihtm. Derived classes must call this method in set-methods of parameters to assure the invalidation of previously generated data.
        Overrides:
        reset in class AbstractOptimiser
      • setBestRange

        protected void setBestRange​(Range value)
        Sets the best range of attributes.
        Parameters:
        value - the range
      • setBestRange

        public void setBestRange​(String value)
        Sets the best range of attributes.
        Parameters:
        value - the range
      • getBestRange

        public String getBestRange()
        Returns the best range of attributes.
        Returns:
        the range
      • bestRangeTipText

        public String bestRangeTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setSeed

        public void setSeed​(int value)
        Sets the seed value to use, resets the random number generator.
        Parameters:
        value - the seed to use
      • getSeed

        public int getSeed()
        Returns the current seed value.
        Returns:
        the seed value
      • seedTipText

        public String seedTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setFavorZeroes

        public void setFavorZeroes​(boolean value)
        Sets whether 0s are favored over 1s.
        Parameters:
        value - if true then 0s are favored over 1s
      • getFavorZeroes

        public boolean getFavorZeroes()
        Returns whether 0s are favored over 1s.
        Returns:
        true if 0s are favored over 1s
      • favorZeroesTipText

        public String favorZeroesTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getNumGenes

        public int getNumGenes()
        Returns the number of genes to use.
        Returns:
        the number
      • setNumChrom

        public void setNumChrom​(int value)
        Sets the number of chromosomes to use.
        Parameters:
        value - the number
      • getNumChrom

        public int getNumChrom()
        Returns the number of chromosomes to use.
        Returns:
        the number
      • numChromTipText

        public String numChromTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setMaxTrainTime

        public void setMaxTrainTime​(int value)
        Sets the maximum number of seconds to perform training.
        Parameters:
        value - the number of seconds
      • getMaxTrainTime

        public int getMaxTrainTime()
        Returns the maximum number of seconds to perform training.
        Returns:
        the seed value
      • maxTrainTimeTipText

        public String maxTrainTimeTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • stop

        public void stop()
        Stops the execution of the algorithm.
      • isRunning

        public boolean isRunning()
        Returns whether the algorithm is still running.
        Returns:
        true if the algorithm is still running
      • getFitness

        public double[] getFitness()
        Returns the current fitness values.
        Returns:
        the fitness values
      • setIterations

        public void setIterations​(int value)
        Sets the iterations to use.
        Parameters:
        value - the iterations.
      • getIterations

        public int getIterations()
        Gets the iterations use.
        Returns:
        the iterations
      • iterationsTipText

        public String iterationsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • init

        protected void init​(int ch,
                            int genes)
        Initializes the algorithm.
        Parameters:
        ch - the chromosome
        genes - the number of genes
      • getGene

        public boolean getGene​(int chromosome,
                               int gene)
        Returns the value of the specified gene.
        Parameters:
        chromosome - the chromosome index
        gene - the gene index
        Returns:
        true if gene is active
      • setGene

        public void setGene​(int chromosome,
                            int gene,
                            int value)
        Sets the value of the specified gene.
        Parameters:
        chromosome - the chromosome index
        gene - the gene index
        value - the value to set, 0=clear, otherwise activate it
      • setGene

        public void setGene​(int chromosome,
                            int gene,
                            boolean value)
        Sets the value of the specified gene.
        Parameters:
        chromosome - the chromosome index
        gene - the gene index
        value - false=clear, true=activate it
      • hasMoreZeroes

        public boolean hasMoreZeroes​(BitSet a,
                                     BitSet b)
        return if a has more zeroes than b.
        Parameters:
        a - the first gene
        b - the second gene
        Returns:
        true if a has more zeroes than b
      • sort

        public void sort()
        Sorts genes and fitness arrays according to fitness.
      • doCrossovers

        public void doCrossovers()
        Performs cross-over.
      • copyGene

        protected void copyGene​(int to,
                                int from)
        Copies the values of one gene to another.
        Parameters:
        to - the gene to copy to
        from - the gene to copy from
      • doMutations

        public void doMutations()
        Performs mutations.
      • doMutations2

        public void doMutations2()
        Performs mutations.
      • calcFitness

        public abstract void calcFitness()
        Override the following function in sub-classes.
      • storeSetup

        protected Properties storeSetup​(weka.core.Instances data)
        Generates a Properties file that stores information on the setup of the genetic algorithm. E.g., it backs up the original relation name. The generated properties file will be used as new relation name for the data. Derived classes can add additional parameters to this properties file.
        Parameters:
        data - the data to create the setup for
        job - the associated job
        Returns:
        the generated setup
        See Also:
        PROPS_RELATION
      • updateHeader

        protected weka.core.Instances updateHeader​(weka.core.Instances data)
        Creates a new dataset, with the setup as the new relation name.
        Parameters:
        data - the data to replace the relation name with the setup
        job - the associated job
        Returns:
        the updated dataset
      • preRun

        protected void preRun()
        Further initializations in derived classes.
      • run

        public boolean run()
        Runs the genetic algorithm.
        Returns:
        true if successfully finished, false when interrupted
      • postRun

        protected void postRun()
                        throws Exception
        Further clean-ups in derived classes.
        Throws:
        Exception - if something goes wrong
      • runGeneticAlgorithm

        public static void runGeneticAlgorithm​(Class env,
                                               Class genetic,
                                               String[] options)
        Runs the genetic algorithm with the given options.
        Parameters:
        env - the environment class to use
        genetic - the genetic algorithm to run
        options - the options to set
      • getGeneticAlgorithms

        public static String[] getGeneticAlgorithms()
        Returns a list with classnames of genetic algorithms.
        Returns:
        the genetic algorithm classnames
      • forName

        public static AbstractGeneticAlgorithm forName​(String classname,
                                                       String[] options)
        Instantiates the genetic algorithm with the given options.
        Parameters:
        classname - the classname of the genetic algorithm to instantiate
        options - the options for the genetic algorithm
        Returns:
        the instantiated genetic algorithm or null if an error occurred
      • forCommandLine

        public static AbstractGeneticAlgorithm forCommandLine​(String cmdline)
        Instantiates the genetic algorithm from the given commandline (i.e., classname and optional options).
        Parameters:
        cmdline - the classname (and optional options) of the genetic algorithm to instantiate
        Returns:
        the instantiated genetic algorithm or null if an error occurred