Package adams.opt.optimise.genetic
Class AbstractGeneticAlgorithm
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.logging.CustomLoggingLevelObject
-
- adams.core.option.AbstractOptionHandler
-
- adams.opt.optimise.AbstractOptimiser
-
- adams.opt.optimise.genetic.AbstractGeneticAlgorithm
-
- All Implemented Interfaces:
adams.core.CleanUpHandler
,adams.core.Destroyable
,adams.core.GlobalInfoSupporter
,adams.core.logging.LoggingLevelHandler
,adams.core.logging.LoggingSupporter
,adams.core.option.OptionHandler
,adams.core.SizeOfHandler
,Serializable
,Comparable
- Direct Known Subclasses:
PackDataGeneticAlgorithm
public abstract class AbstractGeneticAlgorithm extends adams.opt.optimise.AbstractOptimiser
Base class for genetic algorithms.- Author:
- Dale (dale at cs dot waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected adams.core.Range
m_BestRange
stores the best range of attribtues.protected boolean
m_FavorZeroes
whether to favor 0s instead of 1s.protected double[]
m_Fitness
the fitness of the genes.protected BitSet[]
m_Genes
the genes.protected int
m_Iterations
number of iterations.protected int
m_MaxTrainTime
the maximum number of seconds to train.protected int
m_NumChrom
number of chromosomes.protected int
m_NumGenes
number of genes per chromosome.protected Random
m_Random
the random number generator.protected boolean
m_Running
whether the algorithm is still running.protected int
m_Seed
the seed value.protected long
m_TrainStart
the time when training commenced.static String
PROPS_FILTER
the key for a filter setup in the setup properties.static String
PROPS_MASK
the key for the mask in the setup properties.static String
PROPS_RELATION
the key for the relation name in the generated properties file.
-
Constructor Summary
Constructors Constructor Description AbstractGeneticAlgorithm()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description String
bestRangeTipText()
Returns the tip text for this property.abstract void
calcFitness()
Override the following function in sub-classes.protected void
copyGene(int to, int from)
Copies the values of one gene to another.void
defineOptions()
Adds options to the internal list of options.void
doCrossovers()
Performs cross-over.void
doMutations()
Performs mutations.void
doMutations2()
Performs mutations.String
favorZeroesTipText()
Returns the tip text for this property.static AbstractGeneticAlgorithm
forCommandLine(String cmdline)
Instantiates the genetic algorithm from the given commandline (i.e., classname and optional options).static AbstractGeneticAlgorithm
forName(String classname, String[] options)
Instantiates the genetic algorithm with the given options.String
getBestRange()
Returns the best range of attributes.boolean
getFavorZeroes()
Returns whether 0s are favored over 1s.double[]
getFitness()
Returns the current fitness values.boolean
getGene(int chromosome, int gene)
Returns the value of the specified gene.static String[]
getGeneticAlgorithms()
Returns a list with classnames of genetic algorithms.abstract List<int[]>
getInitialSetups()
int
getIterations()
Gets the iterations use.int
getMaxTrainTime()
Returns the maximum number of seconds to perform training.int
getNumChrom()
Returns the number of chromosomes to use.int
getNumGenes()
Returns the number of genes to use.int
getSeed()
Returns the current seed value.boolean
hasMoreZeroes(BitSet a, BitSet b)
return if a has more zeroes than b.protected void
init(int ch, int genes)
Initializes the algorithm.protected void
initialize()
Initializes the members.boolean
isRunning()
Returns whether the algorithm is still running.String
iterationsTipText()
Returns the tip text for this property.String
maxTrainTimeTipText()
Returns the tip text for this property.String
numChromTipText()
Returns the tip text for this property.protected void
postRun()
Further clean-ups in derived classes.protected void
preRun()
Further initializations in derived classes.void
reset()
Resets the genetic algorihtm.boolean
run()
Runs the genetic algorithm.static void
runGeneticAlgorithm(Class env, Class genetic, String[] options)
Runs the genetic algorithm with the given options.String
seedTipText()
Returns the tip text for this property.protected void
setBestRange(adams.core.Range value)
Sets the best range of attributes.void
setBestRange(String value)
Sets the best range of attributes.void
setFavorZeroes(boolean value)
Sets whether 0s are favored over 1s.void
setGene(int chromosome, int gene, boolean value)
Sets the value of the specified gene.void
setGene(int chromosome, int gene, int value)
Sets the value of the specified gene.void
setIterations(int value)
Sets the iterations to use.void
setMaxTrainTime(int value)
Sets the maximum number of seconds to perform training.void
setNumChrom(int value)
Sets the number of chromosomes to use.void
setSeed(int value)
Sets the seed value to use, resets the random number generator.void
sort()
Sorts genes and fitness arrays according to fitness.void
stop()
Stops the execution of the algorithm.protected adams.core.Properties
storeSetup(weka.core.Instances data)
Generates a Properties file that stores information on the setup of the genetic algorithm.protected weka.core.Instances
updateHeader(weka.core.Instances data)
Creates a new dataset, with the setup as the new relation name.-
Methods inherited from class adams.opt.optimise.AbstractOptimiser
checkBest, cleanUp, compareTo, destroy, equals, getOptimisers, optimise, shallowCopy, shallowCopy
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, finishInit, getDefaultLoggingLevel, getOptionManager, globalInfo, loggingLevelTipText, newOptionManager, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
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
-
PROPS_MASK
public static final String PROPS_MASK
the key for the mask 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 adams.core.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.
-
-
Method Detail
-
getInitialSetups
public abstract List<int[]> getInitialSetups()
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classadams.opt.optimise.AbstractOptimiser
-
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 classadams.opt.optimise.AbstractOptimiser
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceadams.core.option.OptionHandler
- Overrides:
defineOptions
in classadams.core.option.AbstractOptionHandler
-
setBestRange
protected void setBestRange(adams.core.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 chromosomegenes
- the number of genes
-
getGene
public boolean getGene(int chromosome, int gene)
Returns the value of the specified gene.- Parameters:
chromosome
- the chromosome indexgene
- 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 indexgene
- the gene indexvalue
- 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 indexgene
- the gene indexvalue
- 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 geneb
- 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 tofrom
- 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 adams.core.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- 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- 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 usegenetic
- the genetic algorithm to runoptions
- 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 instantiateoptions
- 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
-
-