Math-Evol

 view release on metacpan or  search on metacpan

lua/test.lua  view on Meta::CPAN

#!/usr/bin/lua
-- ----------------------------------------------------------------- --
--      This Lua5 script is Copyright (c) 2010, Peter J Billam       --
--                        www.pjb.com.au                             --
--                                                                   --
--   This script is free software; you can redistribute it and/or    --
--          modify it under the same terms as Lua5 itself.           --
-- ----------------------------------------------------------------- --
local Version = '1.0  for Lua5'
local VersionDate  = '25aug2010';
local detailed = false
local M = require 'Evol'

--------------------------- infrastructure ------------------------
local Test = 12 ; local i_test = 0; local Failed = 0;
function ok(b,s)
	i_test = i_test + 1
	if b then
		io.write('ok '..i_test..' - '..s.."\n")
	else
		io.write('not ok '..i_test..' - '..s.."\n")
		Failed = Failed + 1
	end
end
local function warn(str)
	io.stderr:write(str,'\n')
end
local function die(str)
	io.stderr:write(str,'\n')
	os.exit(1)
end
-------------------------------------------------------------------

local function minimise(x)
	local sum = 1.0
	for k,v in pairs(x) do sum = sum + v * v end
	return sum
end
local function contain(x)
	if (x[1] > 1.0) then x[1] = 1.0;  -- it's a greyscale value
	elseif (x[1] < 0.0) then x[1] = 0.0;
	end
	if (x[2] > 1.0) then x[2] = 1.0;  -- it's a greyscale value
	elseif (x[2] < 0.0) then x[2] = 0.0;
	end
	if (x[3] > 1.0) then x[3] = 1.0;  -- it's a greyscale value
	elseif (x[3] < 0.0) then x[3] = 0.0;
	end
	-- warn('contain: x = '..x[1]..' '..x[2]..' '..x[3]..' '..x[4].."=n")
	return x
end
local function choosebetter(arglist)
	local a = arglist[1]; local b = arglist[2];
	local a_sum = 0.0; for k,v in ipairs(a) do a_sum = a_sum + v * v end
	local b_sum = 0.0; for k,v in ipairs(b) do b_sum = b_sum + v * v end
	local preference = 1; if b_sum < a_sum      then preference = 2 end
--warn("a_sum="..tostring(a_sum).." b_sum="..tostring(b_sum).."\n")
	local continue   = false; if a_sum > 0.00000001 then continue   = true end
	return preference, continue
end
local text = [[
/w 3.456  def % evol step 0.8 min 0 max 1
/x 1.234  def % evol step 0.4 min 0 max 1
/y -2.345 def % evol step 0.6 min 0 max 1
/z 4.567  def % evol step 1.2
]]

local x  = {3.456, 1.234, -2.345, 4.567}
local sm = {.8, .4, .6, 1.2}

-- ----------------- test M.evol --------------------
local returns = {M.evol(x, sm, minimise, contain, 10)}



( run in 1.773 second using v1.01-cache-2.11-cpan-39bf76dae61 )