Case Study: Basic AI

A look at the basic Genetic AI execution reveals that it is basically a bunch of randomizations. It is more than that though, so to break it down for great understanding, I decided to a simple test. To say it was a failure is an understatement.

It fails to get past any initial base level. The fail over that was provided seems to be the only reason that it wasn’t even worse. The fail over being that the number of “population” can’t be less than 0 and 10 would be added in that case.

A look at the failure is in order, to prevent any future mistakes.

The Script

The goal is to create something that is genetic.

// Create the Crime Weight
$crime['high'] = 0;
$crime['low'] = 10000;
for($i=0; $i<100; $i++)
{
    // Get random value from 10 to 100
    $rand = mt_rand(10, 10000);

    // Build the Crime AI String
    $crime[$i] = $rand;

    if($rand > $crime['high']) $crime['high'] = $rand;
    if($rand < $crime['low']) $crime['low'] = $rand;
}

$crime['average'] = round(($crime['low']+$crime['high'])/2);

$crime['binary'] = '';
foreach($crime as $key => $value)
{
    if(is_numeric($key)) {
        $weight = ($value < $crime['average']) ? 0 : 1;
        $crime['binary'] .= $weight;
    }
}

		$binary = str_split($crime['binary'], 2);
		$crimeActions['none'] = 0;		// 00
		$crimeActions['murder'] = 0;	// 01
		$crimeActions['burnings'] = 0;	// 10
		$crimeActions['rape'] = 0;		// 11

		foreach($binary as $value)
		{
			if($value == '00') {
				$crimeActions['none']++;
			} else if($value == '01') {
				$crimeActions['murder']++;
			} else if($value == '10') {
				$crimeActions['burnings']++;
			} else if($value == '11') {
				$crimeActions['rape']++;
			}
		}

		// Create the Food Weight
		$food['high'] = 0;	// If this value is set to high then it would never update
		$food['low'] = 10000;	// If this is set to 0 then the value would never update
		for($i=0; $i&lt;100; $i++)
		{
			// Get random value from 10 to 10000
			$rand = mt_rand(10, 10000);

			// Build the AI String
			$food[$i] = $rand;

			if($rand > $food['high']) $food['high'] = $rand;
			if($rand < $food['low']) $food['low'] = $rand;
		}

		$food['average'] = round(($food['low']+$food['high'])/2);

		$food['binary'] = '';
		foreach($crime as $key => $value)
		{
			if(is_numeric($key))
			{
				$weight = ($value < $food['average']) ? 0 : 1;
				$food['binary'] .= $weight;
			}
		}

		$binary = str_split($food['binary'], 2);
		$foodActions['new'] = 0;		// 00 and 10
		$foodActions['disease'] = 0;	// 01 and 11

		foreach($binary as $value)
		{
			if($value == '00') {
				$foodActions['new']++;
			} else if($value == '01') {
				$foodActions['disease']++;
			} else if($value == '10') {
				$foodActions['new']++;
			} else if($value == '11') {
				$foodActions['disease']++;
			}
		}

		$population;
		switch($row['taxes'])
		{
			case 1:		$population = 25; break;
			case 5:		$population = 20; break;
			case 10:	$population = 15; break;
			case 15:	$population = 10; break;
			case 20:	$population = 5; break;
			case 25:	$population = 0; break;
			case 30:	$population = -5; break;
			case 35:	$population = -10; break;
			case 40:	$population = -15; break;
			case 45:	$population = -20; break;
			case 50:	$population = -25; break;
		}

		for($i=1; $i<=$crimeActions['burnings']; $i++)
		{
			$rand = mt_rand(1, 100);

			if($rand < 50)
				$population--;
			else
				$population++;
		}

		for($i=1; $i<=$crimeActions['rape']; $i++)
		{
			$rand = mt_rand(1, 100);

			if($rand < 50)
				$population--;
			else
				$population++;
		}

		// Births
		$birth = ($population >= 0) ? $population+$foodActions['new'] : $foodActions['new'];

		// Deaths
		$death = ($population < 0) ? $population+$crimeActions['murder'] : $crimeActions['murder'];
		$death = $death+$foodActions['disease'];
		$deathpercent = $death/100;
		$birthpercent = $birth/100;
		$death = $city['population']*$deathpercent;
		$birth = $city['population']*$birthpercent;

		// Total
		$total = $birth-$death;

Possibly Related Posts:


Comments are closed.