CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyscipopt

Python interface and modeling environment for SCIP optimization solver

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

variables-constraints.mddocs/

Variables and Constraints

Comprehensive variable types and constraint system for mathematical programming models. PySCIPOpt supports continuous, integer, and binary variables along with extensive constraint types including linear constraints, special ordered sets, logical constraints, and advanced constraint forms.

Capabilities

Variable Types and Properties

Create variables with different types, bounds, and properties for various optimization modeling needs.

def addVar(self, name='', vtype='C', lb=0.0, ub=None, obj=0.0, 
           pricedVar=False, pricedVarScore=1.0):
    """
    Add a variable to the model.
    
    Parameters:
    - name (str): Variable name for identification
    - vtype (str): Variable type
        'C': Continuous variable (default)
        'I': Integer variable  
        'B': Binary variable (automatically sets bounds to [0,1])
    - lb (float): Lower bound (default: 0.0)
    - ub (float): Upper bound (default: infinity for continuous/integer, 1 for binary)
    - obj (float): Objective coefficient (default: 0.0)
    - pricedVar (bool): Mark as priced variable for column generation
    - pricedVarScore (float): Priority score for column generation
    
    Returns:
    Variable: Variable object that can be used in expressions and constraints
    """

Linear Constraints

Add linear constraints using expressions, inequalities, and equalities.

def addCons(self, cons, name='', initial=True, separate=True, 
            enforce=True, check=True, propagate=True, local=False, 
            modifiable=False, dynamic=False, removable=False, 
            stickingatnode=False):
    """
    Add a linear constraint to the model.
    
    Parameters:
    - cons: Constraint expression (Expr <= rhs, Expr >= lhs, lhs <= Expr <= rhs)
    - name (str): Constraint name
    - initial (bool): Include in initial LP relaxation
    - separate (bool): Subject to cutting plane separation
    - enforce (bool): Enforce during node processing
    - check (bool): Check for feasibility
    - propagate (bool): Subject to constraint propagation
    - local (bool): Valid only in current subtree
    - modifiable (bool): Can be modified during solving
    - dynamic (bool): Can be added during solving
    - removable (bool): Can be removed during solving
    - stickingatnode (bool): Stay at node even if removable
    
    Returns:
    Constraint: Linear constraint object
    """

def addConss(self, conss, **kwargs):
    """
    Add multiple constraints at once.
    
    Parameters:
    - conss (list): List of constraint expressions
    - **kwargs: Constraint properties (same as addCons)
    
    Returns:
    list: List of Constraint objects
    """

def addConsCoeff(self, cons, var, coeff):
    """
    Add coefficient to existing linear constraint.
    
    Parameters:
    - cons (Constraint): Existing constraint to modify
    - var (Variable): Variable to add
    - coeff (float): Coefficient value
    """

Special Ordered Sets (SOS)

Special ordered set constraints for modeling piecewise linear functions and logical relationships.

def addConsSOS1(self, vars, weights=None, name="SOS1cons", 
                initial=True, separate=True, enforce=True, 
                check=True, propagate=True, local=False, 
                dynamic=False, removable=False, stickingatnode=False):
    """
    Add SOS1 (Special Ordered Set of type 1) constraint.
    At most one variable in the set can be non-zero.
    
    Parameters:
    - vars (list): List of variables in the SOS1 set
    - weights (list): Priority weights for variables (default: 1, 2, 3, ...)
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: SOS1 constraint object
    """

def addConsSOS2(self, vars, weights=None, name="SOS2cons",
                initial=True, separate=True, enforce=True, 
                check=True, propagate=True, local=False, 
                dynamic=False, removable=False, stickingatnode=False):
    """
    Add SOS2 (Special Ordered Set of type 2) constraint.
    At most two adjacent variables in the ordered set can be non-zero.
    
    Parameters:
    - vars (list): List of variables in the SOS2 set (in order)
    - weights (list): Priority weights defining order (default: 1, 2, 3, ...)
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: SOS2 constraint object
    """

Logical Constraints

Boolean logic constraints for modeling logical relationships between binary variables.

def addConsAnd(self, vars, resvar, name="ANDcons", 
               initial=True, separate=True, enforce=True, 
               check=True, propagate=True, local=False, 
               modifiable=False, dynamic=False, removable=False, 
               stickingatnode=False):
    """
    Add AND constraint: resvar = var1 AND var2 AND ... AND varN
    
    Parameters:
    - vars (list): List of binary input variables
    - resvar (Variable): Binary result variable
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: AND constraint object
    """

def addConsOr(self, vars, resvar, name="ORcons",
              initial=True, separate=True, enforce=True, 
              check=True, propagate=True, local=False, 
              modifiable=False, dynamic=False, removable=False, 
              stickingatnode=False):
    """
    Add OR constraint: resvar = var1 OR var2 OR ... OR varN
    
    Parameters:
    - vars (list): List of binary input variables  
    - resvar (Variable): Binary result variable
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: OR constraint object
    """

def addConsXor(self, vars, rhsvar, name="XORcons",
               initial=True, separate=True, enforce=True, 
               check=True, propagate=True, local=False, 
               modifiable=False, dynamic=False, removable=False, 
               stickingatnode=False):
    """
    Add XOR constraint: rhsvar = var1 XOR var2 XOR ... XOR varN
    
    Parameters:
    - vars (list): List of binary variables
    - rhsvar (Variable): Binary result variable (1 if odd number of vars are 1)
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: XOR constraint object
    """

Cardinality Constraints

Constraints limiting the number of non-zero variables in a set.

def addConsCardinality(self, consvars, cardval, indvars=None, weights=None,
                       name="CARDcons", initial=True, separate=True, 
                       enforce=True, check=True, propagate=True, local=False, 
                       dynamic=False, removable=False, stickingatnode=False):
    """
    Add cardinality constraint limiting number of non-zero variables.
    
    Parameters:
    - consvars (list): Variables subject to cardinality constraint
    - cardval (int): Maximum number of variables that can be non-zero
    - indvars (list): Binary indicator variables (optional)
    - weights (list): Priority weights for variables (optional)
    - name (str): Constraint name
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: Cardinality constraint object
    """

Indicator Constraints

Constraints that are only active when a binary variable takes a specific value.

def addConsIndicator(self, cons, binvar=None, name="INDcons",
                     initial=True, separate=True, enforce=True, 
                     check=True, propagate=True, local=False, 
                     dynamic=False, removable=False, stickingatnode=False):
    """
    Add indicator constraint: binvar = 1 => cons holds
    
    Parameters:
    - cons: Linear constraint that is activated by indicator
    - binvar (Variable): Binary indicator variable (created if None)
    - name (str): Constraint name  
    - Other parameters: Same as addCons
    
    Returns:
    Constraint: Indicator constraint object
    """

Variable and Constraint Information

Access properties and information about variables and constraints.

def getVars(self):
    """
    Get all variables in the model.
    
    Returns:
    list: List of all Variable objects
    """

def getConss(self):
    """
    Get all constraints in the model.
    
    Returns:
    list: List of all Constraint objects
    """

def getNVars(self):
    """
    Get number of variables in the model.
    
    Returns:
    int: Number of variables
    """

def getNConss(self):
    """
    Get number of constraints in the model.
    
    Returns:
    int: Number of constraints
    """

Usage Examples

Variable Types

from pyscipopt import Model

model = Model("variable_types")

# Continuous variable with bounds
x_cont = model.addVar(name="x_continuous", vtype="C", lb=0.0, ub=10.5)

# Integer variable
x_int = model.addVar(name="x_integer", vtype="I", lb=-5, ub=20)

# Binary variable (bounds automatically set to [0,1])
x_bin = model.addVar(name="x_binary", vtype="B")

# Variable with objective coefficient
x_obj = model.addVar(name="x_with_obj", obj=2.5, lb=0)

print(f"Created {model.getNVars()} variables")

Linear Constraints

from pyscipopt import Model

model = Model("linear_constraints")

# Create variables
x = model.addVar(name="x", lb=0)
y = model.addVar(name="y", lb=0) 
z = model.addVar(name="z", lb=0)

# Inequality constraints
model.addCons(2*x + 3*y <= 10, name="resource_limit")
model.addCons(x + y >= 2, name="minimum_requirement")

# Equality constraint
model.addCons(x + 2*y + z == 5, name="balance_constraint")

# Range constraint (double-bounded)
model.addCons(1 <= x + y <= 3, name="range_constraint")

print(f"Created {model.getNConss()} constraints")

Special Ordered Sets

from pyscipopt import Model

model = Model("sos_example")

# Variables for piecewise linear function
x1 = model.addVar(name="x1", lb=0, ub=1)
x2 = model.addVar(name="x2", lb=0, ub=1) 
x3 = model.addVar(name="x3", lb=0, ub=1)
x4 = model.addVar(name="x4", lb=0, ub=1)

# SOS1: At most one variable can be non-zero
model.addConsSOS1([x1, x2, x3, x4], name="sos1_constraint")

# Variables for piecewise linear with adjacent points
y1 = model.addVar(name="y1", lb=0, ub=1)
y2 = model.addVar(name="y2", lb=0, ub=1)
y3 = model.addVar(name="y3", lb=0, ub=1)

# SOS2: At most two adjacent variables can be non-zero
model.addConsSOS2([y1, y2, y3], weights=[1, 2, 3], name="sos2_constraint")

Logical Constraints

from pyscipopt import Model

model = Model("logical_constraints")

# Binary variables
a = model.addVar(name="a", vtype="B")
b = model.addVar(name="b", vtype="B")
c = model.addVar(name="c", vtype="B")
result = model.addVar(name="result", vtype="B")

# AND constraint: result = a AND b AND c
model.addConsAnd([a, b, c], result, name="and_constraint")

# OR constraint: result = a OR b OR c
or_result = model.addVar(name="or_result", vtype="B")
model.addConsOr([a, b, c], or_result, name="or_constraint")

# XOR constraint: result = a XOR b XOR c  
xor_result = model.addVar(name="xor_result", vtype="B")
model.addConsXor([a, b, c], xor_result, name="xor_constraint")

Indicator Constraints

from pyscipopt import Model

model = Model("indicator_example")

# Variables
x = model.addVar(name="x", lb=0, ub=10)
y = model.addVar(name="y", lb=0, ub=10)
indicator = model.addVar(name="indicator", vtype="B")

# Indicator constraint: if indicator = 1, then x + y <= 5
constraint_expr = x + y <= 5
model.addConsIndicator(constraint_expr, indicator, name="indicator_cons")

# Alternative: let the system create the indicator variable
auto_indicator_cons = model.addConsIndicator(x + 2*y >= 3, name="auto_indicator")

Cardinality Constraints

from pyscipopt import Model

model = Model("cardinality_example")

# Create variables for portfolio selection
stocks = []
for i in range(10):
    stock = model.addVar(name=f"stock_{i}", lb=0, ub=1)
    stocks.append(stock)

# Cardinality constraint: select at most 3 stocks
model.addConsCardinality(stocks, 3, name="max_3_stocks")

# With weights to prioritize certain stocks
weights = [i+1 for i in range(10)]  # Higher weight = higher priority
model.addConsCardinality(stocks, 5, weights=weights, name="weighted_selection")

Install with Tessl CLI

npx tessl i tessl/pypi-pyscipopt

docs

core-model.md

expressions.md

index.md

math-functions.md

parameters.md

plugins.md

variables-constraints.md

tile.json