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>
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. |
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.
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.
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.
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.
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.
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.
chromosomeIndex | index of chromosome in population. |
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.
chromosomeIndex | index of chromosome in population. |
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.
chromosomeIndex | index of chromosome which rank in the group is queried. |
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.
pos | position of chromosome in group. |
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.
pos | position of chromosome in 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.
pos | position of chromosome in group. |
int GACALL Population::GaSortedGroup::GetMaxSize | ( | ) | const [inline] |
This method is not thread-safe.
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.
size | new maximal number of chromosome. |
int GACALL Population::GaSortedGroup::GetCurrentSize | ( | ) | const [inline] |
This method is not thread-safe.
GaSortedGroupType GACALL Population::GaSortedGroup::GetType | ( | ) | const [inline] |
This method is not thread-safe.
const GaFitnessComparator* GACALL Population::GaSortedGroup::GetComparator | ( | ) | const [inline] |
This method is not thread-safe.
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.
comparator | pointer to new fitness comparator. |
const GaPopulation* GACALL Population::GaSortedGroup::GetPopulation | ( | ) | const [inline] |
This method is not thread-safe.
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.
pos | position of chromosome in group. |
const GaPopulation* Population::GaSortedGroup::_population [private] |
Pointer to population which hosts chromosomes in sorted group.
int* Population::GaSortedGroup::_chromosomesIndecies [private] |
Pointer to array of chromosomes' indices.
const GaFitnessComparator* Population::GaSortedGroup::_comparator [private] |
Pointer to comparator which is used for sorting chromosomes.
int Population::GaSortedGroup::_maxSize [private] |
Maximal number of chromosomes which can be stored in this sorted group.
int Population::GaSortedGroup::_currentSize [private] |
Current number of chromosomes in this sorted group.
Type of sorted group.