[Avida-SVN] r3363 - in development/source: actions cpu main
beckma24 at myxo.css.msu.edu
beckma24 at myxo.css.msu.edu
Tue Aug 11 05:54:54 PDT 2009
Author: beckma24
Date: 2009-08-11 08:54:54 -0400 (Tue, 11 Aug 2009)
New Revision: 3363
Modified:
development/source/actions/PopulationActions.cc
development/source/cpu/cHardwareBase.h
development/source/cpu/cHardwareCPU.cc
development/source/cpu/cHardwareCPU.h
development/source/cpu/cTestCPU.cc
development/source/main/cResourceHistory.cc
development/source/main/cTaskLib.cc
Log:
Added action KillFractionInSequence_PopLimit and instructions maskoff-lower[4,8,12,16]bits
Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/actions/PopulationActions.cc 2009-08-11 12:54:54 UTC (rev 3363)
@@ -842,6 +842,61 @@
};
/*
+ Kills a fraction of organisms in the population in sequence.
+
+ Parameters:
+ fraction killed (double) default: 0.01
+
+ */
+class cActionKillFractionInSequence_PopLimit : public cAction
+ {
+ private:
+ double m_killFraction;
+ int m_popSize;
+ int killIndex;
+
+ public:
+ cActionKillFractionInSequence_PopLimit(cWorld* world, const cString& args) :
+ cAction(world, args),
+ m_killFraction(0.01),
+ m_popSize(1000),
+ killIndex(0)
+ {
+ cString largs(args);
+ if (largs.GetSize()) m_killFraction = largs.PopWord().AsDouble();
+ if (largs.GetSize()) m_popSize = largs.PopWord().AsInt();
+ }
+
+ static const cString GetDescription() { return "Arguments: [double fraction=0.01]"; }
+
+ void Process(cAvidaContext& ctx)
+ {
+ double avgGestation = m_world->GetStats().GetAveGestation();
+ double instPerUpdate = m_world->GetConfig().AVE_TIME_SLICE.Get();
+ double growthRate = floor(instPerUpdate/avgGestation * 100.0 + 0.5) / 100.0;
+
+ cPopulation& pop = m_world->GetPopulation();
+ const int numOrgsInPop = pop.GetNumOrganisms();
+ if(numOrgsInPop < m_popSize)
+ return;
+
+ int organismsToKill = static_cast<int>(numOrgsInPop * growthRate);
+ int oldKillIndex = killIndex;
+ cerr<< "growth rate: " << growthRate << " kill: " << organismsToKill <<endl;
+ while(organismsToKill > 0) {
+ cPopulationCell& cell = pop.GetCell(killIndex);
+ if (cell.IsOccupied()) {
+ pop.KillOrganism(cell);
+ --organismsToKill;
+ }
+ killIndex = (killIndex + 1) % pop.GetSize();
+ if(killIndex == oldKillIndex)
+ assert(false); // trying to kill organisms that don't exist
+ }
+ }
+ };
+
+/*
Randomly removes a certain proportion of the population whose genomes contain a specified
number (or more) of a certain type of instruction. E.g., the default is to remove 90% of
organisms containing 5 or more nand instructions.
@@ -3406,6 +3461,7 @@
action_lib->Register<cActionKillInstPair>("KillInstPair");
action_lib->Register<cActionKillProb>("KillProb");
action_lib->Register<cActionKillFractionInSequence>("KillFractionInSequence");
+ action_lib->Register<cActionKillFractionInSequence_PopLimit>("KillFractionInSequence_PopLimit");
// Theraputic deme actions
action_lib->Register<cAction_TherapyStructuralNumInst>("TherapyStructuralNumInst");
Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/cpu/cHardwareBase.h 2009-08-11 12:54:54 UTC (rev 3363)
@@ -77,14 +77,15 @@
tSmartArray<int> m_ext_mem;
// -------- Bit masks ---------------
- static const unsigned int MASK24 = 0xFFFFFF;
- static const unsigned int MASK20 = 0xFFFFF;
- static const unsigned int MASK16 = 0xFFFF;
- static const unsigned int MASK12 = 0xFFF;
- static const unsigned int MASK8 = 0xFF;
- static const unsigned int MASK4 = 0xF;
+ static const unsigned int MASK_SIGNBIT = 0x7FFFFFFF;
+ static const unsigned int MASK24 = 0xFFFFFF;
+ static const unsigned int MASKOFF_LOWEST16 = 0xFFFF0000;
+ static const unsigned int MASKOFF_LOWEST12 = 0xFFFFF000;
+ static const unsigned int MASKOFF_LOWEST8 = 0xFFFFFF00;
+ static const unsigned int MASKOFF_LOWEST4 = 0xFFFFFFF0;
+
cHardwareBase(); // @not_implemented
cHardwareBase(const cHardwareBase&); // @not_implemented
cHardwareBase& operator=(const cHardwareBase&); // @not_implemented
Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/cpu/cHardwareCPU.cc 2009-08-11 12:54:54 UTC (rev 3363)
@@ -473,10 +473,11 @@
tInstLibEntry<tMethod>("if-less-cons-24", &cHardwareCPU::Inst_IfLessConsensus24, 0, "Execute next instruction if Count(?BX[0:23]?) < Count(?CX[0:23]?), else skip it"),
// Bit Masking (higher order bit masking is possible, just add the instructions if needed)
- tInstLibEntry<tMethod>("mask-lower16bits", &cHardwareCPU::Inst_MaskLower16Bits),
- tInstLibEntry<tMethod>("mask-lower12bits", &cHardwareCPU::Inst_MaskLower12Bits),
- tInstLibEntry<tMethod>("mask-lower8bits", &cHardwareCPU::Inst_MaskLower8Bits),
- tInstLibEntry<tMethod>("mask-lower4bits", &cHardwareCPU::Inst_MaskLower4Bits),
+ tInstLibEntry<tMethod>("mask-signbit", &cHardwareCPU::Inst_MaskSignBit),
+ tInstLibEntry<tMethod>("maskoff-lower16bits", &cHardwareCPU::Inst_MaskOffLower16Bits),
+ tInstLibEntry<tMethod>("maskoff-lower12bits", &cHardwareCPU::Inst_MaskOffLower12Bits),
+ tInstLibEntry<tMethod>("maskoff-lower8bits", &cHardwareCPU::Inst_MaskOffLower8Bits),
+ tInstLibEntry<tMethod>("maskoff-lower4bits", &cHardwareCPU::Inst_MaskOffLower4Bits),
// Energy usage
@@ -6666,31 +6667,38 @@
/* Bit masking instructions */
+// masks sign bit in a register
+bool cHardwareCPU::Inst_MaskSignBit(cAvidaContext& ctx) {
+ const int reg = FindModifiedRegister(REG_BX);
+ GetRegister(reg) = GetRegister(reg) & MASK_SIGNBIT;
+ return true;
+}
+
// masks lower 16 bits in a register
-bool cHardwareCPU::Inst_MaskLower16Bits(cAvidaContext& ctx) {
+bool cHardwareCPU::Inst_MaskOffLower16Bits(cAvidaContext& ctx) {
const int reg = FindModifiedRegister(REG_BX);
- GetRegister(reg) = GetRegister(reg) & MASK16;
+ GetRegister(reg) = GetRegister(reg) & MASKOFF_LOWEST16;
return true;
}
// masks lower 12 bits in a register
-bool cHardwareCPU::Inst_MaskLower12Bits(cAvidaContext& ctx) {
+bool cHardwareCPU::Inst_MaskOffLower12Bits(cAvidaContext& ctx) {
const int reg = FindModifiedRegister(REG_BX);
- GetRegister(reg) = GetRegister(reg) & MASK12;
+ GetRegister(reg) = GetRegister(reg) & MASKOFF_LOWEST12;
return true;
}
// masks lower 8 bits in a register
-bool cHardwareCPU::Inst_MaskLower8Bits(cAvidaContext& ctx) {
+bool cHardwareCPU::Inst_MaskOffLower8Bits(cAvidaContext& ctx) {
const int reg = FindModifiedRegister(REG_BX);
- GetRegister(reg) = GetRegister(reg) & MASK8;
+ GetRegister(reg) = GetRegister(reg) & MASKOFF_LOWEST8;
return true;
}
// masks lower 4 bits in a register
-bool cHardwareCPU::Inst_MaskLower4Bits(cAvidaContext& ctx) {
+bool cHardwareCPU::Inst_MaskOffLower4Bits(cAvidaContext& ctx) {
const int reg = FindModifiedRegister(REG_BX);
- GetRegister(reg) = GetRegister(reg) & MASK4;
+ GetRegister(reg) = GetRegister(reg) & MASKOFF_LOWEST4;
return true;
}
@@ -7049,17 +7057,22 @@
bool cHardwareCPU::DoSensePheromoneGlobal(cAvidaContext& ctx) {
int reg_to_set = FindModifiedRegister(REG_BX);
+
+ const cResourceLib& resLib = m_world->GetEnvironment().GetResourceLib();
+
+ const tArray<double>& resource_count_array = m_organism->GetOrgInterface().GetResources();
const cResourceCount& resource_count = m_world->GetPopulation().GetResourceCount();
if(resource_count.GetSize() == 0) assert(false); // change to: return false;
+
+ double pher_amount = 0;
+ cResource* res = resLib.GetResource("pheromone");
- double pher_amount = 0;
- for (int i = 0; i < resource_count.GetSize(); i++) {
- if(strncmp(resource_count.GetResName(i), "pheromone", 9) == 0) {
- pher_amount += resource_count.Get(i);
- }
- }
- GetRegister(reg_to_set) = (int)floor(pher_amount + 0.5);
+ if(strncmp(resource_count.GetResName(res->GetID()), "pheromone", 9) == 0) {
+ pher_amount += resource_count_array[res->GetID()];
+ }
+
+ GetRegister(reg_to_set) = static_cast<int>(floor(pher_amount + 0.5));
return true;
}
Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/cpu/cHardwareCPU.h 2009-08-11 12:54:54 UTC (rev 3363)
@@ -696,10 +696,11 @@
bool Inst_IfLessConsensus24(cAvidaContext& ctx);
// Bit masking instructions
- bool Inst_MaskLower16Bits(cAvidaContext& ctx);
- bool Inst_MaskLower12Bits(cAvidaContext& ctx);
- bool Inst_MaskLower8Bits(cAvidaContext& ctx);
- bool Inst_MaskLower4Bits(cAvidaContext& ctx);
+ bool Inst_MaskSignBit(cAvidaContext& ctx);
+ bool Inst_MaskOffLower16Bits(cAvidaContext& ctx);
+ bool Inst_MaskOffLower12Bits(cAvidaContext& ctx);
+ bool Inst_MaskOffLower8Bits(cAvidaContext& ctx);
+ bool Inst_MaskOffLower4Bits(cAvidaContext& ctx);
//// Messaging ////
bool Inst_SendMessage(cAvidaContext& ctx);
Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/cpu/cTestCPU.cc 2009-08-11 12:54:54 UTC (rev 3363)
@@ -146,7 +146,8 @@
// Prepare the resources
InitResources(test_info.m_res_method, test_info.m_res, test_info.m_res_update, test_info.m_res_cpu_cycle_offset);
-
+
+
// Determine if we're tracing and what we need to print.
cHardwareTracer* tracer = test_info.GetTraceExecution() ? (test_info.GetTracer()) : NULL;
std::ostream * tracerStream = NULL;
Modified: development/source/main/cResourceHistory.cc
===================================================================
--- development/source/main/cResourceHistory.cc 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/main/cResourceHistory.cc 2009-08-11 12:54:54 UTC (rev 3363)
@@ -56,8 +56,12 @@
if (entry == -1) return false;
for (int i = 0; i < rc.GetSize(); i++) {
- if (entry >= m_entries.GetSize() || i >= m_entries[entry].values.GetSize()) rc.Set(i, 0.0);
- else rc.Set(i, m_entries[entry].values[i]);
+ if (entry >= m_entries.GetSize() || i >= m_entries[entry].values.GetSize()) {
+ rc.Set(i, 0.0);
+ }
+ else {
+ rc.Set(i, m_entries[entry].values[i]);
+ }
}
return true;
Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc 2009-08-10 16:58:07 UTC (rev 3362)
+++ development/source/main/cTaskLib.cc 2009-08-11 12:54:54 UTC (rev 3363)
@@ -1951,10 +1951,10 @@
test_output = temp_buf[0];
for (int j = 0; j < string_to_match.GetSize(); j++) {
- string_index = string_to_match.GetSize() - j - 1; // start with last char in string
- int k = 1 << j;
- if ((string_to_match[string_index] == '0' && !(test_output & k)) ||
- (string_to_match[string_index] == '1' && (test_output & k))) num_matched++;
+ string_index = string_to_match.GetSize() - j - 1; // start with last char in string
+ int k = 1 << j;
+ if ((string_to_match[string_index] == '0' && !(test_output & k)) ||
+ (string_to_match[string_index] == '1' && (test_output & k))) num_matched++;
}
max_num_matched = num_matched;
}
More information about the Avida-cvs
mailing list