The overview of the problem is as follows:

Given observed data x(1)....x(n) and a known fixed 'target' T with tolerance E, solve for parameters b0, b1, & b2, which satisfy:

*abs{ T - sum[i=1 to n] exp(b0+b1*x(i)+b2*x(i)^2)*x(i) }<E*

and minimise

*sum[i=1 to n] [(exp(b0+b1*x(i)+b2*x(i)^2))^2]*

with the constraint that the sum of the exp(b0+b1*x(i)+b2*x(i)^2) terms equals n,

**i.e. the mean of the exp(b0+b1 x(i)+b2x(i)^2) terms equals 1.**

I am trying to solve the following problem in R using Nloptr:

Objective is to maximise effective sample size (ESS), so I have been attempting to minimise the

inverse of ESS:

```
i.e: obj.func <- function(n, wi) {
ESS<- sum(wi^2)
return(ESS)}
```

Using simulated data as follows:

```
x1 <- runif(5)
n <- 5
y <- function(x1, b0, b1, b2) {
Y <- b0 + b1*x1 + b2*(x1^2)
return(Y)}
ym <- y(x1, b0=1.3,b1=-0.5,b2=0.2)
w <- function(ym, n){n * (exp((ym)) / sum(exp(ym))) } *#Function for weight*
wi <- w(ym, n)
```

We need to do this under the following constraint:

```
con <- function(x1, wi, n){
abs((weighted.mean(x1, wi))-(mean(x1))) <= 0.01 *#where E <- 0.01*}
```

I think that I am unable to use nloptr to complete this minimisation to get the optimum values of the b variables as the objective function and constraint function as the functions are not in the same terms. (constraint relies on x1 as well as n and wi)

Does anyone have any suggestions on how to solve this optimization problem? Or how I can get around my issues with nloptr? I have looked at the 'bb' package but this does not seem suitable.