Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Integer and Binary Variables in Analytics | Integer Programming & Discrete Decision Models
Quizzes & Challenges
Quizzes
Challenges
/
Applied Optimization for Analytics

bookInteger and Binary Variables in Analytics

In analytics, the type of decision variable you use shapes the kinds of problems you can model and solve. Continuous variables can take any real value within a specified range, such as the amount of raw material to purchase or the number of hours to allocate to a taskβ€”these are useful when decisions can be fractional. Integer variables are restricted to whole numbers, making them essential when you need to count items, like the number of trucks to dispatch or products to manufacture. Binary variables are a special case of integer variables, taking only the values 0 or 1, and are critical for modeling yes/no or on/off decisions. For instance, when you must decide whether to select a project (1 if selected, 0 if not), or whether to open a facility, binary variables are the natural choice. In project selection, you might have a list of potential projects and a limited budget, and you need to decide which projects to undertakeβ€”fractional choices make no sense here, so binary variables are required to enforce this discrete selection.

1234567891011121314151617181920212223242526
from pulp import LpProblem, LpMaximize, LpVariable, lpSum, LpBinary # List of available projects with profits and costs projects = ["A", "B", "C", "D"] profits = {"A": 100, "B": 120, "C": 60, "D": 80} costs = {"A": 50, "B": 60, "C": 30, "D": 40} budget = 100 # Define the optimization problem model = LpProblem("Project_Selection", LpMaximize) # Define binary decision variables: 1 if project is selected, 0 otherwise x = {p: LpVariable(f"select_{p}", cat=LpBinary) for p in projects} # Objective: maximize total profit model += lpSum(profits[p] * x[p] for p in projects) # Constraint: total cost of selected projects cannot exceed budget model += lpSum(costs[p] * x[p] for p in projects) <= budget # Solve the model model.solve() # Output selected projects selected = [p for p in projects if x[p].value() == 1] print(f"Selected projects: {selected}")
copy
Note
Definition

An integrality constraint is a modeling requirement that forces certain decision variables to take only integer values, such as whole numbers or binary (0/1). Integrality constraints are essential when fractional solutions are not meaningfulβ€”such as when counting items or making yes/no decisions. In analytics, these constraints often make models more realistic but also increase computational complexity, as the solution space becomes non-continuous and harder to search.

question mark

When must you use binary variables in an analytics optimization model?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how the binary variables work in this example?

What would happen if the budget was increased or decreased?

How can I add more constraints to this optimization problem?

bookInteger and Binary Variables in Analytics

Swipe to show menu

In analytics, the type of decision variable you use shapes the kinds of problems you can model and solve. Continuous variables can take any real value within a specified range, such as the amount of raw material to purchase or the number of hours to allocate to a taskβ€”these are useful when decisions can be fractional. Integer variables are restricted to whole numbers, making them essential when you need to count items, like the number of trucks to dispatch or products to manufacture. Binary variables are a special case of integer variables, taking only the values 0 or 1, and are critical for modeling yes/no or on/off decisions. For instance, when you must decide whether to select a project (1 if selected, 0 if not), or whether to open a facility, binary variables are the natural choice. In project selection, you might have a list of potential projects and a limited budget, and you need to decide which projects to undertakeβ€”fractional choices make no sense here, so binary variables are required to enforce this discrete selection.

1234567891011121314151617181920212223242526
from pulp import LpProblem, LpMaximize, LpVariable, lpSum, LpBinary # List of available projects with profits and costs projects = ["A", "B", "C", "D"] profits = {"A": 100, "B": 120, "C": 60, "D": 80} costs = {"A": 50, "B": 60, "C": 30, "D": 40} budget = 100 # Define the optimization problem model = LpProblem("Project_Selection", LpMaximize) # Define binary decision variables: 1 if project is selected, 0 otherwise x = {p: LpVariable(f"select_{p}", cat=LpBinary) for p in projects} # Objective: maximize total profit model += lpSum(profits[p] * x[p] for p in projects) # Constraint: total cost of selected projects cannot exceed budget model += lpSum(costs[p] * x[p] for p in projects) <= budget # Solve the model model.solve() # Output selected projects selected = [p for p in projects if x[p].value() == 1] print(f"Selected projects: {selected}")
copy
Note
Definition

An integrality constraint is a modeling requirement that forces certain decision variables to take only integer values, such as whole numbers or binary (0/1). Integrality constraints are essential when fractional solutions are not meaningfulβ€”such as when counting items or making yes/no decisions. In analytics, these constraints often make models more realistic but also increase computational complexity, as the solution space becomes non-continuous and harder to search.

question mark

When must you use binary variables in an analytics optimization model?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt