A constraint class represents one optimization rule as a reusable Python object. Its generate_constraint() method converts that rule into solver-specific constraints from problem variables and any required inputs. section 10.3.1 section 13.3.2
Portfolio optimization uses separate subclasses for distinct rules:
- Tracking-error and volatility constraints use asset names, covariance data, reference weights, and an upper bound. section 10.3.1
- Rebalancing constraints can inspect tax-lot holdings, current dates, problem variables, and portfolio information before returning one or more constraints. section 13.3.2
A problem class accepts a list of constraint objects, calls generate_constraint() for each one, and combines the results with an objective such as maximum expected return. Rebalancing adds rules such as preventing trades beyond a target or limiting deviations from it.
A volatility-limited portfolio
Create LongOnlyConstraint(), FullInvestmentConstraint(), and VolatilityConstraint(..., 0.15), pass them to MaxExpectedReturnOpt, solve, and read the resulting weights. Perturbing expected returns can materially change the allocation, illustrating the practical sensitivity of constrained optimization.
