1
votes

I have two variables a >= 1 and b = {0, 1}, so a is an integer positive variable /=0, and b is a binary variable. I want to make b=1 when a=1, and b=0 when a>1, and I need to write it into a linear program. So the condition is:

if a=1 then b=1 else b=0

which is equivalent to:

if a<2 then b=1 else b=0

because a cannot be 0 due to other constraints.

anyone knows how to write it in a linear program?

2
This is not related to "linear programming". What is programming language? What are exact constraints for solving this task? What's your progress so far? - Matt
I think the OP just misuses the term "Linear Programming" - Emilien
I'm writing a linear program, not a high level programming language program. I think the correct answer to my question is: a <= b+My; a >= b+2y; b+y = 1; where y is a binary variable and M is a big number. can somebody confirm that? - nino

2 Answers

3
votes

OP, provided that M is greater than any possible value of the positive integer a, the answer you suggest in your comment is correct, namely:

a <= b+My; a >= b+2y; b+y = 1, where y is binary

To confirm, we just have to examine this set of statements to demonstrate that (1) the desired outcomes are feasible and (2) all other outcomes are infeasible. Since b is a binary variable in this case, "all other outcomes" means simply the other outcome: b=0 vs b=1.


Proof

(a = 1) => (b = 1)

To begin, consider the condition:

if a=1 then b=1

Let a=1 in your suggested answer, obtaining:

1 <= b+My; 1 >= b+2y; b+y = 1

Clearly, b=1 is feasible, because we can find a value of y that allows all of the statements to be true (namely, y=0):

1 <= 1+M*0; 1 >= 1+2*0; 1+0 = 1

On the other hand, if we let b=0:

1 <= 0+My; 1 >= 0+2y; 0+y = 1

This implies that y=1, which leads to a contradiction in the second statement:

1 <= 0+M*1; 1 >= 0+2*1; 0+1 = 1

Therefore, if a=1, then it must be that b=1.


(a > 1) => (b = 0)

Next, consider the contrapositive of the prior condition (to satisfy the "else" clause):

if a>1 then b=0

Following the same procedure, let a>1 in your suggested answer - I'll put a 5, but keep in mind that this could be any integer greater than 1:

5 <= b+My; 5 >= b+2y; b+y = 1

We see that b=0 is feasible, because we can find a value of y that makes all of the statements true (namely, y=1):

5 <= 0+M*1; 5 >= 0+2*1; 0+1 = 1

(This is the point at which it is important that M be greater than any possible value of a, otherwise the first statement could be untrue.)

On the other hand, if we let b=1:

5 <= 1+My; 5 >= 1+2y; 1+y = 1

This implies that y=0, which leads to a contradiction in the first inequality:

5 <= 1+M*0; 5 >= 1+2*0; 1+0 = 1

Therefore, if a>1, then it must be that b=0.


Conclusion

Therefore, this set of statements

a <= b+My; a >= b+2y; b+y = 1, where y is binary

satisfies the condition:

if a=1 then b=1 else b=0

-1
votes

An example in C++:

#include <iostream>

int main()
{
    // declare variables at top...
    int a = 1;
    int b = 0;

    // then some code that changes the initial value of a...

    // code to check the value of a and change the value of b...
    if (a == 1) {
        b = 1;
    } else if (a > 1) {
        b = 0;
    }

    // output value of b
    std::cout << "Value of b: " << b << std::endl;

    return 0;
}