Empirical
Resource.h
Go to the documentation of this file.
1 
16 #ifndef EMP_EVO_RESOURCE_H
17 #define EMP_EVO_RESOURCE_H
18 
19 #include "World.h"
20 
21 namespace emp {
22 
23  class Resource {
24  private:
25  double amount = 0;
26  double inflow = 0;
27  double outflow = 0;
28 
29  public:
30 
31  Resource(){};
32  Resource(double amt, double in, double out) :
33  amount(amt), inflow(in), outflow(out){};
34 
35  double GetAmount() {return amount;}
36  double GetInflow() {return inflow;}
37  double GetOutflow() {return outflow;}
38 
39  void SetAmount(double amt) {amount = amt;}
40  void SetInflow(double in) {inflow = in;}
41  void SetOutflow(double out) {outflow = out;}
42 
43  double Inc() {amount += inflow; return amount;}
44  double Inc(double amt) {amount += amt; return amount;}
45  double Dec() {
46  amount -= amount*outflow;
47  if (amount < 0) {amount = 0;}
48  return amount;
49  }
50  double Dec(double amt) {
51  amount -= amt;
52  if (amount < 0) {amount = 0;}
53  return amount;
54  }
55  double Update() {
56  amount += inflow - (amount*outflow);
57  if (amount < 0) {amount = 0;}
58  return amount;
59  }
60  };
61 
62  template <typename ORG>
63  void ResourceSelect(World<ORG> & world, const emp::vector< std::function<double(const ORG &)> > & extra_funs,
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) {
65 
66  emp_assert(world.GetFitFun(), "Must define a base fitness function");
67  emp_assert(world.GetSize() > 0);
68  emp_assert(t_size > 0, t_size);
69  // emp_assert(world.IsCacheOn() == false, "Ecologies mean constantly changing fitness!");
70 
71  // Setup info to track fitnesses.
72  emp::vector<double> base_fitness(world.GetSize());
73  emp::vector< emp::vector<double> > extra_fitnesses(extra_funs.size());
74  for (size_t i=0; i < extra_funs.size(); i++) {
75  extra_fitnesses[i].resize(world.GetSize());
76  }
77 
78  // Collect all fitness info.
79  // std::cout << extra_funs.size() << std::endl;
80 
81  for (size_t org_id = 0; org_id < world.GetSize(); org_id++) {
82  // std::cout << org_id << std::endl;
83  if (!world.IsOccupied(org_id)) {
84  continue;
85  }
86  // std::cout << "still going" << std::endl;
87  if (use_base) {
88  base_fitness[org_id] = world.CalcFitnessID(org_id);
89  } else {
90  base_fitness[org_id] = 0;
91  }
92 
93  for (size_t ex_id = 0; ex_id < extra_funs.size(); ex_id++) {
94  // std::cout << "Test " << ex_id << std::endl;
95 
96  pools[ex_id].Inc(pools[ex_id].GetInflow()/world.GetNumOrgs());
97  double cur_fit = extra_funs[ex_id](world.GetOrg(org_id));
98  cur_fit = emp::Pow(cur_fit, 2.0);
99  // if (org_id==0) {std::cout << "Allele: " << world[org_id][ex_id] <<" Curr fit: " << extra_funs[ex_id](world[org_id]) << " Curr fit squared: " << cur_fit << " Amount: " << pools[ex_id].GetAmount() << " Frac: " << frac;}
100  cur_fit *= frac*(pools[ex_id].GetAmount()-cost);
101  if (cur_fit > 0) {
102  cur_fit -= cost;
103  } else {
104  cur_fit = 0;
105  }
106  // if (org_id==0) {std::cout << " Multiplied out: " << cur_fit;}
107  cur_fit = std::min(cur_fit, max_bonus);
108  // if (org_id==0) {std::cout << " Final: " << cur_fit << std::endl;}
109  extra_fitnesses[ex_id][org_id] = emp::Pow2(cur_fit);
110  // std::cout << "Fit before: = " << base_fitness[org_id] << " Res: " << pools[ex_id].GetAmount();
111  base_fitness[org_id] *= emp::Pow2(cur_fit);
112  pools[ex_id].Dec(std::abs(cur_fit));
113  // std::cout << " Bonus " << ex_id << " = " << extra_funs[ex_id](world[org_id]) << " "<< emp::Pow(2.0,cur_fit) << " " << emp::to_string(world[org_id])
114  // // << " fitnes = " << base_fitness[org_id]
115  // << std::endl;
116 
117  }
118  }
119 
120  // std::cout << "Resource allocations" << std::endl;
121  // std::cout << emp::to_string(base_fitness) << std::endl;
122  // std::cout << emp::to_string(world[0]) << std::endl;
123  // std::cout << world.CalcFitnessID(0);
124 
125  // for (size_t ex_id = 0; ex_id < extra_funs.size(); ex_id++) {
126  // std::cout << extra_fitnesses[ex_id][0] << " ";
127  // }
128  // std::cout << std::endl;
129 
130  emp::vector<size_t> entries;
131  for (size_t T = 0; T < tourny_count; T++) {
132  entries.resize(0);
133  // std::cout << T << std::endl;
134  for (size_t i=0; i<t_size; i++) entries.push_back( world.GetRandomOrgID() ); // Allows replacement!
135 
136  double best_fit = base_fitness[entries[0]];
137  size_t best_id = entries[0];
138 
139  // Search for a higher fit org in the tournament.
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) {
143  best_fit = cur_fit;
144  best_id = entries[i];
145  }
146  }
147 
148  // Place the highest fitness into the next generation!
149  world.DoBirth( world.GetGenomeAt(best_id), best_id, 1 );
150  }
151 
152  // IndexMap fitness_index(world.GetSize());
153  // for (size_t id = 0; id < world.GetSize(); id++) {
154  // fitness_index.Adjust(id, base_fitness[id]);
155  // }
156  //
157  // for (size_t n = 0; n < tourny_count; n++) {
158  // const double fit_pos = world.GetRandom().GetDouble(std::min(fitness_index.GetWeight(), 99999.9));
159  // const size_t parent_id = fitness_index.Index(fit_pos);
160  // const size_t offspring_id = world.DoBirth( world.GetGenomeAt(parent_id), parent_id ).index;
161  // if (world.IsSynchronous() == false) {
162  // fitness_index.Adjust(offspring_id, world.CalcFitnessID(offspring_id));
163  // }
164  // }
165 
166 
167  }
168 
169 }
170 
171 #endif
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