[Avida-SVN] r1519 - in branches/uml: Avida.xcodeproj source/actions source/cpu source/main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Thu May 3 06:22:28 PDT 2007
Author: hjg
Date: 2007-05-03 09:22:28 -0400 (Thu, 03 May 2007)
New Revision: 1519
Modified:
branches/uml/Avida.xcodeproj/project.pbxproj
branches/uml/source/actions/PrintActions.cc
branches/uml/source/cpu/cHardwareCPU.cc
branches/uml/source/cpu/cHardwareCPU.h
branches/uml/source/main/cBirthChamber.cc
branches/uml/source/main/cOrganism.cc
branches/uml/source/main/cOrganism.h
branches/uml/source/main/cStats.cc
branches/uml/source/main/cStats.h
branches/uml/source/main/cTaskContext.h
branches/uml/source/main/cTaskLib.cc
branches/uml/source/main/cTaskLib.h
Log:
Merge development branch changes into UML branch. (step 2)
Modified: branches/uml/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/uml/Avida.xcodeproj/project.pbxproj 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/Avida.xcodeproj/project.pbxproj 2007-05-03 13:22:28 UTC (rev 1519)
@@ -929,7 +929,7 @@
DCC315CF076253A5008F7A48 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
DCC315D0076253A5008F7A48 /* task_event_gen.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.cc; sourceTree = "<group>"; };
DCC315D1076253A5008F7A48 /* task_event_gen.old.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.old.cc; sourceTree = "<group>"; };
- DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
+ DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
Modified: branches/uml/source/actions/PrintActions.cc
===================================================================
--- branches/uml/source/actions/PrintActions.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/actions/PrintActions.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -86,8 +86,10 @@
STATS_OUT_FILE(PrintMarketData, market.dat );
STATS_OUT_FILE(PrintSenseData, sense.dat );
STATS_OUT_FILE(PrintSenseExeData, sense_exe.dat );
+STATS_OUT_FILE(PrintUMLData, uml.dat );
+
#define POP_OUT_FILE(METHOD, DEFAULT) /* 1 */ \
class cAction ## METHOD : public cAction { /* 2 */ \
private: /* 3 */ \
Modified: branches/uml/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/uml/source/cpu/cHardwareCPU.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/cpu/cHardwareCPU.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -337,6 +337,28 @@
tInstLibEntry<tMethod>("promoter", &cHardwareCPU::Inst_Promoter),
tInstLibEntry<tMethod>("decay-reg", &cHardwareCPU::Inst_DecayRegulation),
+ // UML Element Creation
+ tInstLibEntry<tMethod>("addState", &cHardwareCPU::Inst_AddState, false,
+ "Add a new state"),
+ tInstLibEntry<tMethod>("next", &cHardwareCPU::Inst_Next, false,
+ "Increment to the next position in the list"),
+ tInstLibEntry<tMethod>("prev", &cHardwareCPU::Inst_Prev, false,
+ "Decrement to the previous position in the list"),
+ tInstLibEntry<tMethod>("addTransLab", &cHardwareCPU::Inst_AddTransitionLabel, false,
+ "Add a transition label"),
+ tInstLibEntry<tMethod>("addTrans", &cHardwareCPU::Inst_AddTransition, false,
+ "Add a transition"),
+ tInstLibEntry<tMethod>("addTransT", &cHardwareCPU::Inst_AddTransitionTotal, false,
+ "Add a transition without adding a label."),
+ tInstLibEntry<tMethod>("jump", &cHardwareCPU::Inst_JumpIndex, false,
+ "Jump to a position in the list"),
+ tInstLibEntry<tMethod>("first", &cHardwareCPU::Inst_First, false,
+ "Go to the first position in the list"),
+ tInstLibEntry<tMethod>("last", &cHardwareCPU::Inst_Last, false,
+ "Go to the last position in the list"),
+ tInstLibEntry<tMethod>("jump-d", &cHardwareCPU::Inst_JumpDist, false,
+ "Jump to a position in the list using labels."),
+
// Placebo instructions
tInstLibEntry<tMethod>("skip", &cHardwareCPU::Inst_Skip),
@@ -3843,3 +3865,249 @@
return true;
}
+
+//// UML Element Construction ////
+
+bool cHardwareCPU::Inst_Next(cAvidaContext& ctx)
+{
+ // by default, this instruction increments the triggers vector index
+
+ int reg_used = FindModifiedRegister(REG_AX);
+
+ int jump_amount = 1;
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->relativeJumpTrigger(jump_amount);
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->relativeJumpGuard(jump_amount);
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->relativeJumpAction(jump_amount);
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->relativeJumpTransitionLabel(jump_amount);
+ break;
+ case 4:
+ // decrement the original state index
+ organism->relativeJumpOriginState(jump_amount);
+ break;
+ case 5:
+ // decement the destination state index
+ organism->relativeJumpDestinationState(jump_amount);
+ break;
+ }
+ return true;
+}
+
+bool cHardwareCPU::Inst_Prev(cAvidaContext& ctx)
+{
+ int reg_used = FindModifiedRegister(REG_AX);
+
+ int jump_amount = -1;
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->relativeJumpTrigger(jump_amount);
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->relativeJumpGuard(jump_amount);
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->relativeJumpAction(jump_amount);
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->relativeJumpTransitionLabel(jump_amount);
+ break;
+ case 4:
+ // decrement the original state index
+ organism->relativeJumpOriginState(jump_amount);
+ break;
+ case 5:
+ // decement the destination state index
+ organism->relativeJumpDestinationState(jump_amount);
+ break;
+ }
+ return true;
+}
+
+bool cHardwareCPU::Inst_JumpIndex(cAvidaContext& ctx)
+{
+ const int reg_used = FindModifiedRegister(REG_AX);
+ const int reg_jump = FindModifiedRegister(REG_BX);
+ int jump_amount = GetRegister(reg_jump);
+
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->absoluteJumpTrigger(jump_amount);
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->absoluteJumpGuard(jump_amount);
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->absoluteJumpAction(jump_amount);
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->absoluteJumpTransitionLabel(jump_amount);
+ break;
+ case 4:
+ // decrement the original state index
+ organism->absoluteJumpOriginState(jump_amount);
+ break;
+ case 5:
+ // decement the destination state index
+ organism->absoluteJumpDestinationState(jump_amount);
+ break;
+ }
+ return true;
+}
+
+bool cHardwareCPU::Inst_JumpDist(cAvidaContext& ctx)
+{
+ const int reg_used = FindModifiedRegister(REG_AX);
+ ReadLabel();
+ int jump_amount = GetLabel().AsInt(NUM_NOPS);
+ //const int reg_jump = FindModifiedRegister(REG_BX);
+ //int jump_amount = GetRegister(reg_jump);
+
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->absoluteJumpTrigger(jump_amount);
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->absoluteJumpGuard(jump_amount);
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->absoluteJumpAction(jump_amount);
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->absoluteJumpTransitionLabel(jump_amount);
+ break;
+ case 4:
+ // decrement the original state index
+ organism->absoluteJumpOriginState(jump_amount);
+ break;
+ case 5:
+ // decement the destination state index
+ organism->absoluteJumpDestinationState(jump_amount);
+ break;
+ }
+ return true;
+}
+
+bool cHardwareCPU::Inst_First(cAvidaContext& ctx)
+{
+ // by default, this instruction increments the triggers vector index
+
+ int reg_used = FindModifiedRegister(REG_AX);
+
+// int jump_amount = 1;
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->firstTrigger();
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->firstGuard();
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->firstAction();
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->firstTransitionLabel();
+ break;
+ case 4:
+ // decrement the original state index
+ organism->firstOriginState();
+ break;
+ case 5:
+ // decement the destination state index
+ organism->firstDestinationState();
+ break;
+ }
+ return true;
+}
+
+bool cHardwareCPU::Inst_Last(cAvidaContext& ctx)
+{
+ // by default, this instruction increments the triggers vector index
+
+ int reg_used = FindModifiedRegister(REG_AX);
+
+// int jump_amount = 1;
+
+ switch (reg_used){
+ case 0:
+ // decrement the triggers vector index
+ organism->lastTrigger();
+ break;
+ case 1:
+ // decrement the guards vector index
+ organism->lastGuard();
+ break;
+ case 2:
+ // decrement the actions vector index
+ organism->lastAction();
+ break;
+ case 3:
+ // decrement the transition labels index
+ organism->lastTransitionLabel();
+ break;
+ case 4:
+ // decrement the original state index
+ organism->lastOriginState();
+ break;
+ case 5:
+ // decement the destination state index
+ organism->lastDestinationState();
+ break;
+ }
+ return true;
+}
+
+
+bool cHardwareCPU::Inst_AddTransitionLabel(cAvidaContext& ctx)
+{
+ return organism->addTransitionLabel();
+// return true;
+}
+
+bool cHardwareCPU::Inst_AddState(cAvidaContext& ctx)
+{
+ return organism->addState();
+}
+
+bool cHardwareCPU::Inst_AddTransition(cAvidaContext& ctx)
+{
+ return organism->addTransition();
+}
+
+bool cHardwareCPU::Inst_AddTransitionTotal(cAvidaContext& ctx)
+{
+ return organism->addTransitionTotal();
+}
+
+
Modified: branches/uml/source/cpu/cHardwareCPU.h
===================================================================
--- branches/uml/source/cpu/cHardwareCPU.h 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/cpu/cHardwareCPU.h 2007-05-03 13:22:28 UTC (rev 1519)
@@ -533,6 +533,18 @@
//// Placebo ////
bool Inst_Skip(cAvidaContext& ctx);
+
+ //// UML Element Construction ////
+ bool Inst_AddState(cAvidaContext& ctx);
+ bool Inst_Next(cAvidaContext& ctx);
+ bool Inst_Prev(cAvidaContext& ctx);
+ bool Inst_JumpIndex(cAvidaContext& ctx);
+ bool Inst_JumpDist(cAvidaContext& ctx);
+ bool Inst_AddTransitionLabel(cAvidaContext& ctx);
+ bool Inst_AddTransition(cAvidaContext& ctx);
+ bool Inst_AddTransitionTotal(cAvidaContext& ctx);
+ bool Inst_Last(cAvidaContext& ctx);
+ bool Inst_First(cAvidaContext& ctx);
};
Modified: branches/uml/source/main/cBirthChamber.cc
===================================================================
--- branches/uml/source/main/cBirthChamber.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cBirthChamber.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -165,6 +165,9 @@
child_genotype->IncDeferAdjust();
+ // FOR UML branch - hjg
+ child_array[0]->setParentInfo(parent.getXMI(), parent.getBonus());
+
return true;
}
Modified: branches/uml/source/main/cOrganism.cc
===================================================================
--- branches/uml/source/main/cOrganism.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cOrganism.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -47,7 +47,32 @@
using namespace std;
+// UML load file
+
+std::string loadFile(const char* filename) {
+ std::string data, line; // or maybe stringstream? (strstream?)
+ std::ifstream infile;
+ infile.open(filename);
+ assert(infile.is_open());
+
+ while (getline (infile, line))
+ {
+ data.append(line);
+ line.erase();
+ }
+
+ //read from file; load into string/strstream, and return it.
+
+ return data;
+}
+
+std::string cOrganism::xmi_begin = loadFile("xmi_begin");
+std::string cOrganism::xmi_end = loadFile("xmi_end");
+
+// END UML load file
+
+
cOrganism::cOrganism(cWorld* world, cAvidaContext& ctx, const cGenome& in_genome)
: m_world(world)
, m_genotype(NULL)
@@ -67,6 +92,7 @@
, m_max_executed(-1)
, m_is_running(false)
, m_net(NULL)
+ , parent_xmi("") // UML sets parent XMI -- ""
{
// Initialization of structures...
m_hardware = m_world->GetHardwareManager().Create(this);
@@ -87,6 +113,84 @@
if (m_world->GetConfig().NET_ENABLED.Get()) m_net = new cNetSupport();
m_id = m_world->GetStats().GetTotCreatures();
+
+ // Initialize UML model with elements
+
+/*
+ // create in-memory representation of model
+ AddTrans(0,0,1);
+ AddTrans(1,1,2);
+ AddTrans(2,2,3);
+ AddTrans(3,3,1);
+*/
+
+ trigger_info trig;
+ trig.label = "<null>";
+ trig.operation_id = "<null>";
+ triggers.push_back(trig);
+ trig.label = "setTempOpState";
+ trig.operation_id = "XDE-4437EBF1-9C42-4EB4-B7CF-415697B567CD";
+ triggers.push_back(trig);
+ trig.label = "setTempData";
+ trig.operation_id = "XDE-9517D6BA-8666-4A82-AFEA-62D60FE37B07";
+ triggers.push_back(trig);
+ guards.push_back("<null>");
+ actions.push_back("<null>");
+ actions.push_back("^TempSensor.getOpState()");
+ actions.push_back("^TempSensor.getTempData()");
+
+ // initialize w/ 10 states
+
+ state s;
+ for (int i=0; i<11; i++) {
+ s.identifier = i;
+ s.num_incoming = 0;
+ s.num_outgoing = 0;
+ states.push_back(s);
+ }
+
+ // initialize transitions
+ transition t;
+
+ // State 0->1
+ t.orig_state = 0;
+ t.dest_state = 1;
+ states[0].num_outgoing += 1;
+ states[1].num_incoming += 1;
+ t.trans.trigger = 0;
+ t.trans.guard = "<null>";
+ t.trans.action = "<null>";
+ transitions.push_back(t);
+
+ // State 1->2
+ t.orig_state = 1;
+ t.dest_state = 2;
+ states[1].num_outgoing += 1;
+ states[2].num_incoming += 1;
+ t.trans.trigger = 0;
+ t.trans.guard = "<null>";
+ t.trans.action = "^TempSensor.getTempData()";
+ transitions.push_back(t);
+
+ // State 2->1
+ t.orig_state = 2;
+ t.dest_state = 1;
+ states[2].num_outgoing += 1;
+ states[1].num_incoming += 1;
+ t.trans.trigger = 2;
+ t.trans.guard = "<null>";
+ t.trans.action = "<null>";
+ transitions.push_back(t);
+
+ // initialize the iterators to point to the first element
+ trigger_index = 0;
+ action_index = 0;
+ guard_index = 0;
+ trans_label_index = 0;
+ orig_state_index = 0;
+ dest_state_index = 9;
+// END UML model initialization
+
}
@@ -556,3 +660,528 @@
m_phenotype.IncErrors();
}
+
+
+/// UML Functions ///
+/// This function is a copy of DoOutput ///
+void cOrganism::modelCheck(cAvidaContext& ctx)
+{
+
+ printXMI();
+
+
+ assert(m_interface);
+ const tArray<double> & resource_count = m_interface->GetResources();
+
+ tList<tBuffer<int> > other_input_list;
+ tList<tBuffer<int> > other_output_list;
+
+ // If tasks require us to consider neighbor inputs, collect them...
+ if (m_world->GetEnvironment().UseNeighborInput()) {
+ const int num_neighbors = m_interface->GetNumNeighbors();
+ for (int i = 0; i < num_neighbors; i++) {
+ m_interface->Rotate();
+ cOrganism * cur_neighbor = m_interface->GetNeighbor();
+ if (cur_neighbor == NULL) continue;
+
+ other_input_list.Push( &(cur_neighbor->m_input_buf) );
+ }
+ }
+
+ // If tasks require us to consider neighbor outputs, collect them...
+ if (m_world->GetEnvironment().UseNeighborOutput()) {
+ const int num_neighbors = m_interface->GetNumNeighbors();
+ for (int i = 0; i < num_neighbors; i++) {
+ m_interface->Rotate();
+ cOrganism * cur_neighbor = m_interface->GetNeighbor();
+ if (cur_neighbor == NULL) continue;
+
+ other_output_list.Push( &(cur_neighbor->m_output_buf) );
+ }
+ }
+
+ // bool net_valid = false;
+ // if (m_net) net_valid = NetValidate(ctx, value);
+
+ // Do the testing of tasks performed...
+
+ // if on IO add value to m_output_buf, if on divide don't need to
+ //if (!on_divide) m_output_buf.Add(value);
+
+ tArray<double> res_change(resource_count.GetSize());
+ tArray<int> insts_triggered;
+ bool clear_input = false;
+
+ tBuffer<int>* received_messages_point = &m_received_messages;
+ if (!m_world->GetConfig().SAVE_RECEIVED.Get()) received_messages_point = NULL;
+
+ cTaskContext taskctx(m_interface, m_input_buf, m_output_buf, other_input_list, other_output_list, 0, 0, 0, received_messages_point, this);
+ m_phenotype.TestOutput(ctx, taskctx, resource_count, res_change, insts_triggered);
+ m_interface->UpdateResources(res_change);
+
+ for (int i = 0; i < insts_triggered.GetSize(); i++) {
+ const int cur_inst = insts_triggered[i];
+ m_hardware->ProcessBonusInst(ctx, cInstruction(cur_inst));
+ }
+
+ if (clear_input) m_input_buf.Clear();
+
+ m_world->GetStats().addState(states.size());
+ m_world->GetStats().addTrans(transitions.size());
+ m_world->GetStats().addTransLabel(transition_labels.size());
+
+}
+
+void cOrganism::seedModel() {
+ std::string data, line;
+ std::ifstream infile;
+ infile.open("instinctModel");
+ assert(infile.is_open());
+
+ while (getline (infile, line))
+ {
+ data.append(line);
+ line.erase();
+ }
+
+ //read from file; load into string/strstream, and return it.
+
+ //return data;
+ return;
+
+}
+
+bool cOrganism::findTransLabel(transition_label t) {
+ for(std::vector<transition_label>::iterator i=transition_labels.begin(); i!=transition_labels.end(); ++i){
+ if ((i->trigger == t.trigger) && (i->guard == t.guard) && (i->action == t.action)) {
+// if (i->trigger == t.trigger) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool cOrganism::findTrans(int orig, int dest, int trig, std::string gu, std::string act)
+{
+ // the wild cards for there are
+ // -1 for orig, dest & trigger
+ // "*" for guard, and action
+
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if (((orig == -1) || (orig == i->orig_state)) &&
+ ((dest == -1) || (dest == i->dest_state)) &&
+ ((trig == -1) || (trig == i->trans.trigger)) &&
+ ((gu == "*") || (gu == i->trans.guard)) &&
+ ((act == "*") || (act == i->trans.action))) {
+ return true;
+ }
+ }
+ return false;
+}
+/*
+bool cOrganism::findTrans(int orig, int dest)
+{
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if((i->orig_state == orig) && (i->dest_state == dest)) {
+ return true;
+ }
+ }
+
+ return false;
+
+}
+
+bool cOrganism::findTrans(int orig, int dest, std::string label)
+{
+ std::string t_lab;
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if((i->orig_state == orig) && (i->dest_state == dest)) {
+ t_lab = (StringifyAnInt(i->trans.trigger) + i->trans.guard + i->trans.action);
+ if (t_lab == label) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+bool cOrganism::findTrans(transition t)
+{
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if((i->orig_state == t.orig_state) && (i->dest_state == t.dest_state) &&
+ (i->trans.trigger == t.trans.trigger) && (i->trans.guard == t.trans.guard) &&
+ (i->trans.action == t.trans.action)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+*/
+
+template <typename T>
+bool cOrganism::absoluteMoveIndex (T x, int &index, int amount )
+{
+ index = 0;
+ return relativeMoveIndex(x, index, amount);
+}
+
+template <typename T>
+bool cOrganism::relativeMoveIndex (T x, int &index, int amount )
+{
+ if (x.size() == 0) {
+ return false;
+ }
+
+ if (amount > 0) {
+// index += (amount % x.size()); // this provides relative jumping
+ index += (amount % x.size());
+
+ // index is greater than vector
+ if (index >= x.size()) {
+ index -= x.size();
+ } else if(index < 0) {
+ index += x.size();
+ }
+ }
+
+ return true;
+}
+
+
+bool cOrganism::absoluteJumpTrigger(int jump_amount)
+{
+ return absoluteMoveIndex(triggers, trigger_index, jump_amount);
+}
+
+bool cOrganism::absoluteJumpGuard(int jump_amount)
+{
+ return absoluteMoveIndex(guards, guard_index, jump_amount);
+}
+
+bool cOrganism::absoluteJumpAction(int jump_amount)
+{
+ return absoluteMoveIndex(actions, action_index, jump_amount);
+}
+
+bool cOrganism::absoluteJumpTransitionLabel(int jump_amount)
+{
+ return absoluteMoveIndex(transition_labels, trans_label_index, jump_amount);
+}
+
+bool cOrganism::absoluteJumpOriginState(int jump_amount)
+{
+ return absoluteMoveIndex(states, orig_state_index, jump_amount);
+}
+
+bool cOrganism::absoluteJumpDestinationState(int jump_amount)
+{
+ return absoluteMoveIndex(states, dest_state_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpTrigger(int jump_amount)
+{
+ return relativeMoveIndex(triggers, trigger_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpGuard(int jump_amount)
+{
+ return relativeMoveIndex(guards, guard_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpAction(int jump_amount)
+{
+ return relativeMoveIndex(actions, action_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpTransitionLabel(int jump_amount)
+{
+ return relativeMoveIndex(transition_labels, trans_label_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpOriginState(int jump_amount)
+{
+ return relativeMoveIndex(states, orig_state_index, jump_amount);
+}
+
+bool cOrganism::relativeJumpDestinationState(int jump_amount)
+{
+ return relativeMoveIndex(states, dest_state_index, jump_amount);
+}
+
+int cOrganism::getTriggerIndex()
+{
+ /*if (triggers.size() == 0) {
+ return 0;
+ } else {*/
+
+ return trigger_index;
+ //}
+}
+
+std::string cOrganism::getGuard()
+{
+ if (guards.size() == 0) {
+ return "";
+ } else {
+ return guards[guard_index];
+ }
+}
+
+std::string cOrganism::getAction()
+{
+ if (actions.size() == 0) {
+ return "";
+ } else {
+ return actions[action_index];
+ }
+}
+
+int cOrganism::getOrigStateIndex()
+{
+ return orig_state_index;
+}
+
+int cOrganism::getDestStateIndex()
+{
+ return dest_state_index;
+}
+
+transition_label cOrganism::getTransLabel()
+{
+ return transition_labels[trans_label_index];
+}
+
+bool cOrganism::addState()
+{
+ state s;
+ s.identifier = states.size();
+ s.num_incoming = 0;
+ s.num_outgoing = 0;
+ states.push_back(s);
+
+ return true;
+}
+
+bool cOrganism::addTransitionLabel()
+{
+ transition_label t;
+ t.trigger = getTriggerIndex();
+ t.guard = getGuard();
+ t.action = getAction();
+
+ // no dupes
+ if (findTransLabel(t)){
+ return false;
+ }
+
+ transition_labels.push_back(t);
+
+ // Move the transition label index to the most recently created
+ trans_label_index = transition_labels.size() - 1;
+
+ return true;
+}
+
+
+bool cOrganism::addTransition()
+{
+ if ((states.size() == 0) || (transition_labels.size() == 0)) {
+
+ return false;
+ }
+
+ transition t;
+ t.orig_state = getOrigStateIndex();
+ t.dest_state = getDestStateIndex();
+ // increment number of edges for a state
+ states[getOrigStateIndex()].num_outgoing += 1;
+ states[getDestStateIndex()].num_incoming += 1;
+
+ t.trans = getTransLabel();
+
+ // no dupes
+ if (findTrans(t.orig_state, t.dest_state, t.trans.trigger, t.trans.guard, t.trans.action)) {
+ return false;
+ }
+
+ transitions.push_back(t);
+
+ return true;
+
+}
+
+
+bool cOrganism::addTransitionTotal()
+{
+ if ((states.size() == 0)) {
+
+ return false;
+ }
+
+ transition t;
+ t.orig_state = getOrigStateIndex();
+ t.dest_state = getDestStateIndex();
+
+
+ // Do not create transition if the origin state is unreachable.
+ if ((t.orig_state != 0) && (states[t.orig_state].num_incoming == 0)) {
+ return false;
+ }
+
+ // increment number of edges for a state
+ states[getOrigStateIndex()].num_outgoing += 1;
+ states[getDestStateIndex()].num_incoming += 1;
+
+
+ transition_label tl;
+ tl.trigger = getTriggerIndex();
+ tl.guard = getGuard();
+ tl.action = getAction();
+ t.trans = tl;
+
+
+ // no dupes
+ if (findTrans(t.orig_state, t.dest_state, t.trans.trigger, t.trans.guard, t.trans.action)) {
+ return false;
+ }
+
+ transitions.push_back(t);
+
+ // reset all indices
+ orig_state_index = 0;
+ dest_state_index = 0;
+ trigger_index = 0;
+ action_index = 0;
+ guard_index = 0;
+
+ return true;
+
+}
+
+
+int cOrganism::numStates()
+{
+ return states.size();
+}
+
+int cOrganism::numTrans()
+{
+ return transitions.size();
+}
+
+// print the label. Change - signs to _
+std::string cOrganism::StringifyAnInt(int x) {
+
+ std::ostringstream o;
+
+ if (x < 0) {
+ x = abs(x);
+ o << "_";
+ }
+
+ o << x;
+ return o.str();
+}
+
+std::string cOrganism::getXMI()
+{
+ return (xmi_begin + xmi + xmi_end);
+}
+
+void cOrganism::printXMI()
+{
+ std::string temp, temp1, temp2, temp3;
+ std::string trig_label, trig_op_label;
+
+ int s_count = 0;
+ int t_count = 0;
+ xmi = "";
+
+ // This state is the initial state; thus it should be printed regardless of whether it has an incoming
+ // edge or not.
+ if (numStates() > 0) {
+ temp = StringifyAnInt(s_count);
+ xmi += "<UML:Pseudostate xmi.id=\"s" + temp + "\" kind=\"initial\" outgoing=\"\" name=\"s";
+ xmi += temp + "\" isSpecification=\"false\"/>\n";
+ ++s_count;
+ }
+
+ for (; s_count < numStates(); ++s_count) {
+
+ // only print if this state has an incoming edge.
+ if ((states[s_count]).num_incoming > 0) {
+ temp = "s" + StringifyAnInt(s_count);
+ xmi+="<UML:CompositeState xmi.id=\"";
+ xmi+=temp;
+ xmi+= "\" isConcurrent=\"false\" name=\"";
+ xmi+= temp;
+ xmi+= "\" isSpecification=\"false\"/>\n";
+ }
+ }
+
+ // end the set of states....
+ xmi+= "</UML:CompositeState.subvertex>\n";
+ xmi+= "</UML:CompositeState>\n";
+ xmi+= "</UML:StateMachine.top>\n";
+
+ // start the set of transitions...
+ xmi+="<UML:StateMachine.transitions>\n";
+
+
+
+ for (t_count = 0; t_count < numTrans(); ++t_count) {
+ // info determined from the trans itself....
+ temp = "t" + StringifyAnInt(t_count);
+ temp1 = "s" + StringifyAnInt(transitions[t_count].orig_state);
+ temp2 = "s" + StringifyAnInt(transitions[t_count].dest_state);
+ temp3 = temp + temp1 + temp2;
+
+ xmi+= "<UML:Transition xmi.id=\"" + temp3 + "\"";
+ xmi+= " source=\"" + temp1 + "\"";
+ xmi += " target=\"" + temp2 + "\" name=\"\" isSpecification=\"false\">\n";
+
+ // Get guard, trigger, and action
+// temp = transitions[t_count].trans.trigger;
+ temp1 = transitions[t_count].trans.guard;
+ temp2 = transitions[t_count].trans.action;
+ trig_label = triggers[transitions[t_count].trans.trigger].label;
+ trig_op_label = triggers[transitions[t_count].trans.trigger].operation_id;
+
+
+ // print trigger, if any
+ if (trig_label != "<null>") {
+ xmi+= "<UML:Transition.trigger> <UML:Event> <UML:ModelElement.namespace> <UML:Namespace> ";
+ xmi+= "<UML:Namespace.ownedElement> <UML:CallEvent xmi.id=\"" + temp3;
+ xmi+= "tt\" operation=\""+ trig_op_label + "\" ";
+ xmi+= "name=\"" + trig_label + "\" isSpecification=\"false\"/> ";
+ xmi+= "</UML:Namespace.ownedElement> </UML:Namespace> </UML:ModelElement.namespace> ";
+ xmi+= "</UML:Event> </UML:Transition.trigger> ";
+ }
+
+ // print guard, if any
+ // Note: for guard to work, '<' => '<'
+ if (temp1 != "<null>"){
+ xmi+= "<UML:Transition.guard> <UML:Guard> <UML:Guard.expression> ";
+ xmi+= "<UML:BooleanExpression body=\"" + temp1 + "\" language=\"\"/> ";
+ xmi+= "</UML:Guard.expression> </UML:Guard> </UML:Transition.guard> ";
+ }
+
+ // print action, if any
+ if (temp2 != "<null>") {
+ xmi+= "<UML:Transition.effect> <UML:UninterpretedAction xmi.id=\"" + temp3 + "ui\" ";
+ xmi+= "isAsynchronous=\"false\" name=\"\" isSpecification=\"false\"> ";
+ xmi+= "<UML:Action.script> <UML:ActionExpression language=\"\" body=\"";
+ xmi+= temp2 + "\"/> </UML:Action.script> </UML:UninterpretedAction> </UML:Transition.effect> ";
+ }
+
+ xmi += "</UML:Transition>\n";
+
+
+ }
+
+ return;
+}
+
Modified: branches/uml/source/main/cOrganism.h
===================================================================
--- branches/uml/source/main/cOrganism.h 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cOrganism.h 2007-05-03 13:22:28 UTC (rev 1519)
@@ -27,6 +27,11 @@
#define cOrganism_h
#include <iostream>
+#include <map>
+#include <utility>
+#include <set>
+#include <string>
+#include <vector>
#ifndef cCPUMemory_h
#include "cCPUMemory.h"
@@ -80,6 +85,35 @@
class cSaleItem;
+// UML Structures
+
+struct transition_label {
+ int trigger;
+ std::string guard;
+ std::string action;
+};
+
+struct transition {
+ int orig_state;
+ int dest_state;
+ transition_label trans;
+};
+
+struct state {
+ int identifier;
+ int num_incoming;
+ int num_outgoing;
+};
+
+struct trigger_info {
+ std::string operation_id;
+ std::string label;
+};
+
+// END UML Structures
+
+
+
class cOrganism
{
protected:
@@ -129,7 +163,32 @@
};
cNetSupport* m_net;
+ // UML diagram components
+ static std::string xmi_begin; // what comes before the organism generated xmi
+ std::string xmi; // the xmi created by pretty-printing the diagram generated by the organism
+ static std::string xmi_end; // what comes after the organism generated xmi
+
+ std::string parent_xmi;
+ std::map <std::string, double> parent_bonus;
+ std::map <std::string, double> self_bonus;
+
+ std::vector<state> states;
+ std::vector<trigger_info> triggers;
+ std::vector<std::string> guards;
+ std::vector<std::string> actions;
+ std::vector<transition> transitions;
+ std::vector<transition_label> transition_labels;
+
+ int orig_state_index;
+ int dest_state_index;
+ int trans_label_index;
+ int trigger_index;
+ int guard_index;
+ int action_index;
+
+ // END UML diagram components
+
cOrganism(); // @not_implemented
cOrganism(const cOrganism&); // @not_implemented
cOrganism& operator=(const cOrganism&); // @not_implemented
@@ -269,6 +328,89 @@
bool GetSterilizePos() const;
double GetNeutralMin() const;
double GetNeutralMax() const;
+
+ // UML Stuff
+ void modelCheck(cAvidaContext& ctx); // evaluate the model
+ void printXMI(); // print the XMI version of the model
+ std::string getXMI (); // get the xmi string (including beginning & end read from file.)
+ std::string StringifyAnInt(int);
+ int numStates();
+ int numTrans();
+ void seedModel();
+
+ // UML parent information tracking
+ void setParentInfo(std::string x, std::map <std::string, double> pb) {parent_xmi = x; parent_bonus = pb;}
+// void setParentHyBonus (double y) {parent_hy_bonus = y;}
+// void setParentSp1Bonus (double y) {parent_sp1_bonus = y;}
+ double getParentBonusInfo(std::string x) {return parent_bonus[x];}
+ std::map <std::string, double> getBonus() {return self_bonus;}
+ void setBonusInfo(std::string x, double y) {self_bonus[x] = y;}
+ //double getParentSp1Bonus() {return parent_sp1_bonus;}
+ //void addBonus (double y) {bonus += y;}
+ //double getBonus () {return bonus;}
+
+ // find functions
+// bool findTrans(int, int); // find a transition between two states
+// bool findTrans(int, int, std::string); // find a transition between two states with a specific label.
+// bool findTrans(transition); // find a transition between two states with a specific label.
+ bool findTrans(int, int, int, std::string, std::string) ;
+ bool findTransLabel(transition_label); // find a specific transition label
+
+ template <typename T>
+ bool absoluteMoveIndex (T x, int &y, int z);
+
+ template <typename T>
+ bool relativeMoveIndex (T x, int &y, int z);
+
+// The jump functions jump the index of the various vectors either forward (+ int) or backwards (- int)
+ bool absoluteJumpGuard(int);
+ bool absoluteJumpAction(int);
+ bool absoluteJumpTrigger(int);
+ bool absoluteJumpTransitionLabel(int);
+ bool absoluteJumpOriginState(int);
+ bool absoluteJumpDestinationState(int);
+ bool relativeJumpGuard(int);
+ bool relativeJumpAction(int);
+ bool relativeJumpTrigger(int);
+ bool relativeJumpTransitionLabel(int);
+ bool relativeJumpOriginState(int);
+ bool relativeJumpDestinationState(int);
+
+
+// The first functions jump the index to the beginning of various vectors
+ bool firstGuard() {guard_index = 0;}
+ bool firstAction() {action_index = 0;}
+ bool firstTrigger() {trigger_index = 0;}
+ bool firstTransitionLabel() {trans_label_index = 0;}
+ bool firstOriginState() {orig_state_index = 0;}
+ bool firstDestinationState() {dest_state_index = 0;}
+
+// The last functions jump the index to the end of various vectors
+ bool lastGuard() {guard_index = (guards.size()-1);}
+ bool lastAction() {action_index = (actions.size()-1);}
+ bool lastTrigger() {trigger_index = (triggers.size()-1);}
+ bool lastTransitionLabel() {trans_label_index = (transition_labels.size()-1);}
+ bool lastOriginState() {orig_state_index = (states.size()-1);}
+ bool lastDestinationState() {dest_state_index = (states.size()-1);}
+
+
+// The get functions get the value of the index of various vectors
+// std::string getTriggerIndex();
+ int getTriggerIndex();
+ std::string getGuard();
+ std::string getAction();
+ transition_label getTransLabel();
+ int getOrigStateIndex();
+ int getDestStateIndex();
+ std::string getParentXMI() {return parent_xmi;}
+
+// Add functions
+ bool addState();
+ bool addTransitionLabel();
+ bool addTransition();
+ bool addTransitionTotal();
+ // END UML functions
+
};
Modified: branches/uml/source/main/cStats.cc
===================================================================
--- branches/uml/source/main/cStats.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cStats.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -329,16 +329,10 @@
assert(sum_fitness.Average() >= 0.0);
assert(dom_fitness >= 0);
-
- // Note: When average fitness and dominant fitness are close in value (i.e. should be identical)
- // floating point rounding error can cause output variances. To mitigate this, threshold
- // caps off values that differ by less than it, flushing the effective output value to zero.
- const double ave_fitness = sum_fitness.Average();
- const double threshold = 1.0e-14;
- if (ave_fitness == 0.0 || dom_fitness == 0.0 || fabs(ave_fitness - dom_fitness) < threshold) {
+ if (sum_fitness.Average() == 0.0 || dom_fitness == 0.0) {
energy = 0.0;
} else {
- energy = Log(dom_fitness / ave_fitness);
+ energy = Log(dom_fitness / sum_fitness.Average());
}
}
@@ -991,3 +985,36 @@
}
df.Endl();
}
+
+void cStats::PrintUMLData(const cString& filename)
+{
+ cDataFile& df = m_world->GetDataFile(filename);
+
+ df.WriteComment( "Avida uml data\n" );
+ df.WriteComment("the average number of transitions and states per organism");
+ df.WriteTimeStamp();
+ df.Write( GetUpdate(), "update" );
+ df.Write( av_number_of_states.Average(), "av num states");
+ df.Write( av_number_of_trans.Average(), "av num trans");
+ df.Write( av_number_of_trans_lab.Average(), "av num of trans lab");
+ df.Write( m_hydraAttempt.Sum(), "total number of hydra attempts" );
+ df.Write( m_hydraPassed.Sum(), "total number of hydra passes" );
+ df.Write( m_spinAttempt.Sum(), "total number of spin attempts" );
+ df.Write( m_spinPassed.Sum(), "total number of spin passes" );
+ df.Write( m_panAttempt.Sum(), "total number of pan attempts" );
+ df.Write( m_panPassed.Sum(), "total number of pan passes" );
+
+ av_number_of_states.Clear();
+ av_number_of_trans.Clear();
+ av_number_of_trans_lab.Clear();
+
+ m_hydraAttempt.Clear();
+ m_hydraPassed.Clear();
+ m_spinAttempt.Clear();
+ m_spinPassed.Clear();
+ m_panAttempt.Clear();
+ m_panPassed.Clear();
+
+
+df.Endl();
+}
Modified: branches/uml/source/main/cStats.h
===================================================================
--- branches/uml/source/main/cStats.h 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cStats.h 2007-05-03 13:22:28 UTC (rev 1519)
@@ -247,6 +247,17 @@
tArray<int> sense_last_count;
tArray<int> sense_last_exe_count;
tArray<cString> sense_names;
+
+ // Stats for UML state diagrams
+ cDoubleSum av_number_of_states;
+ cDoubleSum av_number_of_trans;
+ cDoubleSum av_number_of_trans_lab;
+ cDoubleSum m_hydraAttempt;
+ cDoubleSum m_hydraPassed;
+ cDoubleSum m_spinAttempt;
+ cDoubleSum m_spinPassed;
+ cDoubleSum m_panAttempt;
+ cDoubleSum m_panPassed;
cStats(); // @not_implemented
cStats(const cStats&); // @not_implemented
@@ -478,6 +489,17 @@
void AddMarketItemSold() { num_sold++; }
void AddMarketItemUsed() { num_used++; }
void AddMarketOwnItemUsed() { num_own_used++; }
+
+ // UML Data Function
+ void addState(int x) { av_number_of_states.Add(x); }
+ void addTrans(int x) { av_number_of_trans.Add(x); }
+ void addTransLabel(int x) { av_number_of_trans_lab.Add(x); }
+ void HydraAttempt() { m_hydraAttempt.Add(1); }
+ void HydraPassed() { m_hydraPassed.Add(1); }
+ void SpinAttempt() { m_spinAttempt.Add(1); }
+ void SpinPassed() { m_spinPassed.Add(1); }
+ void PanAttempt() { m_panAttempt.Add(1); }
+ void PanPassed() { m_panPassed.Add(1); }
// Information retrieval section...
@@ -602,6 +624,8 @@
void PrintMarketData(const cString& filename);
void PrintSenseData(const cString& filename);
void PrintSenseExeData(const cString& filename);
+ void PrintUMLData(const cString& filename);
+
};
Modified: branches/uml/source/main/cTaskContext.h
===================================================================
--- branches/uml/source/main/cTaskContext.h 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cTaskContext.h 2007-05-03 13:22:28 UTC (rev 1519)
@@ -37,13 +37,21 @@
#ifndef tHashTable_h
#include "tHashTable.h"
#endif
+#ifndef cOrganism
+#include "cOrganism.h"
+#endif
+
class cTaskEntry;
class cTaskState;
+class cOrganism;
class cTaskContext
{
+
+ friend class cTaskLib;
+
private:
cOrgInterface* m_interface;
const tBuffer<int>& m_input_buffer;
@@ -55,15 +63,19 @@
tBuffer<int>* m_received_messages;
int m_logic_id;
bool m_on_divide;
+ int task_success_complete;
+
cTaskEntry* m_task_entry;
tHashTable<void*, cTaskState*>* m_task_states;
+ cOrganism* organism;
+
public:
cTaskContext(cOrgInterface* interface, const tBuffer<int>& inputs, const tBuffer<int>& outputs,
const tList<tBuffer<int> >& other_inputs, const tList<tBuffer<int> >& other_outputs,
bool in_net_valid, int in_net_completed, bool in_on_divide = false,
- tBuffer<int>* in_received_messages = NULL)
+ tBuffer<int>* in_received_messages = NULL, cOrganism* in_org = NULL)
: m_interface(interface)
, m_input_buffer(inputs)
, m_output_buffer(outputs)
@@ -76,6 +88,8 @@
, m_on_divide(in_on_divide)
, m_task_entry(NULL)
, m_task_states(NULL)
+ , task_success_complete(0)
+
{
}
Modified: branches/uml/source/main/cTaskLib.cc
===================================================================
--- branches/uml/source/main/cTaskLib.cc 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cTaskLib.cc 2007-05-03 13:22:28 UTC (rev 1519)
@@ -35,6 +35,7 @@
#include <cstdlib>
#include <cmath>
#include <climits>
+#include <errno.h>
// Various workarounds for Visual Studio shortcomings
#if AVIDA_PLATFORM(WINDOWS)
@@ -379,8 +380,29 @@
else if (name == "net_receive")
NewTask(name, "Successfully Received Network Message", &cTaskLib::Task_NetReceive);
+ // UML tasks
+ else if (name == "trans1") //
+ NewTask(name, "Successfully created trans 1", &cTaskLib::Task_Trans1);
+ else if (name == "trans2") //
+ NewTask(name, "Successfully created trans 2", &cTaskLib::Task_Trans2);
+ else if (name == "trans3") //
+ NewTask(name, "Successfully created trans 3", &cTaskLib::Task_Trans3);
+ else if (name == "trans4") //
+ NewTask(name, "Successfully created trans 4", &cTaskLib::Task_Trans4);
+ else if (name == "trans5") //
+ NewTask(name, "Successfully created trans 5", &cTaskLib::Task_Trans5);
+ else if (name == "numStates") //
+ NewTask(name, "Successfully created 5 states", &cTaskLib::Task_NumStates);
+ else if (name == "numTrans") //
+ NewTask(name, "Successfully created 5 transitions", &cTaskLib::Task_NumTrans);
+ else if (name == "hydra") //
+ NewTask(name, "Successfully ran hydra", &cTaskLib::Task_Hydra);
+ else if (name == "spin1") //
+ NewTask(name, "Successfully ran Spin", &cTaskLib::Task_SpinN1);
+ else if (name == "mult_trans") //
+ NewTask(name, "Successfully completed multiple transitions", &cTaskLib::Task_MultTrans);
+
-
// Make sure we have actually found a task
if (task_array.GetSize() == start_size) {
if (errors != NULL && errors->GetSize() == 0) {
@@ -2615,3 +2637,242 @@
if (ctx.NetIsValid()) return 1.0;
return 0.0;
}
+
+
+
+double cTaskLib::Task_Trans1(cTaskContext& ctx) const
+{
+ double bonus = 0.0;
+ if (ctx.organism->findTrans(0,1,1, "ga", "aa")) {
+ ctx.task_success_complete = 1;
+ bonus = 1.0;
+ }
+
+// ctx->task_success_complete = 0;
+ return bonus;
+}
+
+double cTaskLib::Task_Trans2(cTaskContext& ctx) const
+{
+ double bonus = 0.0;
+ if (ctx.organism->findTrans(1,2,1,"gd", "ab")){
+ ctx.task_success_complete += 1;
+ bonus = 1.0;
+ }
+
+ return bonus;
+
+}
+
+double cTaskLib::Task_Trans3(cTaskContext& ctx) const
+{
+ double bonus = 0.0;
+ if (ctx.organism->findTrans(2,3,3,"gb", "ac")){
+ bonus = 1.0;
+ ctx.task_success_complete += 1;
+ }
+
+ return bonus;
+
+}
+
+double cTaskLib::Task_Trans4(cTaskContext& ctx) const
+{
+ double bonus = 0.0;
+ if (ctx.organism->findTrans(3,4,2,"gc", "ad")){
+ ctx.task_success_complete += 1;
+ bonus = 1.0;
+ }
+
+ return bonus;
+
+}
+
+double cTaskLib::Task_Trans5(cTaskContext& ctx) const
+{
+ double bonus = 0.0;
+ if (ctx.organism->findTrans(4,0,4,"ga","ac")){
+ ctx.task_success_complete += 1;
+ bonus = 1.0;
+ }
+
+ return bonus;
+
+}
+
+double cTaskLib::Task_NumStates(cTaskContext& ctx) const
+{
+ double ns = (double) ctx.organism->numStates();
+
+ if (ns <= 5 ) {
+ return (ns/5);
+ } else{
+ return 0.0;
+ }
+
+}
+
+double cTaskLib::Task_NumTrans(cTaskContext& ctx) const
+{
+ double nt = (double) ctx.organism->numTrans();
+
+ if (nt <= 5 ) {
+ return (nt/5);
+ } else{
+ return 1;
+ }
+}
+
+
+// broken - 4/11
+double cTaskLib::Task_Hydra(cTaskContext& ctx) const
+{
+ cOrganism* organism = ctx.organism;
+ std::string temp;
+
+ temp = organism->getXMI();
+
+ if (temp == organism->getParentXMI()) {
+ ctx.task_success_complete += organism->getParentBonusInfo("hydra");
+ organism->setBonusInfo("hydra", organism->getParentBonusInfo("hydra"));
+ return organism->getParentBonusInfo("hydra");
+ }
+
+// Check for task success...
+/* if (ctx->task_failed == 0) {
+ return 0;
+ }
+
+*/
+ m_world->GetStats().HydraAttempt();
+
+ double bonus = 0.0;
+ unsigned int status_total = 0;
+ int status=0;
+
+ int to_subavida[2]={0};
+ int from_subavida[2]={0};
+
+ pipe(to_subavida); //write to 1, read from 0
+ pipe(from_subavida);
+
+ pid_t subavida = fork();
+ if(subavida == 0) {
+ //child
+ close(to_subavida[1]);
+ close(from_subavida[0]);
+ dup2(to_subavida[0], STDIN_FILENO); //oldd, newd
+ dup2(from_subavida[1], STDOUT_FILENO);
+ execl("/usr/bin/java", "-cp .", "-jar", "./hydraulic.jar", NULL);
+ // We don't ever get here.
+ }
+ //parent
+ close(to_subavida[0]);
+ close(from_subavida[1]);
+
+ // At this point, forget about subavida - It's running.
+ // Write the model to to_subavida[1]. Close to_subavida[1] (which wakes up subavida).
+ // Then, read from from_subavida[0] as long as is possible, after which point subavida will die.
+
+ // Write the model to STDIN of subavida (be careful; write may not write all that you ask!)
+ do {
+ status = write(to_subavida[1], temp.c_str()+status_total, temp.size());
+ if (status < 0) {
+ break;
+ } else {
+ status_total += status;
+ }
+ } while (status_total < temp.size());
+ close(to_subavida[1]); // Wakes up subavida.
+
+ // Time passes...
+
+ // Read the output from subavida. Keep reading until subavida closes the pipe.
+ const int read_size=128; // The number of bytes that we're going to try to read from subavida.
+ std::string subavida_output;
+ char line[read_size]={0};
+ do {
+ status = read(from_subavida[0], line, read_size-1);
+ if(status > 0) {
+ subavida_output += line;
+ memset(line, 0, read_size);
+ }
+// } while(((status==-1) && (errno == EINTR)) || (status>0));
+ } while(((status==-1) )|| (status>0));
+
+ // Done with subavida.
+ close(from_subavida[0]);
+ // Make sure that subavida dies.
+ pid_t done=0;
+ while((done=waitpid(subavida, &status, 0))==-1 && (errno == EINTR));
+ assert(done==subavida);
+
+ // if there are no errors, return 0 from hydraulic. otherwise, return non-zero.
+ if(status != 0) {
+// ctx->task_failed = 0;
+ organism->setBonusInfo("hydra", 0.0);
+ return 0.0;
+ } else {
+ // ctx->task_failed = ctx->task_failed && 1;
+ ctx.task_success_complete += 1;
+ m_world->GetStats().HydraPassed();
+ organism->setBonusInfo("hydra", 1.0);
+ return 1.0;
+ }
+
+}
+
+
+double cTaskLib::SpinCoprocess(cTaskContext& ctx, const std::string& neverclaimFile) const {
+ cOrganism* organism = ctx.organism;
+ m_world->GetStats().SpinAttempt();
+ int status=0;
+ std::string cmd = "cat " + neverclaimFile + " >> tmp.pr && ./spin -a tmp.pr &> /dev/null";
+ if(system(cmd.c_str())!=0) return 0.0;
+ m_world->GetStats().SpinPassed();
+ m_world->GetStats().PanAttempt();
+
+ if(system("/usr/bin/gcc -DMEMLIM=512 pan.c -o pan &> /dev/null")!=0) return 0.0;
+ if(system("./pan -a &> ./pan.out")!=0) return 0.0;
+ if(system("cat pan.out | perl -e 'while(<STDIN>) { if(/errors:\\s(\\d+)/) {exit($1);}}'")!=0) return 0.0;
+ if(system("cat pan.out | perl -e 'while(<STDIN>) { if(/unreached/) {exit(1);}}'")!=0) return 0.0;
+
+
+ std::ostringstream strstrm;
+ strstrm << "cp tmp.xmi " << m_world->GetStats().GetUpdate() << "." << organism->GetID();
+ strstrm << ".xml";
+ if(system(strstrm.str().c_str())!=0) return 0.0;
+
+ m_world->GetStats().PanPassed();
+ return 3.0;
+}
+
+double cTaskLib::Task_SpinN1(cTaskContext& ctx) const {
+ cOrganism* organism = ctx.organism;
+ std::string temp = organism->getXMI();
+ double temp1 = 0.0;
+
+ if (temp == organism->getParentXMI()) {
+ ctx.task_success_complete += organism->getParentBonusInfo("spinn1");
+ organism->setBonusInfo("spinn1", organism->getParentBonusInfo("spinn1"));
+ return organism->getParentBonusInfo("spinn1");
+ }
+
+ // check if the trigger is present
+ if (organism->findTrans(-1,-1,1,"*","*")){
+ temp += 1;
+
+ // check property
+ if (ctx.task_success_complete) {
+ temp1 += SpinCoprocess(ctx, "N1");
+ }
+ }
+
+ organism->setBonusInfo("spinn1", temp1);
+ return temp1;
+}
+
+double cTaskLib::Task_MultTrans(cTaskContext& ctx) const {
+ return (2^ctx.task_success_complete);
+}
+
Modified: branches/uml/source/main/cTaskLib.h
===================================================================
--- branches/uml/source/main/cTaskLib.h 2007-05-03 13:14:24 UTC (rev 1518)
+++ branches/uml/source/main/cTaskLib.h 2007-05-03 13:22:28 UTC (rev 1519)
@@ -275,6 +275,20 @@
// Network Tasks
double Task_NetSend(cTaskContext& ctx) const;
double Task_NetReceive(cTaskContext& ctx) const;
+
+ // UML Tasks
+ double Task_Trans1(cTaskContext& ctx) const;
+ double Task_Trans2(cTaskContext& ctx) const;
+ double Task_Trans3(cTaskContext& ctx) const;
+ double Task_Trans4(cTaskContext& ctx) const;
+ double Task_Trans5(cTaskContext& ctx) const;
+ double Task_NumStates(cTaskContext& ctx) const;
+ double Task_NumTrans(cTaskContext& ctx) const;
+ double Task_Hydra(cTaskContext& ctx) const;
+ double SpinCoprocess(cTaskContext& ctx, const std::string& neverclaimFile) const;
+ double Task_SpinN1(cTaskContext& ctx) const;
+ double Task_MultTrans(cTaskContext& ctx) const;
+
};
More information about the Avida-cvs
mailing list