kaiwu.hobo package#

Module contents#

Module: hobo

Function: HOBO modeling tool

class kaiwu.hobo.HoboModel(objective=None, hobo_default_penalty=1)#

Bases: BinaryModel

HOBO model class that supports adding constraints

Parameters:
  • objective (Expression) – Set the objective function

  • hobo_default_penalty (float) – HOBO约束的默认惩罚系数, 默认值为1

verify_hobo_constraint(solution_dict)#

Verify whether the HOBO reduction constraints are satisfied.

Parameters:

solution_dict (dict) – solution dictionary after HOBO reduction.

Returns:

constraint satisfaction information
  • int: number of unsatisfied HOBO constraints

  • dict: dictionary containing each HOBO constraint value

Return type:

tuple

reduce(predefined_pairs=None)#

Reduce the order of high-order expressions in the HOBO Model (to second-order)

Parameters:

predefined_pairs (list) – Predefined list of variable pairs to merge, formatted as [(var1, var2), …]

Returns:

QuboModel: QuboModel. modelconstraint,

Return type:

QuboModel

add_constraint(constraint_in, name=None, constr_type: Literal['soft', 'hard'] = 'hard', penalty=1, slack_var_expr=None)#

Add constraint terms; single or multiple constraints are supported

Parameters:
  • constraint_in

    constraint expression; two input types are supported:

    1. Single constraint: BinaryExpression Constraint for example: quicksum(x) - 1 Constraint(quicksum(x) - 1, "==", 1)

    2. Multiple constraints: list/tuple/np.ndarray, automatically iterated and added one by one for example: [constraint1, constraint2, constraint3]

  • name (str or list, optional) – constraint name, automatically named by default. When multiple constraints are provided, a string is used as a common prefix; a list of strings must match the number of constraints.

  • penalty (float, optional) – default penalty coefficient

  • constr_type (str, optional) – Constraint type, can be set to “soft” or “hard”, defaults to “hard”

  • slack_var_expr (BinaryExpression, optional) – slack variable expression, used only in inequality constraints and generated automatically by default

Examples

Example1 (Single BinaryExpression):
>>> import kaiwu as kw
>>> model = kw.core.QuboModel()
>>> x = [kw.core.Binary(f"x{i}") for i in range(3)]
>>> model.add_constraint(kw.core.quicksum(x) - 1)
Example2 (Single Constraint with relation operator):
>>> from kaiwu.core._constraint import Constraint
>>> model.add_constraint(Constraint(kw.core.quicksum(x) - 1, "==", 1))
Example3 (Multiple Constraints):
>>> constraints = [x[i] - 1 for i in range(3)]
>>> model.add_constraint(constraints, name="my_constraints")
compile_constraints()#

Convert constraint terms to Expression according to different styles

get_constraints_expr_list()#

Get all current constraints.

Returns:

list of all constraints.

Return type:

list

get_value(solution_dict)#

Substitute variable values into QUBO variables based on the result dictionary.

Parameters:

solution_dict (dict) – Result dictionary generated by get_sol_dict.

Returns:

Value obtained after substituting into the QUBO

Return type:

float

initialize_penalties()#

Automatically initialize all penalty coefficients

set_constraint_handler(constraint_handler)#

Set the unconstrained conversion method for constraint terms

Parameters:

constraint_handler – Set the unconstrained conversion method for constraint terms

set_objective(objective)#

Set the objective function

Parameters:

objective (BinaryExpression) – Objective function expression

verify_constraint(solution_dict, constr_type: Literal['soft', 'hard'] = 'hard')#

Verify whether constraints are satisfied

Parameters:
  • solution_dict (dict) – QUBO model solution dictionary

  • constr_type (str, optional) – Constraint type, can be set to “soft” or “hard”, defaults to “hard”

Returns:

Constraint satisfaction information
  • int: Number of unsatisfied constraints

  • dict: Dictionary containing constraint values

Return type:

tuple