Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

If-then-else construction

  1. Dec 9, 2012 #1
    How can we define a constraint saying:

    if b is larger than zero then y has to equal 1
    if b is equal to zero then y has to equal 0


    It concerns the following problem:
    b[k] = amount of vegetable i growing on patch k
    y[k] = 1 if vegetable i is grown on patch k
    = 0 otherwise

    So we want to say that if the amount of vegetable i on patch k is larger than zero (i.e. b[k] > 0), vegetable i must be assigned to patch k (i.e. y[k] = 1)
    I was thinking that it could be something like that:

    for all i
    sum(k) b[k]*y[k] = b[k]

    But I don't know if this is correct or not, since you could cancel out both terms b[k]

    What do you guys think?

    Kind regards,
    Last edited: Dec 9, 2012
  2. jcsd
  3. Dec 9, 2012 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi Alexx1!

    Perhaps like this?
    y[k] = (b[k] > 0);
  4. Dec 9, 2012 #3
    If you know that b[k] will never be negative, you have y[k] = sgn( b[k] ).
  5. Dec 9, 2012 #4
    The thing is I need a constraint to use in the program
    IBM ILOG CPLEX Optimization Studio

    None of both constraints is accepted in CPLEX
  6. Dec 9, 2012 #5
    According to Wikipedia's entry on OPL (the programming lanuage used in the program you mention), it does support an IF-THEN-ELSE construct. Why don't you use that?
  7. Dec 10, 2012 #6
    I would prefer stating it in a constraint only using =/>=/<=/</>
    Do you know an alternative?
  8. Dec 10, 2012 #7
    Here's some examples from IBM's manual on OPL:
    Code (Text):
    int value = ...;
    int signValue = (value>0) ? 1 : (value<0) ? -1 : 0;
    int absValue = (value>=0) ? value : -value;
    It seems that OPL supports a "... ? ... : ..." construct. Have you tried that? E.g.

    y[k] = (b[k] > 0) ? 1 : 0;
  9. Dec 10, 2012 #8

    jim mcnamara

    User Avatar

    Staff: Mentor

    That is a ternary operator like the one in the C language:
    Code (Text):

                       if()               then             else
    int variable=(boolean statement) ? [when true] : [when false];
    The example on line two above "concatenates" or nests operators
    Code (Text):

    int signValue = [color=red](value>0) ? 1 :[/color][color=blue] (value<0) ? -1 : 0;[/color]
    Maybe that will help....
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook