选择R中的约束向量

时间:2017-11-26 20:15:43

标签: r optimization constraints

尝试在R中重现Excel中的结果。尝试优化手机计划组合而不是预期使用一组客户。目标是为in.each找到一个最小化pool.cost函数的向量。但是,我需要添加总共只有17,060个客户的约束。也就是说:

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

尝试过优化,但不确定如何添加约束。还调查了带有框限制的optim,但又不确定这些将被添加到何处。谢谢你的帮助!

#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

输出

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."

0 个答案:

没有答案