I have the following function and would like to optimise parameters x and y in order to maximise the output. I am trying to do that using optimise.
EDIT
Thanks to the comments here I am able to optimise using Rsolnp::solnp.
But now I get error when I add upper and lower bounds.
See example below
Ojective function:
my_fn <- function(x) { 
z <- ifelse(x[1] < 1000000, 0, 
     ifelse(x[1] < 19500000, x[1] * 0.0175,
     ifelse(x[1] < 20500000, x[1] * 0.0190,
     ifelse(x[1] < 21500000, x[1] * 0.0220,
     ifelse(x[1] < 22500000, x[1] * 0.0270,
            x[1] * 0.0340))))) +
    ifelse(x[2] < 750000, 0, 
    ifelse(x[2] < 15750000, x[2] * 0.0100,
    ifelse(x[2] < 16500000, x[2] * 0.0150,
    ifelse(x[2] < 17250000, x[2] * 0.0275,
    ifelse(x[2] < 18000000, x[2] * 0.0375,
    ifelse(x[2] < 18750000, x[2] * 0.0450,
           x[2] * 0.0550)))))) + 
return(-z) # because I want to maximise
}
Equality function:
my_fn_equal <- function(x) {
        z1 = x[1] + x[2]
        return(z1)
    }
I am using the following:
library(Rsolnp)
solnp(pars = c(1000000, 750000),   # initial values
      fun = my_fn,                 # objective function
      eqfun = my_fn_equal,         # equality function
      eqB = c(17000000)            # euqlity constraint
)
And it works fine. But once I add lower and upper bounds I start to get errors:
solnp(pars = c(1000000, 750000),        # initial values
      fun = my_fn,                       # objective function
      eqfun = my_fn_equal,               # equality function
      eqB = c(17000000),                 # euqlity constraint
      LB = c(1000000, 750000),           # lower bound for parameters 
      UB = c(16000000, 16000000)         # upper bound for parameters (I chose 16M randomly)
            )
Result with Problem Inverting Hessian:
solnp-->The linearized problem has no feasible
solnp-->solution.  The problem may not be feasible.
Iter: 1 fn: -25000.0000  Pars:  1000000.00000  750000.00000
solnp--> Solution not reliable....Problem Inverting Hessian.
$pars
[1] 1000000  750000
$convergence
[1] 2
$values
[1] -25000 -25000
$lagrange
[1] 0
$hessian
     [,1] [,2]
[1,]    1    0
[2,]    0    1
$ineqx0
NULL
$nfuneval
[1] 7
$outer.iter
[1] 1
$elapsed
Time difference of 0.1664431 secs
$vscale
[1]    25000 15250000        1        1
The constraints are:
# x[1] >= 1000000
# x[2] >= 750000
# x[1] + x[2] = 17000000
Why do I get these errors when I select bounds? What do they mean?
Also, it seems that everytime I change my initial values, the optimised values change. Why don't I get the same parameters that maximise my output everytime?
Thanks!