Empirical
World_reflect.h
Go to the documentation of this file.
1 
14 #ifndef EMP_EVO_WORLD_REFLECT_H
15 #define EMP_EVO_WORLD_REFLECT_H
16 
17 #include <functional>
18 #include <type_traits>
19 
20 #include "../base/assert.h"
21 #include "../meta/reflection.h"
22 
23 namespace emp {
24 
25  namespace {
26 
27  // Setup Fitness reflection.
28  // 1. If an organism has a "GetFitness()" member function, use it!
29  // 2. If an organim can be cast to double, use it!
30  // 3. Start with a fitness function that throws an assert indicating function must be set.
31 
32  using std::declval;
33 
34  template <typename WORLD, typename ORG>
35  void SetDefaultFitFun_impl(WORLD & world, bool_decoy<decltype( declval<ORG>().GetFitness() )>) {
36  world.SetFitFun( [](ORG & org){ return (double) org.GetFitness(); } );
37  }
38 
39  template <typename WORLD, typename ORG>
40  void SetDefaultFitFun_impl(WORLD & world, int_decoy<decltype( (double) declval<ORG>() )>) {
41  world.SetFitFun( [](ORG & org){ return (double) org; } );
42  }
43 
44  template <typename WORLD, typename ORG>
45  void SetDefaultFitFun_impl(WORLD & world, ... ) {
46  world.SetFitFun( [](ORG & org){
47  emp_assert(false, "No default fitness function available");
48  return 0.0;
49  } );
50  }
51 
52  }
53 
54  template <typename WORLD, typename ORG>
55  void SetDefaultFitFun(WORLD & world) { SetDefaultFitFun_impl<WORLD, ORG>(world, true); }
56 
57  namespace {
58  // Setup Mutation function
59  // 1. DoMutations(random)
60  // 2. Empty, with assert.
61 
62  template <typename WORLD, typename ORG>
63  void SetDefaultMutFun_impl(WORLD & world, bool_decoy<decltype( declval<ORG>().DoMutations( *((Random*)nullptr) ) )>) {
64  world.SetMutFun( [](ORG & org, Random & random) {
65  return (double) org.DoMutations(random);
66  } );
67  }
68 
69  template <typename WORLD, typename ORG>
70  void SetDefaultMutFun_impl(WORLD & world, ... ) {
71  world.SetMutFun( [](ORG & org, Random & random) {
72  emp_assert(false, "No default DoMutations available");
73  return 0;
74  } );
75  }
76 
77  }
78 
79  template <typename WORLD, typename ORG>
80  void SetDefaultMutFun(WORLD & world) { SetDefaultMutFun_impl<WORLD, ORG>(world, true); }
81 
82 
83  namespace {
84  // Setup Print function
85  // 1. Org Print()
86  // 2. Proper operator<<
87  // 3. Assert
88  // @CAO: Also try emp::to_string ??
89 
90  template <typename WORLD, typename ORG>
91  void SetDefaultPrintFun_impl(WORLD & world, bool_decoy<decltype( declval<ORG>().Print(std::cout) )>) {
92  world.SetPrintFun( [](ORG & org, std::ostream & os){ org.Print(os); } );
93  }
94 
95  template <typename WORLD, typename ORG>
96  void SetDefaultPrintFun_impl(WORLD & world, int_decoy<decltype( std::cout << declval<ORG>() )>) {
97  world.SetPrintFun( [](ORG & org, std::ostream & os){ os << org; } );
98  }
99 
100  template <typename WORLD, typename ORG>
101  void SetDefaultPrintFun_impl(WORLD & world, ... ) {
102  world.SetPrintFun( [](ORG & org, std::ostream & os){
103  emp_assert(false, "No default Print function available");
104  } );
105  }
106 
107  }
108 
109  template <typename WORLD, typename ORG>
110  void SetDefaultPrintFun(WORLD & world) { SetDefaultPrintFun_impl<WORLD, ORG>(world, true); }
111 
112 
113  namespace {
114  // Setup genome type identification
115  template <typename ORG>
116  auto Org2Genome( bool_decoy< decltype( declval<ORG>().GetGenome() ) >)
117  -> std::decay_t< decltype( declval<ORG>().GetGenome() ) >;
118 
119  template <typename ORG>
120  ORG Org2Genome( ... );
121  }
122 
123  template <typename ORG>
124  using find_genome_t = decltype( Org2Genome<ORG>(true) );
125 
126 
127  namespace {
128  // Setup Org -> Genome function
129  // 1. GetGenome member function
130  // 2. Return org AS genome.
131 
132  template <typename WORLD, typename ORG>
133  void SetOrgGetGenome_impl(WORLD & world, bool_decoy<decltype( declval<ORG>().GetGenome() )>) {
134  world.SetGetGenomeFun( [](ORG & org) -> const auto & { return org.GetGenome(); } );
135  }
136 
137  template <typename WORLD, typename ORG>
138  void SetOrgGetGenome_impl(WORLD & world, ... ) {
139  world.SetGetGenomeFun( [](ORG & org) -> const ORG & { return org; } );
140  }
141 
142  }
143 
144  template <typename WORLD, typename ORG>
145  void SetDefaultGetGenomeFun(WORLD & world) { SetOrgGetGenome_impl<WORLD, ORG>(world, true); }
146 
147 }
148 
149 #endif
void SetDefaultPrintFun(WORLD &world)
Definition: World_reflect.h:110
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
decltype(Org2Genome< ORG >(true)) find_genome_t
Definition: World_reflect.h:124
bool bool_decoy
Definition: meta.h:95
If we are in emscripten, make sure to include the header.
Definition: array.h:37
#define emp_assert(...)
Definition: assert.h:199
void SetDefaultFitFun(WORLD &world)
Definition: World_reflect.h:55
void Print(const emp::vector< T > &v, std::ostream &os=std::cout, const std::string &spacer=" ")
Print the contects of a vector.
Definition: vector_utils.h:35
void SetDefaultMutFun(WORLD &world)
Definition: World_reflect.h:80
void SetDefaultGetGenomeFun(WORLD &world)
Definition: World_reflect.h:145
int int_decoy
Definition: meta.h:96