Operator.md - Open-CP/OCP GitHub Wiki

'Operator' Class

The "Operator class" is a fundamental component in our graph modeling framework, which represents a constraint/operator object. It represents a type of node that can only be linked to Variable nodes, handling operations or constraints between groups of variables.


Attributes

  • input_vars: List of input variables associated with the operator.
  • output_vars: List of output variables associated with the operator.
  • model_version: Integer indicating the version of the model that this operator is associated with.
  • ID: Unique identifier string for the operator.

Methods

display()

Returns the name of the operator's class.

Outputs:

  • Returns a string representing the name of the operator's class.

Example:

op = Operator(input_vars=[var1, var2], output_vars=[var3], ID="op1")
print(op.display())  # Output: Operator

get_var_ID(in_out, index, index2=None, unroll=False)

Returns the ID of a variable at a specified index from either the input or output list. It allows for returning either the full ID or a compressed version based on the unroll parameter.

Inputs

  • in_out: String specifying whether to access an input ('in') or an output ('out') variable.
  • index: Integer index of the variable in the specified list.
  • index2 (optional): Integer index for nested lists of variables, applicable when variables are structured in multi-dimensional arrays.
  • unroll (optional): Boolean indicating whether to return the full ID or a compressed version. Defaults to False which compresses the ID.

Outputs

  • String: The ID of the variable at the specified index. The format of the ID depends on the unroll parameter.

Example

# Example setup: Creating an instance of Operator with input and output variables
op = Operator(input_vars=[Variable(bitsize=8, ID="in_1"), Variable(bitsize=8, ID="in_2")],
              output_vars=[Variable(bitsize=8, ID="out_1")],
              ID="op1")

# Example 1: Get the ID of the first input variable without unrolling
input_id = op.get_var_ID('in', 0)
print(input_id)  # Expected Output: "in_1"

# Example 2: Get the ID of the output variable with unrolling
output_id = op.get_var_ID('out', 0, unroll=True)
print(output_id)  # Expected Output: "out_1"

# Example 3: Accessing a nested variable, assuming nested structure in inputs
# Here the second parameter index2 is used for nested structures
nested_input_id = op.get_var_ID('in', 0, 1, unroll=False)
print(nested_input_id)  # Output depends on nested structure and ID compression