<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://old.hacdc.org/index.php?action=history&amp;feed=atom&amp;title=Genetic_programming_example_in_lua</id>
	<title>Genetic programming example in lua - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://old.hacdc.org/index.php?action=history&amp;feed=atom&amp;title=Genetic_programming_example_in_lua"/>
	<link rel="alternate" type="text/html" href="https://old.hacdc.org/index.php?title=Genetic_programming_example_in_lua&amp;action=history"/>
	<updated>2026-05-07T12:07:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://old.hacdc.org/index.php?title=Genetic_programming_example_in_lua&amp;diff=7070&amp;oldid=prev</id>
		<title>Tslagle at 20:20, 4 April 2012</title>
		<link rel="alternate" type="text/html" href="https://old.hacdc.org/index.php?title=Genetic_programming_example_in_lua&amp;diff=7070&amp;oldid=prev"/>
		<updated>2012-04-04T20:20:44Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:20, 4 April 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l449&quot;&gt;Line 449:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 449:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print_program(population[best_index].program)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print_program(population[best_index].program)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:NARG]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key hacdc_wiki:diff::1.12:old-3463:rev-7070 --&gt;
&lt;/table&gt;</summary>
		<author><name>Tslagle</name></author>
	</entry>
	<entry>
		<id>https://old.hacdc.org/index.php?title=Genetic_programming_example_in_lua&amp;diff=3463&amp;oldid=prev</id>
		<title>Omaha: genetic programming w/ lua</title>
		<link rel="alternate" type="text/html" href="https://old.hacdc.org/index.php?title=Genetic_programming_example_in_lua&amp;diff=3463&amp;oldid=prev"/>
		<updated>2010-06-18T00:07:01Z</updated>

		<summary type="html">&lt;p&gt;genetic programming w/ lua&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The following code is capable of converging on a solution to Brad&amp;#039;s parabola problem, which can be found on the [[NARG]] page&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
master_stack = {}&lt;br /&gt;
function_table = {}&lt;br /&gt;
type_array = {}&lt;br /&gt;
type_stack = {}&lt;br /&gt;
&lt;br /&gt;
stack_size = 32&lt;br /&gt;
&lt;br /&gt;
num_types = 0&lt;br /&gt;
&lt;br /&gt;
num_x_samples = 5&lt;br /&gt;
constant = {0,1,2,3,4}&lt;br /&gt;
expected_result = {0, 4.75, 12.75, 23.98, 38.45, 56.15}&lt;br /&gt;
function sleep(n)&lt;br /&gt;
	os.execute(&amp;quot;sleep &amp;quot; .. tonumber(n))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function deepcopy(object)&lt;br /&gt;
    local lookup_table = {}&lt;br /&gt;
	local function _copy(object)&lt;br /&gt;
	if type(object) ~= &amp;quot;table&amp;quot; then&lt;br /&gt;
	    return object&lt;br /&gt;
	elseif lookup_table[object] then&lt;br /&gt;
		return lookup_table[object]&lt;br /&gt;
    end&lt;br /&gt;
    local new_table = {}&lt;br /&gt;
    lookup_table[object] = new_table&lt;br /&gt;
    for index, value in pairs(object) do&lt;br /&gt;
        new_table[_copy(index)] = _copy(value)&lt;br /&gt;
	end&lt;br /&gt;
	return setmetatable(new_table, getmetatable(object))&lt;br /&gt;
    end&lt;br /&gt;
    return _copy(object)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function print_table(theTable, indent)&lt;br /&gt;
	&lt;br /&gt;
	local iString = &amp;quot;&amp;quot;&lt;br /&gt;
	for index = 1, indent do&lt;br /&gt;
		iString = iString .. &amp;quot;-&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- walk all the topmost values in the table&lt;br /&gt;
	for k,v in pairs(theTable) do&lt;br /&gt;
		print(iString ,k ,v)&lt;br /&gt;
		if type(v) == &amp;quot;table&amp;quot; then&lt;br /&gt;
			print_table(v, indent + 1)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function print_program(theProgram)&lt;br /&gt;
	print(&amp;quot;Program Stack Bottom&amp;quot;)&lt;br /&gt;
	for counter = 1, table.getn(theProgram) do&lt;br /&gt;
		if (theProgram[counter].name == &amp;quot;real&amp;quot;) then&lt;br /&gt;
			print(theProgram[counter].value)&lt;br /&gt;
		else&lt;br /&gt;
			print(theProgram[counter].name)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	print(&amp;quot;Program Stack Top&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- add the type to the type table&lt;br /&gt;
function insert_function(functionName, functionCall, functionType, nArguments)&lt;br /&gt;
	-- ensure we haven&amp;#039;t already inserted this function&lt;br /&gt;
	if function_table[functionName] ~= nil then&lt;br /&gt;
		print(&amp;quot;function already defined: &amp;quot; .. functionName)&lt;br /&gt;
		return false&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- add this function to the function_table&lt;br /&gt;
	tempFunction = {}&lt;br /&gt;
	tempFunction.fName = functionCall&lt;br /&gt;
	tempFunction.fType = functionType&lt;br /&gt;
	tempFunction.nArgs = nArguments&lt;br /&gt;
&lt;br /&gt;
	function_table[functionName] = tempFunction&lt;br /&gt;
	return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function insert_type(typeName, fPointer, weight)&lt;br /&gt;
	-- walk the list of types and make sure this one hasn&amp;#039;t already been defined&lt;br /&gt;
	for index = 1, table.getn(type_array) do&lt;br /&gt;
		if (type_array[index].name == typeName) then&lt;br /&gt;
			print(&amp;quot;type already defined: &amp;quot; .. typeName)&lt;br /&gt;
			return false&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	num_types = num_types + 1&lt;br /&gt;
	type_array[num_types] = {}&lt;br /&gt;
	type_array[num_types].name = typeName&lt;br /&gt;
	type_array[num_types].fPointer = fPointer&lt;br /&gt;
	type_array[num_types].weight = weight&lt;br /&gt;
&lt;br /&gt;
	if fPointer == nil then&lt;br /&gt;
		-- establish the stack for this type&lt;br /&gt;
		type_stack[typeName] = {}&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function local_add(arguments)&lt;br /&gt;
	-- print(&amp;quot;adding &amp;quot; .. arguments[1] .. &amp;quot; and &amp;quot; .. arguments[2])&lt;br /&gt;
	return arguments[1] + arguments[2]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function local_subtract(arguments)&lt;br /&gt;
	-- print(&amp;quot;subtracting &amp;quot; .. arguments[1] .. &amp;quot; and &amp;quot; .. arguments[2])&lt;br /&gt;
	return arguments[1] + arguments[2]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function local_multiply(arguments)&lt;br /&gt;
	-- print(&amp;quot;multiplying &amp;quot; .. arguments[1] .. &amp;quot; and &amp;quot; .. arguments[2])&lt;br /&gt;
	return arguments[1] * arguments[2]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function local_divide(arguments)&lt;br /&gt;
	-- print(&amp;quot;dividing &amp;quot; .. arguments[1] .. &amp;quot; and &amp;quot; .. arguments[2])&lt;br /&gt;
	-- dragon&lt;br /&gt;
	if (arguments[2] == 0) then&lt;br /&gt;
			arguments[2] = 0.00001&lt;br /&gt;
	end&lt;br /&gt;
	return arguments[1] / arguments[2]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function some_constant()&lt;br /&gt;
	-- print(&amp;quot;pushing constant onto stack:&amp;quot; .. constant[currentConstant])&lt;br /&gt;
	return constant[currentConstant]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function establish_types()&lt;br /&gt;
	-- add each of our types to the type_table&lt;br /&gt;
	insert_type(&amp;quot;real&amp;quot;, nil, 10)&lt;br /&gt;
	insert_type(&amp;quot;+&amp;quot;, local_add, 1)&lt;br /&gt;
	insert_type(&amp;quot;*&amp;quot;, local_multiply, 1)&lt;br /&gt;
	--insert_type(&amp;quot;-&amp;quot;, local_subtract, 1)&lt;br /&gt;
	--insert_type(&amp;quot;/&amp;quot;, local_divide, 1)&lt;br /&gt;
	insert_type(&amp;quot;X&amp;quot;, some_constant, 5)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function establish_functions()&lt;br /&gt;
	insert_function(&amp;quot;+&amp;quot;, local_add, &amp;quot;real&amp;quot;, 2)&lt;br /&gt;
	insert_function(&amp;quot;-&amp;quot;, local_subtract, &amp;quot;real&amp;quot;, 2)&lt;br /&gt;
	insert_function(&amp;quot;*&amp;quot;, local_multiply, &amp;quot;real&amp;quot;,  2)&lt;br /&gt;
	insert_function(&amp;quot;/&amp;quot;, local_divide, &amp;quot;real&amp;quot;,  2)&lt;br /&gt;
	insert_function(&amp;quot;X&amp;quot;, some_constant, &amp;quot;real&amp;quot;, 0)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function generate_program(programSize )&lt;br /&gt;
	return_stack = {}&lt;br /&gt;
	&lt;br /&gt;
	for counter = 1, programSize do&lt;br /&gt;
		currentNode = {}&lt;br /&gt;
		ranVal = math.random(1,table.getn(type_array))&lt;br /&gt;
		currentNode.name = type_array[ranVal].name&lt;br /&gt;
		-- beware hardcoded stuffs&lt;br /&gt;
		if currentNode.name == &amp;quot;real&amp;quot; then&lt;br /&gt;
			currentNode.value = math.random()&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
		table.insert(return_stack, currentNode)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return return_stack&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function process_master()&lt;br /&gt;
	while table.getn(master_stack) ~= 0 do&lt;br /&gt;
	--	print(&amp;quot;frame begin-------------------------------&amp;quot;)&lt;br /&gt;
	--	print(&amp;quot;current table:&amp;quot;)&lt;br /&gt;
		-- print_table(type_stack[&amp;quot;real&amp;quot;], 0)&lt;br /&gt;
		&lt;br /&gt;
		currentNode = table.remove(master_stack)&lt;br /&gt;
	--	print(&amp;quot;curret node name: &amp;quot; .. currentNode.name )&lt;br /&gt;
&lt;br /&gt;
		-- treat functions and values differently&lt;br /&gt;
		if currentNode.name == &amp;quot;real&amp;quot; then&lt;br /&gt;
		--	print(&amp;quot;current node value: &amp;quot; .. currentNode.value)&lt;br /&gt;
			-- add this value to the &amp;#039;real&amp;#039; stack&lt;br /&gt;
			table.insert(type_stack[&amp;quot;real&amp;quot;], currentNode.value)&lt;br /&gt;
		else&lt;br /&gt;
			-- grab the num of params needed for this function&lt;br /&gt;
			nRequired = function_table[currentNode.name].nArgs&lt;br /&gt;
			theType = function_table[currentNode.name].fType&lt;br /&gt;
&lt;br /&gt;
			-- make sure there are enough objects on the param stack to call this function&lt;br /&gt;
			-- print(&amp;quot;name = &amp;quot; .. currentNode.name)&lt;br /&gt;
			-- print(function_table[currentNode.name].fType)&lt;br /&gt;
			-- print(type_stack[&amp;quot;real&amp;quot;])&lt;br /&gt;
			if (table.getn(type_stack[function_table[currentNode.name].fType]) &amp;lt; nRequired) then&lt;br /&gt;
				-- not enough params available, NOOP&lt;br /&gt;
			--	print(&amp;quot;not enough params, NOOP&amp;quot;)&lt;br /&gt;
			else&lt;br /&gt;
				theArguments = {}&lt;br /&gt;
				-- build an array for passing the params to the function&lt;br /&gt;
				for counter = 1, nRequired do&lt;br /&gt;
					theArguments[counter] = table.remove(type_stack[theType])&lt;br /&gt;
				end&lt;br /&gt;
&lt;br /&gt;
				-- call the function&lt;br /&gt;
				returnVal = function_table[currentNode.name].fName(theArguments)&lt;br /&gt;
&lt;br /&gt;
				-- push the return val to the appropriate stack&lt;br /&gt;
				table.insert(type_stack[function_table[currentNode.name].fType], returnVal)&lt;br /&gt;
&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	--	print(&amp;quot;frame end---------------------------------&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function grab_result()&lt;br /&gt;
	-- print the top of the &amp;quot;real&amp;quot; stack&lt;br /&gt;
	if (table.getn(type_stack[&amp;quot;real&amp;quot;]) == 0) then&lt;br /&gt;
		return 9999999&lt;br /&gt;
	end&lt;br /&gt;
	return table.remove(type_stack[&amp;quot;real&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--generate_master()&lt;br /&gt;
&lt;br /&gt;
--process_master()&lt;br /&gt;
&lt;br /&gt;
--print_result()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function create_population()&lt;br /&gt;
	-- loop through each member in the population&lt;br /&gt;
	for count = 1, population_size do&lt;br /&gt;
		current_member = {}&lt;br /&gt;
		current_member._error = 99999&lt;br /&gt;
		&lt;br /&gt;
		-- generate the member&amp;#039;s program data&lt;br /&gt;
		current_member.program = generate_program(initial_member_size)&lt;br /&gt;
		&lt;br /&gt;
		-- add this member to the population&lt;br /&gt;
		table.insert(population, current_member)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function get_best_candidate()&lt;br /&gt;
	local best_error = 99999&lt;br /&gt;
	local best_index = 0&lt;br /&gt;
&lt;br /&gt;
	for tIndex = 1, table.getn(candidates) do&lt;br /&gt;
		if candidates[tIndex]._error &amp;lt; best_error then&lt;br /&gt;
			best_index = tIndex&lt;br /&gt;
			best_error = candidates[tIndex]._error&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	--print(&amp;quot;candidate size: &amp;quot; .. table.getn(candidates) .. &amp;quot;\nbest error from candidates: &amp;quot; .. best_error)&lt;br /&gt;
&lt;br /&gt;
	-- remove and return the *best* candidate&lt;br /&gt;
	return table.remove(candidates, best_index)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function mutate_child(origin, n_mutations)&lt;br /&gt;
	dest_node = deepcopy(origin)&lt;br /&gt;
&lt;br /&gt;
	for counter = 1, n_mutations do&lt;br /&gt;
		-- random point inside this child&lt;br /&gt;
		index_mutate = math.random(1, table.getn(dest_node.program))&lt;br /&gt;
		&lt;br /&gt;
		ranVal = math.random(1,table.getn(type_array))&lt;br /&gt;
		dest_node.program[index_mutate].name = type_array[ranVal].name&lt;br /&gt;
		-- beware hardcoded stuffs&lt;br /&gt;
		if dest_node.program[index_mutate].name == &amp;quot;real&amp;quot; then&lt;br /&gt;
			dest_node.program[index_mutate].value = math.random()&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return dest_node&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function crossover_parents(mommy, daddy)&lt;br /&gt;
	index_m = math.random(1, table.getn(mommy))&lt;br /&gt;
	--index_d = math.random(1, table.getn(daddy))&lt;br /&gt;
&lt;br /&gt;
	the_child = {}&lt;br /&gt;
&lt;br /&gt;
	-- add the first index_m elements of mommy to the_child&lt;br /&gt;
	for xxx = 1, index_m do&lt;br /&gt;
		table.insert(the_child, mommy[xxx])&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- add the elements index_d to #daddy of daddy to the_child&lt;br /&gt;
	for xxx = index_m+1, table.getn(daddy) do&lt;br /&gt;
		table.insert(the_child, daddy[xxx])&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return the_child&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- initialize the population (1 program for each member in the population)&lt;br /&gt;
establish_functions()&lt;br /&gt;
establish_types()&lt;br /&gt;
population = {}&lt;br /&gt;
population_size = 10000&lt;br /&gt;
initial_member_size = 24&lt;br /&gt;
create_population()&lt;br /&gt;
&lt;br /&gt;
error_history = {}&lt;br /&gt;
error_threshhold = 0.016&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
max_num_iterations = 10000&lt;br /&gt;
current_iteration = 1&lt;br /&gt;
&lt;br /&gt;
-- while we haven&amp;#039;t reached our error threshhold&lt;br /&gt;
while current_iteration &amp;lt;= max_num_iterations do&lt;br /&gt;
&lt;br /&gt;
	print(&amp;quot;iteration #&amp;quot; .. current_iteration)&lt;br /&gt;
&lt;br /&gt;
	--print_table(population, 2)&lt;br /&gt;
&lt;br /&gt;
	-- get the error for each of the members of our population&lt;br /&gt;
	for pcount = 1, population_size do&lt;br /&gt;
&lt;br /&gt;
		--print_table(population[pcount], 1)&lt;br /&gt;
&lt;br /&gt;
		-- initialize the error for this program&lt;br /&gt;
		population[pcount]._error = 0&lt;br /&gt;
		--print(&amp;quot;pcount = &amp;quot; .. pcount)&lt;br /&gt;
&lt;br /&gt;
		if (current_iteration == 2) then&lt;br /&gt;
			--print_table(population[pcount], 1)&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
			-- for each value of X&lt;br /&gt;
			for icount = 1, num_x_samples do&lt;br /&gt;
				-- establish the index to the current X/Y pair&lt;br /&gt;
				currentConstant = icount&lt;br /&gt;
&lt;br /&gt;
			--	print(&amp;quot;population size: &amp;quot; .. table.getn(population))&lt;br /&gt;
			--	print(&amp;quot;master_stack size: &amp;quot; .. table.getn(population[pcount].program))&lt;br /&gt;
&lt;br /&gt;
				-- make a copy of this guy&amp;#039;s program&lt;br /&gt;
				master_stack = deepcopy(population[pcount].program)&lt;br /&gt;
&lt;br /&gt;
				-- initialize the stacks for each data type&lt;br /&gt;
				type_stack[&amp;quot;real&amp;quot;] = {}&lt;br /&gt;
				&lt;br /&gt;
				-- evaluate the program&lt;br /&gt;
				process_master()&lt;br /&gt;
&lt;br /&gt;
			--	print(&amp;quot;master_stack size: &amp;quot; .. table.getn(population[pcount].program))&lt;br /&gt;
&lt;br /&gt;
			--	print(&amp;quot;!!! - &amp;quot; .. table.getn(type_stack[&amp;quot;real&amp;quot;]))&lt;br /&gt;
&lt;br /&gt;
				the_result = grab_result()&lt;br /&gt;
&lt;br /&gt;
				--print(&amp;quot;the result = &amp;quot; .. the_result)&lt;br /&gt;
&lt;br /&gt;
				-- add the current error to the total error for this program&lt;br /&gt;
				population[pcount]._error = population[pcount]._error + math.abs(the_result - expected_result[currentConstant])&lt;br /&gt;
			end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- scan the population and find the lowest error&lt;br /&gt;
	total_error = 0&lt;br /&gt;
	best_error = 99999&lt;br /&gt;
	best_index = 0&lt;br /&gt;
	for pcount = 1, population_size do&lt;br /&gt;
		total_error = total_error + population[pcount]._error&lt;br /&gt;
		if (population[pcount]._error &amp;lt; best_error) then&lt;br /&gt;
			best_index = pcount&lt;br /&gt;
			best_error = population[pcount]._error&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	average_error = total_error / population_size&lt;br /&gt;
&lt;br /&gt;
	print(&amp;quot;lowest error : &amp;quot; .. best_error)&lt;br /&gt;
	print(&amp;quot;average error: &amp;quot; .. average_error)&lt;br /&gt;
&lt;br /&gt;
	table.insert(error_history, best_error)&lt;br /&gt;
&lt;br /&gt;
	-- if the error is under our threshhold, break out and report success&lt;br /&gt;
	if (best_error &amp;lt; error_threshhold) then&lt;br /&gt;
		break&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	--=======================================================================&lt;br /&gt;
	-- evolution FTW&lt;br /&gt;
	--=======================================================================&lt;br /&gt;
&lt;br /&gt;
	children = {}&lt;br /&gt;
	child = {}&lt;br /&gt;
	candidates = deepcopy(population)&lt;br /&gt;
&lt;br /&gt;
	-- find the top 10% of the population, keep them&lt;br /&gt;
	tnum = math.ceil(table.getn(population) / 100)&lt;br /&gt;
	for cpop = 1, tnum do&lt;br /&gt;
		table.insert(children, get_best_candidate())&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	--sleep(10)&lt;br /&gt;
	-- the next 25% should be mutations of the top 10%&lt;br /&gt;
	xnum = tnum + math.floor(tnum * 10)&lt;br /&gt;
	candidates = deepcopy(children) -- reset candidates&lt;br /&gt;
	-- print(&amp;quot;we have &amp;quot; .. table.getn(candidates) .. &amp;quot; candidates to chose from&amp;quot;)&lt;br /&gt;
	for cpop = (tnum+1), xnum do&lt;br /&gt;
		child = mutate_child(candidates[math.random(1, table.getn(candidates))], 5)&lt;br /&gt;
		table.insert(children, deepcopy(child))&lt;br /&gt;
		-- print(&amp;quot;size of children: &amp;quot; .. table.getn(children))&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- the remainder should be crossovers of the full population&lt;br /&gt;
	for pcount = (xnum+1), population_size do&lt;br /&gt;
&lt;br /&gt;
		-- take 7 random values from the population&lt;br /&gt;
		candidates = {}&lt;br /&gt;
		child = {}&lt;br /&gt;
		for ccount = 1, 7 do&lt;br /&gt;
			table.insert(candidates, deepcopy(population[math.random(1, table.getn(population))]))&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
		-- print_table(candidates, 1)&lt;br /&gt;
&lt;br /&gt;
		mom = get_best_candidate()&lt;br /&gt;
		dad = get_best_candidate()&lt;br /&gt;
		child.program = crossover_parents(mom.program, dad.program)&lt;br /&gt;
&lt;br /&gt;
		-- write the child data to the list of children&lt;br /&gt;
		table.insert(children, child)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- move the new population to their proper home&lt;br /&gt;
	population = deepcopy(children)&lt;br /&gt;
&lt;br /&gt;
	current_iteration = current_iteration + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;all done!&amp;quot;)&lt;br /&gt;
--print_table(population[best_index].program, 1)&lt;br /&gt;
print_program(population[best_index].program)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Omaha</name></author>
	</entry>
</feed>