liveBook · Manning

Constraint function

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A constraint function is a function that expresses a feasibility restriction on the values of decision variables in an optimization problem.

A constraint function expresses a feasibility restriction on decision variables. In optimization, it limits which candidate values are allowed, while a solver evaluates the objective and constraint functions for proposed decisions. section 1.4.3 section 2.1.3

For event ticket sales, let x represent physical tickets and y virtual tickets. The constraint x + y ≤ 10,000 prevents combined sales from exceeding venue capacity, turning profit maximization into a constrained optimization problem. section 2.1.3

Ticket allocation with a Lagrangian

The constraint can be written as g(x,y) = x + y − 10,000. A Lagrangian combines the profit function and g with a multiplier, then solves the resulting derivative equations. The reported optimum allocates 6,424 physical tickets and 3,576 virtual tickets. section 2.1.3

Listing 2.5 Maximizing profits using Lagrange multipliers
import sympy as sym
  
x,y=sym.var('x, y', positive=True)                     #1
  
f=155*x+(0.001*x**sym.Rational(3,2)+70)*y-1000000      #2
  
g=x+y-10000                                            #3
  
lamda=sym.symbols('lambda')                            #4
Lagr=f-lamda*g                                         #5
  
eqs = [sym.diff(Lagr, x), sym.diff(Lagr, y), g]        #6
  
sol=sym.solve(eqs,[x,y,lamda], dict=True)              #7
   
def getValueOf(k, L):
    for d in L:
        if k in d:
            return d[k]
  
profit=[f.subs(p) for p in sol]
  
print("optimal number of physical ticket sales: x = %.0f" % getValueOf(x, sol))
print("optimal number of online ticket sales: y = %.0f" % getValueOf(y, sol))
print("Expected profil: f(x,y) = $%.4f" % profit[0])

The Python listing encodes the constraint, constructs the Lagrangian, differentiates it, and solves for the decision variables and multiplier. section 2.1.3

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains inequality constraints, Lagrangian reformulation, derivative equations, and a SymPy implementation.
Examples
Uses a 10,000-ticket capacity limit and a physical-versus-virtual ticket allocation problem.
What it assumes
Assumes objective functions and decision variables, while introducing constraints through a concrete ticket-sales problem.
What you can do after
Apply it via SymPy to formulate and solve a constrained optimization problem with a Lagrangian.
How it connects
Connects constraint functions with objective functions, decision variables, Lagrange multipliers, and unconstrained optimization.

Also explained in

Compare how 2 books explain constraint function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.