Topic
• 2 replies
• Latest Post - ‏2013-07-19T20:47:49Z by Hosssein
Hosssein
26 Posts

# Pinned topic how to create a boolean expression

‏2013-07-19T16:25:49Z |

Hi,

In my CP model, W is the set of variables.

could you please tell me how I can create the following expression?

(W[1]==1) && (W[2]==1) .... && (W[n]==1)

Note that I dont know the value of n beforehand, So I need to create it in a loop like below:

for (int i=1; i<=n ; i++){

Expression := Expression && (W[i]==1)

}
I could not do this by IloNumExpr. Could you please tell me what I have to do ?

• rdumeur
134 Posts

#### Re: how to create a boolean expression

‏2013-07-19T16:54:35Z

Dear Hussein,

the && operator is only defined on constraints. If you want to use it within an expression you need to do something like:

#include <ilcp/cp.h>

IloInt someValue(IloInt x);

int main() {
IloEnv        env;
IloModel        model(env);
IloInt        n(10);
IloIntVarArray    vars(env, n, 0, 10);
IloConstraint       c;
for(IloInt i(0); i < 10; ++i) {
IloConstraint    test(vars[i] == someValue(i));
if(i  == 0)
e = test;
else
e = e && test; // compose constraints with &&
}
// do whatever you want with e

// to use constraint e as an expression you can  do (e == <boolean value>).

}

I hope this helps.

Cheers,

Renaud

Updated on 2013-07-19T18:10:30Z at 2013-07-19T18:10:30Z by rdumeur
• rdumeur
134 Posts

#### Re: how to create a boolean expression

‏2013-07-19T16:54:35Z

Dear Hussein,

the && operator is only defined on constraints. If you want to use it within an expression you need to do something like:

#include <ilcp/cp.h>

IloInt someValue(IloInt x);

int main() {
IloEnv        env;
IloModel        model(env);
IloInt        n(10);
IloIntVarArray    vars(env, n, 0, 10);
IloConstraint       c;
for(IloInt i(0); i < 10; ++i) {
IloConstraint    test(vars[i] == someValue(i));
if(i  == 0)
e = test;
else
e = e && test; // compose constraints with &&
}
// do whatever you want with e

// to use constraint e as an expression you can  do (e == <boolean value>).

}

I hope this helps.

Cheers,

Renaud

Updated on 2013-07-19T18:10:30Z at 2013-07-19T18:10:30Z by rdumeur
• Hosssein
26 Posts

#### Re: how to create a boolean expression

‏2013-07-19T20:47:49Z
• rdumeur
• ‏2013-07-19T16:54:35Z

Dear Hussein,

the && operator is only defined on constraints. If you want to use it within an expression you need to do something like:

#include <ilcp/cp.h>

IloInt someValue(IloInt x);

int main() {
IloEnv        env;
IloModel        model(env);
IloInt        n(10);
IloIntVarArray    vars(env, n, 0, 10);
IloConstraint       c;
for(IloInt i(0); i < 10; ++i) {
IloConstraint    test(vars[i] == someValue(i));
if(i  == 0)
e = test;
else
e = e && test; // compose constraints with &&
}
// do whatever you want with e

// to use constraint e as an expression you can  do (e == <boolean value>).

}

I hope this helps.

Cheers,

Renaud

Thank you so much ;))))