Forum Discussion

FICO's avatar
FICO
Community Manager
1 year ago

Mapping methods between XPRBctr and new class Inequality

I'm porting a Java application to c++ (I'm using latest Xpress Version). From guide I understood that XPRBctr old BCL class has been mapped to Inequality, but I cannot find some methods (i.e. addTerm, setTerm),

I tried to figure out the mapping inspecting the given includes but I was unable to understand how to 'convert' those methods.

Can you help me? Thanks!

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    Sorry, for the late reply. It is probably best to look at the examples that ship in the examples/solver/optimizer/cxx/objects directory and at the introduction here.

    The way to build a linear constraint in the C++ API involves these steps:

    1. Create expressions for the left-hand side and right-hand side.
    2. Use relational operators to turn this into an inequality definition.
    3. Use XpressProblem::addConstraint() to add this as a constraint. This function returns a handle to the newly added constraint, an instance of xpress::objects::Inequality.
    4. You can then later change coefficients in this constraint (directly in the matrix) use the chgCoef(), chgCoefs() and setRhs() functions of class Inequality.

    Here is a short example:

    #include <xpress.hpp>
    #include <xpress_objects.hpp>
     
    using namespace xpress::objects;
     
    int
    main(void)
    {
      XpressProblem prob;
      auto x = prob.addVariables(3).toArray();
      Expression lhs = x[0] + 2*x[1];         // Create expression for left-hand side.
      Expression rhs = x[2] + 4;              // Create expression for right-hand side.
      auto def = lhs <= rhs;                  // Create constraint _definition_.
      Inequality i = prob.addConstraint(def); // Create an actual constraint from the definition.
      prob.writeProb("p1.lp"); // LP file has: R1: C1 + 2 C2 - C3 <= 4
      i.chgCoef(x[1], 3);                     // Modify the constraint.
      prob.writeProb("p2.lp"); // LP file has: R1: C1 + 3 C2 - C3 <= 4 
      return 0;
    }