2014-04-14 4 views
1

아주 복잡한 기능을 최소화해야합니다. 최소화를 위해 Extreme Optimization Library의 NonLinearProgram을 사용합니다. 전역 적 최소값을 찾을 방법이 없기 때문에 다른 시작점을 사용하고 "가장 좋은 최소값"을 선택합니다. 내 문제는 평가판에 매우 오랜 시간이 걸리는 시작점이있을 수 있다는 것입니다. 몇 가지 일반적인 방법이 F #이나 Extreme Optimization의 특별한 방법으로 평가를 중단하고 10 분 후에 말하게하고 [nan; 나노; 나노; 나노; 나노; 뒤로]?f # break function evaluation

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]) 
당신은 async { }과 기능을 포장 및 시간 제한과 함께 RunSynchronously에 그것을 전달할 수
+0

Parallel Library ->'CancellationToken') 그러면 쉽습니다. 그렇지 않으면, 내가 아는 한 동일한 과정에서 작업을 안전하게 중지하는 것이 불가능합니다. –

답변

3

: 작업을 통해, 그것은 (예를 들어 협력 취소를 구현하는 경우 나, 도서관에 익숙하지 않다

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]