Gurobi constraints, list comprehension - constraints

I am unable to create a list of constraints in gurobi with list comprehension.
I made this able to work
m.addConstr(0_decision[0] + 1_decision[0] + 2_decision[0] + 3_decision[0] + 4_decision[0] + 5_decision[0] + 6_decision[0] , GRB.EQUAL, test_list[0])
However, I have too much data to enter manually, and substituting i in for the numbers does not work: This doesn't work
m.addConstrs([0_decision[i] + 1_decision[i] + 2_decision[i] + 3_decision[i] + 4_decision[i] + 5_decision[i] + 6_decision[i] for i in range(500)], GRB.EQUAL, test_list[i])
I get this error:
TypeError: addConstrs() takes at most 3 positional arguments (4 given)
I tried different forms of comprehension, I tried to do it one at a time and that works! But I have too many I cannot afford to do it hundreds of times.

Use a generator expression across the entire expression:
m.addConstrs(0_decision[i] + 1_decision[i] + 2_decision[i] +
3_decision[i] + 4_decision[i] + 5_decision[i] +
6_decision[i] == test_list[i] for i in range(500))
Better yet, refactor the *_decision variables using Model.addVars() so that you can write:
m.addConstrs(decision.sum('*',i) == test_list[i] for i in range(500))

Related

How to nest an expression or quote in another expression or quote?

Let's say I have
first <- quote(beta_a*a + beta_b*b)
second <- quote(beta_d*d + beta_e*e)
third <- quote(first + second)
I want third to return beta_a*a + beta_b*b + beta_d*d + beta_e*e but instead it returns first + second
Desired result:
third
> beta_a*a + beta_b*b + beta_d*d + beta_e*e
I know that it makes sense that it then returns first + second, but what can I do to get the desired result?
For context, imagine that I need to use this for a specification for an ordered logit model in Apollo. Each first and second type is a variable, and the content is the dummies for that variable. Third then represents a specification. Imagine then I have 10+ variables with 10+ dummies each and I have to make a specification for each combination of variables. It's a lot of manual labour to write out all these utility functions. If I could make the specifications beforehand by condensing first the dummies to variables and then the variables to specifications, it would save me a lot of lines of code and time.
With substitute:
first <- quote(beta_a*a + beta_b*b)
second <- quote(beta_d*d + beta_e*e)
substitute(a + b, list(a = first, b = second))
#beta_a * a + beta_b * b + (beta_d * d + beta_e * e)

How to find the order of floating point operations(additions), given a list of floats and the sum of the list

Introduction
Floating point operations are not associative.
This means if you have a list of floats, the sum you will get, depends on the order of the operations.
I am wondering whether someone already wrote an algorithm to find a combination of floating point additions which sum up to a given result.
Problem
Given
arr = [ 0.13802005, -0.04358668, -0.02762653, 0.12191868, -0.00974631, 0.16959566, -0.02888574, 0.13568747](dtype=float32)
sum_of_floats = 0.45537661015987396
Find
Order of operations which result in the same value as sum_of_floats
Examples
Possible Solution
In[19]: ((arr[0] + arr[4]) + (arr[2] + arr[6])) + ((arr[3] + arr[7]) + (arr[1] + arr[5])).item()
Out[19]: 0.45537661015987396
Wrong Solution
In[24]: ((arr[0] + arr[1]) + (arr[2] + arr[3])) + ((arr[4] + arr[5]) + (arr[6] + arr[7])).item()
Out[24]: 0.45537659525871277
Another wrong solution
In[25]: (((((((arr[0] + arr[1]) + arr[2]) + arr[3]) + arr[4]) + arr[5]) + arr[6]) + arr[7]).item()
Out[25]: 0.45537659525871277
Question
Is there an existing algorithm to find one/all possible solutions to this problem ?

Limited output: PLM R

I am trying to run a regression in R, using the plm package, on a unbalanced, relatively large data set (111738x66). However, in the output I get, only the coefficients are displayed, but the std errors, p values, etc, are not.
Further, there are 21 variables in the regression.
This is essentially the code that I am using
year1logyearC <- plm(formula = eddata.lfsato ~ eddata.sex + eddata.agesq + eddata.wav1 + eddata.wav2 + eddata.wav3 + eddata.wav4 + eddata.wav5 + eddata.wav6 + eddata.wav7 + eddata.wav8 + eddata.wav9 + eddata.wav10 + eddata.wav11 + eddata.wav12 + eddata.wav13 + eddata.wav14 + eddata.wav15 + eddata.wav16 + eddata.wav17 + eddata.eth1 + eddata.eth2 + cycle + eddata.logfiyr + cycle:eddata.logfiyr , method =“pooling”, data = PMCombFinNewRef1)
Might there be a way to get the data displayed?
Further, if to the output of this, I use the following commands
kable(tidy(year1logyearC), digits=5,
caption="Pooled model")
I get a nice table with most of the things I need (apart from DF and other stuff)...
Any reasons why that might be the case?
I think it is more to do with something like preferences or other setting perhaps?
I know - that I should add a reproducible example, but it is really difficult, because not only is the data sensitive, but also because there are 200 lines of code before. (Yes, I have tested those 200 lines and they work just fine! Ran a bunch of regressions using lm... and I have the complete output using lm)
Using 'summary' I am able to get the results I need
Thus,
summary(year1logyearC)

How to change number of decimal places displayed in output of multilevel model

I am displaying a multilevel model and would like to show three decimal places when output to screen.
M3 <- lmer(stflife ~ sclmeet + health + wkdcorga + hincfel + fltlnl
+ rshpsts + dosprt + c_corruption_2014 + c_ticpi_2014 +
(1 | cntry), data = countries)
display(M3)
Assuming this is the display function from the arm package, display(M3, digits=3) will display results to three decimal places. The digits argument is documented in the help for display, which can be accessed by running ?display.

Passing in an unknown number of "geom_text"s to ggplot as function parameters

I'd like to wrap chart creation in my own function that ultimately creates a ggplot object. This is to reduce code repetition. Because various charts may have different requirements, there are a few parameters.
One variation between different charts may be displaying one or more geom_text sets. I had naively thought that because the + operator is overloaded by ggplot, I may be able to call sum() on a list of geom_text objects as follows:
outputPlot <- basePlot +
geomBar +
sum(geomTextList) +
titles +
theme +
yScale +
plotFill +
if (coordFlip) { coord_flip() } else { NULL }
However, this returns Error in sum(geomTextList) : invalid 'type' (list) of argument.
Does anyone have any ideas for how I might be able to achieve this, or am I just shooting for the moon? It also ultimately extends to having a list of geom_bar as well if required...
No need for sum: ggplot2 can deal with lists of layers so the following should work:
baseplot + geomBar + geomtextList + …

Resources