R optimization with equality and inequality constraints

后端 未结 3 752
情深已故
情深已故 2020-12-03 15:14

I am trying to find the local minimum of a function, and the parameters have a fixed sum. For example,

Fx = 10 - 5x1 + 2x2 - x3

and the cond

3条回答
  •  执念已碎
    2020-12-03 15:44

    This is actually a linear programming problem, so a natural approach would be to use a linear programming solver such as the lpSolve package. You need to provide an objective function and a constraint matrix and the solver will do the rest:

    library(lpSolve)
    mod <- lp("min", c(-5, 2, -1), matrix(c(1, 1, 1), nrow=1), "=", 15)
    

    Then you can access the optimal solution and the objective value (adding the constant term 10, which is not provided to the solver):

    mod$solution
    # [1] 15  0  0
    mod$objval + 10
    # [1] -65
    

    A linear programming solver should be a good deal quicker than a general nonlinear optimization solver and shouldn't have trouble returning the exact optimal solution (instead of a nearby point that may be subject to rounding errors).

提交回复
热议问题