有人可以帮助将以下 C# 示例代码从Alglib库转换为 F# 吗?我无法在互联网上找到任何关于如何从 F# 使用它的示例。
public static void function1_grad(double[] x, ref double func, double[] grad, object obj)
{
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
// and its derivatives df/d0 and df/dx1
func = 100*System.Math.Pow(x[0]+3,4) + System.Math.Pow(x[1]-3,4);
grad[0] = 400*System.Math.Pow(x[0]+3,3);
grad[1] = 4*System.Math.Pow(x[1]-3,3);
}
public static int Main(string[] args)
{
//
// This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
// using LBFGS method.
//
// Several advanced techniques are demonstrated:
// * upper limit on step size
// * restart from new point
//
double[] x = new double[]{0,0};
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
double stpmax = 0.1;
int maxits = 0;
alglib.minlbfgsstate state;
alglib.minlbfgsreport rep;
// first run
alglib.minlbfgscreate(1, x, out state);
alglib.minlbfgssetcond(state, epsg, epsf, epsx, maxits);
alglib.minlbfgssetstpmax(state, stpmax);
alglib.minlbfgsoptimize(state, function1_grad, null, null);
alglib.minlbfgsresults(state, out x, out rep);
System.Console.WriteLine("{0}", alglib.ap.format(x,2)); // EXPECTED: [-3,3]
// second run - algorithm is restarted
x = new double[]{10,10};
alglib.minlbfgsrestartfrom(state, x);
alglib.minlbfgsoptimize(state, function1_grad, null, null);
alglib.minlbfgsresults(state, out x, out rep);
System.Console.WriteLine("{0}", rep.terminationtype); // EXPECTED: 4
System.Console.WriteLine("{0}", alglib.ap.format(x,2)); // EXPECTED: [-3,3]
System.Console.ReadLine();
return 0;
}
这是我对 F# 翻译的(糟糕的)尝试:
let function1_grad(x : float [], func : float, grad : float [], obj_ : obj) =
func <- 100.*System.Math.Pow(x.[0]+3.,4.) + System.Math.Pow(x.[1]-3., 4.)
grad.[0] <- 400.*System.Math.Pow(x.[0]+3,3.);
grad.[1] <- 4.*System.Math.Pow(x.[1]-3.,3.);
let runOptim() =
let x = [|0.; 0.|]
let epsg = 0.0000000001
let epsf = 0.
let epsx = 0.
let stpmax = 0.1
let maxits = 0
let state = alglib.minlbfgsstate
let mutable rep = alglib.minlbfgsreport()
// first run
alglib.minlbfgscreate(1, x, state)
alglib.minlbfgssetcond(state, epsg, epsf, epsx, maxits)
alglib.minlbfgssetstpmax(state, stpmax)
alglib.minlbfgsoptimize(state, function1_grad, null, null)
alglib.minlbfgsresults(state, x, rep)
printfn "{0}", alglib.ap.format(x,2)
let x = [|10.; 10.|]
alglib.minlbfgsrestartfrom(state, x)
alglib.minlbfgsoptimize(state, function1_grad, null, null)
alglib.minlbfgsresults(state, x, rep)
printfn "%A" rep.terminationtype
printfn "%A" (alglib.ap.format(x, 2))
System.Console.ReadLine() |> ignore