|
| 1 | +/**************************************************************** |
| 2 | +
|
| 3 | + eco_system.c |
| 4 | +
|
| 5 | + ============================================================= |
| 6 | +
|
| 7 | + Copyright 1996-2015 Tom Barbalet. All rights reserved. |
| 8 | +
|
| 9 | + Permission is hereby granted, free of charge, to any person |
| 10 | + obtaining a copy of this software and associated documentation |
| 11 | + files (the "Software"), to deal in the Software without |
| 12 | + restriction, including without limitation the rights to use, |
| 13 | + copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | + sell copies of the Software, and to permit persons to whom the |
| 15 | + Software is furnished to do so, subject to the following |
| 16 | + conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be |
| 19 | + included in all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | + OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +
|
| 30 | + This software and Noble Ape are a continuing work of Tom Barbalet, |
| 31 | + begun on 13 June 1996. No apes or cats were harmed in the writing |
| 32 | + of this software. |
| 33 | +
|
| 34 | + ****************************************************************/ |
| 35 | + |
| 36 | +#include "ecosim.h" |
| 37 | +#include <stdio.h> |
| 38 | +#include <string.h> |
| 39 | + |
| 40 | +/* test a given ecosystem */ |
| 41 | +void search_test(ecosim_genome * eco, unsigned int itterations, int show) |
| 42 | +{ |
| 43 | + unsigned int p, i; |
| 44 | + unsigned int all_surviving = 1; |
| 45 | + |
| 46 | + system_init(eco); |
| 47 | + i = eco->survival_steps = 0; |
| 48 | + while (i < itterations) |
| 49 | + { |
| 50 | + system_cycle(eco, show); |
| 51 | + if (show != 0) show_output_short(eco); |
| 52 | + for (p = 0; p < cSize; p++) { |
| 53 | + if (all_surviving == 1) { |
| 54 | + if ((eco->pop[p] <= 0) || |
| 55 | + (eco->pop[p] >= 200)) { |
| 56 | + all_surviving = 0; |
| 57 | + if (show == 0) { |
| 58 | + break; |
| 59 | + i = itterations; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + if (all_surviving == 1) { |
| 65 | + eco->survival_steps++; |
| 66 | + } |
| 67 | + i++; |
| 68 | + } |
| 69 | + |
| 70 | + /* include the duration of survival in the score */ |
| 71 | + eco->score = eco->survival_steps; |
| 72 | +} |
| 73 | + |
| 74 | +/* sort by score */ |
| 75 | +void search_sort(ecosim_search * sim) |
| 76 | +{ |
| 77 | + unsigned int i, j, winner; |
| 78 | + ecosim_genome swap; |
| 79 | + double max; |
| 80 | + |
| 81 | + for (i = 0; i < SEARCH_SAMPLES-1; i++) { |
| 82 | + max = sim->genome[i].score; |
| 83 | + winner = i; |
| 84 | + for (j = i+1; j < SEARCH_SAMPLES; j++) { |
| 85 | + if (sim->genome[j].score > max) { |
| 86 | + max = sim->genome[j].score; |
| 87 | + winner = j; |
| 88 | + } |
| 89 | + } |
| 90 | + if (winner != i) { |
| 91 | + memcpy((void*)&swap, (void*)&sim->genome[i], sizeof(ecosim_genome)); |
| 92 | + memcpy((void*)&sim->genome[i], (void*)&sim->genome[winner], sizeof(ecosim_genome)); |
| 93 | + memcpy((void*)&sim->genome[winner], (void*)&swap, sizeof(ecosim_genome)); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/* produce the next set of samples */ |
| 99 | +void samples_renew(ecosim_search * sim) |
| 100 | +{ |
| 101 | + int i, j, e, elitism, parent1, parent2; |
| 102 | + ecosim_search prev_sim; |
| 103 | + |
| 104 | + memcpy((void*)&prev_sim, (void*)sim, sizeof(ecosim_search)); |
| 105 | + |
| 106 | + elitism = SEARCH_SAMPLES/2; |
| 107 | + |
| 108 | + for (i = 0; i < SEARCH_SAMPLES; i++) { |
| 109 | + parent1 = rand() % elitism; |
| 110 | + parent2 = rand() % elitism; |
| 111 | + for (j = 0; j < cSize; j++) { |
| 112 | + for (e = 0; e < 6; e++) { |
| 113 | + /* breed */ |
| 114 | + sim->genome[i].eats[j][e] = prev_sim.genome[rand()%2].eats[j][e]; |
| 115 | + /* mutate */ |
| 116 | + sim->genome[i].eats[j][e] += ((rand()%20000/10000.0)-1.0)*sim->mutation_rate; |
| 117 | + if (sim->genome[i].eats[j][e] < MIN_EATS) sim->genome[i].eats[j][e] = MIN_EATS; |
| 118 | + if (sim->genome[i].eats[j][e] > MAX_EATS) sim->genome[i].eats[j][e] = MAX_EATS; |
| 119 | + } |
| 120 | + /* breed */ |
| 121 | + sim->genome[i].growth[j] = prev_sim.genome[rand()%2].growth[j]; |
| 122 | + /* mutate */ |
| 123 | + sim->genome[i].growth[j] += ((rand()%20000/10000.0)-1.0)*sim->mutation_rate; |
| 124 | + if (sim->genome[i].growth[j] < MIN_GROWTH) sim->genome[i].growth[j] = MIN_GROWTH; |
| 125 | + if (sim->genome[i].growth[j] > MAX_GROWTH) sim->genome[i].growth[j] = MAX_GROWTH; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/* initialise with random values */ |
| 131 | +void search_init(ecosim_search * sim) |
| 132 | +{ |
| 133 | + int i, j, e; |
| 134 | + |
| 135 | + for (i = 0; i < SEARCH_SAMPLES; i++) { |
| 136 | + for (j = 0; j < cSize; j++) { |
| 137 | + for (e = 0; e < 6; e++) { |
| 138 | + sim->genome[i].eats[j][e] = MIN_EATS + ((rand()%20000/20000.0)*(MAX_EATS-MIN_EATS)); |
| 139 | + } |
| 140 | + sim->genome[i].growth[j] = MIN_GROWTH + ((rand()%20000/20000.0)*(MAX_GROWTH-MIN_GROWTH)); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +void search_cycle(ecosim_search * sim) |
| 146 | +{ |
| 147 | + int i; |
| 148 | + |
| 149 | +#pragma omp parallel for |
| 150 | + for (i = 0; i < SEARCH_SAMPLES; i++) { |
| 151 | + search_test(&sim->genome[i], sim->itterations, 0); |
| 152 | + } |
| 153 | + |
| 154 | + sim->collapses=0; |
| 155 | + for (i = 0; i < SEARCH_SAMPLES; i++) { |
| 156 | + if (sim->genome[i].survival_steps < sim->itterations) { |
| 157 | + sim->collapses++; |
| 158 | + } |
| 159 | + } |
| 160 | + printf("score %.2f collapse %.2f\n", |
| 161 | + sim->genome[0].score, |
| 162 | + sim->collapses*100.0/SEARCH_SAMPLES); |
| 163 | + |
| 164 | + search_sort(sim); |
| 165 | +} |
0 commit comments