kaiwu.hobo package#

Module contents#

Module: hobo

Function: HOBO modeling tool

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

Bases: BinaryModel

HOBO model class that supports adding constraints

Args:

objective (Expression): objective function. hobo_default_penalty (float): default penalty coefficient for HOBO constraints, defaulting to 1

verify_hobo_constraint(solution_dict)[source]#

Verify whether the HOBO reduction constraints are satisfied.

Args:

solution_dict (dict): solution dictionary after HOBO reduction.

Returns:
tuple: constraint satisfaction information
  • int: number of unsatisfied HOBO constraints

  • dict: dictionary containing each HOBO constraint value

reduce(predefined_pairs=None)[source]#

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

Args:

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

Returns:

QuboModel: QuboModel. modelconstraint,

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

Args:

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,

If a string is passed, it is used as a common prefix; if a list of strings is passed, it 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,

generated automatically by default

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: list of all constraints.

get_value(solution_dict)#

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

Args:

solution_dict (dict): Result dictionary generated by get_sol_dict.

Returns:

float: Value obtained after substituting into the QUBO

initialize_penalties()#

Automatically initialize all penalty coefficients

set_constraint_handler(constraint_handler)#

Set the unconstrained conversion method for constraint terms

Args:

constraint_handler: Class for setting the unconstrained representation method of constraint terms

set_objective(objective)#

Set the objective function

Args:

objective (BinaryExpression): Objective function expression

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

Verify whether constraints are satisfied

Args:

solution_dict (dict): QUBO model solution dictionary

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

Returns:
tuple: Constraint satisfaction information
  • int: Number of unsatisfied constraints

  • dict: Dictionary containing constraint values