3

Attempting to reproduce results from Excel in R. Trying to optimize a portfolio of cell phone plans over expected usage of a pool of customers. Goal is to find a vector for in.each that minimizes the pool.cost function. However, I need to add the constraint that there are only 17,060 customers total. That is to say that:

sum(in.each) == 17060) == TRUE

Have tried the optimize, but unsure of how to add the constraints. Have also looked into the optim with box constraints, but again not sure where these would be added. Thanks for the help!

#Starting group Allocations
    in.each = c(0,8000,0,8000,0,1260)
#Starting weights
start.weights <- in.each/sum(in.each)
#Function to Minimize
pool.cost <- function(in.each){

    #Fixed Pool Fee
    pool.fee = 4
    #Get group mean
    group.mean = sum(problem.2.a.usage$mean*problem.2.a.usage$In.Group)
    #Get group std
    group.std = sqrt(sum((((problem.2.a.usage$std)^2)* problem.2.a.usage$In.Group)))
    #Get group fixed cost
    group.F = sum(crossprod(in.each, (problem.2.a.plans$F+pool.fee)))
    #Get group base mins 
    group.B = sum(crossprod(in.each, problem.2.a.plans$B))
    group.alt_B = (group.B - group.mean) / group.std
    #Get group overage cost
    group.delta = sum(crossprod(in.each,problem.2.a.plans$delta))/sum(in.each)
    #Iota: Group Norm Std Dis
    group.iota = pnorm(group.alt_B)

    first =  (group.delta * group.std) / sqrt(2*pi) * 
                exp(-.5*((group.B-group.mean)/group.std)^2)

    second = (1-group.iota)*group.delta*(group.mean-group.B)

    #Calulate Total Pool Cost
    pool.cost <- roud(group.F + first + second, digits = 2)

    return(pool.cost)
}


#Example of Pool Cost (objective function to minimize)
-pool.cost(in.each)

#Realize without constrains this will just return max interval as well as not a vetor
optimize(pool.cost, interval = c(0:100000))

#Unsure of where to add constraing factors
out <- optim(par     =  in.each,  #Must add to sum(problem.2.a.usage$In.Group)
             fn      = pool.cost,
             method  = "L-BFGS-B",
             lower   = 0,
             upper   = 1)

#Visualise output
opt.weights <- out$par / sum(out$par)
pie(opt.weights, problem.2.a.plans$Plan.ID)
pie(start.weights, problem.2.a.plans$Plan.ID)

#Added and get answer, but not min
constraints <- function(in.each) {
    return(sum(in.each) - sum(problem.2.a.usage$In.Group))
}

S <- slsqp(in.each, pool.cost, hin = constraints,
           nl.info = TRUE, control = list(xtol_rel = 1e-8))
S

Output

Call:
nloptr(x0 = x0, eval_f = fn, eval_grad_f = gr, lb = lower, ub = upper, 
    eval_g_ineq = hin, eval_jac_g_ineq = hinjac, eval_g_eq = heq,     eval_jac_g_eq = heqjac, opts = opts)


Minimization using NLopt version 2.4.2 

NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was 
reached. )

Number of Iterations....: 1 
Termination conditions:  stopval: -Inf  xtol_rel: 1e-08 maxeval: 1000   ftol_rel: 0 ftol_abs: 0 
Number of inequality constraints:  1 
Number of equality constraints:    0 
Optimal value of objective function:  1600867.4 
Optimal value of controls: 0 8000 0 8000 0 1260


$par
[1]    0 8000    0 8000    0 1260

$value
[1] 1600867

$iter
[1] 1

$convergence
[1] 4

$message
[1] "NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was reached."
Dannellyz
  • 31
  • 2

0 Answers0