-1

(edited) I've tried to repeat an example found in the Internet (the code is below). Unfortunately, I had an error: "Error in h[1] <- x[1] - 900 : replacement has length zero" The task is to minimize a function given equality and inequality constraints:

# To minimize:  -(0.653*x[1] + 0.234*x[1]*x[1]+ 0.437*x[2]
# + 0.769*x[3] + 0.453*x[4] + 0.744*x[5] + 0.476*x[5]*x[5])

# Equality constraint: x[1] + x[2] + x[3] + x[4] + x[5] = 2600

# Inequality constraints:
# x[1] > 900      # x[4] > 175
# x[1] < 1100     # x[4] < 225
# x[2] > 400      # x[5] > 295
# x[2] < 600      # x[5] < 305
# x[3] > 250
# x[3] < 350

fn <- function(x) {
  -(0.653 * x[1]+ 0.234 * x[1] * x[1]+ 0.437 * x[2] + 0.769 * x[3] +
      0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5] * x[5])
}

heq <- function(x) {
  h <- rep(NA, 1)
  h[1] <- x[1] + x[2] + x[3] + x[4] +x[5] - 2600
  h
  }

hin <- function(x) {
  h <- rep(NA, 1)
  h[1] <- x[1] - 900
  h[2] <- 1100 - x[1]
  h[3] <- x[2] - 400
  h[4] <- 600 - x[2]
  h[5] <- x[3] - 250
  h[6] <- 350 - x[3]
  h[7] <- x[4] - 175
  h[8] <- 225 - x[4]
  h[9] <- x[5] - 295
  h[10] <- 305 - x[5]
  h
}

ans <- auglag(par=NULL,fn=fn, gr=NULL,hin=hin, heq=heq)
kimsergeo
  • 1
  • 1
  • 1
    Did you check the examples in the `?auglag` help page? Just requesting examples really isn't a type of "question" that's welcomed here. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 05 '19 at 20:25

1 Answers1

0

The problem is that you did not enter a initial starting point.

auglag(par = 1:5, fn = fn, hin = hin, heq = heq)

Rex
  • 96
  • 6