00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "../game/q_shared.h"
00033 #include "l_memory.h"
00034 #include "l_log.h"
00035 #include "l_utils.h"
00036 #include "l_script.h"
00037 #include "l_precomp.h"
00038 #include "l_struct.h"
00039 #include "aasfile.h"
00040 #include "../game/botlib.h"
00041 #include "../game/be_aas.h"
00042 #include "be_aas_funcs.h"
00043 #include "be_interface.h"
00044 #include "../game/be_ai_gen.h"
00045
00046
00047
00048
00049
00050
00051
00052 int GeneticSelection(int numranks, float *rankings)
00053 {
00054 float sum, select;
00055 int i, index;
00056
00057 sum = 0;
00058 for (i = 0; i < numranks; i++)
00059 {
00060 if (rankings[i] < 0) continue;
00061 sum += rankings[i];
00062 }
00063 if (sum > 0)
00064 {
00065
00066
00067 select = random() * sum;
00068 for (i = 0; i < numranks; i++)
00069 {
00070 if (rankings[i] < 0) continue;
00071 sum -= rankings[i];
00072 if (sum <= 0) return i;
00073 }
00074 }
00075
00076 index = random() * numranks;
00077 for (i = 0; i < numranks; i++)
00078 {
00079 if (rankings[index] >= 0) return index;
00080 index = (index + 1) % numranks;
00081 }
00082 return 0;
00083 }
00084
00085
00086
00087
00088
00089
00090 int GeneticParentsAndChildSelection(int numranks, float *ranks, int *parent1, int *parent2, int *child)
00091 {
00092 float rankings[256], max;
00093 int i;
00094
00095 if (numranks > 256)
00096 {
00097 botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too many bots\n");
00098 *parent1 = *parent2 = *child = 0;
00099 return qfalse;
00100 }
00101 for (max = 0, i = 0; i < numranks; i++)
00102 {
00103 if (ranks[i] < 0) continue;
00104 max++;
00105 }
00106 if (max < 3)
00107 {
00108 botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too few valid bots\n");
00109 *parent1 = *parent2 = *child = 0;
00110 return qfalse;
00111 }
00112 Com_Memcpy(rankings, ranks, sizeof(float) * numranks);
00113
00114 *parent1 = GeneticSelection(numranks, rankings);
00115 rankings[*parent1] = -1;
00116
00117 *parent2 = GeneticSelection(numranks, rankings);
00118 rankings[*parent2] = -1;
00119
00120 max = 0;
00121 for (i = 0; i < numranks; i++)
00122 {
00123 if (rankings[i] < 0) continue;
00124 if (rankings[i] > max) max = rankings[i];
00125 }
00126 for (i = 0; i < numranks; i++)
00127 {
00128 if (rankings[i] < 0) continue;
00129 rankings[i] = max - rankings[i];
00130 }
00131
00132 *child = GeneticSelection(numranks, rankings);
00133 return qtrue;
00134 }