Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Time f#中断函数评估_Time_F#_Break - Fatal编程技术网

Time f#中断函数评估

Time f#中断函数评估,time,f#,break,Time,F#,Break,我必须最小化一个相当复杂的函数。对于最小化,我使用极端优化库中的非线性程序。因为无法找到全局最小值,所以我使用不同的起始点,然后选择“最佳最小值”。我的问题是可能会有一些起点,评估可能需要很长时间。在F#中是否有一些通用的方法,或者在极端优化中是否有一些特殊的方法,停止评估,比如说在10分钟后,然后给出一个返回[nan;nan;nan;nan;nan;nan]的列表 let funcFindPara (startpoint:float list) func = let nlp = n

我必须最小化一个相当复杂的函数。对于最小化,我使用极端优化库中的非线性程序。因为无法找到全局最小值,所以我使用不同的起始点,然后选择“最佳最小值”。我的问题是可能会有一些起点,评估可能需要很长时间。在F#中是否有一些通用的方法,或者在极端优化中是否有一些特殊的方法,停止评估,比如说在10分钟后,然后给出一个返回[nan;nan;nan;nan;nan;nan]的列表

let funcFindPara (startpoint:float list) func = 

    let nlp = new NonlinearProgram(6)

    // add the function
    nlp.ObjectiveFunction <- (fun x -> func x.[0] x.[1] x.[2] x.[3] x.[4] x.[5])

    // add lineare constraints
    nlp.AddLinearConstraint("a + d > 0", Vector.Create(1.0, 0.0, 0.0, 1.0, 0.0, 0.0), 1.0e-5, infinity) |> ignore
    nlp.AddLinearConstraint("c > 0", Vector.Create(0.0, 0.0, 1.0, 0.0, 0.0, 0.0), 1.0e-5, infinity) |> ignore
    nlp.AddLinearConstraint("d > 0", Vector.Create(0.0, 0.0, 0.0, 1.0, 0.0, 0.0), 1.0e-5, infinity) |> ignore
    nlp.AddLinearConstraint("gamma > 0", Vector.Create(0.0, 0.0, 0.0, 0.0, 1.0, 0.0), 1.0e-5, infinity) |> ignore
    nlp.AddLinearConstraint("0 < rho_infty <= 1", Vector.Create(0.0, 0.0, 0.0, 0.0, 0.0, 1.0), 1.0e-5, 1.0) |> ignore

    // add nonlinear constrains 
    // gamma <= -ln(rho_infty)
    nlp.AddNonlinearConstraint((fun (x : Vector) -> x.[4] + log(x.[5])), ConstraintType.LessThanOrEqual, 0.0, (fun (x : Vector) -> fun (y : Vector) -> 
          y.[0] <- 0.0 
          y.[1] <- 0.0 
          y.[2] <- 0.0
          y.[3] <- 0.0
          y.[4] <- 1.0
          y.[5] <- 1.0 / x.[5]
          y
        )
    ) |> ignore

    // add starting point
    nlp.InitialGuess <- Vector.Create(startpoint.[0], startpoint.[1], startpoint.[2], startpoint.[3], startpoint.[4], startpoint.[5])

    // solve
    let solution = nlp.Solve()

    // return list with parameters
    List.init 6 (fun index -> solution.[index])
让funcFindPara(起始点:浮点列表)func=
设nlp=新的非线性程序(6)
//添加函数
nlp.ObjectiveFunction函数x[0]x[1]x[2]x[3]x[4]x[5])
//添加线性约束
nlp.AddLinearConstraint(“a+d>0”,Vector.Create(1.0,0.0,0.0,1.0,0.0,0.0),1.0e-5,无穷大)|>忽略
nlp.AddLinearConstraint(“c>0”,Vector.Create(0.0,0.0,1.0,0.0,0.0,0.0),1.0e-5,无穷大)|>忽略
nlp.AddLinearConstraint(“d>0”,Vector.Create(0.0,0.0,0.0,1.0,0.0,0.0),1.0e-5,无穷大)|>忽略
nlp.AddLinearConstraint(“gamma>0”,Vector.Create(0.0,0.0,0.0,1.0,0.0),1.0e-5,无穷大)|>忽略

nlp.AddLinearConstraint(“0async{}
包装函数,并将其传递给
RunSynchronously
以及超时:

let withTimeout f timeout defaultValue =
    try Async.RunSynchronously((async { return f() }), timeout)
    with :? System.TimeoutException -> defaultValue

let longFn() = 
    System.Threading.Thread.Sleep(5000)
    [1.0; 2.0; 3.0]

//Usage
withTimeout longFn 2000 [nan; nan; nan]

我不熟悉这个库,如果它实现了协作取消(例如,通过Task Parallel library->
CancellationToken
),那么它应该很容易。否则,据我所知,几乎不可能在同一个过程中安全地中止工作。