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

be_ai_gen.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 
00023 /*****************************************************************************
00024  * name:        be_ai_gen.c
00025  *
00026  * desc:        genetic selection
00027  *
00028  * $Archive: /MissionPack/code/botlib/be_ai_gen.c $
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 // Parameter:           -
00049 // Returns:             -
00050 // Changes Globals:     -
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     } //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
00084 //===========================================================================
00085 //
00086 // Parameter:           -
00087 // Returns:             -
00088 // Changes Globals:     -
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     } //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

Generated on Thu Aug 25 12:37:11 2005 for Quake III Arena by  doxygen 1.3.9.1