Connor
Connor

Reputation: 675

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()));
    }
    expr.addTerms(&coeffs[0], &grb_vars[0], (int) vars.size());
    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
  }

Upvotes: 4

Views: 476

Answers (1)

rkersh
rkersh

Reputation: 4465

For your addConstr method you will have to use a switch statement (unfortunately, as you were hoping to avoid). With the CPLEX C++ API, constraints are built using the overloaded <=, ==, and >= operators or with one of the IloRange constructors. For example:

   IloRange constraint;
   switch (sense) {
   case 'L':
      constraint = (expr <= rhs);
      // Equivalent to:
      // constraint = IloRange(env, 0.0, expr, rhs);
      break;
   case 'G':
      constraint = (expr >= rhs);
      // Equivalent to:
      // constraint = IloRange(env, rhs, expr, IloInfinity);
      break;
   case 'E':
      constraint = (expr == rhs);
      // Equivalent to:
      // constraint = IloRange(env, rhs, expr, rhs);
      break;
   default:
      // This should not happen.
      abort();
   }
   _cplex_model.add(constraint);

I'm not very familiar with the Gurobi API, but for your addVar method it looks like you will want to refactor it to call GRBModel::setObjective() separately (after having created the variables). With the CPLEX C++ API you create the objective separately in a similar way. For example, using IloMinimize, like so:

model.add(IloMinimize(env, objexpr));

NOTE: The CPLEX C API (aka Callable Library) is perhaps more in spirit with your original code. That is, you can set the sense of a constraint with a char argument and you can set the objective value of a variable when creating it. It may be more work that you want to take on, but you could consider creating a custom lightweight object-oriented wrapper around the CPLEX C API to suit your needs.

Upvotes: 3

Related Questions