16 #ifndef EMP_EVO_RESOURCE_H 17 #define EMP_EVO_RESOURCE_H 33 amount(amt), inflow(in), outflow(out){};
43 double Inc() {amount += inflow;
return amount;}
44 double Inc(
double amt) {amount += amt;
return amount;}
46 amount -= amount*outflow;
47 if (amount < 0) {amount = 0;}
50 double Dec(
double amt) {
52 if (amount < 0) {amount = 0;}
56 amount += inflow - (amount*outflow);
57 if (amount < 0) {amount = 0;}
62 template <
typename ORG>
64 emp::vector<emp::Resource> & pools,
size_t t_size,
size_t tourny_count=1,
double frac = .0025,
double max_bonus = 5,
double cost = 0,
bool use_base =
true) {
74 for (
size_t i=0; i < extra_funs.size(); i++) {
81 for (
size_t org_id = 0; org_id < world.
GetSize(); org_id++) {
90 base_fitness[org_id] = 0;
93 for (
size_t ex_id = 0; ex_id < extra_funs.size(); ex_id++) {
97 double cur_fit = extra_funs[ex_id](world.
GetOrg(org_id));
100 cur_fit *= frac*(pools[ex_id].GetAmount()-cost);
107 cur_fit = std::min(cur_fit, max_bonus);
109 extra_fitnesses[ex_id][org_id] =
emp::Pow2(cur_fit);
111 base_fitness[org_id] *=
emp::Pow2(cur_fit);
112 pools[ex_id].Dec(std::abs(cur_fit));
131 for (
size_t T = 0; T < tourny_count; T++) {
136 double best_fit = base_fitness[entries[0]];
137 size_t best_id = entries[0];
140 for (
size_t i = 1; i < t_size; i++) {
141 const double cur_fit = base_fitness[entries[i]];
142 if (cur_fit > best_fit) {
144 best_id = entries[i];
void SetInflow(double in)
Definition: Resource.h:40
void SetAmount(double amt)
Definition: Resource.h:39
double Update()
Definition: Resource.h:55
double GetInflow()
Definition: Resource.h:36
double Inc(double amt)
Definition: Resource.h:44
double Dec()
Definition: Resource.h:45
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
double Inc()
Definition: Resource.h:43
static constexpr double Pow2(double exp)
A fast 2^x command.
Definition: math.h:157
void push_back(PB_Ts &&...args)
Definition: vector.h:189
Setup a World with a population of organisms that can evolve or deal with ecological effects...
Definition: World.h:94
Definition of a base class for a World template for use in evolutionary algorithms.
double GetOutflow()
Definition: Resource.h:37
void ResourceSelect(World< ORG > &world, const emp::vector< std::function< double(const ORG &)> > &extra_funs, emp::vector< emp::Resource > &pools, size_t t_size, size_t tourny_count=1, double frac=.0025, double max_bonus=5, double cost=0, bool use_base=true)
Definition: Resource.h:63
size_t GetSize() const
How many organisms can fit in the world?
Definition: World.h:245
Definition: Resource.h:23
const genome_t & GetGenomeAt(size_t id)
Retrive the genome corresponding to the organism at the specified position.
Definition: World.h:342
static constexpr type_if< T, std::is_integral > Pow(T base, T p)
A fast (O(log p)) integral-power command.
Definition: math.h:150
void resize(size_t new_size)
Definition: vector.h:161
ORG & GetOrg(size_t id)
Definition: World.h:316
double Dec(double amt)
Definition: Resource.h:50
If we are in emscripten, make sure to include the header.
Definition: array.h:37
double GetAmount()
Definition: Resource.h:35
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
size_t GetRandomOrgID()
Get the id of a random occupied cell.
Definition: World.h:1274
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
Resource(double amt, double in, double out)
Definition: Resource.h:32
size_t GetNumOrgs() const
How many organisms are currently in the world?
Definition: World.h:248
void SetOutflow(double out)
Definition: Resource.h:41
Resource()
Definition: Resource.h:31
fun_calc_fitness_t GetFitFun()
Get the fitness function currently in use.
Definition: World.h:400
bool IsOccupied(WorldPosition pos) const
Does the specified cell ID have an organism in it?
Definition: World.h:277