Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

be_ai_gen.c File Reference

#include "../game/q_shared.h"
#include "l_memory.h"
#include "l_log.h"
#include "l_utils.h"
#include "l_script.h"
#include "l_precomp.h"
#include "l_struct.h"
#include "aasfile.h"
#include "../game/botlib.h"
#include "../game/be_aas.h"
#include "be_aas_funcs.h"
#include "be_interface.h"
#include "../game/be_ai_gen.h"

Include dependency graph for be_ai_gen.c:

Include dependency graph

Go to the source code of this file.

Functions

int GeneticParentsAndChildSelection (int numranks, float *ranks, int *parent1, int *parent2, int *child)
int GeneticSelection (int numranks, float *rankings)


Function Documentation

int GeneticParentsAndChildSelection int  numranks,
float *  ranks,
int *  parent1,
int *  parent2,
int *  child
 

Definition at line 90 of file be_ai_gen.c.

References botimport, Com_Memcpy(), GeneticSelection(), i, max, and PRT_WARNING.

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     } //end if
00101     for (max = 0, i = 0; i < numranks; i++)
00102     {
00103         if (ranks[i] < 0) continue;
00104         max++;
00105     } //end for
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     } //end if
00112     Com_Memcpy(rankings, ranks, sizeof(float) * numranks);
00113     //select first parent
00114     *parent1 = GeneticSelection(numranks, rankings);
00115     rankings[*parent1] = -1;
00116     //select second parent
00117     *parent2 = GeneticSelection(numranks, rankings);
00118     rankings[*parent2] = -1;
00119     //reverse the rankings
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     } //end for
00126     for (i = 0; i < numranks; i++)
00127     {
00128         if (rankings[i] < 0) continue;
00129         rankings[i] = max - rankings[i];
00130     } //end for
00131     //select child
00132     *child = GeneticSelection(numranks, rankings);
00133     return qtrue;
00134 } //end of the function GeneticParentsAndChildSelection

Here is the call graph for this function:

int GeneticSelection int  numranks,
float *  rankings
 

Definition at line 52 of file be_ai_gen.c.

References i, and random.

Referenced by GeneticParentsAndChildSelection().

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     } //end for
00063     if (sum > 0)
00064     {
00065         //select a bot where the ones with the higest rankings have
00066         //the highest chance of being selected
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         } //end for
00074     } //end if
00075     //select a bot randomly
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     } //end for
00082     return 0;
00083 } //end of the function GeneticSelection


Generated on Thu Aug 25 12:41:16 2005 for Quake III Arena by  doxygen 1.3.9.1