1

I want to minimize function

f <- function(u){ return(-(1+u[1]+u[2]+u[3]+u[4])) } 

with gradient grad

And I have constraints:

1) u[1]+u[2]+u[3]+u[4] = 1

2) 0<=u[1]<=1, 0<=u[2]<=1, 0<=u[3]<=1, 0<=u[4]<=1

How to make it correctly? I can make it only for 2 constraint

optim(par=c(0,0,0,0), fn=f,lower=c(0, 0, 0, 0), upper=c(1, 1, 1, 1),method="L-BFGS-B") 

But 1 constraint is not true in this case

4
  • Thank you, but its incorrect too because if i make lower=c(0.5, 0.5, 0.5, 0.5) result will be 0.5 0.5 0.5 0.5 (1 constraint will be wrong) Commented Feb 1, 2020 at 18:30
  • I need only way to add 1 constraint Commented Feb 1, 2020 at 18:33
  • 1
    Since the sum of the u's equals 1because of (1) the objective function, f, equals -2 regardless of the u values. Commented Feb 1, 2020 at 19:48
  • @G.Grothendieck sure, i know it. it was comment to idea to make f = (u1+u2+u3+u4-1) and minimize it. In my task in zero iteration capital = 1 and u's is profitability.(This comment was deleted) Commented Feb 2, 2020 at 6:02

1 Answer 1

1

Maybe you can try fmincon from package pracma like below

pracma::fmincon(c(0,0,0,0), f, gr = grad, Aeq = cbind(1,1,1,1), beq = 1, lb = c(0,0,0,0), ub = c(1,1,1,1)) 
Sign up to request clarification or add additional context in comments.

3 Comments

It seems like true solution but i have Argument 'Aeq' must be a matrix with length(x0) columns. Though length(x0) = length(Aeq) = 4
Got it. Aeq = cbind(1,1,1,1)
@ИльшатМурзурбеков Great that you fixed it. Does this address your question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.