Code: Select all
function hash64(h as ulongint) as ulongint
h = ( h xor ( h shr 30 ) ) * &hBF58476D1CE4E5B9
h = ( h xor ( h shr 27 ) ) * &h94D049BB133111EB
return h xor ( h shr 31 )
end function
type rnd256
as ulongint s0,s1,s2,s3
declare sub init()
declare function next64() as ulongint
declare function nextinc (max as ulong) as ulong
declare function nextinc (min as long, max as long) as long
declare function nextex (max as ulong) as ulong
declare function nextex (min as long, max as long) as long
declare function nextsingle() as single
declare function nextsinglesym() as single
end type
function rnd256.next64() as ulongint
dim as ulongint res=hash64(s0)
dim as ulongint t = s1 shl 17
s2 xor= s0
s3 xor= s1
s1 xor= s2
s0 xor= s3
s2 xor= t
s3=(s3 shl 45) or (s3 shr 19)
return res
end function
sub rnd256.init()
s0=hash64(&hffffffffffffffffull*rnd())
s1=hash64(&hffffffffffffffffull*rnd())
s2=hash64(&hffffffffffffffffull*rnd())
s3=hash64(&hffffffffffffffffull*rnd())
end sub
function rnd256.nextinc (max as ulong) as ulong
dim as ulongint r = next64() and &hffffffff
r *= CULngInt(max) + 1
r = r shr 32
return r
end function
function rnd256.nextinc (min as long, max as long) as long
dim as ulongint r = next64() and &hffffffff
r *= CULngInt(max-min) + 1
r = r shr 32
return r+min
end function
function rnd256.nextex overload (max as ulong) as ulong
dim as ulongint r = next64() and &hffffffff
r *= CULngInt(max)
r = r shr 32
return r
end function
function rnd256.nextex overload (min as long, max as long) as long
dim as ulongint r = next64() and &hffffffff
r *= CULngInt(max-min)
r = r shr 32
return r+min
end function
function rnd256.nextsingle() as single
return next64()*(0.99999997!/&hffffffffffffffffULL)
end function
function rnd256.nextsinglesym() as single
dim as longint r=next64()
return r*(-0.99999997!/&h8000000000000000)
end function
type mutator
as rnd256 rand
as ulongint positions(any)
as single values(any),prec
declare sub init(size as ulong,precision as single)
declare sub mutate(x() as single)
declare sub undo(x() as single)
end type
sub mutator.init(size as ulong,precision as single)
rand.init()
redim positions(size-1),values(size-1)
prec=precision
end sub
sub mutator.mutate(x() as single)
dim as ulongint m=ubound(x)
for i as ulong=0 to ubound(positions)
dim as ulongint idx=rand.nextinc(m)
positions(i)=idx
dim as single m=2!*exp(-prec*rand.nextsingle()),v=x(idx)
values(i)=v
m*=1-(culng(rand.next64()) and 2)
while (v+m)>1! orelse (v+m)<-1!
m*=-0.5!
wend
x(idx)=v+m
next
end sub
sub mutator.undo(x() as single)
for i as long=ubound(positions) to 0 step -1
x(positions(i))=values(i)
next
end sub
'Test
function test(x() as single) as single
dim as single cost,dx,dy
dim as ulong n=ubound(x)+1
for i as ulong=0 to ubound(x) step 2
dx=x(i)-x((i+2) mod n)
dy=x(i+1)-x((i+3) mod n)
cost-=sqr(dx*dx+dy*dy)
dx=x(i)
dy=x(i+1)
cost+=dx*dx+dy*dy
next
return cost
end function
screenres 640,480,32
randomize()
dim as single parent(99),parentcost=1!/0!
for i as long=0 to ubound(parent):parent(i)=2*rnd()-1:next
dim as mutator mut
mut.init(20,15!)
do
mut.mutate(parent())
dim as single cost=test(parent())
if cost<parentcost then
parentcost=cost
cls
line (320+200*parent(0),240+200*parent(1))-(320+200*parent(2),240+200*parent(3))
for i as ulong=4 to ubound(parent) step 2
line -(320+200*parent(i),240+200*parent(i+1))
next
print parentcost
else
mut.undo(parent())
end if
loop until inkey=chr(27)