# Gurobi C++: adding variables with vector

I am solving an LP by using GUROBI - C++ combo.

Assume I have a variable $$p \in \mathbb{R}^n$$. I am adding this as a vector:

GRBEnv* env;
GRBModel* model;
vector<GRBVar> p;


Now I have $$a\in\mathbb{R}^n$$ parameters, and I want to add a constraint $$a^\top p\ \leq 0$$, so I say:

GRBLinExpr sum_expo = 0;
for (int i = 0; i < p.size(); i++) {// add all the coeffs given
sum_expo += a[i] * p[i];
}


Obviously, this is too inefficient. Is there a way that I can:

1. Define the whole $$p$$ vector as a multivariate GRBVar, rather than having a vector with all GRBVar values
2. Add a constraint $$a^\top p \leq 0$$ in a single line.
• Thanks for the answer. I somehow taught using addVars in one command and then somehow giving inner-product defined constraint would be more efficient. So is what I am doing above standard? – independentvariable Feb 13 '20 at 23:51

• thanks for this answer. How can I call that? Something like:sum_expo::addTerms(a, p, p.size()); – independentvariable Feb 14 '20 at 14:10
• ok fied by sum_expo.addTerms(&a[0], &p[0], X.size()); – independentvariable Feb 14 '20 at 14:15