| 1085 | } |
| 1086 | |
| 1087 | int cHardwareBase::PointMutate(cAvidaContext& ctx, double override_mut_rate) |
| 1088 | { |
| 1089 | const int max_genome_size = m_world->GetConfig().MAX_GENOME_SIZE.Get(); |
| 1090 | const int min_genome_size = m_world->GetConfig().MIN_GENOME_SIZE.Get(); |
| 1091 | |
| 1092 | cCPUMemory& memory = GetMemory(); |
| 1093 | int totalMutations = 0; |
| 1094 | |
| 1095 | // const int num_muts = ctx.GetRandom().GetRandBinomial(memory.GetSize(), mut_rate); |
| 1096 | // |
| 1097 | // for (int i = 0; i < num_muts; i++) { |
| 1098 | // const int pos = ctx.GetRandom().GetUInt(memory.GetSize()); |
| 1099 | // memory[pos] = m_inst_set->GetRandomInst(ctx); |
| 1100 | // memory.SetFlagMutated(pos); |
| 1101 | // memory.SetFlagPointMut(pos); |
| 1102 | // } |
| 1103 | |
| 1104 | |
| 1105 | // Point Substitution Mutations (per site) |
| 1106 | if (m_organism->GetPointMutProb() > 0.0 || override_mut_rate > 0.0) { |
| 1107 | double mut_rate = (override_mut_rate > 0.0) ? override_mut_rate : m_organism->GetPointMutProb(); |
| 1108 | int num_mut = ctx.GetRandom().GetRandBinomial(memory.GetSize(), mut_rate); |
| 1109 | |
| 1110 | // If we have lines to mutate... |
| 1111 | if (num_mut > 0) { |
| 1112 | for (int i = 0; i < num_mut; i++) { |
| 1113 | int site = ctx.GetRandom().GetUInt(memory.GetSize()); |
| 1114 | memory[site] = m_inst_set->GetRandomInst(ctx); |
| 1115 | totalMutations++; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | // Point Insert Mutations (per site) |
| 1121 | if (m_organism->GetPointInsProb() > 0.0) { |
| 1122 | int num_mut = ctx.GetRandom().GetRandBinomial(memory.GetSize(), m_organism->GetPointInsProb()); |
| 1123 | |
| 1124 | // If would make creature too big, insert up to max_genome_size |
| 1125 | if (num_mut + memory.GetSize() > max_genome_size) { |
| 1126 | num_mut = max_genome_size - memory.GetSize(); |
| 1127 | } |
| 1128 | |
| 1129 | // If we have lines to insert... |
| 1130 | if (num_mut > 0) { |
| 1131 | // Build a sorted list of the sites where mutations occured |
| 1132 | Apto::Array<int> mut_sites(num_mut); |
| 1133 | for (int i = 0; i < num_mut; i++) mut_sites[i] = ctx.GetRandom().GetUInt(memory.GetSize() + 1); |
| 1134 | Apto::QSort(mut_sites); |
| 1135 | |
| 1136 | // Actually do the mutations (in reverse sort order) |
| 1137 | for (int i = mut_sites.GetSize() - 1; i >= 0; i--) { |
| 1138 | memory.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx)); |
| 1139 | } |
| 1140 | |
| 1141 | totalMutations += num_mut; |
| 1142 | } |
| 1143 | } |
| 1144 |
no test coverage detected