Population::GaSortedGroup Class Reference

Sorted group stores indices of chromosomes in sorted order, chromosomes must be from same population. For sorting, group use provided fitness comparator. Direction depends on type of group. It group type is GASGT_BEST or GASGT_OTHER chromosomes are sorted in descending order by their fitness value, if group type is GASGT_WORST chromosomes are sorted in ascending order by their fitness value. Sorted group can only accept fixed number of chromosomes. More...

#include <D:/Temp/vs/gal/source/SortedGroup.h>

List of all members.

Public Member Functions

 GaSortedGroup (const GaPopulation *population, int maxSize, GaSortedGroupType type, const GaFitnessComparator *comparator)
 This constructor allocates memory for array of chromosomes' indices, and initializes comparator, group type, and binds group to specified population.
 GaSortedGroup (const GaPopulation *population, GaSortedGroupType type)
 This constructor just bounds group to population and initializes its type, but doesn't allocate memory for array of chromosomes' indices. Because maximal group size is not specified, chromosomes cannot be inserted in this group before calling SetMaxSize.
 ~GaSortedGroup ()
 Destructor frees memory used by the array of chromosomes' indices.
GAL_API void GACALL CopyTo (GaSortedGroup &destination, bool sameSorting) const
 This method copies chromosomes' indices to destination group. It copies indices until destination group is not full, or until all chromosomes from source group aren't copied.
GAL_API int GACALL Add (int chromosomeIndex)
 Add method inserts chromosome in sorted group. This method preserves sorting. Before this method inserts chromosome it checks to see if chromosome is already in the group. Check is done based on group membership flags of scaled chromosome object. If group is not full and chromosome is not already in the group, it is inserted. If group is full, chromosome is inserted only if it has better fitness (by sorting policy: sorting comparator + group type) then the chromosome which has worst fitness (by sorting policy) in the group, it this is the case, worst chromosome is removed from the group (and membership flag of the group in scaled chromosome object is cleared) and new chromosome is inserted. If chromosome is inserted successfully this method sets membership flag of the group in scaled chromosome object.
GAL_API bool GACALL Remove (int chromosomeIndex)
 Remove method removes chromosome from sorted group. This method preserves sorting of the group. Before this method removes chromosome it checks to see if chromosome is in the group. Check is done based on group membership flags of scaled chromosome object. If chromosome is removed successfully this method clears membership flag of the group in scaled chromosome object.
GAL_API void GACALL Clear ()
 Clear method removes all chromosomes from the group and clears theirs membership flags.
GAL_API int GACALL GetRanking (int chromosomeIndex)
 GetRanking method returns ranking of chromosome in this sorted group.
int GACALL GetAt (int pos) const
 GetAt method returns index of chromosome at specified position in the group.
GAL_API GaScaledChromosome &GACALL GetScaledChromosomeAt (int pos) const
 GetScaledChromosomeAt method returns reference to scaled chromosome object at specified position in the group.
GaChromosomePtr GACALL GetChromosomeAt (int pos) const
 GetChromosomeAt method returns smart pointer to chromosome object at specified position in the group.
int GACALL GetMaxSize () const
 This method is not thread-safe.
GAL_API void GACALL SetMaxSize (int size)
 SetMaxSize method sets maximal number of chromosomes which can be stored in this group.
int GACALL GetCurrentSize () const
 This method is not thread-safe.
GaSortedGroupType GACALL GetType () const
 This method is not thread-safe.
const GaFitnessComparator *GACALL GetComparator () const
 This method is not thread-safe.
GAL_API void GACALL SetComparator (const GaFitnessComparator *comparator)
 SetComparator method sets comparator which is used for sorting chromosome in this group. If comparator is changed, this method resort chromosomes in group, according to new sorting policy.
const GaPopulation *GACALL GetPopulation () const
 This method is not thread-safe.
int GACALL operator[] (int pos) const
 operator [] returns index of chromosome at specified position in the group.

Private Attributes

const GaPopulation_population
 Pointer to population which hosts chromosomes in sorted group.
int * _chromosomesIndecies
 Pointer to array of chromosomes' indices.
const GaFitnessComparator_comparator
 Pointer to comparator which is used for sorting chromosomes.
int _maxSize
 Maximal number of chromosomes which can be stored in this sorted group.
int _currentSize
 Current number of chromosomes in this sorted group.
GaSortedGroupType _type
 Type of sorted group.


Detailed Description

Sorted group stores indices of chromosomes in sorted order, chromosomes must be from same population. For sorting, group use provided fitness comparator. Direction depends on type of group. It group type is GASGT_BEST or GASGT_OTHER chromosomes are sorted in descending order by their fitness value, if group type is GASGT_WORST chromosomes are sorted in ascending order by their fitness value. Sorted group can only accept fixed number of chromosomes.

This class has built-in synchronizator so it is allowed to use LOCK_OBJECT and LOCK_THIS_OBJECT macros with instances of this class, but no public or private methods are thread-safe.


Constructor & Destructor Documentation

Population::GaSortedGroup::GaSortedGroup ( const GaPopulation population,
int  maxSize,
GaSortedGroupType  type,
const GaFitnessComparator comparator 
) [inline]

This constructor allocates memory for array of chromosomes' indices, and initializes comparator, group type, and binds group to specified population.

Parameters:
population pointer to population to which this group is bound.
maxSize maximal number of chromosomes which can be stored in the group.
type type of group.
comparator pointer to fitness comparator which is used for sorting.

Population::GaSortedGroup::GaSortedGroup ( const GaPopulation population,
GaSortedGroupType  type 
) [inline]

This constructor just bounds group to population and initializes its type, but doesn't allocate memory for array of chromosomes' indices. Because maximal group size is not specified, chromosomes cannot be inserted in this group before calling SetMaxSize.

Parameters:
population pointer to population to which this group is bound.
type type of group.

Population::GaSortedGroup::~GaSortedGroup (  )  [inline]

Destructor frees memory used by the array of chromosomes' indices.


Member Function Documentation

void Population::GaSortedGroup::CopyTo ( GaSortedGroup destination,
bool  sameSorting 
) const

This method copies chromosomes' indices to destination group. It copies indices until destination group is not full, or until all chromosomes from source group aren't copied.

This method is not thread-safe.

Parameters:
destination reference to group which is destination of copying.
sameSorting instructs to copy method not to resort chromosomes in destination population. If two groups use same sorting policy this parameter should be true, to improve performance because resorting is skipped. If they use different sorting policies this parameter should be false, because that instructs method to resort indices in destination group according to its sorting policy.

int Population::GaSortedGroup::Add ( int  chromosomeIndex  ) 

Add method inserts chromosome in sorted group. This method preserves sorting. Before this method inserts chromosome it checks to see if chromosome is already in the group. Check is done based on group membership flags of scaled chromosome object. If group is not full and chromosome is not already in the group, it is inserted. If group is full, chromosome is inserted only if it has better fitness (by sorting policy: sorting comparator + group type) then the chromosome which has worst fitness (by sorting policy) in the group, it this is the case, worst chromosome is removed from the group (and membership flag of the group in scaled chromosome object is cleared) and new chromosome is inserted. If chromosome is inserted successfully this method sets membership flag of the group in scaled chromosome object.

This method is not thread-safe.

Parameters:
chromosomeIndex index of chromosome in population.
Returns:
Method returns position in group at which it is inserted, if it is inserted successfully. If chromosome is not inserted, method returns -1.

bool Population::GaSortedGroup::Remove ( int  chromosomeIndex  ) 

Remove method removes chromosome from sorted group. This method preserves sorting of the group. Before this method removes chromosome it checks to see if chromosome is in the group. Check is done based on group membership flags of scaled chromosome object. If chromosome is removed successfully this method clears membership flag of the group in scaled chromosome object.

This method is not thread-safe.

Parameters:
chromosomeIndex index of chromosome in population.
Returns:
Method returns true if chromosome is found and removed successfully. If chromosome doesn't belong to the group, this method returns false.

void Population::GaSortedGroup::Clear (  ) 

Clear method removes all chromosomes from the group and clears theirs membership flags.

This method is not thread-safe.

int Population::GaSortedGroup::GetRanking ( int  chromosomeIndex  ) 

GetRanking method returns ranking of chromosome in this sorted group.

This method is not thread-safe.

Parameters:
chromosomeIndex index of chromosome which rank in the group is queried.
Returns:
Method returns ranking of chromosome in this group. If chromosome doesn't belong to this group, method returns -1.

int GACALL Population::GaSortedGroup::GetAt ( int  pos  )  const [inline]

GetAt method returns index of chromosome at specified position in the group.

This method is not thread-safe.

Parameters:
pos position of chromosome in group.
Returns:
Method returns index of chromosome at specified position in the group. If position is out of range, method returns -1.

GaScaledChromosome & Population::GaSortedGroup::GetScaledChromosomeAt ( int  pos  )  const

GetScaledChromosomeAt method returns reference to scaled chromosome object at specified position in the group.

This method is not thread-safe.

Parameters:
pos position of chromosome in group.
Returns:
Method returns reference to scaled chromosome object at specified position in the group.

GaChromosomePtr GACALL Population::GaSortedGroup::GetChromosomeAt ( int  pos  )  const [inline]

GetChromosomeAt method returns smart pointer to chromosome object at specified position in the group.

This method is not thread-safe.

Parameters:
pos position of chromosome in group.
Returns:
Method returns smart pointer to chromosome object at specified position in the group.

int GACALL Population::GaSortedGroup::GetMaxSize (  )  const [inline]

This method is not thread-safe.

Returns:
Method returns maximal number of chromosomes which can be stored in this group.

void Population::GaSortedGroup::SetMaxSize ( int  size  ) 

SetMaxSize method sets maximal number of chromosomes which can be stored in this group.

This method is not thread-safe.

Parameters:
size new maximal number of chromosome.

int GACALL Population::GaSortedGroup::GetCurrentSize (  )  const [inline]

This method is not thread-safe.

Returns:
Method returns number of chromosomes which are currently in group.

GaSortedGroupType GACALL Population::GaSortedGroup::GetType (  )  const [inline]

This method is not thread-safe.

Returns:
Method returns type of the group.

const GaFitnessComparator* GACALL Population::GaSortedGroup::GetComparator (  )  const [inline]

This method is not thread-safe.

Returns:
Method returns pointer to fitness comparator which is used for sorting chromosomes.

void Population::GaSortedGroup::SetComparator ( const GaFitnessComparator comparator  ) 

SetComparator method sets comparator which is used for sorting chromosome in this group. If comparator is changed, this method resort chromosomes in group, according to new sorting policy.

This method is not thread-safe.

Parameters:
comparator pointer to new fitness comparator.

const GaPopulation* GACALL Population::GaSortedGroup::GetPopulation (  )  const [inline]

This method is not thread-safe.

Returns:
Method returns pointer to population to which this group is bound.

int GACALL Population::GaSortedGroup::operator[] ( int  pos  )  const [inline]

operator [] returns index of chromosome at specified position in the group.

This operator is not thread-safe.

Parameters:
pos position of chromosome in group.
Returns:
Operator returns index of chromosome at specified position in the group. If position is out of range, operator returns -1.


Member Data Documentation

Pointer to population which hosts chromosomes in sorted group.

Pointer to array of chromosomes' indices.

Pointer to comparator which is used for sorting chromosomes.

Maximal number of chromosomes which can be stored in this sorted group.

Current number of chromosomes in this sorted group.

Type of sorted group.


The documentation for this class was generated from the following files:

Genetic Algorithm Library
Coolsoft Software Development