## Numerical optimizer

sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

### Numerical optimizer

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 functiontype 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 singleend 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 resend functionsub rnd256.init()   s0=hash64(&hffffffffffffffffull*rnd())   s1=hash64(&hffffffffffffffffull*rnd())   s2=hash64(&hffffffffffffffffull*rnd())   s3=hash64(&hffffffffffffffffull*rnd())end subfunction rnd256.nextinc (max as ulong) as ulong        dim as ulongint r = next64() and &hffffffff        r *= CULngInt(max) + 1        r = r shr 32        return  rend functionfunction 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+minend functionfunction rnd256.nextex overload (max as ulong) as ulong        dim as ulongint r = next64() and &hffffffff        r *= CULngInt(max)        r = r shr 32        return  rend functionfunction 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+minend functionfunction 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 functiontype 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 typesub mutator.init(size as ulong,precision as single)    rand.init()   redim positions(size-1),values(size-1)   prec=precisionend subsub 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   nextend subsub mutator.undo(x() as single)   for i as long=ubound(positions) to 0 step -1      x(positions(i))=values(i)   nextend sub'Testfunction test(x() as single) as singledim as single cost,dx,dydim as ulong n=ubound(x)+1for 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*dynextreturn costend function screenres 640,480,32randomize()dim as single parent(99),parentcost=1!/0!for i as long=0 to ubound(parent):parent(i)=2*rnd()-1:nextdim as mutator mutmut.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)`
deltarho[1859]
Posts: 2749
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Numerical optimizer

Tips and Tricks popup: "Post your FreeBASIC tips and tricks here. Please don't post your code without including an explanation."

Did a Compile and Quick Run - Screenres opened and shortly after the code closed. Before publishing code, it is worthwhile to do a final compile and run just to make sure an edit has not screwed things up.

A few views before mine and not a word. I suppose it is possible that without an explanation folk went back a page and moved on which is a pity because you have obviously put a fair amount of work into your code.
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

### Re: Numerical optimizer

I didn't put any work into it. Maybe this business between 32 and 64 bit systems again. Who knows.
Anyway on my computer it does some mutations, sees if that makes an improvement, if not undoes. I need to decide an optimizer for something I'm doing. I'm not very happy with the code anyway, also I think the built in FB RNG would suffice for it.
jj2007
Posts: 1812
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

### Re: Numerical optimizer

deltarho[1859] wrote:don't post your code without including an explanation
Exactly. And don't post code that isn't thoroughly tested. This code compiles in 32-bit land but then throws exceptions, definitely no good for the Tips & Tricks section.
deltarho[1859]
Posts: 2749
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Numerical optimizer

Actually, I did not write that and is within inverted commas. We get that on hovering on 'Tips and Tricks'.

I would rather see the two comments in a sticky and as the first post. That way we can simply say "Please read the first post"; when needed.
dodicat
Posts: 6759
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Numerical optimizer

The problem in 32 bits is

Code: Select all

` var n= &hffffffff var m= 4294967295print nprint m#print typeof(n)#print typeof(m)sleep `

if you substitute the number instead of the hex in rnd256.nextinc , it doesn't overflow the array in mutator.mutate()
jj2007
Posts: 1812
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

### Re: Numerical optimizer

deltarho[1859] wrote:Actually, I did not write that
Sorry, I quoted badly - of course, I support the spirit of your comment.
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

### Re: Numerical optimizer

You can't expect people to maintain 2 systems and two compilers before they post some code. If you want to loosen up maybe the Dandy Warhols are a good band to listen to. Or is it roast beef, Yorkshire pudding and oxo every Sunday.
jj2007
Posts: 1812
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

### Re: Numerical optimizer

sean_vn wrote:You can't expect people to maintain 2 systems and two compilers before they post some code.
You can expect people who post here in the Tips & Tricks section that they compile their code at least with standard Gas, Gcc32 and Gcc64. It's only a mouseclick or a keystroke away, right?

People who come here are mostly n00bs, and if they see code crash with an exception, as does happen with your code, that throws a really bad light on the language FreeBasic. That's why the administrators of this forum imposed that rule. So, please relax, fix your code, add an explanation and some comments (good practice anyway), and all will be fine.
deltarho[1859]
Posts: 2749
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Numerical optimizer

Seconded.

sean_vn wrote:If you want to loosen up

I am from Yorkshire and "roast beef, Yorkshire pudding and oxo every Sunday" is not my idea of loosening up. On the other hand this is. Twenty minutes of that, with a decent volume, is the polar opposite to a twenty-minute cardiovascular workout on an exercise bike and brings blood pressure down like a stone.
dodicat
Posts: 6759
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Numerical optimizer

This is for nostalgic purposes only, a 32 bit debug macro I made up years ago.
Purportedly catches early signs of bugs.
Windows only.
(Only 32 bits due to a bug in 64 bits)

Code: Select all

` #macro debug#if sizeof(integer)=4Declare Function ScaleWindow Alias "MoveWindow"(As Any Ptr,As Integer=0,As Integer=0,As Integer,As Integer,As Integer=1) As Integerscope    dim as string s,t    #macro rd(a)    s=string(ubound(a)," ")    for n as integer=1 to ubound(a)        s[n-1]=a(n)    next n    t+=s    #endmacro    screen 0dim as integer desktopW,desktopH,xres,yresscreeninfo desktopW,desktopHscreenres DesktopW/1.8,DesktopH/1.8,32,,64 or 8color rgb(200,200,200),rgb(0,0,200)clsscreeninfo xres,yreswidth xres\8,yres\16Dim As Integer IScreencontrol(2,I)ScaleWindow(Cast(Any Ptr,I),0,0,desktopW,desktopH)dim as ubyte a1(1 to 68)={65,32,102,97,116,97,108,32,101,120,99,101,112,116,105,111,110,32,79,69,32,104,97,_115,32,111,99,99,117,114,114,101,100,32,97,116,32,48,48,50,56,58,67,48,48,49,49,_69,51,54,32,105,110,32,86,88,68,32,86,77,77,40,48,49,41,32,43,10}dim as ubyte a2(1 to 58) ={32,32,32,32,48,48,48,49,48,69,51,54,46,32,84,104,101,32,99,117,114,114,101,110,116,32,97,112,_112,108,105,99,97,116,105,111,110,32,119,105,108,108,32,98,101,32,116,101,114,109,_105,110,97,116,101,100,46,10}dim as ubyte a3(1 to 2)= {10,10}dim as ubyte a4(1 to 57)= {32,32,32,32,42,32,80,114,101,115,115,32,97,110,121,32,107,101,121,32,116,111,32,_116,101,114,109,105,110,97,116,101,32,116,104,101,32,99,117,114,114,101,110,116,_32,97,112,112,108,105,99,97,116,105,111,110,10}dim as ubyte a5(1 to 69)= {32,32,32,32,42,32,80,114,101,115,115,32,67,84,82,76,32,43,32,65,76,84,32,43,32,68,_69,76,32,97,103,97,105,110,32,116,111,32,114,101,115,116,97,114,116,32,121,111,_117,114,32,99,111,109,112,117,116,101,114,46,89,111,117,32,119,105,108,108,10}dim as ubyte a6(1 to 56)= { 32,32,32,32,32,32,108,111,111,115,101,32,97,110,121,32,117,110,115,97,118,101,100,_32,105,110,102,111,114,109,97,116,105,111,110,32,105,110,32,97,108,108,32,97,112,_112,108,105,99,97,116,105,111,110,115,46}dim as ubyte a7(1 to 2)= {10,10}dim as ubyte a8(1 to 56)= { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,_80,114,101,115,115,32,97,110,121,32,107,101,121,32,116,111,32,99,111,110,116,105,_110,117,101,32,95} rd(a1):rd(a2):rd(a3):rd(a4):rd(a5):rd(a6):rd(a7):rd(a8)locate 10,5print tsleepend scope#elseprint "Sorry, can't debug in 64 bits"#endif #endmacro   screen 19,32 locate 20,20 print "Press a key to debug" sleep var t=1/0 debug sleep `
jj2007
Posts: 1812
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

### Re: Numerical optimizer

Code: Select all

`TmpFb.bas(62) error 57: Type mismatch, at parameter 2`
Is this a demo why code should be thoroughly tested for T+T?
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

### Re: Numerical optimizer

I was just starting to get back to coding after a break. This forum got very unfriendly very quick. Maybe Brexit is causing a general increase in grumpiness and a lack of movement. Maybe a chia seed and prune juice pudding would help? if not consult your doctor.
While not an OAP like some of you guys, I am getting along a bit. I should really be using modern languages like python and javascript.
The problem is I tend not to get a lot done in those languages. Then there is a tension between getting at least something done with FB and doing what I should be doing with modern languages.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

### Re: Numerical optimizer

Here is the post that has been alluded to.
Since you have been away, you should know that there has been tension in forums. Enough so that a moderator has been brought on to aid in administering the forum. Here is that forum topic.
I have to admit that the first thing I thought when I saw the title was "what is this code about?". And then disappointment when it was code with no explanation.
I am not the moderator nor am I chastising. I recommend that you can avoid the tension by one or two lines explaining at least what the code is doing.
And then posting "Tested on 32bits only". This makes your post more "friendly".
deltarho[1859]
Posts: 2749
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Numerical optimizer

In sancho3's first link the thread terminates with "Shouldn't this topic be a sticky one (always on top)?" by badidea. Personally, I think that is a brilliant idea but only for counting_pine's opening post. <smile>