# Specify the sense of a constraint in CPLEX

I am writing a small wrapper around Gurobi and Cplex so that the models I write are solver independent. I'm very familiar with Gurobi but I'm very new to Cplex and I'm having trouble replicating certain api calls that I use very frequently.

Specifically I'm having trouble figuring out how to pass the sense to the Cplex API:

std::shared_ptr<Constraint> Model::addConstr(const std::vector<std::shared_ptr<Variable>>& vars,
const std::vector<double>& coeffs, char sense,
double rhs, const std::string& name)
#ifdef GUROBI
GRBLinExpr expr;
std::vector<GRBVar> grb_vars;
for(auto var : vars) {
grb_vars.push_back(*(var->getGRBVar()));
}
GRBConstr constraint = _grb_model->addConstr(expr, sense, rhs, name);
std::shared_ptr<GRBConstr> grb_constr_shared = std::make_shared<GRBConstr>(constraint);
return std::make_shared<Constraint>(grb_constr_shared);
#elif defined CPLEX
// do exactly the same process for cplex
IloExpr expr(_cplex_env);
for(int i = 0; i < vars.size(); ++i) {
expr += coeffs[i] * vars[i];
}
// this line below doesn't work -- I don't know how to pass the sense of
// the constraint. I'd like to avoid using a switch statement if possible..
IloConstraint constraint = _cplex_model.add(expr, sense, rhs);
#endif
}


And I'm having trouble figuring out how to pass the coefficient for a variable in the objective. I'd like to be able to do this on creation of the variable (or at least immediately after creating the variable) so that I can have similar functionality to gurobi.

std::shared_ptr<Variable> Model::addVar(double lb, double ub, double obj,
char var_type, std::string name) {
#ifdef GUROBI
GRBVar grb_var = _grb_model->addVar(lb, ub, obj, var_type, std::move(name));
std::shared_ptr<GRBVar> grb_var_shared = std::make_shared<GRBVar>(grb_var);
return std::make_shared<Variable>(grb_var_shared);
#elif defined CPLEX
// do the same process for CPLEX and return std::make_shared<Variable>(cplex_var_shared);
// this line defines the variable but doesn't set its coeff in the
// objective function -- how do I set the variable's objective coefficient?
IloNumVar var(*_cplex_env, lb, ub, IloNumVar::Int, name.c_str());

#endif
}


For specifying constraint direction, look to the various IloRange constructors. You can specify both a lower and upper bound (for constraints of the form $$b_0 \le a'x \le b_1$$, or use a constructor that either defaults the lower bound to $$-\infty$$ or defaults the upper bound to $$+\infty$$.