Filter:   InfoImg
download PhpGa.inc
Language: NonCode
LOC: 0
Project Info
PHP Genetic Algorithm Class(phpga)
Server: SourceForge
Type: cvs
...\phpga\phpga\PhpGa\include\
   BitString.inc
   CommonFunctions.inc
   FormChek.js
   PhpGa.inc
   PhpGaLimits.inc
   PhpGaStylesheet.css
   select_algorithms_thesis.C

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?  // -*-Mode: c++;-*-

/*  
	PhpGa - PHP Genetic Algorithm Class Library
  Copyright (C) 2000 Bill C. White

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
	PhpGa.inc - Bill White - 7/19/00

	This class is used to perform genetic optimization of binary strings. It is
	based on a the simple genetic algorithm as described by David Goldberg in
	"Genetic Algorithms in Search, Optimization and Machine Learning" (1989). 

	7/30/00 - Working (?) with binary tournament selection and no mutation.
*/

if(defined("_Php_Ga_inc_"))
	return;

define("_Php_Ga_inc_", 1);

// currently the GA supports only bit string genomes
include("include/BitString.inc");
include("include/PhpGaLimits.inc");

class PhpGa
{
	// class data members

	// set this flag to "1" for debugging output
	var $debug = 0;

	// GA parameters
	var $bitStringLength = 8;
	var $populationSize = 10;
	var $pCrossover  = 0.9;
	var $pMutation   = 0.0;
	var $population = array();
	var $selectionMethod = "rouletteWheelSelection";

	// GA processing
	var $curGeneration = 0;
	var $numGenerationsToRun = 10;
	var $parents = array();

	// population statistics - 7/22/00
	var $fitnessValues = array();
	var $fitnessSum = 0; // used in roulette wheel selection - 8/2/00
	var $popMean;
	var $popStdDev;
	var $popVar;
	
	// class methods
	
	/****************************************************************************
	 * PhpGa - constructor
	 * 
	 * $stringLength     = length of binary string being optimized
	 */
	function PhpGa($stringLength) {
		if(($stringLength >= MIN_STRING_LENGTH) && ($stringLength <= MAX_STRING_LENGTH)) {
			$this->bitStringLength = $stringLength;
		}
		else {
			echo "Attempt to set string length to $stringLength. Outside valid range " . 
				MIN_STRING_LENGTH . " < stringLength < " . MAX_STRING_LENGTH . "<br>\n";
			echo "Using default string length: $this->bitStringLength<br>\n";
		}
		if($this->debug) { echo "In PhpGa constructor: \$bitStringLength = $this->bitStringLength<br>\n"; }
	}
	
	/****************************************************************************
	 * PhpGa - SetPopulationSize
	 *
	 * Set the population size
	 */
	function SetPopulationSize($popSize) 
	{
		if(($popSize > 1) && ($popSize < 100)) {
			$this->populationSize = $popSize;
		}
		else {
			echo "Attempt to set population size to $popSize. Outside valid range " . 
				MIN_POP_SIZE . " < N < " . MAX_POP_SIZE . "<br>\n";
			echo "Using default population size: $this->populationSize<br>\n";
		}
	}
	
	/****************************************************************************
	 * PhpGa - SetCrossoverRate
	 *
	 * Set the crossover rate
	 */
	function SetCrossoverRate($crossRate) 
	{
		if(($crossRate >= 0.0) && ($crossRate <= 1.0)) {
			$this->pCrossover = $crossRate;
		}
		else {
			echo "Attempt to set crossover probability to $crossRate. Outside valid range " . 
				MIN_P_CROSSOVER . "< pC < " . MAX_P_CROSSOVER . "<br>\n";
			echo "Using default crossover probability: $this->pCrossover<br>\n";
		}
	}

	/****************************************************************************
	 * PhpGa - SetSelectionMethod
	 *
	 * Set the mthod to use for selecting parents for the next generationn
	 */
	function SetSelectionMethod($selectChoice) 
	{
		// ehh, this is not very good design, but it'll do for now - 8/2/00
		$this->selectionMethod = $selectChoice;
	}
	
	/****************************************************************************
	 * PhpGa - SetGenerationCount
	 *
	 * Set the number of generations to run
	 */
	function SetGenerationCount($genCount) 
	{
		if(($genCount >= 1) && ($genCount <= 100)) {
			$this->numGenerationsToRun = $genCount;
		}
		else {
			echo "Attempt to set generation count to $genCount. Outside valid range " . 
				MIN_GEN_COUNT . " < genCount < " . MAX_GEN_COUNT . "<br>\n";
			echo "Using default generation count: $this->numGenerationsToRun<br>\n";
		}
	}
	
	/****************************************************************************
	 * PhpGa - SetDebugFlag
	 *
	 * Set the debugging output flag
	 */
	function SetDebugFlag($flag)
	{
		if($flag) {
			$this->debug = 1;
		}
		else {
			$this->debug = 0;
		}
	}
	
	/****************************************************************************
	 * PhpGa - Initialize
	 *
	 * Initialize the GA by creating and evaluating the initial (random) population 
	 */
	function Initialize()
	{
		// seed random number generator
		//		srand((double) microtime() * 1000000);
		mt_srand((double)microtime()*1000000);

		// initialize the population with random strings
		$this->curGeneration = 0;
		for($i=0; $i < $this->populationSize; $i++) {
			$bitString = new BitString($this->bitStringLength);
			$this->population[$i] = $bitString;
		}
		$this->EvaluatePopulation();
	}

	/****************************************************************************
	 * PhpGa - EvaluatePopulation
	 *
	 * Evaluate the fitness of each member of the population and update 
	 * the population statistics
	 */
	function EvaluatePopulation() 
	{
		// sum the fitnesses
		$this->fitnessSum = 0;
		for($i=0; $i < $this->populationSize; $i++) {
			$this->fitnessValues[$i] = FitnessFunction($this->population[$i]);
			$this->fitnessSum += $this->fitnessValues[$i];
		}

		// mean/average fitness
		$this->popMean = $this->fitnessSum / $this->populationSize;

		// variance/standard deviation
		$SSE = 0;
		for($i=0; $i < $this->populationSize; $i++) {
			$SSE += (($this->fitnessValues[$i] - $this->popMean)*($this->fitnessValues[$i] - $this->popMean));
		}
		$this->popVar = $SSE / ($this->populationSize - 1);
		$this->popStdDev = sqrt($this->popVar);
	}

	/****************************************************************************
	 * PhpGa - PrintPopulation
	 *
	 * Print out the bit strings in the population with descriptive fitness statistics 
	 */
	function PrintPopulation() 
	{
		EmitTextBoxHeader("Population at generation $this->curGeneration");
		for($i=0; $i < $this->populationSize; $i++) {
			$bitString = $this->population[$i];
			$bitString->EmitAsHTML();
			echo "&nbsp;" . $this->fitnessValues[$i] . "<br>\n";
		}

		printf("\nPopulation mean:               %7.2f<br>\n", $this->popMean);
		printf("Population variance:           %7.2f<br>\n", $this->popVar);
		printf("Population standard deviation: %7.2f<br>\n", $this->popStdDev);
		EmitTextBoxFooter();
	}

	/****************************************************************************
	 * PhpGa - Step - 7/30/00
	 *
	 * Take one step in the population's evolution:
	 * - grab two parents by "select()"ing them (various selection schemes)
	 * - reproduce/probabilistic crossover: creates two offspring
	 * - probabilistic mutation
	 * - add child(ren) to new population
	 */
	function Step() 
	{
		if($this->curGeneration < $this->numGenerationsToRun) {
			$newPopulation = array();
			
			// select parents: how can we make this variable? switch() on constant?
			switch($this->selectionMethod) {
 			case "rouletteWheelSelection":
				$this->SelectParentsByRouletteWheel();
				break;
 			case "binaryTournamentSelection":
				$this->SelectParentsByBinaryTournament();
				break;
			default:
				echo "PANIC: Invalid selection method: $this->selectionMethod<br>\n";
				exit;
			}

			if($this->debug) {
				for($i=0; $i < count($this->parents); $i++) {
					$pi = $this->parents[$i];
					$pbs = $this->population[$pi];
					$ps = $pbs->myString;
					echo "Parent $i: $pi: $ps<br>\n";
				}
			}

			// crossover at pCrossover and add to new population
			for($i=0; $i < count($this->parents); $i += 2) {
				$index1 = $this->parents[$i];
				$index2 = $this->parents[$i+1];
				$bitString1 = $this->population[$index1];
				$bitString2 = $this->population[$index2];

				// if random number exceeds crossover probability
				$thisProb = mt_rand(0,1);
				if($thisProb <= $this->pCrossover) {
					$string1 = $bitString1->myString;
					$string2 = $bitString2->myString;

					$crossPoint = mt_rand(0, $this->bitStringLength-2);

					$s1prefix = substr($string2, 0, $crossPoint+1);
					$s2prefix = substr($string1, 0, $crossPoint+1);

					$bitString1->myString = $s1prefix . substr($string1, $crossPoint+1, strlen($string1) - $crossPoint - 1);
					$bitString2->myString = $s2prefix . substr($string2, $crossPoint+1, strlen($string2) - $crossPoint - 1);
				}

				// add offspring to new population
				$newPopulation[$i] = $bitString1;
				$newPopulation[$i+1] = $bitString2;
			}

			// replace old population with new population
			for($i=0; $i < $this->populationSize; $i++) {
				$this->population[$i] = $newPopulation[$i];
			}
			
			// regenerate population stats
			$this->EvaluatePopulation();

			$this->curGeneration++;

			return 1;
		}
		else {
			return 0;
		}
	}

	/*
		##!
		### NAME: SelectParentsByBinaryTournament
		### PURPOSE: Populates the parents array using binary tournament algorithm
		### DATE: 7/30/00
		###	LAST MODIFIED: 7/30/00
		###	NOTES: used to generate pairings of parents in PhpGa
		###	EXPECTS: nothing
		###	RETURNS: nothing
		##!
	*/
	function SelectParentsByBinaryTournament()
	{
		$randomIndexes1 = array();
		$randomIndexes2 = array();

		// initialize the random index arrays
		for($i=0; $i < $this->populationSize; $i++) {
			$randomIndexes1[$i] = $i;
			$randomIndexes2[$i] = $i;
			if($this->debug) { echo "[init] $i: $randomIndexes1[$i], $randomIndexes2[$i]<br>\n"; }
		}

		// shuffle 'em up!
		for ($i=0; $i < $this->populationSize-1; $i++) {
			/* pick a random array index j in [i,ihi] to swap with */
			$j = mt_rand($i, $this->populationSize-1);
			$contents = $randomIndexes1[$i];
			$randomIndexes1[$i] = $randomIndexes1[$j];
			$randomIndexes1[$j] = $contents;

			$j = mt_rand($i, $this->populationSize-1);
			$contents = $randomIndexes2[$i];
			$randomIndexes2[$i] = $randomIndexes2[$j];
			$randomIndexes2[$j] = $contents;
    }

		// if odd number of elements, add one more random index
		if(($this->populationSize % 2) == 1) {
			$randomIndexes1[$this->populationSize] = mt_rand(0, $this->populationSize-1);
			$randomIndexes2[$this->populationSize] = mt_rand(0, $this->populationSize-1);
		}

		if($this->debug) { 	
			for($i=0; $i < count($randomIndexes1); $i++) {	
				echo "[shuffled] $i: $randomIndexes1[$i], $randomIndexes2[$i]<br>\n"; 
			}
		}

		// pair up the parents
		unset($this->parents);
		$this->parents = array();
		for($i=0; $i < count($randomIndexes1); $i += 2) {
			if($this->fitnessValues[$randomIndexes1[$i]] > $this->fitnessValues[$randomIndexes1[$i+1]]) {
				$this->parents[] = $randomIndexes1[$i];
			}
			else {
				$this->parents[] = $randomIndexes1[$i+1];
			}
		}

		for($i=0; $i < count($randomIndexes2); $i += 2) {
			if($this->fitnessValues[$randomIndexes2[$i]] > $this->fitnessValues[$randomIndexes2[$i+1]]) {
				$this->parents[] = $randomIndexes2[$i];
			}
			else {
				$this->parents[] = $randomIndexes2[$i+1];
			}
		}

		if($this->debug) { 	
			for($i=0; $i < count($this->parents); $i++) {	
				$t = $this->parents[$i];
				echo "[parents] $i: $t<br>\n"; 
			}
		}

	}

	/*
		##!
		### NAME: SelectParentsByRouletteWheel
		### PURPOSE: Populates the parents array using spins of a "roulette
		### wheel"; biased towards individuals with best fitness relative to 
		### population mean
		### DATE: 8/2/00
		###	LAST MODIFIED: 8/2/00
		###	NOTES: used to generate pairings of parents in PhpGa
		###	EXPECTS: nothing
		###	RETURNS: nothing
		##!
	*/
	function SelectParentsByRouletteWheel()
	{
		$parentsGenerated = 0;
		while($parentsGenerated <= $this->populationSize) {
			$partSum = 0.0; 
			$randNum = 0.0;
			$newParentIndex = 0;
			$randNum = (mt_rand()/mt_getrandmax()) * $this->fitnessSum;
			while(($partSum < $randNum) && ($newParentIndex < $this->populationSize-1)) {
				$newParentIndex++;
				$partSum += $this->fitnessValues[$newParentIndex] ;
			}
			$this->parents[$parentsGenerated++] = $newParentIndex;
		}
		if(($parentsGenerated % 2) == 1) {
			$this->parents[$parentsGenerated++] = mt_rand(0, $this->populationSize-1);
		}
		
	}

} // end of class definition

?>