VBA - Bypass Application.evaulate limit - vba

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?

Related

Optimizing Non-Linear Function using nloptr - Unexpected Results

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.

Subtract in SQL Server code returning NULL

I'm having problems in this SQL server code, even using the ISNULL or COALESCE functions it keeps returning NULL, could someone tell me how to solve it?
CASE WHEN TIPO_X = 'F'
AND TIPO_CALCULOX IN ('N','S','D')
AND TRPR_COD_X IN (7,13,15,17) THEN CASE WHEN EVEN_COD_X IN (623,70623,947,70947,1871,71871,2697,72697,2871,72871,30623,30947,31871,22697,22871,20623,20947,21871) THEN CONVERT(int, (SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
AND MONTH(DT_COMP_X) = #MES)) - CONVERT(int,(SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (22697 + 22871 + 20623 + 20947 + 21871)
AND MONTH(DT_COMP_X) = #MES))
END
ELSE ''
END AS RENUMERAÇÃO_DA_CONTRIBUIÇÃO
Somehow I doubt this part of the code is as intended:
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
It's equivalent to this:
AND EVEN_COD_X IN (397898)
You probably want commas there instead.
Among your three EVEN_COD_X IN clauses, the last two are wrong : values should be separated with a comma (,).
CASE WHEN EVEN_COD_X IN (623 + 7062) -- It should be (623, 7062)
A good practice is :
to simplify your code (use CTE or temporary tables instead of doing everything in one query) ;
to use indentation (mistakes like this will become more visible) ;
for debugging, simplify it even more until it works.

sql Update column with sum other columns data

I want to update with sum full datatable data row wise but how?
See what want I do:
This code only for shown sum data with row-wise but its not working with NULL value.
select
sum(tc6671 + tf6671 + pc6671 + pf6671 + tc8572 + tf8572 + pc8572 + pf8572 +
tc6672 + tf6672 + tc6673 + tf6673 + pc6673 + pf6673 + pc6674 + pf6674 + tc5852 + tf5852 +
tc5853 + tf5853) as Test
from
cmt_7th;
But I want to update my specific column with all sum data.
I was tried this code but failed:
update cmt_7th
set roll = sum(tc6671+tf6671+pc6671+pf6671+tc8572+tf8572+pc8572+pf8572+
tc6672+tf6672+tc6673+tf6673+pc6673+pf6673+
pc6674+pf6674+tc5852+tf5852+tc5853+tf5853);
And I was tried also by this code:
UPDATE c
SET c.total = c.tc6671+c.tf6671+c.pc6671+c.pf6671+c.tc8572+c.tf8572+c.pc8572+
c.pf8572+c.tc6672+c.tf6672+c.tc6673+c.tf6673+c.pc6673+c.pf6673+
c.pc6674+c.pf6674+c.tc5852+c.tf5852+c.tc5853+c.tf5853
FROM cmt_7th c
Instead of tc6671+tf6671, use coalesce to handle nulls.
Like coalesce(tc6671,0)+coalesce(tf6671,0)+...
So your update statement will be something like
UPDATE c
SET c.total = coalesce(c.tc6671,0) + coalesce(c.tf6671,0) + coalesce(c.pc6671,0)
+ coalesce(c.pf6671,0) + coalesce(c.tc8572,0) + coalesce(c.tf8572,0)
+ coalesce(c.pc8572,0) + coalesce(c.pf8572,0) + coalesce(c.tc6672,0)
+ coalesce(c.tf6672,0) + coalesce(c.tc6673,0) + coalesce(c.tf6673,0)
+ coalesce(c.pc6673,0) + coalesce(c.pf6673,0) + coalesce(c.pc6674,0)
+ coalesce(c.pf6674,0) + coalesce(c.tc5852,0) + coalesce(c.tf5852,0)
+ coalesce(c.tc5853,0) + coalesce(c.tf5853,0)
FROM cmt_7th c
But if you use select sum(coalesce(c.tc6671,0) ... then you will get only 1 row and column with sum of sum of all columns.
See what I mean here
http://rextester.com/LUIPOW59089

And multiple in a camlquery

I'm trying to do a camlquery with 3 and where but i get back an exception:
Values doesn't fall within expected range
I've tried to place the and tag everywhere but still doesn't work
List<string> columns = new List<string>();
foreach (SP.Field fieldTemp in fieldColl)
{
//logger.WriteInfo(fieldTemp.InternalName.ToString());
columns.Add("<FieldRef Name='" + fieldTemp.InternalName.ToString() + "' />");
}
//construit la query
SP.ListItemCollection resultat = null;
SP.CamlQuery query = new SP.CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll'>" +
"<Query>" +
"<Where>" +
"<And>" +
"<Eq>" +
"<FieldRef Name='Ann_x00e9_e' />" +
"<Value Type='Text'>" + year + "</Value>" +
"</Eq>" +
"<And>" +
"<Eq>" +
"<fieldref name='N_x00B0__x0020_circulaire' />" +
"<value type='Text'>" + numCiruclaire + "</value>" +
"</Eq>" +
"<Eq>" +
"<FieldRef Name='Doc_x0020_Language' />" +
"<Value Type='Text'>" + lang + "</Value>" +
"</Eq>" +
"</And>" +
"</And>" +
"</Where>" +
"</Query>" +
"<ViewFields>" +
//"<FieldRef Name='Modified' />"+ //i try this alors but still not work
string.Join("", columns.ToArray()) +
"</ViewFields>" +
"</View>";
resultat = list.GetItems(query);
clientContext.Load(resultat);
clientContext.ExecuteQuery();
if i do a query with where on this only field, i get the exception too
'N_x00B0__x0020_circulaire'
I checked the internalname of the fields in the list and it's correct
the issue came from the second equal 'fileref' was in lowercase and not in CamlCase

How to shorten the boolean function

I have the following function:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
How can I make the expression like:
f(x) = x1x2x3 + x2x3x4 + ...
out of this? Is there any method?
I'm not sure if SO is the right place to post this...I guess it's not, but still, I found the tag and around 100 posts with it, so here I am :P
As far as I remember the boolean algebra you can just multiply them as ordinary numbers.
So (x2 + x1x3x5)(x5 + x6) will be x2x5 + x2x6 + x1x3x5x5 + x1x3x5x6. But again as far as I remember this applied only to "AND" not to "OR"
Well this looks more like a math question.
But if I understand what you want correctly it would look like this
x2x4x5+x2x4x6+x2x3x5x6x5+x2x3x5x6x6+x1x3x5x4 ext.
basically
(a1+a2)a3=a1a3+a2a3
(a1+a2)(a3+a4)=a1a3+a1a4+a2a3+a3a4
Here you go:
(x2x4x5 + x1x3x5)(x4x6) + (x2x5 + x1x3x5)(x3x5x6)
(x2x4x5x6 + x1x3x4x5x6) + (x2x5 + x1x3x5)(x3x5x6)
x2x4x5x6 + x1x3x4x5x6 + x2x3x5x6 + x1x3x5x6
x2x4x5x6 + x1x3(x4x5x6 + x5x6) + x2x3x5x6
x2x5x6(x4+1) + x1x3(x4x5x6 + x5x6)
x2x4x5x6 + x1x3(x5x6(x4+1))
x2x4x5x6 + x1x3(x4x5x6)
x2x4x5x6 + x1x3x4x5x6
(x2+x1x3)x4x5x6
I probably made a mistake somewhere, so you should test it first.
Your original:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
Now using some simple maths:
f(x) = (x2x4 + x2x3x5x6 + x1x3x5x4 + x1x3x5x3x5x6)(x5 + x6)
f(x) = x2x4x5 + x2x3x5x6x5 + x1x3x5x4x5 + x1x3x5x3x5x6x5 + x2x4x6 + x2x3x5x6x6 + x1x3x5x4x6 + x1x3x5x3x5x6x6
Simplifying gets us the answer (though not necessarily shorter),
f(x) = x2x4x5 + x2x3x5x6 + x1x3x5x4x5 + x1x3x5x6 + x2x4x6 + x2x3x5x6 + x1x3x5x4x6 + x1x3x5x6