Learn
Lesson · Integer Programming · Modelling with binaries

Binary Variables and Logical Constraints

A variable restricted to 0 or 1 is a yes-or-no decision, and the connectives of logic become linear inequalities on such variables. The point of the lesson is not the list of encodings but the check: an inequality is compared with the condition on every assignment, and a wrong encoding is refuted by a row rather than argued about.

y ∈ {0, 1} y = 1 the project is chosen, y = 0 it is not if A then B y_A − y_B ≤ 0 not y_B − y_A ≤ 0 at least one of A, B y_A + y_B ≥ 1 at most k Σ y ≤ k exactly one Σ y = 1 both y_A + y_B ≥ 2 an encoding is checked on all 2ⁿ assignments; one disagreeing row refutes it
Five conditions, and the check that settles each one

Core ideas

What this lesson establishes

The encodings are a short list and they are not the hard part. The hard part is that an encoding is a claim about every assignment, and claims like that are settled by evidence rather than by plausibility.

1

A binary is a decision, not a quantity

y_A = 1 means “project A is chosen” and y_A = 0 means it is not. There is no y_A = 0.6, and the first thing to write next to any binary is the sentence it stands for. Half of all wrong encodings are wrong because the author never fixed which way round 1 meant.

2

A connective becomes an inequality, and the direction is the content

“If A then B” is y_A − y_B ≤ 0, which reads: A may not be chosen unless B is. Swap the two letters and you have the converse, which permits A without B — the one case the condition forbids. The inequality is short enough that nothing in its appearance tells you which of the two you have written.

3

An encoding is refuted by a row

There are 2ⁿ assignments and the encoding must agree with the condition on every one of them. With three binaries that is eight rows, which can be read in a few seconds, and the converse of “if A then B” disagrees on four of them. A reader who has seen the row y_A = 1, y_B = 0 does not write the converse again.

The material

Yes-or-no decisions, and encodings that have been checked

The five conditions almost every practical model is built from, and the eight-row check that certifies each one.

Definition · Binary variable

A binary variable is a variable restricted to the set {0, 1}. It carries a decision: 1 for the thing happening and 0 for it not happening. A model with binaries and nothing else is a binary or 0/1 programme; a model with binaries and continuous variables together is a mixed integer programme.

Definition · Encoding of a condition

A linear inequality on binaries encodes a condition when, for every assignment of 0 and 1 to the variables, the inequality holds exactly when the condition holds. One assignment on which the two disagree refutes the encoding, and one is all it takes.

That definition is why the lab evaluates your inequality on every assignment beside the truth table of the condition. Discrete Mathematics builds those tables in “Logical Connectives” and “Truth Tables”, and the check here is the same object with one extra column: what the inequality evaluates to, and whether it holds.

Example · If A then B, and the row that refutes the converse

The condition forbids one combination and permits the other three: y_A = 1, y_B = 0 is out, and (0,0), (0,1), (1,1) are in. The inequality y_A − y_B ≤ 0 agrees on all eight assignments of three binaries. The converse y_B − y_A ≤ 0 disagrees on four of them, and the first the lab reports is y_A y_B y_C = 1 0 1: the condition fails there and the inequality holds, so the converse permits exactly the case the condition was written to forbid.

Five conditions and the inequality each one is

  • If A then B. y_A − y_B ≤ 0. Equivalently y_A ≤ y_B: choosing A forces B, and B alone is allowed.
  • At least one of A and B. y_A + y_B ≥ 1. Both is allowed, because “or” here is the inclusive one.
  • At most k of them. y_A + y_B + y_C ≤ k. With three projects and k = 2 the only excluded assignment is all three.
  • Exactly one. y_A + y_B + y_C = 1. An equality, and the ≤ 1 version is a different condition.
  • Both A and B. y_A + y_B ≥ 2, or simply the two assignments y_A = 1 and y_B = 1 if one row is not required.

The ≤ 1 against = 1 pair is worth one more sentence, because the two conditions differ on a single assignment and it is the empty one. “At most one” permits choosing nothing; “exactly one” does not. The lab refutes y_A + y_B + y_C ≤ 1 as an encoding of “exactly one” on the row 0 0 0, and on that row alone.

Example · Both is not a product

The condition “both A and B” describes one assignment, and the expression y_A · y_B is not linear, so it has no place in a linear model at all. Nothing is lost: y_A + y_B ≥ 2 holds on 1 1 and fails on the other three combinations, which is exactly the condition. y_A + y_B ≥ 1 is the encoding readers reach for instead, and it is refuted on four rows — the first being 1 0 1, where one project is chosen and the inequality is satisfied.

Checking costs 2ⁿ rows, so it is affordable for a handful of binaries and not for fifty. That is not a limitation of the method: the encodings being checked are local — two or three variables at a time — and a model with fifty binaries is built from rows that each involve a few. Verify the row, then use it.

The habit generalises past the point where a truth table fits. In the two lessons after next, the condition is about continuous quantities and the check is a pair of re-solves rather than eight rows, but the discipline is identical: fix the binary at each value, look at what the model then says, and confirm it is what you meant.

Interactive

Logic as inequalities

An encoding is checked on every assignment, and a wrong one is refuted by a row

An encoding, checkedan inequality either agrees with the condition on every assignment or it is refuted by one
the two agreethey disagreethe condition holdsit does not

Method

Writing an encoding you can defend

The first two steps are prose and the last two are evidence. Skipping either pair is how a wrong row ships.

1

Name each binary and say what 1 means

In a sentence, on the page. “y_A = 1 when the extension is built.” An encoding cannot be checked against a condition until both are written down in the same words.

2

State the condition with no arithmetic in it

“We may not build the extension unless the yard is resurfaced.” Which combinations are forbidden is now readable, and it is the only thing the inequality has to reproduce.

3

Propose the inequality

Usually a sum of the binaries against a constant. Write the relation and the right-hand side deliberately: ≥ 1, ≤ 1 and = 1 are three different conditions, and ≤ 2 differs from ≤ 3 on exactly one assignment.

4

Evaluate it on every assignment beside the condition

Two columns, 2ⁿ rows, and a third column saying whether they agree. This is the step that turns an opinion into a fact, and it is the whole reason the lab exists.

5

Keep the refuting row

When an encoding fails, write down the assignment that killed it next to the encoding that survived. y_A = 1, y_B = 0 is worth more than the rule it corrects, because it is what you will remember.

Conditions, encodings and refuting rows

Score 0 / 0

Four projects, four conditions, one table each

A capital budget with four candidate projects. Each condition is stated, encoded, and then checked on the assignments its variables range over.

y₁ y₂ y₃ y₄ ∈ {0, 1} y = 1 means the project is funded 1 the warehouse needs the access road first if y₁ then y₂ y₁ − y₂ ≤ 0 y₁ y₂ = 0 0 condition holds 0 ≤ 0 holds agree 0 1 holds −1 ≤ 0 holds agree 1 0 FAILS 1 ≤ 0 fails agree 1 1 holds 0 ≤ 0 holds agree the converse y₂ − y₁ ≤ 0 holds at 1 0, where the condition fails 2 fund at most two of the first three y₁ + y₂ + y₃ ≤ 2 excluded assignment: 1 1 1 only ≤ 3 is refuted by that one row, and by no other 3 fund exactly one of the two depots y₃ + y₄ = 1 ≤ 1 is refuted by 0 0 4 fund both or neither of the paired upgrades y₁ − y₂ ≤ 0 and y₂ − y₁ ≤ 0, which together are y₁ = y₂ here the converse is not an error: both directions were wanted

Condition 4 is the reason the converse must be recognised rather than merely avoided. y_B − y_A ≤ 0 is a perfectly good inequality; it encodes “if B then A”. Writing both directions encodes equivalence, and that is sometimes the condition. What is never right is writing one and believing it says the other.

Condition 2 shows how thin the margin can be. ≤ 2 and ≤ 3 differ on one assignment out of eight, and with ≤ 3 the row is satisfied by every assignment there is — a constraint that constrains nothing, which is a harder defect to notice than an infeasible model.

For a faded rehearsal, encode “if either depot is funded then the access road is” and check it. The supplied first move is that this is two implications rather than one, so it is y₃ − y₂ ≤ 0 together with y₄ − y₂ ≤ 0. Decide before opening the quiz whether the single row y₃ + y₄ − 2y₂ ≤ 0 encodes the same condition, and name the assignment that settles it.

Common mistakes

1
Writing the converse for if-theny_A ≥ y_B permits choosing A while leaving B unfunded, which is the one thing “if A then B” forbids. The assignment y_A = 1, y_B = 0 is where it shows, and it is the row to keep: the lab reports it as the first of four disagreements once the third binary is free to be either value.
2
Using ≤ 1 for at least one“At most one” and “at least one” differ on two assignments and share the word “one”, which is the whole trap. ≥ 1 forbids the empty choice; ≤ 1 forbids the full one. Reading the relation aloud as “at least” or “at most” before writing it catches this every time.
3
Multiplying binariesy_A y_B for “both”, y_A(1 − y_B) for “A but not B”: both are natural and neither is linear, so a solver that accepts them is not solving a linear programme. Every condition on this list has a linear encoding, and where a product genuinely seems necessary the model usually wants one more binary rather than one fewer.

Check your understanding

✓
Finish when writing an encoding without checking it feels like asserting a theorem without proving one.

You should be able to translate if-then, at-least-one, at-most-k, exactly-one and both into inequalities on binaries, verify each on every assignment of its variables, and name the row that would refute the encoding a reader reaches for instead — y_A = 1, y_B = 0 for the converse, and 0 0 0 for “at most one” standing in for “exactly one”.

Note: “Both” is not y_A · y_B. A product is not linear and it is not needed: “both” is the pair of assignments y_A = 1 and y_B = 1, or the single row y_A + y_B ≥ 2 when one row is wanted. The same instinct produces y_A(1 − y_B) for “A but not B”, which is y_A − y_B ≥ 1 written badly — and that one the lab will check for you.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.