Skip to content

Commit

Permalink
Merge pull request #75 from devosoft/parasite-serialization
Browse files Browse the repository at this point in the history
Parasite serialization
  • Loading branch information
mmore500 authored Sep 5, 2023
2 parents a18369f + 766da8b commit eb65103
Show file tree
Hide file tree
Showing 33 changed files with 3,571 additions and 8 deletions.
35 changes: 35 additions & 0 deletions avida-core/include/public/avida/systematics/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef AvidaSystematicsUnit_h
#define AvidaSystematicsUnit_h

#include <iostream>

#include "apto/platform.h"
#include "avida/core/Types.h"
#include "avida/systematics/Types.h"
Expand Down Expand Up @@ -53,6 +55,39 @@ namespace Avida {
LIB_EXPORT inline Source() : transmission_type(UNKNOWN), external(false), unused(0) { ; }
LIB_EXPORT inline Source(TransmissionType t, const Apto::String& a, bool e = false)
: transmission_type(t), external(e), unused(0), arguments(a) { ; }

LIB_EXPORT inline Source(const Apto::String& sourceString, const Apto::String& a)
: transmission_type(UNKNOWN), external(false), unused(0), arguments(a)
{
const int pos = sourceString.Find(':');
if (pos == -1) {
std::cerr
<< "incorrectly formatted Source string: "
<< sourceString
<< std::endl;
// fall back to default initialization
return;
}

Apto::String transmissionStr = sourceString.Substring(0, pos);
Apto::String externalStr = sourceString.Substring(
pos+1, sourceString.GetSize() - pos - 1
);

// Map string to TransmissionType
if (transmissionStr == "div")
transmission_type = DIVISION;
else if (transmissionStr == "dup")
transmission_type = DUPLICATION;
else if (transmissionStr == "horz")
transmission_type = HORIZONTAL;
else if (transmissionStr == "vert")
transmission_type = VERTICAL;

// Map string to external
external = (externalStr == "ext");

}

LIB_EXPORT Apto::String AsString() const;
};
Expand Down
66 changes: 58 additions & 8 deletions avida-core/source/main/cPopulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "avida/systematics/Arbiter.h"
#include "avida/systematics/Group.h"
#include "avida/systematics/Manager.h"
#include "avida/systematics/Unit.h"

#include "avida/private/systematics/GenomeTestMetrics.h"
#include "avida/private/systematics/Genotype.h"
Expand Down Expand Up @@ -65,7 +66,9 @@

#include "cHardwareCPU.h"

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
Expand Down Expand Up @@ -6701,15 +6704,32 @@ struct sTmpGenotype
Apto::Array<double> parent_merit;
Apto::Array<bool> parent_teacher;
Apto::Array<int> parent_ft;


Systematics::Source source{Systematics::TransmissionType::UNKNOWN, "", true};
Systematics::GroupPtr bg;


inline sTmpGenotype() : id_num(-1), props(NULL) { ; }
inline bool operator<(const sTmpGenotype& rhs) const { return id_num > rhs.id_num; }
inline bool operator>(const sTmpGenotype& rhs) const { return id_num < rhs.id_num; }
inline bool operator<=(const sTmpGenotype& rhs) const { return id_num >= rhs.id_num; }
inline bool operator>=(const sTmpGenotype& rhs) const { return id_num <= rhs.id_num; }
inline bool operator<(const sTmpGenotype& rhs) const {
if (source.transmission_type == rhs.source.transmission_type) {
return id_num > rhs.id_num;
} else {
return source.transmission_type == Systematics::TransmissionType::DIVISION || source.transmission_type == Systematics::TransmissionType::UNKNOWN;
}
}
inline bool operator>(const sTmpGenotype& rhs) const {
if (source.transmission_type == rhs.source.transmission_type) {
return id_num < rhs.id_num;
} else {
return source.transmission_type != Systematics::TransmissionType::DIVISION && source.transmission_type != Systematics::TransmissionType::UNKNOWN;
}
}
inline bool operator<=(const sTmpGenotype& rhs) const {
return !(*this > rhs);
}
inline bool operator>=(const sTmpGenotype& rhs) const {
return !(*this < rhs);
}
};

bool cPopulation::LoadGenotypeList(const cString& filename, cAvidaContext& ctx, Apto::Array<GeneticRepresentationPtr>& list_obj)
Expand Down Expand Up @@ -6781,6 +6801,13 @@ bool cPopulation::LoadPopulation(const cString& filename, cAvidaContext& ctx, in
while (cellstr.GetSize()) tmp.cells.Push(cellstr.Pop(',').AsInt());
assert(tmp.cells.GetSize() == tmp.num_cpus);
}

// Process systematics source
assert(tmp.props->Has("src") && tmp.props->Has("src_args"));
tmp.source = Systematics::Source(
tmp.props->Get("src"),
(const char*)filename
);

// Process gestation time offsets
if (!load_rebirth) {
Expand Down Expand Up @@ -6878,8 +6905,9 @@ bool cPopulation::LoadPopulation(const cString& filename, cAvidaContext& ctx, in
assert(tmp.avatar_cells.GetSize() == 0 || tmp.avatar_cells.GetSize() == tmp.num_cpus);
}
}

// Sort genotypes in descending order according to their id_num

// Sort genotypes in descending order according to their id_num,
// with parasites last
Apto::QSort(genotypes);

Systematics::ManagerPtr classmgr = Systematics::Manager::Of(m_world->GetNewWorld());
Expand Down Expand Up @@ -6936,7 +6964,7 @@ bool cPopulation::LoadPopulation(const cString& filename, cAvidaContext& ctx, in

assert(tmp.bg->Properties().Has("genome"));
Genome mg(tmp.bg->Properties().Get("genome"));
cOrganism* new_organism = new cOrganism(m_world, ctx, mg, -1, Systematics::Source(Systematics::DIVISION, (const char*)filename, true));
cOrganism* new_organism = new cOrganism(m_world, ctx, mg, -1, tmp.source);

// Setup the phenotype...
cPhenotype& phenotype = new_organism->GetPhenotype();
Expand Down Expand Up @@ -6996,6 +7024,28 @@ bool cPopulation::LoadPopulation(const cString& filename, cAvidaContext& ctx, in
// Setup the child's mutation rates. Since this organism is being injected
// and has no parent, we should always take the rate from the environment.
new_organism->MutationRates().Copy(cell_array[cell_id].MutationRates());

// handle parasite
const bool is_parasite = (
tmp.source.transmission_type == Systematics::TransmissionType::VERTICAL
|| tmp.source.transmission_type == Systematics::TransmissionType::HORIZONTAL
);
if (is_parasite) {
if (!cell_array[cell_id].IsOccupied()) {
std::cerr << "Loaded parasite before or without host!" << std::endl;
std::abort();
}

ConstInstructionSequencePtr seq;
seq.DynamicCastFrom(mg.Representation());

InjectParasite(
static_cast<const char*>(tmp.source.arguments), // cString label,
*seq, // const InstructionSequence& injected_code,
cell_id // int cell_id
);
continue;
}

// Activate the organism in the population...
bool org_survived = false;
Expand Down
Loading

0 comments on commit eb65103

Please sign in to comment.