Optimization in R with arbitrary constraints
Asked Answered
P

3

1

I have done it in Excel but need to run a proper simulation in R.

I need to minimize function F(x) (x is a vector) while having constraints that sum(x)=1, all values in x are [0,1] and another function G(x) > G_0.

I have tried it with optim and constrOptim. None of them give you this option.

Psychrometer answered 6/2, 2014 at 18:30 Comment(2)
This is too vague at the moment You need to provide an example for the data and the objective function that represents the complexity of your problem.Ideograph
@IShouldBuyABoat I gave more details under tonytonov's comment below. thanksPsychrometer
A
2

The problem you are referring to is (presumably) a non-linear optimization with non-linear constraints. This is one of the most general optimization problems.

The package I have used for these purposes is called nloptr: see here. From my experience, it is both versatile and fast. You can specify both equality and inequality constaints by setting eval_g_eq and eval_g_ineq, correspondingly. If the jacobians are known explicitly (can be derived analytically), specify them for faster convergence; otherwise, a numerical approximation is used.

Use this list as a general reference to optimization problems.

Ammoniacal answered 6/2, 2014 at 19:26 Comment(4)
Thanks @tonytonov. Looks like nloptr is what I needed. However I cannot figure out how to pass the arguments to the user-defined objective and constraints functions. For instance I my values to be optimized is x, eval_f(x,y,A), eval_g_ineq(x,A) and eval_g_eq(x). A is a matrix. It looks like nloptr doesn't recognize which argument goes to which function.. I will give you an example: I optimize f(x,y,A). x,y - vectors, A - matrix. 1-sum(x)=0 - eval_g_eq. sum(y/3)-sum(y*x) <= 0.Psychrometer
All function definitions should look like function(x) for a single vector x. If you have two vectors, x and y, combine them into c(x, y) before passing to eval_f. The same trick applies to the matrix A.Ammoniacal
Thanks @tonytonov. "..." is for arguments that will be passed to the user-defined objective and constraints functions. In the examples they have eval_f0 <- function( x, a, b ). if I combine x,y and A into one input how I can specify which values to optimize? these functions have additional argumentsPsychrometer
I encourage you to ask this question separately, linking back here. Do not forget to accept one of the proposed answers. In a new question, specify your minimal reproducible problem, and your question will get more attention.Ammoniacal
A
1

Write the set of equations using the Lagrange multiplier, then solve using the R command nlm.

Amazon answered 6/2, 2014 at 21:53 Comment(0)
M
0

You can do this in the OpenMx Package (currently host at the site listed below. Aiming for 2.0 relase on cran this year)

It is a general purpose package mostly used for Structural Equation Modelling, but handling nonlinear constraints.

FOr your case, make an mxModel() with your algebras expressed in mxAlgebras() and the constraints in mxConstraints()

When you mxRun() the model, the algebras will be solved within the constraints, if possible.

http://openmx.psyc.virginia.edu/

Mcalister answered 25/7, 2014 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.