Topic
• 21 replies
• Latest Post - ‏2017-09-07T08:26:53Z by Breemes
1883 Posts

Pinned topic If-Decision Variable

‏2012-12-25T13:16:58Z |
hii, I am new to Cplex optimization Studio.Please help me as I am stuck in a problem where I have to use If condition in Decision variable (dvar). Please tell me how it is possible to do in IBM ILOG CPLEX Optimization Studio

Version: 12.4

Thank You
Updated on 2013-01-06T17:37:57Z at 2013-01-06T17:37:57Z by SystemAdmin
1883 Posts

Re: If-Decision Variable

‏2012-12-25T23:19:23Z
It's not very clear what you mean by "if condition in decision variable". I think you probably want to conditionally define a decision expression (dexpr) using a if-then-else semantic. OPL provides a one line syntax for that:

```range A = 1..5;
dvar int b[a in A];
dvar int c[a in A];
dexpr int x[a in A] = a%2 == 0 ? b[a] : c[a];
```

In this example, x[a] is equal to b[a] if a is even, or c[a] if a is odd.

Regards,
Stefano
Updated on 2014-03-26T05:51:45Z at 2014-03-26T05:51:45Z by iron-man
1883 Posts

Re: If-Decision Variable

‏2013-01-06T17:37:57Z
Are you trying to use a decision variable in an if-statement? This is not supported, see the reference manual.
What you can do instead is use logical constraints. A statement like 'if (x>=0) then a*y >= b' is equivalent to the implication constraint 'x >= 0 => a*y >= b'.
• NRouge
16 Posts

Re: If-Decision Variable

‏2016-06-26T11:06:45Z
Are you trying to use a decision variable in an if-statement? This is not supported, see the reference manual.
What you can do instead is use logical constraints. A statement like 'if (x>=0) then a*y >= b' is equivalent to the implication constraint 'x >= 0 => a*y >= b'.

Hi,

I have a model

..

float D[N,N]=...;

..

dvar boolean x[N,N,N];

..

forall (i in N,j in N, k in V)
if (x[i,j,k]==1) then (D[i,k]+t[i,j] <= D[j,k]);

I want dvar x in if statement like this. It has error 'Decision variable (or expression) "x" not allowed." please help. Thanks.

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2016-06-26T12:01:27Z
• NRouge
• ‏2016-06-26T11:06:45Z

Hi,

I have a model

..

float D[N,N]=...;

..

dvar boolean x[N,N,N];

..

forall (i in N,j in N, k in V)
if (x[i,j,k]==1) then (D[i,k]+t[i,j] <= D[j,k]);

I want dvar x in if statement like this. It has error 'Decision variable (or expression) "x" not allowed." please help. Thanks.

Hi,

forall (i in N,j in N, k in V)
if (x[i,j,k]==1) then (D[i,k]+t[i,j] <= D[j,k]);

you should try

forall (i in N,j in N, k in V)
(x[i,j,k]==1) => (D[i,k]+t[i,j] <= D[j,k]);

regards

• NRouge
16 Posts

Re: If-Decision Variable

‏2016-06-26T12:17:02Z

Hi,

forall (i in N,j in N, k in V)
if (x[i,j,k]==1) then (D[i,k]+t[i,j] <= D[j,k]);

you should try

forall (i in N,j in N, k in V)
(x[i,j,k]==1) => (D[i,k]+t[i,j] <= D[j,k]);

regards

Thanks a lot for your quick response .

• Kamran_Sr
16 Posts

Re: If-Decision Variable

‏2017-06-26T12:39:07Z

Hi,

forall (i in N,j in N, k in V)
if (x[i,j,k]==1) then (D[i,k]+t[i,j] <= D[j,k]);

you should try

forall (i in N,j in N, k in V)
(x[i,j,k]==1) => (D[i,k]+t[i,j] <= D[j,k]);

regards

Hi,

I have a problem with this code. I need to code a conditional constraint for decision variables like this :

forall (t in times,a in arcs, q in hh, m in Modes)

(

h[t]<=q.time)=> excost[a]==h[t];

forall (t in times,a in arcs, q in hh ,m in Modes)

(

h[t]>=q.time)=> excost[a]== hh[t];

the problem here is that, the second condition should be h[t]>q.time not >= . However, the > is not supported by the Cplex. How can I solve this issue?

Many thanks,

Kamran

Updated on 2017-06-26T12:39:49Z at 2017-06-26T12:39:49Z by Kamran_Sr
• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-06-26T14:03:14Z
• Kamran_Sr
• ‏2017-06-26T12:39:07Z

Hi,

I have a problem with this code. I need to code a conditional constraint for decision variables like this :

forall (t in times,a in arcs, q in hh, m in Modes)

(

h[t]<=q.time)=> excost[a]==h[t];

forall (t in times,a in arcs, q in hh ,m in Modes)

(

h[t]>=q.time)=> excost[a]== hh[t];

the problem here is that, the second condition should be h[t]>q.time not >= . However, the > is not supported by the Cplex. How can I solve this issue?

Many thanks,

Kamran

Hi,

(h[t]>=q.time)=> excost[a]== hh[t];

could you try

(

h[t]>=q.time+epsilon)=> excost[a]== hh[t];

with a small enough epsilon

?

regards

• Kamran_Sr
16 Posts

Re: If-Decision Variable

‏2017-06-26T15:16:28Z

Hi,

(h[t]>=q.time)=> excost[a]== hh[t];

could you try

(

h[t]>=q.time+epsilon)=> excost[a]== hh[t];

with a small enough epsilon

?

regards

Hi Alex,

Thanks for your reply. I will try that if there is no other way for that.

Best

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-06-26T16:12:27Z
• Kamran_Sr
• ‏2017-06-26T15:16:28Z

Hi Alex,

Thanks for your reply. I will try that if there is no other way for that.

Best

Well you could also try to use ! in order to get that behaviour:

dvar float x;
dvar float y;

subject to
{
(!(x<=4)) => (y>=5);
}

regards

• Kamran_Sr
16 Posts

Re: If-Decision Variable

‏2017-06-26T18:03:50Z

Well you could also try to use ! in order to get that behaviour:

dvar float x;
dvar float y;

subject to
{
(!(x<=4)) => (y>=5);
}

regards

• Kamran_Sr
16 Posts

Re: If-Decision Variable

‏2017-06-28T20:40:04Z

Well you could also try to use ! in order to get that behaviour:

dvar float x;
dvar float y;

subject to
{
(!(x<=4)) => (y>=5);
}

regards

Dear Alex,

I have to define a cost based on a condition (if else) which contains decision variable. I tried to code this problem like this:

forall (t in times,a in arcs, q in hh, m in Modes)

(h[t]<=q.time)=> cost[a]==h[t];

forall (t in times,a in arcs, q in hh ,m in Modes)

(!(h[t]<=q.time))=> cost[a]== hh[t];

However, the solution for this is infeasible and I guess this infeasibility is because the cost[a] is defined with two different formulations.

I was wondering if you could help me to solve this issue.

Kindest Regards,

Kamran

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-06-30T17:06:04Z
• Kamran_Sr
• ‏2017-06-28T20:40:04Z

Dear Alex,

I have to define a cost based on a condition (if else) which contains decision variable. I tried to code this problem like this:

forall (t in times,a in arcs, q in hh, m in Modes)

(h[t]<=q.time)=> cost[a]==h[t];

forall (t in times,a in arcs, q in hh ,m in Modes)

(!(h[t]<=q.time))=> cost[a]== hh[t];

However, the solution for this is infeasible and I guess this infeasibility is because the cost[a] is defined with two different formulations.

I was wondering if you could help me to solve this issue.

Kindest Regards,

Kamran

Hi,

can you attach your .mod and .dat so that other users could help ?

regards

• Breemes
15 Posts

Re: If-Decision Variable

‏2017-09-05T11:23:57Z

Hi,

i tried to implement a constrain like the instructions above - i don't get an error from Cplex but also no results in Excel.

Can anyone assist?

dvar boolean ReturnBereitschaft [Region];

forall (r in Region, w in Wertstoffhof)
Entfernung[r][w]<=maxEntfernung => ReturnBereitschaft[r]==1;

forall (r in Region, w in Wertstoffhof)
Entfernung[r][w]>=maxEntfernung => ReturnBereitschaft[r]==0;

Thanks

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-09-05T11:47:20Z
• Breemes
• ‏2017-09-05T11:23:57Z

Hi,

i tried to implement a constrain like the instructions above - i don't get an error from Cplex but also no results in Excel.

Can anyone assist?

dvar boolean ReturnBereitschaft [Region];

forall (r in Region, w in Wertstoffhof)
Entfernung[r][w]<=maxEntfernung => ReturnBereitschaft[r]==1;

forall (r in Region, w in Wertstoffhof)
Entfernung[r][w]>=maxEntfernung => ReturnBereitschaft[r]==0;

Thanks

Hi,

if you are in a situation where

Entfernung[r][w]==maxEntfernung

then return is both 0 and 1

Why do not you write

ReturnBereitschaft[r] == (Entfernung[r][w]<=maxEntfernung);

?

regards

• Breemes
15 Posts

Re: If-Decision Variable

‏2017-09-05T13:26:43Z

Hi,

if you are in a situation where

Entfernung[r][w]==maxEntfernung

then return is both 0 and 1

Why do not you write

ReturnBereitschaft[r] == (Entfernung[r][w]<=maxEntfernung);

?

regards

Hi Alex,

but it still doesn't work.

The idea behind the constraint is that the decision variable will be 1 if the distance is smaller than the max.distance (0 if it is higher)

The boolean variable is always 0 although the distance is smaller.

I tried different ways of implementing like the addition above, but it doesn't work.

forall (r in Region, w in Wertstoffhof)
Entfernung [w][r] * ReturnBereitschaft[w][r] <= maxEntfernung;

forall (r in Region, w in Wertstoffhof)
Entfernung[w][r]<=maxEntfernung => ReturnBereitschaft[w][r]==1;

Do you have any other idea, how an implementation of the restriction would be successful?

Kind regards

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-09-05T13:30:18Z
• Breemes
• ‏2017-09-05T13:26:43Z

Hi Alex,

but it still doesn't work.

The idea behind the constraint is that the decision variable will be 1 if the distance is smaller than the max.distance (0 if it is higher)

The boolean variable is always 0 although the distance is smaller.

I tried different ways of implementing like the addition above, but it doesn't work.

forall (r in Region, w in Wertstoffhof)
Entfernung [w][r] * ReturnBereitschaft[w][r] <= maxEntfernung;

forall (r in Region, w in Wertstoffhof)
Entfernung[w][r]<=maxEntfernung => ReturnBereitschaft[w][r]==1;

Do you have any other idea, how an implementation of the restriction would be successful?

Kind regards

Hi,

can you attach .mod and .dat so that other users could try ?

regards

• Breemes
15 Posts

Re: If-Decision Variable

‏2017-09-05T13:45:20Z

Hi,

can you attach .mod and .dat so that other users could try ?

regards

Hi,

i attach the dat. and mod. file and appreciate every help.

Regards,

Attachments

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-09-05T13:47:48Z
• Breemes
• ‏2017-09-05T13:45:20Z

Hi,

i attach the dat. and mod. file and appreciate every help.

Regards,

and "DatenInput.xlsx"  ?

• Breemes
15 Posts

Re: If-Decision Variable

‏2017-09-05T14:06:06Z

attached,

Attachments

• AlexFleischer
2970 Posts

Re: If-Decision Variable

‏2017-09-05T15:26:03Z
• Breemes
• ‏2017-09-05T14:06:06Z

attached,

Hi,

if you remove

forall (t in time:t!=1)
sum (i in Handy, w in Wertstoffhof,r in Region) ReverseFlow [t][i][w][r] == sum (i in Handy, w in Wertstoffhof) Inspektion [t][i][w];

then

forall (r in Region, w in Wertstoffhof)
ReturnBereitschaft[w][r] == (Entfernung[w][r]<=maxEntfernung);

works fine

regards

• Breemes
15 Posts

Re: If-Decision Variable

‏2017-09-07T08:26:53Z

Hi,

if you remove

forall (t in time:t!=1)
sum (i in Handy, w in Wertstoffhof,r in Region) ReverseFlow [t][i][w][r] == sum (i in Handy, w in Wertstoffhof) Inspektion [t][i][w];

then

forall (r in Region, w in Wertstoffhof)
ReturnBereitschaft[w][r] == (Entfernung[w][r]<=maxEntfernung);

works fine

regards

Hi Alex,