[Avida-SVN] r3345 - in branches/bdc-biofilmsize/source: cpu main
connel42 at myxo.css.msu.edu
connel42 at myxo.css.msu.edu
Fri Jun 26 08:12:34 PDT 2009
Author: connel42
Date: 2009-06-26 11:12:33 -0400 (Fri, 26 Jun 2009)
New Revision: 3345
Modified:
branches/bdc-biofilmsize/source/cpu/cHardwareCPU.cc
branches/bdc-biofilmsize/source/cpu/cHardwareCPU.h
branches/bdc-biofilmsize/source/main/cAvidaConfig.h
Log:
added donate and share instructions
Modified: branches/bdc-biofilmsize/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/bdc-biofilmsize/source/cpu/cHardwareCPU.cc 2009-06-25 21:37:46 UTC (rev 3344)
+++ branches/bdc-biofilmsize/source/cpu/cHardwareCPU.cc 2009-06-26 15:12:33 UTC (rev 3345)
@@ -230,6 +230,13 @@
tInstLibEntry<tMethod>("sense-unit", &cHardwareCPU::Inst_SenseUnit, nInstFlag::STALL), // and want to keep stats, also add
tInstLibEntry<tMethod>("sense-m100", &cHardwareCPU::Inst_SenseMult100, nInstFlag::STALL), // the names to cStats::cStats() @JEB
+ tInstLibEntry<tMethod>("sense-resource0", &cHardwareCPU::Inst_SenseResource0, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("sense-resource1", &cHardwareCPU::Inst_SenseResource1, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("sense-resource2", &cHardwareCPU::Inst_SenseResource2, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("sense-faced-resource0", &cHardwareCPU::Inst_SenseFacedResource0, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("sense-faced-resource1", &cHardwareCPU::Inst_SenseFacedResource1, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("sense-faced-resource2", &cHardwareCPU::Inst_SenseFacedResource2, nInstFlag::STALL),
+
tInstLibEntry<tMethod>("if-resources", &cHardwareCPU::Inst_IfResources, nInstFlag::STALL),
tInstLibEntry<tMethod>("collect", &cHardwareCPU::Inst_Collect, nInstFlag::STALL),
tInstLibEntry<tMethod>("collect-no-env-remove", &cHardwareCPU::Inst_CollectNoEnvRemove, nInstFlag::STALL),
@@ -262,6 +269,9 @@
tInstLibEntry<tMethod>("request-energy-off", &cHardwareCPU::Inst_RequestEnergyFlagOff, nInstFlag::STALL),
tInstLibEntry<tMethod>("increase-energy-donation", &cHardwareCPU::Inst_IncreaseEnergyDonation, nInstFlag::STALL),
tInstLibEntry<tMethod>("decrease-energy-donation", &cHardwareCPU::Inst_DecreaseEnergyDonation, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-resource0", &cHardwareCPU::Inst_DonateResource0, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-resource1", &cHardwareCPU::Inst_DonateResource1, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-resource2", &cHardwareCPU::Inst_DonateResource2, nInstFlag::STALL),
tInstLibEntry<tMethod>("IObuf-add1", &cHardwareCPU::Inst_IOBufAdd1, nInstFlag::STALL),
tInstLibEntry<tMethod>("IObuf-add0", &cHardwareCPU::Inst_IOBufAdd0, nInstFlag::STALL),
@@ -3428,7 +3438,7 @@
int start_index = start_label.AsInt(num_nops);
int end_index = end_label.AsInt(num_nops);
-
+
// If the label refers to ONLY resources that
// do not exist, then the operation fails
if (start_index >= res_count.GetSize()) return false;
@@ -3469,7 +3479,7 @@
resource_result = (int)(log(dresource_result)/log(base));
}
}
-
+
//Dump this value into an arbitrary register: BX
GetRegister(reg_to_set) = resource_result;
@@ -3489,6 +3499,57 @@
// Note that we are converting <double> resources to <int> register values
}
+
+bool cHardwareCPU::Inst_SenseResource0(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_organism->GetCellID(), 0);
+}
+
+bool cHardwareCPU::Inst_SenseResource1(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_organism->GetCellID(), 1);
+}
+
+bool cHardwareCPU::Inst_SenseResource2(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_organism->GetCellID(), 2);
+}
+
+bool cHardwareCPU::Inst_SenseFacedResource0(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_world->GetPopulation().GetCell(m_organism->GetCellID()).GetCellFaced().GetID(), 0);
+}
+
+bool cHardwareCPU::Inst_SenseFacedResource1(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_world->GetPopulation().GetCell(m_organism->GetCellID()).GetCellFaced().GetID(), 1);
+}
+
+bool cHardwareCPU::Inst_SenseFacedResource2(cAvidaContext& ctx)
+{
+ return DoSenseResourceX(REG_BX, m_world->GetPopulation().GetCell(m_organism->GetCellID()).GetCellFaced().GetID(), 2);
+}
+
+
+bool cHardwareCPU::DoSenseResourceX(int reg_to_set, int cell_id, int resid)
+{
+ assert(resid >= 0);
+
+ cPopulation& pop = m_world->GetPopulation();
+
+ const tArray<double> & res_count = pop.GetCellResources(cell_id) +
+ pop.GetDemeCellResources(pop.GetCell(cell_id).GetDemeID(), cell_id);
+
+ // Make sure we have the resource requested
+ if (resid >= res_count.GetSize()) return false;
+
+ //cout << "BDCDEBUG: amount of recource in cell " << cell_id << " is " << res_count[resid] << endl;
+ GetRegister(reg_to_set) = (int) res_count[resid];
+
+ return true;
+
+}
+
/* Convert modifying NOPs to the index of a resource. If there are fewer
* than the number of NOPs required to specify a resource, find the subset
* of resources. (Motivation: can evolve to be more specific if there is
@@ -3737,7 +3798,60 @@
to_org->GetPhenotype().SetIsEnergyReceiver();
}
+// Move a fraction of the given resource present at the current cell to the specified cell.
+// Note: This function doesn't work with deme-level resources.
+void cHardwareCPU::DoResourceDonatePercent(const int to_cell, const int resource_id, const double frac_resource_given)
+{
+ assert(to_cell >= 0);
+ assert(resource_id >= 0);
+ assert(frac_resource_given >= 0);
+ assert(frac_resource_given <= 1);
+
+ const tArray<double> &resources = m_organism->GetOrgInterface().GetResources();
+ if(resource_id >= resources.GetSize()) return;
+
+ const double amount = max(0.0, frac_resource_given * resources[resource_id]);
+
+ DoResourceDonateAmount(to_cell, resource_id, amount);
+
+} //End DoResourceDonatePercent()
+
+// Donate a portion of the given resource present at the current cell to the specified cell.
+// Note: This function doesn't work with deme-level resources.
+void cHardwareCPU::DoResourceDonateAmount(const int to_cell, const int resource_id, const double amount)
+{
+ assert(to_cell >= 0);
+ assert(amount >= 0);
+ assert(resource_id >= 0);
+
+ const tArray<double> &src_resources = m_organism->GetOrgInterface().GetResources();
+ const tArray<double> &dest_resources = m_world->GetPopulation().GetCellResources(to_cell);
+
+ assert(resource_id < src_resources.GetSize());
+ assert(resource_id < dest_resources.GetSize());
+
+ const double donation = min(amount, src_resources[resource_id]);
+ const double decay = m_world->GetConfig().RESOURCE_SHARING_LOSS.Get();
+
+ assert(decay >= 0);
+ assert(decay <= 1);
+
+ tArray<double> src_change;
+ tArray<double> dest_change;
+
+ src_change.Resize(src_resources.GetSize(), 0);
+ dest_change.Resize(dest_resources.GetSize(), 0);
+
+ src_change[resource_id] = -1 * donation;
+ dest_change[resource_id] = (1 - decay) * donation;
+
+ m_organism->GetOrgInterface().UpdateResources(src_change);
+ m_world->GetPopulation().UpdateCellResources(dest_change, to_cell);
+
+} //End DoResourceDonateAmount()
+
+
void cHardwareCPU::DoEnergyDonatePercent(cOrganism* to_org, const double frac_energy_given)
{
assert(to_org != NULL);
@@ -3754,7 +3868,7 @@
// energy may be lost in transfer
void cHardwareCPU::DoEnergyDonateAmount(cOrganism* to_org, const double amount)
{
- double losspct = m_world->GetConfig().ENERGY_SHARING_LOSS.Get();
+ double losspct = m_world->GetConfig().RESOURCE_SHARING_LOSS.Get();
assert(to_org != NULL);
assert(amount >= 0);
@@ -4625,6 +4739,56 @@
} //End Inst_DonateEnergy()
+//Donate a fraction of nop-specified resource at organism's location to cell faced
+bool cHardwareCPU::DonateResourceX(cAvidaContext& ctx, const int res_id)
+{
+ assert(m_organism != 0);
+ assert(res_id >= 0);
+
+ const double pct = 0.1;
+
+ int current_cell, faced_cell;
+
+ current_cell = m_organism->GetCellID();
+
+ if(current_cell == -1) {
+ return false;
+ }
+
+ cPopulation& pop = m_world->GetPopulation();
+ faced_cell = pop.GetCell(current_cell).GetCellFaced().GetID();
+
+ if(faced_cell == -1) {
+ return false;
+ }
+
+ DoResourceDonatePercent(faced_cell, res_id, pct);
+
+ return true;
+
+} //End DonateResourceX()
+
+
+//Donate a fraction of nop-specified resource at organism's location to cell faced
+bool cHardwareCPU::Inst_DonateResource0(cAvidaContext& ctx)
+{
+ return DonateResourceX(ctx ,0);
+} //End Inst_DonateResource0()
+
+
+//Donate a fraction of nop-specified resource at organism's location to cell faced
+bool cHardwareCPU::Inst_DonateResource1(cAvidaContext& ctx)
+{
+ return DonateResourceX(ctx, 1);
+} //End Inst_DonateResource1()
+
+//Donate a fraction of nop-specified resource at organism's location to cell faced
+bool cHardwareCPU::Inst_DonateResource2(cAvidaContext& ctx)
+{
+ return DonateResourceX(ctx, 2);
+} //End Inst_DonateResource2()
+
+
//Update the organism's metabolic rate
bool cHardwareCPU::Inst_UpdateMetabolicRate(cAvidaContext& ctx)
{
Modified: branches/bdc-biofilmsize/source/cpu/cHardwareCPU.h
===================================================================
--- branches/bdc-biofilmsize/source/cpu/cHardwareCPU.h 2009-06-25 21:37:46 UTC (rev 3344)
+++ branches/bdc-biofilmsize/source/cpu/cHardwareCPU.h 2009-06-26 15:12:33 UTC (rev 3345)
@@ -491,6 +491,13 @@
bool Inst_SenseUnit(cAvidaContext& ctx);
bool Inst_SenseMult100(cAvidaContext& ctx);
bool DoSense(cAvidaContext& ctx, int conversion_method, double base);
+ bool DoSenseResourceX(int reg_to_set, int cell_id, int resid);
+ bool Inst_SenseResource0(cAvidaContext& ctx);
+ bool Inst_SenseResource1(cAvidaContext& ctx);
+ bool Inst_SenseResource2(cAvidaContext& ctx);
+ bool Inst_SenseFacedResource0(cAvidaContext& ctx);
+ bool Inst_SenseFacedResource1(cAvidaContext& ctx);
+ bool Inst_SenseFacedResource2(cAvidaContext& ctx);
// Resources
bool FindModifiedResource(int& start_index, int& end_index, int& spec_id);
@@ -502,6 +509,8 @@
// Donation
void DoDonate(cOrganism * to_org);
+ void DoResourceDonatePercent(const int to_cell, const int resource_id, const double frac_resource_given);
+ void DoResourceDonateAmount(const int to_cell, const int resource_id, const double amount);
void DoEnergyDonate(cOrganism* to_org);
void DoEnergyDonatePercent(cOrganism* to_org, const double frac_energy_given);
void DoEnergyDonateAmount(cOrganism* to_org, const double amount);
@@ -532,6 +541,10 @@
bool Inst_RequestEnergyFlagOff(cAvidaContext& ctx);
bool Inst_IncreaseEnergyDonation(cAvidaContext& ctx);
bool Inst_DecreaseEnergyDonation(cAvidaContext& ctx);
+ bool DonateResourceX(cAvidaContext& ctx, const int res_id);
+ bool Inst_DonateResource0(cAvidaContext& ctx);
+ bool Inst_DonateResource1(cAvidaContext& ctx);
+ bool Inst_DonateResource2(cAvidaContext& ctx);
bool Inst_SearchF(cAvidaContext& ctx);
bool Inst_SearchB(cAvidaContext& ctx);
Modified: branches/bdc-biofilmsize/source/main/cAvidaConfig.h
===================================================================
--- branches/bdc-biofilmsize/source/main/cAvidaConfig.h 2009-06-25 21:37:46 UTC (rev 3344)
+++ branches/bdc-biofilmsize/source/main/cAvidaConfig.h 2009-06-26 15:12:33 UTC (rev 3345)
@@ -543,7 +543,7 @@
CONFIG_ADD_VAR(ENERGY_SHARING_METHOD, int, 0, "Method for sharing energy. 0=receiver must actively receive/request, 1=energy pushed on receiver");
CONFIG_ADD_VAR(ENERGY_SHARING_PCT, double, 0.0, "Percent of energy to share");
CONFIG_ADD_VAR(ENERGY_SHARING_INCREMENT, double, 0.01, "Amount to change percent energy shared");
- CONFIG_ADD_VAR(ENERGY_SHARING_LOSS, double, 0.0, "Percent of shared energy lost in transfer");
+ CONFIG_ADD_VAR(RESOURCE_SHARING_LOSS, double, 0.0, "Percent of shared resource lost in transfer");
CONFIG_ADD_VAR(ENERGY_SHARING_UPDATE_METABOLIC, bool, 0, "0/1 (off/on) - Whether to update an organism's metabolic rate on donate or reception/application of energy");
CONFIG_ADD_VAR(LOG_ENERGY_SHARING, bool, 0, "Whether or not to log energy shares. 0/1 (off/on)");
More information about the Avida-cvs
mailing list