# Scale free optimization

Kinda discussing scale free optimization again over here:
http://www.freebasic.net/forum/viewtopic.php?f=5&t=25133
Very simple and effective. It should only take an hour to convert to Java if you wanted to using the Float class methods.

package neonandroid;

public class Opt {

``````private static long s0, s1;
``````

//http://xoroshiro.di.unimi.it/
private static long nextLong() {
final long s0 = Opt.s0;
long s1 = Opt.s1;
final long result = s0 + s1;
s1 ^= s0;
Opt.s0 = Long.rotateLeft(s0, 55) ^ s1 ^ s1 << 14;
Opt.s1 = Long.rotateLeft(s1, 36);
return result;
}

``````public static float mutateSymP127(float x){
float m=x+Float.intBitsToFloat((int)nextLong() & 0xbfffffff);
if((m>1f) || (m<-1f) ) return x;
return m;
}
``````

//https://en.wikipedia.org/wiki/Test_functions_for_optimization
//Lévi function N.13
public static double levi(double x,double y){
x*=10; // -10 to 10
y*=10; // -10 to 10
return Math.sin(3Math.PIx)Math.sin(3Math.PIx)+(x-1)(x-1)(1+Math.sin(3Math.PIy)Math.sin(3Math.PIy))+(y-1)(y-1)(1+Math.sin(2Math.PIy)Math.sin(2Math.PI*y));
}

``````public static void main(String[] args) {
float x=0,y=0,cx,cy,cost=Float.POSITIVE_INFINITY;
for(int i=0;i<5000;i++){
cx=mutateSymP127(x);
cy=mutateSymP127(y);
float newcost=(float)levi(cx,cy);
if(newcost<cost){
cost=newcost;
x=cx;
y=cy;
}
}
System.out.println("Cost: "+cost);
System.out.println("At: "+10*x+"  "+10*y);
System.out.println("Should be 0 at 1,1");
}

static {
s0=System.nanoTime();
s1=s0+1;
}
``````

}