[Avida-SVN] r3545 - in branches/biounit/source: classification main
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Wed Dec 2 14:00:04 PST 2009
Author: brysonda
Date: 2009-12-02 17:00:03 -0500 (Wed, 02 Dec 2009)
New Revision: 3545
Modified:
branches/biounit/source/classification/cBGGenotype.cc
branches/biounit/source/classification/cBGGenotype.h
branches/biounit/source/classification/cBGGenotypeManager.cc
branches/biounit/source/classification/cBGGenotypeManager.h
branches/biounit/source/classification/cBioGroup.cc
branches/biounit/source/classification/cBioGroup.h
branches/biounit/source/main/cBirthChamber.cc
Log:
Handle bggenotype adjustment. Split biogroup references into active and passive forms.
Modified: branches/biounit/source/classification/cBGGenotype.cc
===================================================================
--- branches/biounit/source/classification/cBGGenotype.cc 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBGGenotype.cc 2009-12-02 22:00:03 UTC (rev 3545)
@@ -47,8 +47,9 @@
if (parents) {
m_parents.Resize(parents->GetSize());
for (int i = 0; i < m_parents.GetSize(); i++) {
- m_parents[i] = (*parents)[i];
- m_parents[i]->AddReference();
+ // @TODO - is there a better way to handle biogroup -> genotype transformation or a good way to eliminate the need?
+ m_parents[i] = static_cast<cBGGenotype*>((*parents)[i]);
+ m_parents[i]->AddPassiveReference();
}
}
if (m_parents.GetSize()) m_depth = m_parents[0]->GetDepth() + 1;
@@ -56,7 +57,7 @@
cBGGenotype::~cBGGenotype()
{
- for (int i = 0; i < m_parents.GetSize(); i++) m_parents[i]->RemoveReference();
+ for (int i = 0; i < m_parents.GetSize(); i++) m_parents[i]->RemovePassiveReference();
}
int cBGGenotype::GetRoleID() const
@@ -79,7 +80,7 @@
m_breed_true.Inc();
m_total_organisms++;
m_mgr->AdjustGenotype(this, m_num_organisms++, m_num_organisms);
-
+ AddActiveReference();
return this;
}
@@ -91,7 +92,8 @@
void cBGGenotype::RemoveBioUnit(cBioUnit* bu)
{
m_deaths.Inc();
- m_mgr->AdjustGenotype(this, m_num_organisms--, m_num_organisms);
+ RemoveActiveReference();
+ m_mgr->AdjustGenotype(this, m_num_organisms--, m_num_organisms);
}
Modified: branches/biounit/source/classification/cBGGenotype.h
===================================================================
--- branches/biounit/source/classification/cBGGenotype.h 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBGGenotype.h 2009-12-02 22:00:03 UTC (rev 3545)
@@ -70,7 +70,7 @@
int m_last_num_organisms;
int m_total_organisms;
- tArray<cBioGroup*> m_parents;
+ tArray<cBGGenotype*> m_parents;
cCountTracker m_births;
cCountTracker m_deaths;
@@ -119,6 +119,10 @@
void SetThreshold() { m_threshold = true; }
void ClearThreshold() { m_threshold = false; }
+
+ void Deactivate() { m_active = false; }
+
+ inline const tArray<cBGGenotype*> GetParents() const { return m_parents; }
bool Matches(cBioUnit* bu);
inline void NotifyNewBioUnit(cBioUnit* bu) { m_total_organisms++; m_num_organisms++; m_breed_in.Inc(); }
Modified: branches/biounit/source/classification/cBGGenotypeManager.cc
===================================================================
--- branches/biounit/source/classification/cBGGenotypeManager.cc 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBGGenotypeManager.cc 2009-12-02 22:00:03 UTC (rev 3545)
@@ -48,6 +48,23 @@
cBioGroup* cBGGenotypeManager::ClassifyNewBioUnit(cBioUnit* bu) { return ClassifyNewBioUnit(bu, NULL); }
+
+void cBGGenotypeManager::UpdateReset()
+{
+ if (m_active_sz.GetSize() < nBGGenotypeManager::HASH_SIZE) {
+ for (int i = 0; i < m_active_sz.GetSize(); i++) {
+ tListIterator<cBGGenotype> list_it(m_active_sz[i]);
+ while (list_it.Next() != NULL) list_it.Get()->UpdateReset();
+ }
+ } else {
+ for (int i = 0; i < nBGGenotypeManager::HASH_SIZE; i++) {
+ tListIterator<cBGGenotype> list_it(m_active_hash[i]);
+ while (list_it.Next() != NULL) list_it.Get()->UpdateReset();
+ }
+ }
+}
+
+
cBGGenotype* cBGGenotypeManager::ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents)
{
int list_num = hashGenome(bu->GetMetaGenome().GetGenome());
@@ -74,9 +91,20 @@
}
-void cBGGenotypeManager::UpdateReset()
+void cBGGenotypeManager::AdjustGenotype(cBGGenotype* genotype, int old_size, int new_size)
{
- // @TODO
+ // Remove from old size list
+ m_active_sz[old_size].Remove(genotype);
+
+ // Handle defunct genotypes
+ if (new_size == 0 && genotype->GetActiveReferenceCount() == 0) {
+ removeGenotype(genotype);
+ return;
+ }
+
+ // Add to new size list
+ resizeActiveList(new_size);
+ m_active_sz[new_size].Push(genotype);
}
@@ -103,3 +131,30 @@
return cStringUtil::Stringf("%03d-%s", size, alpha);
}
+
+void cBGGenotypeManager::removeGenotype(cBGGenotype* genotype)
+{
+ if (genotype->GetActiveReferenceCount()) return;
+
+ if (genotype->IsActive()) {
+ int list_num = hashGenome(genotype->GetMetaGenome().GetGenome());
+ m_active_hash[list_num].Remove(genotype);
+ genotype->Deactivate();
+ m_historic.Push(genotype);
+ }
+
+ if (genotype->IsThreshold()) {
+ // @TODO handle threshold removal
+ // m_world->GetStats().RemoveThreshold();
+ // genotype->ClearThreshold();
+ }
+
+ if (genotype->GetPassiveReferenceCount()) return;
+
+ const tArray<cBGGenotype*>& parents = genotype->GetParents();
+ for (int i = 0; i < parents.GetSize(); i++) {
+ parents[i]->RemovePassiveReference();
+ // @TODO - update coalescent?
+ removeGenotype(parents[i]);
+ }
+}
Modified: branches/biounit/source/classification/cBGGenotypeManager.h
===================================================================
--- branches/biounit/source/classification/cBGGenotypeManager.h 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBGGenotypeManager.h 2009-12-02 22:00:03 UTC (rev 3545)
@@ -43,7 +43,7 @@
class cWorld;
namespace nBGGenotypeManager {
- const unsigned int HASH_SIZE = 3203;
+ const int HASH_SIZE = 3203;
}
class cBGGenotypeManager : public cBioGroupManager
@@ -71,24 +71,18 @@
// Genotype Manager Methods
cBGGenotype* ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents);
- inline void AdjustGenotype(cBGGenotype* genotype, int old_size, int new_size);
+ void AdjustGenotype(cBGGenotype* genotype, int old_size, int new_size);
private:
unsigned int hashGenome(const cGenome& genome) const;
cString nameGenotype(int size, int num) const;
+ void removeGenotype(cBGGenotype* genotype);
+
inline void resizeActiveList(int size);
};
-
-void cBGGenotypeManager::AdjustGenotype(cBGGenotype* genotype, int old_size, int new_size)
-{
- m_active_sz[old_size].Remove(genotype);
- resizeActiveList(new_size);
- m_active_sz[new_size].Push(genotype);
-}
-
inline void cBGGenotypeManager::resizeActiveList(int size)
{
if (m_active_sz.GetSize() <= size) m_active_sz.Resize(size + 1);
Modified: branches/biounit/source/classification/cBioGroup.cc
===================================================================
--- branches/biounit/source/classification/cBioGroup.cc 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBioGroup.cc 2009-12-02 22:00:03 UTC (rev 3545)
@@ -29,5 +29,5 @@
cBioGroup::~cBioGroup()
{
- assert(m_refs == 0);
+ assert(m_a_refs == 0 && m_p_refs == 0);
}
Modified: branches/biounit/source/classification/cBioGroup.h
===================================================================
--- branches/biounit/source/classification/cBioGroup.h 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/classification/cBioGroup.h 2009-12-02 22:00:03 UTC (rev 3545)
@@ -37,10 +37,11 @@
class cBioGroup
{
protected:
- int m_refs;
+ int m_a_refs;
+ int m_p_refs;
public:
- cBioGroup() : m_refs(0) { ; }
+ cBioGroup() : m_a_refs(0), m_p_refs(0) { ; }
virtual ~cBioGroup() = 0;
virtual int GetRoleID() const = 0;
@@ -51,9 +52,13 @@
virtual int GetDepth() const = 0;
- int GetReferenceCount() const { return m_refs; }
- void AddReference() { m_refs++; }
- void RemoveReference() { m_refs--; }
+ int GetReferenceCount() const { return m_a_refs + m_p_refs; }
+ int GetActiveReferenceCount() const { return m_a_refs; }
+ void AddActiveReference() { m_a_refs++; }
+ void RemoveActiveReference() { m_a_refs--; }
+ int GetPassiveReferenceCount() const { return m_p_refs; }
+ void AddPassiveReference() { m_p_refs++; }
+ void RemovePassiveReference() { m_p_refs--; }
};
#endif
Modified: branches/biounit/source/main/cBirthChamber.cc
===================================================================
--- branches/biounit/source/main/cBirthChamber.cc 2009-11-30 20:12:37 UTC (rev 3544)
+++ branches/biounit/source/main/cBirthChamber.cc 2009-12-02 22:00:03 UTC (rev 3545)
@@ -113,7 +113,7 @@
entry.groups = parent->GetBioGroups();
for (int i = 0; i < entry.groups.GetSize(); i++) {
- entry.groups[i]->AddReference();
+ entry.groups[i]->AddActiveReference();
}
}
@@ -123,7 +123,7 @@
entry.timestamp = -1;
for (int i = 0; i < entry.groups.GetSize(); i++) {
- entry.groups[i]->RemoveReference();
+ entry.groups[i]->RemoveActiveReference();
}
}
More information about the Avida-cvs
mailing list