R中约束向量的Optmize
试图重现R中Excel的结果。试图优化手机计划组合,使其超过客户群的预期使用率。目标是为in.each找到一个使pool.cost函数最小化的向量。但是,我需要添加一个限制,即总共只有17060个客户。也就是说:R中约束向量的Optmize,r,optimization,constraints,R,Optimization,Constraints,试图重现R中Excel的结果。试图优化手机计划组合,使其超过客户群的预期使用率。目标是为in.each找到一个使pool.cost函数最小化的向量。但是,我需要添加一个限制,即总共只有17060个客户。也就是说: sum(in.each) == 17060) == TRUE 已尝试优化,但不确定如何添加约束。我们还研究了带有框约束的optim,但仍然不确定这些约束将添加到何处。谢谢你的帮助 #Starting group Allocations in.each = c(0,8000,0
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
L-BFGS-B仅适用于边界约束问题。你可能想考虑使用约束的NLP求解器。但是,由于解决方案不是二进制的,如何解决我的特定问题仍然是不确定的。事实上,那个帖子对我来说似乎不太相关。Scipy.optimize有几个受约束的nlp解算器。检查文档。准确地说:COBYLA(仅限不等式约束)和SLSQP。感谢注释。如果我能够完成我的目标,我将研究这些问题并发布解决方案。
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."