26 #ifndef EMP_EVO_WORLD_H 27 #define EMP_EVO_WORLD_H 31 #include <unordered_map> 33 #include "../base/Ptr.h" 34 #include "../base/vector.h" 35 #include "../control/Signal.h" 36 #include "../control/SignalControl.h" 37 #include "../data/DataFile.h" 38 #include "../data/DataManager.h" 39 #include "../data/Trait.h" 40 #include "../meta/reflection.h" 41 #include "../tools/map_utils.h" 42 #include "../tools/Random.h" 43 #include "../tools/Range.h" 44 #include "../tools/random_utils.h" 45 #include "../tools/string_utils.h" 93 template <
typename ORG>
189 EMP_CREATE_OPTIONAL_METHOD(SetupOrg, Setup);
192 double GetCache(
size_t id)
const {
return (
id < fit_cache.
size()) ? fit_cache[
id] : 0.0; }
203 World(std::string _name=
"",
bool gen_random=
true)
204 : update(0), random_ptr(nullptr), random_owner(false), pops(), pop(pops[0]), num_orgs(0)
206 , name(_name), cache_on(false), pop_sizes(1,0), phenotypes(), files()
207 , is_synchronous(false), is_space_structured(false), is_pheno_structured(false)
208 , fun_calc_fitness(), fun_do_mutations(), fun_print_org(), fun_get_genome()
209 , fun_find_inject_pos(), fun_find_birth_pos(), fun_kill_org(), fun_get_neighbor()
210 , attributes(), control()
211 , before_repro_sig(
to_string(name,
"::before-repro"), control)
212 , offspring_ready_sig(
to_string(name,
"::offspring-ready"), control)
213 , inject_ready_sig(
to_string(name,
"::inject-ready"), control)
214 , before_placement_sig(
to_string(name,
"::before-placement"), control)
215 , on_placement_sig(
to_string(name,
"::on-placement"), control)
216 , on_update_sig(
to_string(name,
"::on-update"), control)
217 , on_death_sig(
to_string(name,
"::on-death"), control)
218 , on_swap_sig(
to_string(name,
"::on-swap"), control)
219 , world_destruct_sig(
to_string(name,
"::wolrd-destruct"), control)
222 SetDefaultFitFun<this_t, ORG>(*this);
223 SetDefaultMutFun<this_t, ORG>(*this);
224 SetDefaultPrintFun<this_t, ORG>(*this);
225 SetDefaultGetGenomeFun<this_t, ORG>(*this);
231 world_destruct_sig.Trigger();
233 if (random_owner) random_ptr.
Delete();
237 for (
auto file : files) {
271 if (file->GetFilename() == filename)
return *file;
273 emp_assert(
false,
"Trying to lookup a file that does not exist.", filename);
335 return *(pops[1][id]);
348 emp_assert(systematics.
size() > 0,
"Cannot get systematics file. No systematics file to track.");
349 emp_assert(
id < (
int)systematics.
size(),
"Invalid systematics file requested.", id, systematics.
size());
350 return systematics[id];
357 emp_assert(
Has(systematics_labels, label),
"Invalid systematics manager label");
359 return systematics[systematics_labels[label]];
364 emp_assert(systematics.
size() > 0,
"Cannot remove systematics file. No systematics file to track.");
365 emp_assert(
id < systematics.
size(),
"Invalid systematics file requested to be removed.", id, systematics.
size());
367 systematics[id].Delete();
368 systematics[id] =
nullptr;
370 for (
auto el : systematics_labels) {
371 if (el.second ==
id) {
372 systematics_labels.
erase(el.first);
378 emp_assert(
Has(systematics_labels, label),
"Invalid systematics manager label");
380 systematics[systematics_labels[label]].Delete();
381 systematics[systematics_labels[label]] =
nullptr;
382 systematics_labels.
erase(label) ;
385 template <
typename ORG_INFO,
typename DATA_STRUCT>
387 if (
Has(systematics_labels, label)) {
390 systematics_labels[label] = systematics.
size();
392 if (is_synchronous) {
393 s->SetTrackSynchronous(
true);
435 s->SetTrackSynchronous(synchronous);
443 template <
typename... Ts>
445 phenotypes.
AddTrait(std::forward<Ts>(args)...);
452 if (!data_nodes.
HasNode(
"fitness")) {
458 for (
size_t i = 0; i < pop.
size(); i++) {
464 return &(data_nodes.
Get(
"fitness"));
472 return &(data_nodes.
New(name));
476 return &(data_nodes.
Get(name));
538 double sharing_threshold,
double alpha);
551 return before_repro_sig.AddAction(fun);
560 return offspring_ready_sig.AddAction(fun);
568 return inject_ready_sig.AddAction(fun);
577 return before_placement_sig.AddAction(fun);
586 return on_placement_sig.AddAction(fun);
594 return on_update_sig.AddAction(fun);
602 return on_death_sig.AddAction(fun);
610 return on_swap_sig.AddAction(fun);
618 return world_destruct_sig.AddAction(fun);
630 return Find(attributes, name,
"UNKNOWN");
635 template <
typename T>
650 template <
typename... ARGS>
652 for (
Ptr<ORG> org : pop) {
if (org) org->Process(args...); }
657 template <
typename... ARGS>
659 if (pop[
id]) pop[id]->Process(std::forward<ARGS>(args)...);
664 for (
Ptr<ORG> org : pop) {
if (org) org->ResetHardware(); }
677 emp_assert(cache_on,
"Trying to calculate fitness of all orgs without caching.");
704 for (
size_t id = start_id;
id < pop.
size();
id++) {
if (pop[
id])
DoMutationsID(
id); }
718 on_swap_sig.Trigger(pos1, pos2);
725 pop.
resize(new_size,
nullptr);
729 void Resize(
size_t new_width,
size_t new_height) {
730 Resize(new_width * new_height);
732 pop_sizes[0] = new_width; pop_sizes[1] = new_height;
813 void DoBottleneck(
const size_t new_size,
bool choose_random=
true);
821 void Print(std::ostream & os = std::cout,
const std::string & empty=
"-",
const std::string & spacer=
" ");
827 void PrintGrid(std::ostream& os=std::cout,
const std::string & empty=
"-",
const std::string & spacer=
" ");
855 template <
typename ORG>
861 if (pos.
IsActive()) { before_placement_sig.Trigger(*new_org, pos.
GetIndex()); }
864 s->SetNextParent((
int) p_pos.
GetIndex());
886 template<
typename ORG>
890 if (
id >= cur_pop.
size() || !cur_pop[id])
return;
891 if (pos.
IsActive()) on_death_sig.Trigger(
id);
892 cur_pop[id].Delete();
893 cur_pop[id] =
nullptr;
903 s->RemoveNextOrg((
int) pos.
GetIndex());
908 template<
typename ORG>
911 is_synchronous = synchronous_gen;
912 is_space_structured =
false;
913 is_pheno_structured =
false;
917 fun_find_inject_pos = [
this](
Ptr<ORG> new_org) {
926 fun_kill_org = [
this](){
927 const size_t last_id = pop.
size() - 1;
934 if (synchronous_gen) {
954 template<
typename ORG>
957 is_synchronous = synchronous_gen;
958 is_space_structured =
false;
959 is_pheno_structured =
false;
963 fun_find_inject_pos = [
this](
Ptr<ORG> new_org) {
972 fun_kill_org = [
this](){
978 if (synchronous_gen) {
998 template<
typename ORG>
1001 is_synchronous = synchronous_gen;
1002 is_space_structured =
true;
1003 is_pheno_structured =
false;
1007 fun_find_inject_pos = [
this](
Ptr<ORG> new_org) {
1016 const size_t size_x = pop_sizes[0];
1017 const size_t size_y = pop_sizes[1];
1018 const size_t id = pos.GetIndex();
1019 const int offset = random_ptr->GetInt(9);
1020 const int rand_x = (int) (
id%size_x) + offset%3 - 1;
1021 const int rand_y = (int) (
id/size_x) + offset/3 - 1;
1022 const auto neighbor_id =
emp::Mod(rand_x, (
int) size_x) +
emp::Mod(rand_y, (
int) size_y) * (int)size_x;
1023 return pos.SetIndex(neighbor_id);
1026 fun_kill_org = [
this](){
1032 if (synchronous_gen) {
1053 template<
typename ORG>
1055 size_t id = files.
size();
1062 template<
typename ORG>
1064 size_t id = files.
size();
1066 files[id].New(filename);
1071 template<
typename ORG>
1075 file.AddVar(update,
"update",
"Update");
1076 file.AddMean(*node,
"mean_fitness",
"Average organism fitness in current population.");
1077 file.AddMin(*node,
"min_fitness",
"Minimum organism fitness in current population.");
1078 file.AddMax(*node,
"max_fitness",
"Maximum organism fitness in current population.");
1079 file.AddInferiority(*node,
"inferiority",
"Average fitness / maximum fitness in current population.");
1080 if (print_header) file.PrintHeaderKeys();
1086 template<
typename ORG>
1088 emp_assert(
Has(systematics_labels, label),
"Invalid systematics tracker requested.", label);
1094 template<
typename ORG>
1096 emp_assert(systematics.
size() > 0,
"Cannot track systematics file. No systematics file to track.");
1097 emp_assert(
id < systematics.
size(),
"Invalid systematics file requested to be tracked.");
1099 file.AddVar(update,
"update",
"Update");
1100 file.template AddFun<size_t>( [
this, id](){
return systematics[id]->GetNumActive(); },
"num_taxa",
"Number of unique taxonomic groups currently active." );
1101 file.template AddFun<size_t>( [
this, id](){
return systematics[id]->GetTotalOrgs(); },
"total_orgs",
"Number of organisms tracked." );
1102 file.template AddFun<double>( [
this, id](){
return systematics[id]->GetAveDepth(); },
"ave_depth",
"Average Phylogenetic Depth of Organisms." );
1103 file.template AddFun<size_t>( [
this, id](){
return systematics[id]->GetNumRoots(); },
"num_roots",
"Number of independent roots for phlogenies." );
1104 file.template AddFun<int>( [
this, id](){
return systematics[id]->GetMRCADepth(); },
"mrca_depth",
"Phylogenetic Depth of the Most Recent Common Ancestor (-1=none)." );
1105 file.template AddFun<double>( [
this, id](){
return systematics[id]->CalcDiversity(); },
"diversity",
"Genotypic Diversity (entropy of taxa in population)." );
1107 if (print_header) file.PrintHeaderKeys();
1112 template<
typename ORG>
1115 file.AddVar(update,
"update",
"Update");
1116 file.template AddFun<size_t>( [
this](){
return GetNumOrgs(); },
"num_orgs",
"Number of organisms currently living in the population." );
1117 if (print_header) file.PrintHeaderKeys();
1121 template<
typename ORG>
1124 double sharing_threshold,
double alpha)
1126 fun_calc_fitness = [
this, fit_fun, dist_fun, sharing_threshold, alpha](ORG & org) {
1127 double niche_count = 0;
1129 if (!org2)
continue;
1130 double dist = dist_fun(org, *org2);
1131 niche_count += std::max(1.0 - std::pow(dist/sharing_threshold, alpha), 0.0);
1133 return fit_fun(org)/niche_count;
1139 template<
typename ORG>
1142 on_update_sig.Trigger(update);
1146 if (pops[1].
size()) {
1148 for (
size_t i = 0; i < pops[1].
size(); i++) {
1149 if (!pops[1][i])
continue;
1150 before_placement_sig.Trigger(*pops[1][i], i);
1156 std::swap(pops[0], pops[1]);
1160 for (
size_t i = 0; i < pop.
size(); i++) {
1161 if (!pop[i])
continue;
1163 on_placement_sig.Trigger(i);
1174 for (
auto file : files) file->Update(update);
1180 template<
typename ORG>
1186 template<
typename ORG>
1188 if (!pop[
id])
return 0.0;
1191 if (cur_fit == 0.0 && pop[
id]) {
1192 if (
id >= fit_cache.
size()) fit_cache.
resize(
id+1, 0.0);
1194 fit_cache[id] = cur_fit;
1200 template<
typename ORG>
1202 for (
size_t pop_id = 0; pop_id < 2; pop_id++) {
1208 template <
typename ORG>
1210 for (
size_t i = 0; i < copy_count; i++) {
1211 Ptr<ORG> new_org = NewPtr<ORG>(mem);
1212 inject_ready_sig.Trigger(*new_org);
1220 template <
typename ORG>
1223 Ptr<ORG> new_org = NewPtr<ORG>(mem);
1224 inject_ready_sig.Trigger(*new_org);
1228 template <
typename ORG>
1229 template <
typename... ARGS>
1231 emp_assert(random_ptr !=
nullptr &&
"InjectRandomOrg() requires active random_ptr");
1233 inject_ready_sig.Trigger(*new_org);
1243 template <
typename ORG>
1245 before_repro_sig.Trigger(parent_pos);
1247 for (
size_t i = 0; i < copy_count; i++) {
1248 Ptr<ORG> new_org = NewPtr<ORG>(mem);
1249 offspring_ready_sig.Trigger(*new_org);
1258 template<
typename ORG>
1260 if (random_owner) random_ptr.
Delete();
1262 random_owner =
false;
1265 template<
typename ORG>
1267 if (random_owner) random_ptr.
Delete();
1268 random_ptr.
New(seed);
1269 random_owner =
true;
1273 template<
typename ORG>
1276 size_t pos = random_ptr->GetUInt(0, pop.
size());
1277 while (pop[pos] ==
nullptr) pos = random_ptr->GetUInt(0, pop.
size());
1282 template<
typename ORG>
1285 for (
size_t i = 0; i < pop.
size(); i++) {
1286 if (filter(pop[i].Raw())) valid_IDs.
push_back(i);
1292 template<
typename ORG>
1294 if (new_size >= num_orgs)
return;
1296 if (is_space_structured || is_pheno_structured) {
1301 if (choose_random) {
1302 for (
size_t to = 0; to < new_size; to++) {
1303 const size_t from = random_ptr->GetUInt(to, pop.
size());
1304 if (from == to)
continue;
1316 template<
typename ORG>
1318 emp_assert(keep_frac >= 0.0 && keep_frac <= 1.0, keep_frac);
1321 if (is_space_structured || is_pheno_structured) {
1323 const double remove_frac = 1.0 - keep_frac;
1324 for (
size_t i = 0; i < pop.
size(); ++i) {
1325 if (random_ptr->P(remove_frac)) {
1334 size_t live_pos = 0;
1335 for (
size_t test_pos = 0; test_pos < pop.
size(); test_pos++) {
1337 if (random_ptr->P(keep_frac)) {
1338 Swap(live_pos, test_pos);
1352 template<
typename ORG>
1361 template<
typename ORG>
1363 std::map<ORG,size_t> org_counts;
1364 for (
Ptr<ORG> org : pop)
if (org) org_counts[*org] = 0;
1365 for (
Ptr<ORG> org : pop)
if (org) org_counts[*org] += 1;
1366 for (
auto x : org_counts) {
1367 ORG cur_org = x.first;
1373 template<
typename ORG>
1375 const std::string & empty,
const std::string & spacer) {
1377 const size_t size_x = pop_sizes[0];
1378 const size_t size_y = pop_sizes[1];
1379 for (
size_t y=0; y < size_y; y++) {
1380 for (
size_t x = 0; x < size_x; x++) {
void SetMutFun(const fun_do_mutations_t &mut_fun)
Definition: World.h:500
iterator_t begin()
[std::vector compatibility] Return iterator to first organism.
Definition: World.h:842
Ptr< DataMonitor< double > > GetFitnessDataNode()
Definition: World.h:451
Signal< void(ORG &, size_t)> before_placement_sig
...before placing any organism into target cell.
Definition: World.h:181
void NewRandom(int seed=-1)
Create a new random number generator (that World will manage)
Definition: World.h:1266
SignalKey tracks a specific function triggered by a signal. For now, its just a value pair...
Definition: Signal.h:30
fun_do_mutations_t fun_do_mutations
...mutate an organism.
Definition: World.h:160
fun_print_org_t fun_print_org
...print an organism.
Definition: World.h:161
void AddPhenotype(Ts &&...args)
Add a new phenotype measuring function.
Definition: World.h:444
Ptr< SystematicsBase< ORG > > GetSystematics(int id=0)
Definition: World.h:347
Functions for popular world structure methods.
Definition: Systematics.h:214
Definition: World_structure.h:29
Signal< void(size_t)> on_placement_sig
...after any organism is placed into world.
Definition: World.h:182
void SetPopStruct_Mixed(bool synchronous_gen=false)
Definition: World.h:955
void Resize(size_t new_width, size_t new_height)
Change the size of the world based on width and height.
Definition: World.h:729
const emp::TraitSet< ORG > & GetPhenotypes() const
What phenotypic traits is the population tracking?
Definition: World.h:263
std::function< WorldPosition(Ptr< ORG >, WorldPosition)> fun_find_birth_pos_t
Function type for adding a newly born organism into a world (returns birth position) ...
Definition: World.h:126
bool IsSpaceStructured() const
Definition: World.h:288
void resize(size_t new_size)
[std::vector compatibility] Update world size.
Definition: World.h:836
World_iterator< this_t > iterator_t
Type for this world's iterators.
Definition: World.h:102
size_t GetHeight() const
How many cells tall is the world? (assumes grids are active.)
Definition: World.h:257
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
void Process(ARGS &&...args)
Definition: World.h:651
double CalcFitnessOrg(ORG &org)
Use the configured fitness function on the specified organism.
Definition: World.h:1181
void RemoveSystematics(int id)
Definition: World.h:363
Signal< void()> world_destruct_sig
...in the World destructor.
Definition: World.h:186
std::function< WorldPosition(Ptr< ORG >)> fun_find_inject_pos_t
Function type for injecting organisms into a world (returns inject position)
Definition: World.h:123
fun_kill_org_t fun_kill_org
...kill an organism.
Definition: World.h:165
emp::TraitSet< ORG > phenotypes
What phenotypes are we tracking?
Definition: World.h:148
void Delete()
Definition: Ptr.h:737
void SetRandom(Random &r)
Setup a new random number generator created elsewhere.
Definition: World.h:1259
Random & GetRandom()
Return a reference to the random number generator currently being used by world.
Definition: World.h:770
DataFile & AddDataFile(emp::Ptr< DataFile > file)
Add an already-constructed datafile.
Definition: World.h:1054
WorldPosition DoBirth(const genome_t &mem, size_t parent_pos, size_t copy_count=1)
Place one or more copies of an offspring into population; return position of last placed...
Definition: World.h:1244
DataManagers handle sets of DataNode objects that all have the same tracking settings.
Definition: DataManager.h:25
fit_cache_t fit_cache
vector size == 0 when not caching; uncached values == 0.
Definition: World.h:142
void DoBottleneck(const size_t new_size, bool choose_random=true)
Run population through a bottleneck to (potentially) shrink it.
Definition: World.h:1293
void SetKillOrgFun(const fun_kill_org_t &_fun)
Definition: World.h:522
std::string name
Name of this world (for use in configuration.)
Definition: World.h:145
void SetSynchronousSystematics(bool synchronous)
Tell systematics managers that this world has synchronous generations.
Definition: World.h:433
uint32_t GetPopID() const
Definition: World_structure.h:45
node_t & New(const std::string &name)
Creates and adds a new DataNode, with the name specified in.
Definition: DataManager.h:48
uint32_t GetIndex() const
Definition: World_structure.h:44
DataFile & SetupPopulationFile(const std::string &filename="population.csv", const bool &print_header=true)
Setup a file to be printed that collects population information over time.
Definition: World.h:1113
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
fun_find_inject_pos_t fun_find_inject_pos
...find where to inject a new, external organism.
Definition: World.h:163
fun_find_birth_pos_t fun_find_birth_pos
...find where to add a new offspring organism.
Definition: World.h:164
void CalcFitnessAll() const
Calculate the fitness of all organisms, storing the results in the cache.
Definition: World.h:676
Handle reflection on organisms to setup reasonable defaults in World.
decltype(Org2Genome< ORG >(true)) find_genome_t
Definition: World_reflect.h:124
const genome_t & GetGenome(ORG &org)
Retrieve the genome corresponding to a specified organism.
Definition: World.h:339
bool IsSynchronous() const
Definition: World.h:284
void InjectAt(const genome_t &mem, const WorldPosition pos)
Inject an organism at a specific position.
Definition: World.h:1221
emp::vector< Ptr< SystematicsBase< ORG > > > systematics
Phylogeny and line-of-descent data collection.
Definition: World.h:172
void DoMutationsOrg(ORG &org)
Use mutation function on a single, specified organism.
Definition: World.h:691
SignalKey OnBeforePlacement(const std::function< void(ORG &, size_t)> &fun)
Definition: World.h:576
void push_back(PB_Ts &&...args)
Definition: vector.h:189
void MakeValid(WorldPosition pos)
Make sure position is valid; if not expand relevant vector.
Definition: World_structure.h:71
void SetGetNeighborFun(const fun_get_neighbor_t &_fun)
Definition: World.h:526
void ClearCache()
Remove all currently cached fitness values (useful with changing environments, etc.)
Definition: World.h:685
void SetAddInjectFun(const fun_find_inject_pos_t &_fun)
Definition: World.h:513
std::unordered_map< std::string, int > systematics_labels
Definition: World.h:173
DataManager< double, data::Current, data::Info, data::Range, data::Stats > data_nodes
Potential data nodes – these should be activated only if in use.
Definition: World.h:156
std::function< size_t(ORG &, Random &)> fun_do_mutations_t
Function type for a mutation operator on an organism.
Definition: World.h:114
void ResetHardware()
Reset the hardware for all organisms.
Definition: World.h:663
this_t & MarkSpaceStructured(bool in=true)
Definition: World.h:300
size_t update
How many times has Update() been called?
Definition: World.h:136
size_t num_orgs
How many organisms are actually in the population.
Definition: World.h:141
SignalKey OnInjectReady(const std::function< void(ORG &)> &fun)
Definition: World.h:567
void DoDeath(const WorldPosition pos)
Definition: World.h:762
Setup a World with a population of organisms that can evolve or deal with ecological effects...
Definition: World.h:94
std::vector< size_t > pop_sizes
Sizes of population dimensions (eg, 2 vals for grid)
Definition: World.h:147
fun_calc_fitness_t fun_calc_fitness
...evaluate fitness for provided organism.
Definition: World.h:159
void AddTrait(const std::string &name, const std::function< VALUE_T(target_t &)> &fun, EXTRA &&...extra)
Definition: Trait.h:145
size_t size() const
Definition: vector.h:151
WorldPosition GetRandomNeighborPos(WorldPosition pos)
Use the specified function to get a neighbor (if not set, assume well mixed).
Definition: World.h:788
void Inject(const genome_t &mem, size_t copy_count=1)
Inject an organism using the default injection scheme.
Definition: World.h:1209
void emplace_back(ARGS &&...args)
Definition: vector.h:219
std::function< WorldPosition(WorldPosition)> fun_get_neighbor_t
Function type for identifying an organism's random neighbor.
Definition: World.h:132
void DoDeath()
Definition: World.h:765
Definition: DataNode.h:648
ORG & GetOrg(size_t x, size_t y)
Definition: World.h:324
std::function< WorldPosition()> fun_kill_org_t
Function type for determining picking and killing an organism (returns newly empty position) ...
Definition: World.h:129
void Resize(size_t new_size)
Definition: World.h:723
WorldPosition & SetPopID(size_t _id)
Definition: World_structure.h:51
constexpr uint32_t GetUInt(const uint32_t max)
Definition: ce_random.h:191
T Product(const emp::vector< T > &v)
Multiply all of the contents of a vector.
Definition: vector_utils.h:86
Functions for popular selection methods applied to worlds.
bool IsActive() const
Definition: World_structure.h:47
std::function< double(ORG &, ORG &)> fun_calc_dist_t
Function type for calculating the distance between two organisms.
Definition: World.h:111
void SetPopStruct_Grid(size_t width, size_t height, bool synchronous_gen=false)
Definition: World.h:999
void Update()
Definition: World.h:1140
std::function< const genome_t &(ORG &)> fun_get_genome_t
Function type for retrieving a genome from an organism.
Definition: World.h:120
size_t size() const
[std::vector compatibility] How big is the world?
Definition: World.h:833
iterator_t end()
[std::vector compatibility] Return iterator just past last organism.
Definition: World.h:845
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
void DoMutationsID(size_t id)
Use mutation function on the organism at the specified position in the population.
Definition: World.h:697
void SetPrintFun(const fun_print_org_t &print_fun)
Definition: World.h:505
size_t GetSize() const
How many organisms can fit in the world?
Definition: World.h:245
void Resize(const emp::vector< size_t > &dims)
Change the size of the world based on a vector of dimensions.
Definition: World.h:736
bool IsCacheOn() const
Are we currently caching fitness values?
Definition: World.h:280
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
constexpr int Mod(int in_val, int mod_val)
% is actually remainder; Mod is a proper modulus command that handles negative #'s correctly ...
Definition: math.h:26
void SetSharedFitFun(const fun_calc_fitness_t &fit_fun, const fun_calc_dist_t &dist_fun, double sharing_threshold, double alpha)
Definition: World.h:1122
void resize(size_t new_size)
Definition: array.h:165
Definition: World_iterator.h:23
pop_t & pop
A shortcut to pops[0].
Definition: World.h:140
void Clear()
Remove all organisms from the world.
Definition: World.h:1201
Signal< void(ORG &)> inject_ready_sig
...when outside organism is ready to inject.
Definition: World.h:180
This file defines iterators for use with emp::World objects.
ORG & GetRandomOrg()
Get an organism from a random occupied cell.
Definition: World.h:794
void SetAddBirthFun(const fun_find_birth_pos_t &_fun)
Definition: World.h:518
bool cache_on
Should we be caching fitness values?
Definition: World.h:146
const genome_t & GetGenomeAt(size_t id)
Retrive the genome corresponding to the organism at the specified position.
Definition: World.h:342
bool random_owner
Did we create our own random number generator?
Definition: World.h:138
World(Random &rnd, std::string _name="")
Definition: World.h:228
void SetPopStruct_Grow(bool synchronous_gen=false)
Definition: World.h:909
A vector that can be indexed with a WorldPosition.
Definition: World_structure.h:58
void SetAttribute(const std::string &name, T &&val)
Definition: World.h:636
size_t GetUpdate() const
What update number is the world currently on? (assumes Update() is being used)
Definition: World.h:251
void clear()
[std::vector compatibility] Remove all organisms.
Definition: World.h:839
const ORG & operator[](size_t id) const
Definition: World.h:309
constexpr size_t size() const
Definition: array.h:137
bool Has(const MAP_T &in_map, const KEY_T &key)
Take any map type, and run find to determine if a key is present.
Definition: map_utils.h:21
bool IsValid(WorldPosition pos) const
Test if a position is currently valid.
Definition: World_structure.h:63
void Print(std::ostream &os=std::cout, const std::string &empty="-", const std::string &spacer=" ")
Print all organisms in the population using previously provided print function.
Definition: World.h:1353
SignalKey OnUpdate(const std::function< void(size_t)> &fun)
Definition: World.h:593
void resize(size_t new_size)
Definition: vector.h:161
void RemoveSystematics(std::string label)
Definition: World.h:377
void ClearCache(size_t id)
Clear the cache value at the specified position.
Definition: World.h:195
std::function< double(ORG &)> fun_calc_fitness_t
Function type for calculating fitness.
Definition: World.h:108
org_t value_type
Identical to org_t; vector compatibility.
Definition: World.h:100
DataFile & SetupFitnessFile(const std::string &filename="fitness.csv", const bool &print_header=true)
Setup a file to be printed that collects fitness information over time.
Definition: World.h:1072
SignalKey OnSwapOrgs(const std::function< void(WorldPosition, WorldPosition)> &fun)
Definition: World.h:609
const Ptr< ORG > GetOrgPtr(size_t id) const
Definition: World.h:328
void SerialTransfer(const double keep_frac)
Perform a Serial Transfer where a fixed percentage of current organisms are maintained.
Definition: World.h:1317
SignalKey OnOffspringReady(const std::function< void(ORG &)> &fun)
Definition: World.h:559
ORG & GetOrg(size_t id)
Definition: World.h:316
SignalKey OnBeforeRepro(const std::function< void(size_t)> &fun)
Definition: World.h:550
void SetAutoMutate()
Definition: World.h:419
WorldVector< Ptr< ORG > > pops
The set of active [0] and "next" [1] organisms in population.
Definition: World.h:139
std::string GetAttribute(const std::string) const
Get the value for an attribute that you know exists.
Definition: World.h:628
void SetGetGenomeFun(const fun_get_genome_t &_fun)
Definition: World.h:509
emp::vector< size_t > GetEmptyPopIDs()
Return IDs of all empty cells in the population.
Definition: World.h:807
SignalKey OnPlacement(const std::function< void(size_t)> &fun)
Definition: World.h:585
void DoMutations(size_t start_id=0)
Use mutation function on ALL organisms in the population.
Definition: World.h:703
void PrintOrgCounts(std::ostream &os=std::cout)
Print unique organisms and the number of copies of each that exist.
Definition: World.h:1362
bool IsPhenoStructured() const
Definition: World.h:292
std::map< std::string, std::string > attributes
Attributes are a dynamic way to track extra characteristics about a world.
Definition: World.h:169
Ptr< Random > random_ptr
Random object to use.
Definition: World.h:137
SignalControl & GetSignalControl()
Access signal controller used for this world directly.
Definition: World.h:543
If we are in emscripten, make sure to include the header.
Definition: array.h:37
A tool to track phylogenetic relationships among organisms. The systematics class tracks the relation...
Definition: Systematics.h:355
size_t GetRandomCellID()
Get the position a cell, at random.
Definition: World.h:779
Signal< void(WorldPosition, WorldPosition)> on_swap_sig
...after org positions are swapped
Definition: World.h:185
fun_get_neighbor_t fun_get_neighbor
...choose a random neighbor "near" specified id.
Definition: World.h:166
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
~World()
Definition: World.h:230
void InjectRandomOrg(ARGS &&...args)
Inject a random organism (constructor must facilitate!)
Definition: World.h:1230
SignalKey OnWorldDestruct(const std::function< void()> &fun)
Definition: World.h:617
size_t GetWidth() const
How many cells wide is the world? (assumes grids are active.)
Definition: World.h:254
Signal< void(size_t)> on_death_sig
...immediately before any organism dies.
Definition: World.h:184
size_t GetRandomOrgID()
Get the id of a random occupied cell.
Definition: World.h:1274
void PrintGrid(std::ostream &os=std::cout, const std::string &empty="-", const std::string &spacer=" ")
Print the organisms layed out in a grid structure (assumes a grid population.)
Definition: World.h:1374
void Reset()
Methods to reset data.
Definition: DataNode.h:670
double CalcFitnessID(size_t id)
Use the configured fitness function on the organism at the specified position.
Definition: World.h:1187
#define emp_assert(...)
Definition: assert.h:199
World(std::string _name="", bool gen_random=true)
Definition: World.h:203
void ProcessID(size_t id, ARGS &&...args)
Definition: World.h:658
typename emp::find_genome_t< ORG > genome_t
Type of underlying genomes.
Definition: World.h:105
Definition: SignalControl.h:28
void SetAutoMutate(std::function< bool(size_t pos)> test_fun)
Definition: World.h:427
DataFile & GetFile(const std::string &filename)
Lookup a file by name.
Definition: World.h:269
bool HasAttribute(const std::string &name) const
Definition: World.h:625
emp::vector< emp::Ptr< DataFile > > files
Output files.
Definition: World.h:149
size_t GetNumOrgs() const
How many organisms are currently in the world?
Definition: World.h:248
iterator erase(ARGS &&...args)
Definition: vector.h:207
void New(T &&...args)
Definition: Ptr.h:735
SignalControl control
Definition: World.h:176
void SetFitFun(const fun_calc_fitness_t &fit_fun)
Definition: World.h:496
void RemoveOrgAt(WorldPosition pos)
Definition: World.h:887
Ptr< DataMonitor< double > > AddDataNode(const std::string &name)
Definition: World.h:470
void AddOrgAt(Ptr< ORG > new_org, WorldPosition pos, WorldPosition p_pos=WorldPosition())
Definition: World.h:856
const pop_t & GetFullPop() const
Get the full population to analyze externally.
Definition: World.h:260
Ptr< DataMonitor< double > > GetDataNode(const std::string &name)
Definition: World.h:475
bool IsValid() const
Definition: World_structure.h:48
Definition: DataFile.h:32
const node_t & Get(const std::string &name) const
Definition: DataManager.h:63
this_t & MarkPhenoStructured(bool in=true)
Definition: World.h:304
Ptr< SystematicsBase< ORG > > GetSystematics(std::string label)
Definition: World.h:356
Signal< void(size_t)> on_update_sig
...at the beginning of Update()
Definition: World.h:183
void AddSystematics(Ptr< Systematics< ORG, ORG_INFO, DATA_STRUCT > > s, std::string label="systematics")
Definition: World.h:386
Signal< void(size_t)> before_repro_sig
...before organism gives birth w/parent position.
Definition: World.h:178
fun_get_genome_t fun_get_genome
...determine the genome object of an organism.
Definition: World.h:162
fun_calc_fitness_t GetFitFun()
Get the fitness function currently in use.
Definition: World.h:400
std::function< void(ORG &, std::ostream &)> fun_print_org_t
Function type for printing an organism's info to an output stream.
Definition: World.h:117
ORG org_t
Type of organisms in this world.
Definition: World.h:99
emp::vector< size_t > GetValidOrgIDs()
Return IDs of all occupied cells in the population.
Definition: World.h:804
size_t GetRandomCellID(size_t min_id, size_t max_id)
Get the position a cell in a range, at random.
Definition: World.h:782
bool is_pheno_structured
Do we have a phenotypically structured population?
Definition: World.h:153
void Reset()
Clear all of the orgs and reset stats.
Definition: World.h:713
bool is_space_structured
Do we have a spatially structured population?
Definition: World.h:152
Signal< void(ORG &)> offspring_ready_sig
...when offspring organism is built.
Definition: World.h:179
Track genotypes, species, clades, or lineages of organisms in a world.
emp::vector< size_t > FindCellIDs(const std::function< bool(ORG *)> &filter)
Find ALL cell IDs that return true in the provided filter.
Definition: World.h:1283
void SetCache(bool _in=true)
Turn on (or off) fitness caching for individual organisms.
Definition: World.h:682
DataFile & SetupSystematicsFile(std::string label, const std::string &filename="systematics.csv", const bool &print_header=true)
Setup a file (by label) to be printed that collects systematics information over time.
Definition: World.h:1087
double GetCache(size_t id) const
Get the current cached value for the organism at the specified position.
Definition: World.h:192
bool IsOccupied(WorldPosition pos) const
Does the specified cell ID have an organism in it?
Definition: World.h:277
SignalKey OnOrgDeath(const std::function< void(size_t)> &fun)
Definition: World.h:601
this_t & MarkSynchronous(bool in=true)
Definition: World.h:296
ORG & GetNextOrg(size_t id)
Definition: World.h:332
bool is_synchronous
Does this world have synchronous generations?
Definition: World.h:151
bool HasNode(const std::string &name)
Definition: DataManager.h:43
void Swap(WorldPosition pos1, WorldPosition pos2)
Swap the positions of two organisms.
Definition: World.h:716
DataFile & SetupFile(const std::string &filename)
Setup an arbitrary file; no default filename available.
Definition: World.h:1063