Optimizing Non-Linear Function using nloptr - Unexpected Results - optimization

When I try to maximize my objective function using nloptr, it is terminating on the initial values that I have set, which I think is very unlikely to return the maximum.
For some background, I am trying to allocate 80M of investment (x) between 114 products in a way that maximizes revenue (y). Predicted revenue for each product and investment level is based off a two-part model i.e. it is the result of multiplying the predicted probability of any revenue being generated, by the predicted level of revenue, conditional on revenue being generated. Each line of the objective function represents the fitted model for each product.
library(nloptr)
# objective function
eval_f0 <- function(x){
return(-(plogis(0.872 + 0.0000471*x[1]-1.04483)*exp(8.459+0.468*log(x[1])+1.07743) +
plogis(0.872 + 0.0000471*x[2]-1.04483)*exp(8.459+0.468*log(x[2])+1.07743) +
plogis(0.872 + 0.0000471*x[3]-1.04483)*exp(8.459+0.468*log(x[3])+1.07743) +
plogis(0.872 + 0.0000471*x[4]-1.04483)*exp(8.459+0.468*log(x[4])+1.07743) +
plogis(0.872 + 0.0000471*x[5]-1.04483)*exp(8.459+0.468*log(x[5])+1.07743) +
plogis(0.872 + 0.0000471*x[6]-1.04483)*exp(8.459+0.468*log(x[6])+1.07743) +
plogis(0.872 + 0.0000471*x[7]-1.04483)*exp(8.459+0.468*log(x[7])+1.07743) +
plogis(0.872 + 0.0000471*x[8]-1.04483)*exp(8.459+0.468*log(x[8])+1.07743) +
plogis(0.872 + 0.0000471*x[9]-1.04483)*exp(8.459+0.468*log(x[9])+1.07743) +
plogis(0.872 + 0.0000471*x[10]-1.04483)*exp(8.459+0.468*log(x[10])+1.07743) +
plogis(0.872 + 0.0000471*x[11]-1.04483)*exp(8.459+0.468*log(x[11])+1.07743) +
plogis(0.872 + 0.0000471*x[12]-1.04483)*exp(8.459+0.468*log(x[12])+1.07743) +
plogis(0.872 + 0.0000471*x[13]-1.04483)*exp(8.459+0.468*log(x[13])+1.07743) +
plogis(0.872 + 0.0000471*x[14]-1.04483)*exp(8.459+0.468*log(x[14])+1.07743) +
plogis(0.872 + 0.0000471*x[15]-1.04483)*exp(8.459+0.468*log(x[15])+1.07743) +
plogis(0.872 + 0.0000471*x[16]-1.04483)*exp(8.459+0.468*log(x[16])+1.07743) +
plogis(0.872 + 0.0000471*x[17]-1.04483)*exp(8.459+0.468*log(x[17])+1.07743) +
plogis(0.872 + 0.0000471*x[18]-1.04483)*exp(8.459+0.468*log(x[18])+1.07743) +
plogis(0.872 + 0.0000471*x[19]-1.04483)*exp(8.459+0.468*log(x[19])+1.07743) +
plogis(0.872 + 0.0000471*x[20]-1.04483)*exp(8.459+0.468*log(x[20])+1.07743) +
plogis(0.872 + 0.0000471*x[21]-1.04483)*exp(8.459+0.468*log(x[21])+1.07743) +
plogis(0.872 + 0.0000471*x[22]-1.04483)*exp(8.459+0.468*log(x[22])+1.07743) +
plogis(0.872 + 0.0000471*x[23]-1.04483)*exp(8.459+0.468*log(x[23])+1.07743) +
plogis(0.872 + 0.0000471*x[24]-1.04483)*exp(8.459+0.468*log(x[24])+1.07743) +
plogis(0.872 + 0.0000471*x[25]-1.04483)*exp(8.459+0.468*log(x[25])+1.07743) +
plogis(0.872 + 0.0000471*x[26]-1.04483)*exp(8.459+0.468*log(x[26])+1.07743) +
plogis(0.872 + 0.0000471*x[27]-1.04483)*exp(8.459+0.468*log(x[27])+1.07743) +
plogis(0.872 + 0.0000471*x[28]-1.04483)*exp(8.459+0.468*log(x[28])+1.07743) +
plogis(0.872 + 0.0000471*x[29]-1.04483)*exp(8.459+0.468*log(x[29])+1.07743) +
plogis(0.872 + 0.0000471*x[30]-1.04483)*exp(8.459+0.468*log(x[30])+1.07743) +
plogis(0.872 + 0.0000471*x[31]-1.04483)*exp(8.459+0.468*log(x[31])+1.07743) +
plogis(0.872 + 0.0000471*x[32]-1.04483)*exp(8.459+0.468*log(x[32])+1.07743) +
plogis(0.872 + 0.0000471*x[33]-1.04483)*exp(8.459+0.468*log(x[33])+1.07743) +
plogis(0.872 + 0.0000471*x[34]-1.04483)*exp(8.459+0.468*log(x[34])+1.07743) +
plogis(0.872 + 0.0000471*x[35]-1.04483+0.289)*exp(8.459+0.468*log(x[35])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[36]-1.04483+0.289)*exp(8.459+0.468*log(x[36])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[37]-1.04483+0.289)*exp(8.459+0.468*log(x[37])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[38]-1.04483+0.289)*exp(8.459+0.468*log(x[38])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[39]-1.04483+0.289)*exp(8.459+0.468*log(x[39])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[40]-1.04483+0.289)*exp(8.459+0.468*log(x[40])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[41]-1.04483+0.289)*exp(8.459+0.468*log(x[41])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[42]-1.04483+0.289)*exp(8.459+0.468*log(x[42])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[43]-1.04483+0.289)*exp(8.459+0.468*log(x[43])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[44]-1.04483+0.289)*exp(8.459+0.468*log(x[44])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[45]-1.04483+0.289)*exp(8.459+0.468*log(x[45])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[46]-1.04483+0.289)*exp(8.459+0.468*log(x[46])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[47]-1.04483+0.289)*exp(8.459+0.468*log(x[47])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[48]-1.04483+0.289)*exp(8.459+0.468*log(x[48])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[49]-1.04483+0.289)*exp(8.459+0.468*log(x[49])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[50]-1.04483+0.289)*exp(8.459+0.468*log(x[50])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[51]-1.04483+0.289)*exp(8.459+0.468*log(x[51])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[52]-1.04483+0.289)*exp(8.459+0.468*log(x[52])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[53]-1.04483+0.289)*exp(8.459+0.468*log(x[53])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[54]-1.04483+0.289)*exp(8.459+0.468*log(x[54])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[55]-1.04483+0.289)*exp(8.459+0.468*log(x[55])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[56]-1.04483+0.289)*exp(8.459+0.468*log(x[56])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[57]-1.04483+0.289)*exp(8.459+0.468*log(x[57])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[58]-1.04483+0.289)*exp(8.459+0.468*log(x[58])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[59]-1.04483+0.289)*exp(8.459+0.468*log(x[59])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[60]-1.04483+0.289)*exp(8.459+0.468*log(x[60])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[61]-1.04483+0.289)*exp(8.459+0.468*log(x[61])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[62]-1.04483+0.289)*exp(8.459+0.468*log(x[62])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[63]-1.04483+0.289)*exp(8.459+0.468*log(x[63])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[64]-1.04483+0.289)*exp(8.459+0.468*log(x[64])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[65]-1.04483+0.289)*exp(8.459+0.468*log(x[65])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[66]-1.04483+0.289)*exp(8.459+0.468*log(x[66])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[67]-1.04483+0.289)*exp(8.459+0.468*log(x[67])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[68]-1.04483+0.289)*exp(8.459+0.468*log(x[68])+1.07743-0.83) +
plogis(0.872 + 0.0000471*x[69]-1.04483+1.081)*exp(8.459+0.468*log(x[69])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[70]-1.04483+1.081)*exp(8.459+0.468*log(x[70])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[71]-1.04483+1.081)*exp(8.459+0.468*log(x[71])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[72]-1.04483+1.081)*exp(8.459+0.468*log(x[72])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[73]-1.04483+1.081)*exp(8.459+0.468*log(x[73])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[74]-1.04483+1.081)*exp(8.459+0.468*log(x[74])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[75]-1.04483+1.081)*exp(8.459+0.468*log(x[75])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[76]-1.04483+1.081)*exp(8.459+0.468*log(x[76])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[77]-1.04483+1.081)*exp(8.459+0.468*log(x[77])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[78]-1.04483+1.081)*exp(8.459+0.468*log(x[78])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[79]-1.04483+1.081)*exp(8.459+0.468*log(x[79])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[80]-1.04483+1.081)*exp(8.459+0.468*log(x[80])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[81]-1.04483+1.081)*exp(8.459+0.468*log(x[81])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[82]-1.04483+1.081)*exp(8.459+0.468*log(x[82])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[83]-1.04483+1.081)*exp(8.459+0.468*log(x[83])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[84]-1.04483+1.081)*exp(8.459+0.468*log(x[84])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[85]-1.04483+1.081)*exp(8.459+0.468*log(x[85])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[86]-1.04483+1.081)*exp(8.459+0.468*log(x[86])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[87]-1.04483+1.081)*exp(8.459+0.468*log(x[87])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[88]-1.04483+1.081)*exp(8.459+0.468*log(x[88])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[89]-1.04483+1.081)*exp(8.459+0.468*log(x[89])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[90]-1.04483+1.081)*exp(8.459+0.468*log(x[90])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[91]-1.04483+1.081)*exp(8.459+0.468*log(x[91])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[92]-1.04483+1.081)*exp(8.459+0.468*log(x[92])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[93]-1.04483+1.081)*exp(8.459+0.468*log(x[93])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[94]-1.04483+1.081)*exp(8.459+0.468*log(x[94])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[95]-1.04483+1.081)*exp(8.459+0.468*log(x[95])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[96]-1.04483+1.081)*exp(8.459+0.468*log(x[96])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[97]-1.04483+1.081)*exp(8.459+0.468*log(x[97])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[98]-1.04483+1.081)*exp(8.459+0.468*log(x[98])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[99]-1.04483+1.081)*exp(8.459+0.468*log(x[99])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[100]-1.04483+1.081)*exp(8.459+0.468*log(x[100])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[101]-1.04483+1.081)*exp(8.459+0.468*log(x[101])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[102]-1.04483+1.081)*exp(8.459+0.468*log(x[102])+1.07743-0.637) +
plogis(0.872 + 0.0000471*x[103]-1.04483-1.978)*exp(8.459+0.468*log(x[103])+1.07743+0.336) +
plogis(0.872 + 0.0000471*x[104]-1.04483-1.978)*exp(8.459+0.468*log(x[104])+1.07743+0.336) +
plogis(0.872 + 0.0000471*x[105]-1.04483-1.978)*exp(8.459+0.468*log(x[105])+1.07743+0.336) +
plogis(0.872 + 0.0000471*x[106]-1.04483-1.978)*exp(8.459+0.468*log(x[106])+1.07743+0.336) +
plogis(0.872 + 0.0000471*x[107]-1.04483+0.289-1.978)*exp(8.459+0.468*log(x[107])+1.07743-0.83+0.336) +
plogis(0.872 + 0.0000471*x[108]-1.04483+0.289-1.978)*exp(8.459+0.468*log(x[108])+1.07743-0.83+0.336) +
plogis(0.872 + 0.0000471*x[109]-1.04483+0.289-1.978)*exp(8.459+0.468*log(x[109])+1.07743-0.83+0.336) +
plogis(0.872 + 0.0000471*x[110]-1.04483+0.289-1.978)*exp(8.459+0.468*log(x[110])+1.07743-0.83+0.336) +
plogis(0.872 + 0.0000471*x[111]-1.04483+1.081-1.978)*exp(8.459+0.468*log(x[111])+1.07743-0.637+0.336) +
plogis(0.872 + 0.0000471*x[112]-1.04483+1.081-1.978)*exp(8.459+0.468*log(x[112])+1.07743-0.637+0.336) +
plogis(0.872 + 0.0000471*x[113]-1.04483+1.081-1.978)*exp(8.459+0.468*log(x[113])+1.07743-0.637+0.336) +
plogis(0.872 + 0.0000471*x[114]-1.04483+1.081-1.978)*exp(8.459+0.468*log(x[114])+1.07743-0.637+0.336)) )
}
# constraint function
eval_g0 <- function(x)
{
return(x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+x[7]+x[8]+x[9]+x[10]+x[11]+x[12]+x[13]+x[14]+x[15]+x[16]+x[17]+x[18]+x[19]+x[20]+x[21]+x[22]+x[23]+x[24]+x[25]+x[26]+x[27]+x[28]+x[29]+x[30]+x[31]+x[32]+x[33]+x[34]+x[35]+x[36]+x[37]+x[38]+x[39]+x[40]+x[41]+x[42]+x[43]+x[44]+x[45]+x[46]+x[47]+x[48]+x[49]+x[50]+x[51]+x[52]+x[53]+x[54]+x[55]+x[56]+x[57]+x[58]+x[59]+x[60]+x[61]+x[62]+x[63]+x[64]+x[65]+x[66]+x[67]+x[68]+x[69]+x[70]+x[71]+x[72]+x[73]+x[74]+x[75]+x[76]+x[77]+x[78]+x[79]+x[80]+x[81]+x[82]+x[83]+x[84]+x[85]+x[86]+x[87]+x[88]+x[89]+x[90]+x[91]+x[92]+x[93]+x[94]+x[95]+x[96]+x[97]+x[98]+x[99]+x[100]+x[101]+x[102]+x[103]+x[104]+x[105]+x[106]+x[107]+x[108]+x[109]+x[110]+x[111]+x[112]+x[113]+x[114]-80000)
}
# Set optimization options.
opts <- list( "algorithm"= "NLOPT_GN_ISRES",
"xtol_rel"= 1.0e-7,
"maxeval"= 760000,
"print_level" = 0 )
# Solve
res1 <- nloptr( x0=c(500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500),
eval_f=eval_f0,
lb = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
ub = c(30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000),
eval_g_ineq = eval_g0,
opts = opts)
print(res1)
The results are below:
Minimization using NLopt version 2.4.2
NLopt solver status: 5 ( NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was
reached. )
Number of Iterations....: 760000
Termination conditions: xtol_rel: 0.000000000000001 maxeval: 760000
Number of inequality constraints: 1
Number of equality constraints: 0
Current value of objective function: -4536536.34788097
Current value of controls: 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100
Could anyone take a look at my code and let me know whether I am doing anything wrong? Do I need to specify more iterations?
Edit: As #AirSquid suggested, I tried cutting the number of options down to 6 instead and that seems to have worked. So I need to understand why it doesn't work for a larger number of options

This is an extremely easy linear constrained problem. A solver like IPOPT solves this in no time: 0.004 seconds, 8 iterations (I used your exact formulation, with a lower bound on the variables of 0.0001 to protect the log -- I hope I transcribed the problem correctly). Note that nloptr contains largely somewhat simple algorithms. So I suggest picking a better solver.
This is Ipopt version 3.14.6, running with linear solver ma27.
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 114
Number of nonzeros in Lagrangian Hessian.............: 114
Total number of variables............................: 114
variables with only lower bounds: 0
variables with lower and upper bounds: 114
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 1
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 -1.6299318e+07 0.00e+00 5.96e+01 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 -1.9108212e+07 0.00e+00 5.08e+01 3.0 9.26e+04 - 1.00e+00 2.46e-01f 1
2 -1.9489289e+07 0.00e+00 4.44e+01 2.0 1.58e+03 - 1.00e+00 1.44e-01f 1
3 -2.0776301e+07 0.00e+00 2.03e+01 1.4 9.55e+02 - 1.00e+00 1.00e+00f 1
4 -2.0890126e+07 0.00e+00 6.06e+00 -0.1 9.27e+02 - 1.00e+00 8.42e-01f 1
5 -2.0908516e+07 0.00e+00 5.78e-01 -1.4 5.14e+02 - 9.89e-01 1.00e+00f 1
6 -2.0908784e+07 0.00e+00 8.26e-03 -3.0 7.22e+01 - 9.97e-01 1.00e+00f 1
7 -2.0908784e+07 0.00e+00 6.72e-06 -4.9 1.05e+00 - 9.96e-01 1.00e+00f 1
8 -2.0908784e+07 0.00e+00 1.54e-10 -10.7 2.17e-04 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 8
(scaled) (unscaled)
Objective...............: -6.4238210381431347e+06 -2.0908784212199997e+07
Dual infeasibility......: 1.5371171002698247e-10 5.0031359167058148e-10
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 5.5985347935841295e-10 1.8222574260471783e-09
Overall NLP error.......: 5.5985347935841295e-10 1.8222574260471783e-09
Number of objective function evaluations = 9
Number of objective gradient evaluations = 9
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 9
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 9
Number of Lagrangian Hessian evaluations = 8
Total seconds in IPOPT = 0.004
EXIT: Optimal Solution Found.

Related

Rounding Non-LinearExpr with google or-tools SAT solver

Using CP-SAT of google or-tools I'm trying to write this constraint:
q >= (50x + 100y + 150z + 200k + 250p + 300v) / (x + y + z + k + p + v)
Where q is a simple integer.
The thing is I need to round the right side of the equation (let's call it expression) as follows:
if(expression < 75) {
expression = 50;
} else if(expression < 125) {
expression = 100;
} else if(expression < 175) {
expression = 150;
} else if(expression < 225) {
expression = 200;
} else if(expression < 275) {
expression = 250;
} else {
expression = 300;
}
So I need to round the expression
(50x + 100y + 150z + 200k + 250p + 300v) / (x + y + z + k + p + v)
So that it gets one of the following values:
{50, 100, 150, 200, 250, 300}
Let's review 2 cases:
Case 1
q = 180 and expression = 176.
Although the condition 180 >= 176 is true, after rounding up 176 to 200 the tested condition should be 180 >= 200 which is false.
So for q = 180 and expression = 176 I would like the condition to return false.
Case 2
q = 210 and expression = 218.
Although the condition 210 >= 218 is false, after rounding down 218 to 200 the tested condition should be 210 >= 200 which is true.
So for q = 210 and expression = 218 I would like the condition to return true.
I got a great answer here for resolving this challenge over a linear expression but now I need to resolve it for a non-linear expression.
Any suggestions?
Let's rephrase
you have an integer variable e with a value between 0 and 300.
You want to round it to the nearest multiple of 50
if you do:
(e div 50) * 50
you will get the max multiple of 50 less or equal to e
(70 / 50) * 50 -> 50
(99 / 50) * 50 -> 50
(102 / 50) * 50 -> 100
To get a round to nearest, you need to add 25 to e before the division
((e + 25) div 50) * 50
Which will do the correct rounding
((70 + 25) / 50) * 50 -> 50
((99 + 25) / 50) * 50 -> 100
((102 + 25) / 50) * 50 -> 100
with the correct or-tools CP-SAT python code:
numerator = model.NewIntVar(...)
model.Add(numerator == 50x + 100y + 150z + 200k + 250p + 300v)
denom = model.NewIntVar(...)
model.Add(denom == 50x + 100y + 150z + 200k + 250p + 300v)
e = model.NewIntVar(0, 300, 'e')
model.AddDivisionEquality(e, numerator, denom)
shifted_e = model.NewIntVar(25, 325, 'shifted_e')
model.Add(shifted_e == e + 25)
multiple_of_fifty = model.NewIntVar(0, 6, 'multiple_of_fifty')
model.AddDivisionEquality(multiple_of_fifty, shifted_e, 50)
result = model.NewIntVar(0, 300, 'result')
model.Add(result = multiple_of_fifty * 50)
if a and b are positive then
a div b >= q
is equivalent to
a >= q * b
now, your example does not specify how to round (nearest or down)
if you want to round down
q * (x + y + z + k + p + v) <= (50x + 100y + 150z + 200k + 250p + 300v)
If you want to round to nearest, you need to add q / 2 in the right place
q * (x + y + z + k + p + v) <= (50x + 100y + 150z + 200k + 250p + 300v + q / 2)
Now, if you want the other direction
a div b <= q
is equivalent to
a <= q * b + q - 1
The rest of the transformation is the same.

For Statement in assigning random values to integers in VBA

NN1 = Int((500 - 100 + 1) * Rnd + 100)
NN2 = Int((500 - 100 + 1) * Rnd + 100)
NN3 = Int((500 - 100 + 1) * Rnd + 100)
NN4 = Int((500 - 100 + 1) * Rnd + 100)
I assume that we can reduce this into
For i = 1 to 4
"NN" & i = Int((500 - 100 + 1) * Rnd + 100)
Next i
However, I am getting a compilation error. How do I rectify this?
Dim NN() As Integer
For i = 1 To 4
NN(i) = Int((500 - 100 + 1) * Rnd + 100)
Next i
I would suggest using an Array instead of multiple Integers

Determine time complexity of arithmetic progression

I am novice in analysing time complexity.some one can help me with the time complexity of below algorithm?
public void test(int n)
{
for(int i=1;i<=n;i=i+2)
{
for(int j=1;j<=i;j++)
{}
}
}
outer loop will run n/2 times.Inner loop will run (1+3+5+7+9...n) times.
what will be time complexity of inner loop and how can we calculate sum of such arithmitic progression?
Assume n is odd. Then n = 2k + 1 for some k. Now
1 + 3 + … + n
= 1 + 3 + … + 2k+1
= (1 + 3 + … + 2k + 1) + (1 + 1 + … + 1) - (1 + 1 + … + 1)
= (1 + 1) + (3 + 1) + … + (2k + 1 + 1) - (1 + 1 + … + 1)
= 2 + 4 + … + 2k+2 - (1 + 1 + … + 1)
= 2(1 + 2 + … + k+1) - (1 + 1 + … + 1)
= 2(k+1)(k+2)/2 - (k+1)
= k^2 + 3k + 2 - k - 1
= k^2 + 2k + 1
= (k+1)^2
= (n+1)^2/4
We can test a few terms...
n f(n) Series Sum
1 1 1 = 1
3 4 1 + 3 = 4
5 9 1 + 3 + 5 = 9
7 16 1 + 4 + 5 + 7 = 16
Looks like it checks out.

VBA - Bypass Application.evaulate limit

So I got an string that contains numeric values that I want to calculate into a sum. I tried to use Application.evaluate but I noticed that there is a limit on the amount of characters I can use in the calculation. Is there a way to bypass this or fix it so I can calculate it? I'm thinking of writing my own "calculator" but it feels like a bit to much and there must be an easier way to do it?
0 /(2^16) + 3 /(2^16) + 53 /(2^16) + 139573 /(2^16) + 1844259
/(2^16) + 4005892 /(2^16) + 7546905 /(2^16) + 14629148 /(2^16) +
15078164 /(2^16) + 7477520 /(2^16) + 3158785/(2^16) + 1258795/(2^16)
+ 538914/(2^16) + 396079/(2^16) + 12305/(2^16) + 1555/(2^16) + 258/(2^16) + 8/(2^16) + 4/(2^16)
Another example:
1215 /( 7168 + 5922 + 5390 + 7034 + 10410 + 10494 +
12113 + 6607 + 448 + 67 + 2996 + 4103 + 15581 + 18432 +
7299 + 1215 + 177 + 11 + 3 + 0 )*100
Could you stick it in a cell and read it back?
Sub test()
Dim s As String
s = "0 /(2^16) + 3 /(2^16) + 53 /(2^16) + 139573 /(2^16) + 1844259 /(2^16) + 4005892 /(2^16) + 7546905 /(2^16) + 14629148 /(2^16) + 15078164 /(2^16) + 7477520 /(2^16) + 3158785/(2^16) + 1258795/(2^16) + 538914/(2^16) + 396079/(2^16) + 12305/(2^16) + 1555/(2^16) + 258/(2^16) + 8/(2^16) + 4/(2^16)"
Sheet1.Range("A1").Formula = "=" & s
Debug.Print Sheet1.Range("A1").Value
Sheet1.Range("A1").ClearContents
End Sub
Given that addition is commutative, can you not calculate half your expression (up to 3158785/(2^16)), assign the result to a variable, then calculate the rest of the expression and add that to the previous result?

Solving recurrence relation T(n) = T(n-1) + n

I see from a previous answer to this question, the person gave:
T(n) = T(n-2) + n-1 + n
T(n) = T(n-3) + n-2 + n-1 + n
T(n) = T(n-k) +kn - k(k-1)/2
I'm not understanding completely the third line. I can see they may have derived it from arithmetic series formula summation of 1/2n(n+1)? But how did they get kn and the minus sign in front of k(k-1)/2?
starting from:
T(n) = T(n-2) + n-1 + n
we may rewrite it as follows:
T(n) = T(n-2) + 2n - 1
The second formula says:
T(n) = T(n-3)+n-2+n-1+n
let us convert it the same way we do with the first one:
T(n) = T(n-3)+n+n+n-2-1
T(n) = T(n-3)+3n-2-1
By expanding more terms, we notice that the number subtracted from n in the recursive term:T(n-3) is always the same as the number multiplied by n. we may rewrite it as follows:
T(n) = T(n-k)+kn+...
we also notice that -2 -1 is the arithmetic series but negated and stars from k-1. the arithmetic of k-1 is (k-1)*k/2 just like n(n+1)/2. so the relation would be
T(n) = T(n-k)+kn-(k-1)*k/2 or T(n) = T(n-k)+kn-k*(k-1)/2
Hope this help ;)
The k(k-1)/2 term is just the sum of the numbers 0 to k-1. You can see why you need to subtract it from the following calculation:
T(n) =
T(n-k) + n + (n-1) + (n-2) + ... + (n-(k-1)) =
T(n-k) + (n-0) + (n-1) + (n-2) + ... + (n-(k-1)) =
T(n-k) + n + n + n + ... + n - 0 - 1 - 2 ... - (k-1) =
T(n-k) + kn - (0 + 1 + 2 + ... + (k-1)) =
T(n-k) + kn - k*(k-1)/2
If you look closly:
T(n) = T(n-2) + n-1 + n = T(n-2) + 2n -1
T(n)= T(n-3) + n-2 + n-1 + n = T(n-3)+ 3n -(2+1)
.
.
.
T(n)= T(n-k) + n-(k-1) + n-(k-2) + ... + n = T(n-k) + K * n + (-1 -2 - ... -(k-2) -(k-1))= T(n-k) + kn - k(k-1)/2
You can use recurrence theorem to demonstrate it