Topic
4 replies Latest Post - ‏2014-02-01T06:28:40Z by WesleyJ
WesleyJ
WesleyJ
3 Posts
ACCEPTED ANSWER

Pinned topic how to add a constraint like "x - y = 7" in Java

‏2014-01-31T08:01:36Z |

I have a question while I am reading the manual 'Getting started CP optimizer".


IloEnv env;
  try {
    IloModel model(env);
    IloIntVar x(env, 5, 12, "x");
    IloIntVar y(env, 2, 17, "y");
    model.add(x + y == 17);
    model.add(x - y == 5);
    IloCP cp(model);
    cp.propagate();
    cp.out() << std::endl << "Propagate:" << std::endl;
    cp.out() << "x in " << cp.domain(x) << std::endl;
    cp.out() << "y in " << cp.domain(y) << std::endl << std::endl;
    if (cp.solve()){
      cp.out() << std::endl << "Solution:" << std::endl;
      cp.out() << "x = " << cp.getValue(x) << std::endl;
      cp.out() << "y = " << cp.getValue(y) << std::endl;
    }

I am more comfortable at java than C++, so I tried to code. It works but is there a easy way to add constraint like x + y = 17 in java?

IloCP cp = new IloCP();
IloIntVar x = cp.intVar(5, 12, "x");
IloIntVar y = cp.intVar(2, 17, "y");
 
IloIntVar[] arr = new IloIntVar[] {x, y};
//x + y = 17
cp.add( cp.eq( cp.prod(arr, new int[] {1, 1} ), 17 ) );
 
// x - y = 5
cp.add( cp.eq( cp.prod(arr, new int[] {1, -1} ), 5 ) );
 
if ( cp.solve() )
{
System.out.println();
System.out.println("x = " + cp.getValue(x) );
System.out.println("y = " + cp.getValue(y) );
}
 

 

Updated on 2014-01-31T08:05:10Z at 2014-01-31T08:05:10Z by WesleyJ
  • ChrisBr
    ChrisBr
    17 Posts
    ACCEPTED ANSWER

    Re: how to add a constraint like "x - y = 7" in Java

    ‏2014-01-31T15:26:08Z  in response to WesleyJ

    Hello,

    What about to try:

     //x + y = 17
    cp.add( cp.eq( cp.sum(x,y), 17 ) );
    // x - y = 5
    cp.add( cp.eq( cp.diff(x,y), 5 ) );
    

    I hope this helps,

    Chris.

    • WesleyJ
      WesleyJ
      3 Posts
      ACCEPTED ANSWER

      Re: how to add a constraint like "x - y = 7" in Java

      ‏2014-01-31T17:16:40Z  in response to ChrisBr

      Thanks for suggestion,

      For more general case

         ax + by = c;

         I can not apply your sum or diff method due to coefficient a an b.

      Any other idea?

      Thanks,

       

      • ChrisBr
        ChrisBr
        17 Posts
        ACCEPTED ANSWER

        Re: how to add a constraint like "x - y = 7" in Java

        ‏2014-01-31T17:48:54Z  in response to WesleyJ

        Hello,

        Maybe something like that

        int[] coeff = new int[nb];
        IloIntVar[] vars = new IloIntVar[nb];
        for (int i = 0; i < nb; i++) {
            coeff[i] = ...;
            vars[i] = cp.intVar(min, max);
        }
        cp.add(cp.eq(cp.scalProd(coeff, vars), c));
        

        you might have a look at http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp

        Regards,

        Chris.

        • WesleyJ
          WesleyJ
          3 Posts
          ACCEPTED ANSWER

          Re: how to add a constraint like "x - y = 7" in Java

          ‏2014-02-01T06:28:40Z  in response to ChrisBr

          Thanks,

          Your suggestion is almost same as my first way

          cp.add( cp.eq( cp.prod(arr, new int[] {1, 1} ), 17 ) );

          Thanks anyway