Skip to main content

openjij_decode

class CompiledInstance

CompiledInstance: object return compile_model method.

Attributes

  • sense(jijmodeling.ProblemSense) : problem sense minimize or maximize.
  • objective(SubstitutedExpression) : objective expression.
  • constraint(dict[str, dict[tuple[int, ...], SubstitutedExpression]]) : constraints. str key represents name of constraint. tuple[int,...] is values of forall index.
  • penalty : dict[str, dict[tuple[int, ...], SubstitutedExpression]]
  • var_map : VariableMap
  • data : InstanceData
  • problem : jijmodeling.InstanceData

Examples

import jijmodeling as jm
import jijmodeling_transpiler as jmt
n = jm.Placeholder("n")
x = jm.Binary("x", (n, n))
i = jm.Element("i", n)
problem = jm.Problem("sample")
problem += x[:, :]
problem += jm.Constraint("onehot", x[:, i], forall=i)
compiled_instance = jmt.core.compile_model(problem, {"n": 2}, {})
compiled_instance
CompiledInstance(
objective=SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0}, constant=0.0),
nonlinear=None),
constraint={
'onehot': {
(0,): SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={0: 1.0, 1: 1.0}, constant=0.0),
nonlinear=None),
(1,): SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={2: 1.0, 3: 1.0}, constant=0.0),
nonlinear=None)}
},
penalty={},
var_map=VariableMap(var_map={'x': {(0, 0): 0, (0, 1): 2, (1, 0): 1, (1, 1): 3}},
var_num=4,
integer_bound={}),
...
)

add (self, source) -> -


class PuboBuilder

A class to build a PUBO from a problem and an instance.

Attributes

  • objective(BinaryModel) : a binary model of the objective function
  • linear_penalty(dict[str, dict[tuple[int, ...], BinaryModel]]) : a binary model of linear penalties
  • penalty(dict[str, dict[tuple[int, ...], BinaryModel]]) : a binary model of penalties
  • custom_penalty(dict[str, dict[tuple[int, ...], BinaryModel]]) : a binary model of custom penalties
  • binary_encoder(dict[str, IntegerEncoder]) : a binary encoder for each variable
  • constraint_expr(dict[str, jm.Constraint]) : a constraint expression
  • obj_norm(float) : a normalization factor of the objective function
  • const_norm(dict[str, dict[tuple[int, ...], float]]) : a normalization factor of constraints
  • pena_norm(dict[str, dict[tuple[int, ...], float]]) : a normalization factor of penalties

get_hubo_dict (self, multipliers, detail_parameters) -> tuple[dict[tuple[int, ...], float], float]

Get a HUBO from the built PUBO.

A constraint with label=const

gk(x)=Ck,kg_k(x) = C_k, \forall k

is converted to a penalty term by the following equation:

Aconst[kλk(gk(x)Ck)2+kμk(gk(x)Ck)]A_{\text{const}} \left[\sum_{k} \lambda_k \left( g_k(x) - C_k \right)^2 + \sum_k \mu_k (g_k(x) - C_k) \right]

where λk\lambda_k and μk\mu_k are detail parameters for each constraint and AconstA_{\text{const}} is uniformal weight for each penalties.

AA is parameters provided to set the weights for each penalty, independently of the index of the constraint and penalty. You can set AA using multipliers. By default A=1A=1.

The weight of parameters λk\lambda_k and μk\mu_k are set by detail_parameters. By default, λk=1\lambda_k=1 and μk=0\mu_k=0 for the penalty is converted from equality constraint, and λk=1\lambda_k=1 and μk=1\mu_k=1 for the penalty is converted from inequality constraint.

For example, the following constraint

i=1Nxij=1,j{0,1} label=onehot\sum_{i=1}^N x_{ij} = 1, \forall j \in \{0, 1\}~\text{label=onehot}

is converted to the following penalty term with multipliers={"onehot": 1} and detail_parameters={"onehot": {(1,): (1/2, 1)}} that means Aonehot=1A_{\text{onehot}}=1 and λ1=1/2,μ1=1\lambda_{1}=1/2, \mu_{1}=1:

Aonehotj(λj(i=1Nxij1)2+μj(i=1Nxij1))=(i=1Nxi01)2+12(i=1Nxi11)2+(i=1Nxi11)\begin{align} &A_{\text{onehot}}\sum_j \left(\lambda_j \left(\sum_{i=1}^N x_{ij} - 1\right)^2 + \mu_j \left(\sum_{i=1}^N x_{ij} - 1\right)\right)\\ &= \left(\sum_{i=1}^N x_{i0} - 1\right)^2 + \frac{1}{2}\left(\sum_{i=1}^N x_{i1} - 1\right)^2 + \left(\sum_{i=1}^N x_{i1} - 1\right) \end{align}

Parameters

  • multipliers(dict[str, float]) : a multiplier for each penalty. Defaults to None.
  • detail_parameters(dict[str, dict[tuple[int, ...], tuple[float, float]]]) : detail parameters for each penalty. Defaults to None.

Returns

  • tuple[dict[tuple[int, ...], float], float] : a HUBO dictionary and a constant term

get_qubo_dict (self, multipliers, detail_parameters) -> dict[tuple[int, int], float]

Get a QUBO COO format dictionary.

Please see the document of get_hubo_dict for the detail.

Parameters

  • multipliers(dict[str, float]) : a multiplier for each penalty. Defaults to None.
  • detail_parameters(dict[str, dict[tuple[int, ...], tuple[float, float]]]) : detail parameters for each penalty. Defaults to None.

Returns

  • dict[tuple[int, int], float] : a QUBO dictionary

decode_from_dict_binary_result (samples, binary_encoder, compiled_model) -> -


decode_from_openjij (response, pubo_builder, compiled_model) -> jm.SampleSet

decode from openjij response to jijmodeling SampleSet.

Parameters

  • response(oj.Response) : openjij response.
  • pubo_builder(PuboBuilder) : pubo builder.
  • compiled_model(CompiledInstance) : compiled model.

Returns

  • jm.SampleSet : decoded sample set.