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

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

‏2014-01-31T08:01:36Z | constraint expression linear

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");
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
36 Posts

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

‏2014-01-31T15:26:08Z

Hello,

``` //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
3 Posts

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

‏2014-01-31T17:16:40Z
• ChrisBr
• ‏2014-01-31T15:26:08Z

Hello,

<pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr"> //x + y = 17 cp.add( cp.eq( cp.sum(x,y), 17 ) ); // x - y = 5 cp.add( cp.eq( cp.diff(x,y), 5 ) ); </pre>

I hope this helps,

Chris.

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
36 Posts

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

‏2014-01-31T17:48:54Z
• WesleyJ
• ‏2014-01-31T17:16:40Z

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,

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);
}
```

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

Regards,

Chris.

• WesleyJ
3 Posts

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

‏2014-02-01T06:28:40Z
• ChrisBr
• ‏2014-01-31T17:48:54Z

Hello,

Maybe something like that

<pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">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)); </pre>

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

Regards,

Chris.

Thanks,

Your suggestion is almost same as my first way

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

Thanks anyway