Skip to content

Constraint and Penalty

Penalty

Penalty term without constraint.

Incorporate it into the model as a penalty term. It is not evaluated as a constraint, so it is not used to determine if a solution is feasible. However, it is evaluated separately from the objective function. It is mainly used to include conditions that you want to treat as soft constraints as penalty terms. The penalty class can also be used when you would like to express the constraint as a penalty that you define, rather than one that is automatically changed by QUBO.

penalty_term: _expression.Expression property

Penalty term (without multiplier).

expression: _expression.Expression property

Penalty term (without multiplier).

with_multiplier: bool property

Penalty term has lagrange muliplier or not.

__init__(label, penalty_term, forall=[], with_multiplier=None)

Initializes a Penalty object without constraint.

Parameters:

Name Type Description Default
label str

A unique identifier for the penalty term.

required
penalty_term _expression.Expression

The expression representing the penalty term.

required
forall tuple

A list of elements and optional conditions that the penalty term applies to. Default is an empty list.

[]
with_multiplier typ.Optional[bool], deprecated

A flag indicating whether to use a multiplier in the penalty term. This argument is deprecated and will be removed in the next version. Default is None.

None
Example
import jijmodeling as jm
n = jm.Placeholder('n')
x = jm.BinaryVar('x', shape=n)
pe = jm.Penalty('p', (x[:]-1)**2) # one-hot constraint

Constraint

Constraint of optimization model.

__init__(label, condition, forall=[], with_penalty=None, with_multiplier=None, left_lower=None, auto_qubo=None)

Constraint of optimization model.

Parameters:

Name Type Description Default
name str

A unique identifier for the penalty term.

required
expression Union[CompareCondition, Expression]

constraint condition.

required
forall Element | Tuple[Element, Optional[Condition]] | List[Element | Tuple[Element, Optional[Condition]]]

A list of elements and optional conditions that the penalty term applies to. Default is an empty list.

[]
with_penalty bool

Add constraints as penalty. This argument is deprecated and will be removed in the next version. Defaults to True.

None
with_multiplier bool

prod a multiplier on the penalty term. This argument is deprecated and will be removed in the next version. Defaults to True.

None
left_lower Union[_expression.NumericValue, _expression.Expression, None]

left hand side lower value. usually used for slack variables. This argument is deprecated and will be removed in the next version. Defaults to None.

None
auto_qubo bool

auto qubo convert. This argument is deprecated and will be removed in the next version. Defaults to True.

None

Examples:

import jijmodeling as jm
n = jm.Placeholder("n")
x = jm.BinaryVar("x", shape=(n, ))
term = jm.Constraint("const", x[:] == 1)

constraint with forall

import jijmodeling as jm
d = jm.Placeholder("d", ndim=1)
n = d.shape[0]
x = jm.BinaryVar("x", shape=(n, n))
i = jm.Element("i", n)
term = jm.Constraint("const2", x[i, :] == 1, forall=(i, d[i] > 0))

Raises:

Type Description
TypeError

condition is not CompareCondition or Experssion.

ExpressionValiddteError

right hand side has a desicision variable.

TypeError

left_lower is not numbers.Number or Expression.

TypeError

forall is not Element, dict, tuple or list.

ExpressionIndexError

The indices have not been reduced and remains. Ex: Sum(i, x[i,j,k]) == 1, forall=[j], In this case "k" is needed in forall.

ExpressionIndexError

The conditions of indices have inconsistency.

CustomPenaltyTerm(name, expression, *, forall=[], with_multiplier=None)

Initializes a Penalty object without constraint.

Parameters:

Name Type Description Default
name str

A unique identifier for the penalty term.

required
expression _expression.Expression

The expression representing the penalty term.

required
forall Element | Tuple[Element, Optional[Condition]] | List[Element | Tuple[Element, Optional[Condition]]]

A list of elements and optional conditions that the penalty term applies to. Default is an empty list.

[]
with_multiplier typ.Optional[bool], deprecated

A flag indicating whether to use a multiplier in the penalty term. This argument is deprecated and will be removed in the next version. Default is None.

None
Example
import jijmodeling as jm
n = jm.Placeholder('n')
x = jm.BinaryVar('x', shape=n)
pe = jm.CustomPenaltyTerm('p', (x[:]-1)**2) # one-hot constraint

Last update: 2023年7月5日