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.

Krantz
  • 1,424
  • 1
  • 12
  • 31
mm441
  • 485
  • 5
  • 13
  • 3
    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. – IRTFM Feb 06 '14 at 18:41
  • @IShouldBuyABoat I gave more details under tonytonov's comment below. thanks – mm441 Feb 07 '14 at 10:29

3 Answers3

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.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • 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. – mm441 Feb 07 '14 at 10:15
  • 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`. – tonytonov Feb 07 '14 at 10:36
  • 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 arguments – mm441 Feb 07 '14 at 11:56
  • 1
    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. – tonytonov Feb 07 '14 at 12:12
1

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

wespiserA
  • 3,131
  • 5
  • 28
  • 36
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/

tim
  • 3,559
  • 1
  • 33
  • 46