Pinned topic IfDecision Variable
Version: 12.4
Thank You

Re: IfDecision Variable
20121225T23:19:23ZThis is the accepted answer. This is the accepted answer.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 ifthenelse 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,
StefanoUpdated on 20140326T05:51:45Z at 20140326T05:51:45Z by ironman 
Re: IfDecision Variable
20130106T17:37:57ZThis is the accepted answer. This is the accepted answer.Are you trying to use a decision variable in an ifstatement? 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'. 
Re: IfDecision Variable
20160626T11:06:45ZThis is the accepted answer. This is the accepted answer. SystemAdmin
 20130106T17:37:57Z
Are you trying to use a decision variable in an ifstatement? 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.

Re: IfDecision Variable
20160626T12:01:27ZThis is the accepted answer. This is the accepted answer. NRouge
 20160626T11: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,
instead of
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

Re: IfDecision Variable
20160626T12:17:02ZThis is the accepted answer. This is the accepted answer. AlexFleischer
 20160626T12:01:27Z
Hi,
instead of
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 .

Re: IfDecision Variable
20170626T12:39:07ZThis is the accepted answer. This is the accepted answer. AlexFleischer
 20160626T12:01:27Z
Hi,
instead of
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 20170626T12:39:49Z at 20170626T12:39:49Z by Kamran_Sr 
Re: IfDecision Variable
20170626T14:03:14ZThis is the accepted answer. This is the accepted answer. Kamran_Sr
 20170626T12: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,
instead of
(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

Re: IfDecision Variable
20170626T15:16:28ZThis is the accepted answer. This is the accepted answer. AlexFleischer
 20170626T14:03:14Z
Hi,
instead of
(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

Re: IfDecision Variable
20170626T16:12:27ZThis is the accepted answer. This is the accepted answer. Kamran_Sr
 20170626T15: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

Re: IfDecision Variable
20170626T18:03:50ZThis is the accepted answer. This is the accepted answer. AlexFleischer
 20170626T16:12:27Z
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
Thanks for your help

Re: IfDecision Variable
20170628T20:40:04ZThis is the accepted answer. This is the accepted answer. AlexFleischer
 20170626T16:12:27Z
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

Re: IfDecision Variable
20170630T17:06:04ZThis is the accepted answer. This is the accepted answer. Kamran_Sr
 20170628T20: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