Step-by-step guide to solving optimization problems
Published
February 8, 2026
1 Introduction
This tutorial walks through the complete process of formulating and solving optimization problems with Optyx. By the end, you’ll understand how to:
Define decision variables
Build objective functions
Add constraints
Solve and interpret results
2 Problem: Production Planning
A factory produces two products, A and B:
Product A yields $40 profit per unit
Product B yields $30 profit per unit
Constraints: - Machine time: 2 hours for A, 1 hour for B, 100 hours available - Labor: 1 hour for A, 2 hours for B, 80 hours available - Raw materials limit: 50 units total
Goal: Maximize profit.
3 Step 1: Define Variables
from optyx import Variable# Decision variables: units to producea = Variable("product_a", lb=0) # Non-negativeb = Variable("product_b", lb=0) # Non-negativeprint(f"Created variables: {a.name}, {b.name}")
Created variables: product_a, product_b
Tip
Always use descriptive variable names. They’ll appear in your solution output.
4 Step 2: Build the Objective
# Profit functionprofit =40*a +30*b# Test it workstest_profit = profit.evaluate({"product_a": 10, "product_b": 20})print(f"Profit for 10A + 20B = ${test_profit}")
Profit for 10A + 20B = $1000
5 Step 3: Add Constraints
# Resource constraintsmachine_time =2*a + b <=100# Machine hourslabor = a +2*b <=80# Labor hoursmaterials = a + b <=50# Raw materialsprint(f"Constraints created: {machine_time.sense}, {labor.sense}, {materials.sense}")
Constraints created: <=, <=, <=
6 Step 4: Create and Solve the Problem
from optyx import Problemprob = ( Problem("production_planning") .maximize(profit) .subject_to(machine_time) .subject_to(labor) .subject_to(materials))# Inspect the problem before solvingprint(prob.summary())
==================================================
PRODUCTION PLANNING SOLUTION
==================================================
Status: SolverStatus.OPTIMAL
Solve time: 1.8 ms
Optimal Production:
Product A: 50.0 units
Product B: -0.0 units
Maximum Profit: $2000.00
from optyx import Variable, Problem# Variablesx = Variable("x", lb=0)y = Variable("y", lb=0)# Create empty problemprob = Problem("incremental")# Add objectiveprob.maximize(40*x +30*y)# Add constraints one by oneprob.subject_to(2*x + y <=100)prob.subject_to(x +2*y <=80)prob.subject_to(x + y <=50)# Solvesolution = prob.solve()print(f"Same result: ${solution.objective_value:.2f}")
Same result: $2000.00
11 Common Patterns
11.1 Constraint from List
from optyx import Variable, Problemx = Variable("x", lb=0)y = Variable("y", lb=0)constraints = [ x + y >=10, x <=15, y <=15,2*x + y <=25]prob = Problem().minimize(x + y)for c in constraints: prob.subject_to(c)sol = prob.solve()print(f"Minimum: {sol.objective_value:.2f}")
Minimum: 10.00
11.2 Multiple Variables
from optyx import Variable, Problem# Create many variablesn =5vars= [Variable(f"x_{i}", lb=0, ub=10) for i inrange(n)]# Sum themtotal =sum(vars[i] for i inrange(n))# Objective: minimize sum of squaresobjective =sum(v**2for v invars)sol = ( Problem() .minimize(objective) .subject_to(total >=10) .solve())print("Optimal values:")for i, v inenumerate(vars):print(f" x_{i} = {sol[v.name]:.2f}")