How to Define a Constraint Using If-Then-Else for Vegetable Growth?

In summary, the conversation is about defining a constraint for a problem involving vegetable growth on patches. The constraint states that if the amount of vegetable on a patch is larger than zero, then it must be assigned to that patch. The conversation also discusses alternative ways of writing the constraint, such as using a ternary operator in the programming language OPL.
  • #1
Alexx1
86
0
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,
Alex
 
Last edited:
Physics news on Phys.org
  • #2
Hi Alexx1!

Perhaps like this?
y[k] = (b[k] > 0);
 
  • #3
If you know that b[k] will never be negative, you have y[k] = sgn( b[k] ).
 
  • #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
 
  • #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?
 
  • #6
Michael Redei said:
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?

I would prefer stating it in a constraint only using =/>=/<=/</>
Do you know an alternative?
 
  • #7
Here's some examples from IBM's manual on OPL:
Code:
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;
 
  • #8
That is a ternary operator like the one in the C language:
Code:
                   if()               then             else
int variable=(boolean statement) ? [when true] : [when false];

The example on line two above "concatenates" or nests operators
Code:
int signValue = [color=red](value>0) ? 1 :[/color][color=blue] (value<0) ? -1 : 0;[/color]

Maybe that will help...
 

What is an "If-then-else" construction?

An "If-then-else" construction is a programming structure that allows for conditional execution of code. It consists of an "if" statement which checks for a certain condition, and if that condition is met, it executes the code within the "if" block. If the condition is not met, then the code within the "else" block is executed.

What is the syntax for an "If-then-else" construction?

The syntax for an "If-then-else" construction is as follows:

if (condition) {

//code to be executed if condition is true

} else {

//code to be executed if condition is false

}

Can you have multiple "If-then-else" statements within each other?

Yes, you can have multiple "If-then-else" statements nested within each other. This is called "chaining" and allows for more complex conditional execution of code.

What happens if the condition in an "If-then-else" construction is not met?

If the condition in an "If-then-else" construction is not met, then the code within the "else" block will be executed. If there is no "else" block, then the program will move on to the next line of code outside of the "If-then-else" statement.

Can you have an "If-then" statement without an "else" statement?

Yes, you can have an "If-then" statement without an "else" statement. This means that if the condition is not met, the program will simply move on to the next line of code outside of the "If-then" statement. However, it is considered best practice to always include an "else" statement to handle all possible scenarios.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
444
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
2K
  • Differential Equations
Replies
1
Views
706
  • Introductory Physics Homework Help
Replies
17
Views
277
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
708
Back
Top