Jump to content
Sign in to follow this  
lynkfs

DWS bugs in Alpha (was: randomize)

Recommended Posts

Since the latest alpha the Randomize function gives a runtime error

(Randomize Sets the random generator seed to a random value).

Error message : "Uncaught ReferenceError: $alea is not defined"

Statement :   Randomize; 

generated code  : function Randomize() { Random = $alea() }

 

Share this post


Link to post
Share on other sites

The alea function was originally designed by Johannes Bagoe, as a means to improve generating random numbers
The original posts (musings) from him are not available anymore, but some links remain :
  https://stackoverflow.com/questions/8554243/how-to-use-implement-alea-to-generate-better-random-numbers  
  http://jsfiddle.net/rlemon/PZR4d/

If I copy the function in the links above into a smart project, it eliminates the runtime error. However it still doesn't work correctly so I'll wait till the DWS fix

Randomize / Random is an integral part to the neural network component

Share this post


Link to post
Share on other sites

The reason this bug happens is, commit 5e92a541002cd781ad335f0e7a4cd64615ca0f0b in DWS:

Quote

No longer usea alea.js when SetRandSeed is never used and repeatable Random is not required

Here the code checks if SetRandSeed is used. If not, it uses Math.random instead of alea and removes Random from DWS's dependencies, which results in alea.js not being linked in. The problem is, that Eric should also have checked if Randomize is used, before removing the dependency. So I basically know how to fix this myself, but prefer to get an official fix from Eric.

Currently you can trick DWS to work properly by adding a call to SetRandSeed before calling Randomize.

Share this post


Link to post
Share on other sites

Thanks for your 'Alea' digging.
Your temp solution does work, however it still breaks my neural network component
There might be some more dependencies than randomize and random only, or something else

Wait till fix arrives

(just realised that 'alea' is latin for 'gambling', good choice for a randomiser)

Share this post


Link to post
Share on other sites

Found the problem with my neural network component. It turns out it has nothing to do with the randomize function, but is an elusive compiler error in the latest alpha

To illustrate this :

This code (a matrix multiplication function)

Function DotX(Matrix1,Matrix2:TMatrix) : TMatrix;
var
  MyDotX : TMatrix;
begin
  MyDotX := TMatrix.Create(Matrix1.NrRows,Matrix2.NrColumns);
  For var i := 0 to Matrix1.NrRows -1 do begin
    For var j := 0 to Matrix2.NrColumns -1 do begin
      var sum: float := 0;
      For var k := 0 to Matrix1.NrColumns -1 do begin
        sum := sum + Matrix1.GetValue(i,k) * Matrix2.GetValue(k,j);
        MyDotX.SetValue(i,j,sum);
      end;
    end;
  end;
  result := MyDotX;
end;

is compiled to the following javascript

3.0.0
function DotX(Matrix1$1, Matrix2) {
   var Result = null;
   var MyDotX = null;
   var i$24 = 0;
   var j$10 = 0;
   var sum = 0,
      k$2 = 0;
   MyDotX = TMatrix.Create$163($New(TMatrix),parseInt(TMatrix.NrRows(Matrix1$1),10),parseInt(TMatrix.NrColumns(Matrix2),10));
   var $temp115;
   for(i$24=0,$temp115=TMatrix.NrRows(Matrix1$1)-1;i$24<=$temp115;i$24++) {
      var $temp116;
      for(j$10=0,$temp116=TMatrix.NrColumns(Matrix2)-1;j$10<=$temp116;j$10++) {
         sum = 0;
         var $temp117;
         for(k$2=0,$temp117=TMatrix.NrColumns(Matrix1$1)-1;k$2<=$temp117;k$2++) {
            sum+=Number(TMatrix.GetValue$1(Matrix1$1,i$24,k$2)*TMatrix.GetValue$1(Matrix2,k$2,j$10));
            TMatrix.SetValue$1(MyDotX,i$24,j$10,sum);
         }
      }
   }
   Result = MyDotX;
   return Result
};

which works fine in all SMS versions up to 3.0.0

In the latest alpha though, the line 'sum = 0' is omitted by the compiler, which breaks this function

@jartoattributes this to an optimisation error in the compiler

He'll get this fixed

This error is not tied to matrix multiplication or anything, it will occur in all similar constructs where a variable is declared in the body of a function with a value of zero (in this function the line var sum: float := 0)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×