time - f# break function evaluation -
i have minimize quite complicate function. minimization use nonlinearprogram extreme optimization library. since there´s no way find global minimum, use different startpoints , choose, "best minimum". problem there can startpoints, evaluatin can take long time. there general way in f# or special method in extreme optimization, stop evaluation let´s after 10 min , give list [nan; nan; nan; nan; nan; nan] back?
let funcfindpara (startpoint:float list) func = let nlp = new nonlinearprogram(6) // add 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 parameters list.init 6 (fun index -> solution.[index])
you wrap function async { }
, pass runsynchronously
along timeout:
let withtimeout f timeout defaultvalue = try async.runsynchronously((async { return f() }), timeout) :? system.timeoutexception -> defaultvalue let longfn() = system.threading.thread.sleep(5000) [1.0; 2.0; 3.0] //usage withtimeout longfn 2000 [nan; nan; nan]
Comments
Post a Comment