| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "../qcommon/q_shared.h" |
| 33 |
#include "l_memory.h" |
| 34 |
#include "l_log.h" |
| 35 |
#include "l_utils.h" |
| 36 |
#include "l_script.h" |
| 37 |
#include "l_precomp.h" |
| 38 |
#include "l_struct.h" |
| 39 |
#include "aasfile.h" |
| 40 |
#include "botlib.h" |
| 41 |
#include "be_aas.h" |
| 42 |
#include "be_aas_funcs.h" |
| 43 |
#include "be_interface.h" |
| 44 |
#include "be_ai_gen.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
int GeneticSelection(int numranks, float *rankings) |
| 53 |
{ |
| 54 |
float sum; |
| 55 |
int i, index; |
| 56 |
|
| 57 |
sum = 0; |
| 58 |
for (i = 0; i < numranks; i++) |
| 59 |
{ |
| 60 |
if (rankings[i] < 0) continue; |
| 61 |
sum += rankings[i]; |
| 62 |
} |
| 63 |
if (sum > 0) |
| 64 |
{ |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
for (i = 0; i < numranks; i++) |
| 69 |
{ |
| 70 |
if (rankings[i] < 0) continue; |
| 71 |
sum -= rankings[i]; |
| 72 |
if (sum <= 0) return i; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
index = random() * numranks; |
| 77 |
for (i = 0; i < numranks; i++) |
| 78 |
{ |
| 79 |
if (rankings[index] >= 0) return index; |
| 80 |
index = (index + 1) % numranks; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
int GeneticParentsAndChildSelection(int numranks, float *ranks, int *parent1, int *parent2, int *child) |
| 91 |
{ |
| 92 |
float rankings[256], max; |
| 93 |
int i; |
| 94 |
|
| 95 |
if (numranks > 256) |
| 96 |
{ |
| 97 |
botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too many bots\n"); |
| 98 |
*parent1 = *parent2 = *child = 0; |
| 99 |
return qfalse; |
| 100 |
} |
| 101 |
for (max = 0, i = 0; i < numranks; i++) |
| 102 |
{ |
| 103 |
if (ranks[i] < 0) continue; |
| 104 |
max++; |
| 105 |
} |
| 106 |
if (max < 3) |
| 107 |
{ |
| 108 |
botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too few valid bots\n"); |
| 109 |
*parent1 = *parent2 = *child = 0; |
| 110 |
return qfalse; |
| 111 |
} |
| 112 |
Com_Memcpy(rankings, ranks, sizeof(float) * numranks); |
| 113 |
|
| 114 |
*parent1 = GeneticSelection(numranks, rankings); |
| 115 |
rankings[*parent1] = -1; |
| 116 |
|
| 117 |
*parent2 = GeneticSelection(numranks, rankings); |
| 118 |
rankings[*parent2] = -1; |
| 119 |
|
| 120 |
max = 0; |
| 121 |
for (i = 0; i < numranks; i++) |
| 122 |
{ |
| 123 |
if (rankings[i] < 0) continue; |
| 124 |
if (rankings[i] > max) max = rankings[i]; |
| 125 |
} |
| 126 |
for (i = 0; i < numranks; i++) |
| 127 |
{ |
| 128 |
if (rankings[i] < 0) continue; |
| 129 |
rankings[i] = max - rankings[i]; |
| 130 |
} |
| 131 |
|
| 132 |
*child = GeneticSelection(numranks, rankings); |
| 133 |
return qtrue; |
| 134 |
} |