Python API Reference Manual

The Python API consists of the following modules:
  • Constraint - Used to manage constraints

  • DataFrame - Used for the creation and processing of sets and parameter data

  • MAPL - Used to initiate and manage MAPL instances

  • Objective - Used to manage objective functions

  • Tuple - Used to represent indexes

  • Variable - Used to manage decision variables

Each interface comes with corresponding documentation and examples, which can also be viewed using the help function. Before running the examples under the interface documentation, execute the following statements to complete the necessary model initialization.

>>> import maplpy as mp
>>> m = mp.MAPL()
>>> m.read("steel4.mpl")
class maplpy.Constraint

Bases: pybind11_object

Constraint - The constraint class is used to bind a constraint entity in the optimization model. Use MAPL.getConstraint to get and bind the constraint entity. A constraint entity may be a single constraint or a multi-dimensional constraint; it is a collection of constraints with the same name but different indexes. This entity class allows you to get constraint properties such as name, bounds, and expression value.

>>> cons = m.getConstraint("Time")
>>> x.lb = 10
>>> x.ub = 20
>>> x.name = "x"
>>> x.value
__getitem__(self: maplpy.Constraint, position: int) maplpy.Constraint

When the constraint entity is a collection of constraints, returns the specific constraint at the specified position. If this built-in function exists, it means you can use the [] method to get the value by position index.

Parameters

position (int) – Position index value.

Returns

Specific constraint at the specified position in the constraint collection.

Return type

Constraint

>>> cons = m.getConstraint("Time")
>>> con = cons[1]
__init__(*args, **kwargs)
__iter__(self: maplpy.Constraint) Iterator

When the constraint entity is a collection of constraints, iterates over the constraint collection. If this built-in function exists, it means you can use the for in method to iterate.

>>> cons = m.getConstraint("Time")
>>> for con in cons:
>>>     print(con.name)
__len__(self: maplpy.Constraint) int

When the constraint entity is a collection of constraints, returns the size of the constraint collection. If this built-in function exists, it means you can use the len method to get the size of the constraint collection.

Returns

Size of the constraint collection.

Return type

int

>>> cons = m.getConstraint("Time")
>>> len(cons)
__repr__(self: maplpy.Constraint) str

Returns the string representation of the constraint collection. When the constraint entity is a collection of constraints, returns the string representation of the constraint collection. When the constraint entity is a specific constraint, returns the string representation of the specific constraint. If this built-in function exists, it means you can directly output the class.

Returns

String representation of the constraint collection or constraint.

Return type

str

>>> cons = m.getConstraint("Time")
>>> print(cons)
>>> for con in cons:
>>>     print(con)
property body

When the constraint entity is a specific constraint, returns the value of the constraint expression calculated based on the current variable values.

Returns

Value of the constraint expression.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].body
property expr

When the constraint entity is a specific constraint, returns the string representation of the constraint expression.

Returns

String representation of the constraint expression.

Return type

str

>>> cons = m.getConstraint("Time")
>>> cons[1].expr
find(self: maplpy.Constraint, *args) maplpy.Constraint

When the constraint entity is a collection of constraints, returns the specific constraint indexed by Tuple.

Parameters

index (Tuple) – Tuple index value.

Returns

Specific constraint indexed by Tuple in the constraint collection.

Return type

Constraint

>>> cons = m.getConstraint("Time")
>>> t = mp.Tuple(["reheat"])
>>> con_reheat = cons.find(t)
>>> con_roll = cons.find("roll")
property lb

When the constraint entity is a specific constraint, returns the lower bound of the constraint.

Returns

Lower bound of the constraint.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].lb
property lbs

When the constraint entity is a specific constraint, returns the lower bound of the constraint.

Returns

Lower bound of the constraint.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].lbs
property lslack

When the constraint entity is a specific constraint, returns the amount of improvement (difference) relative to the lower bound of the current constraint value.

Returns

Amount of improvement (difference) relative to the lower bound of the constraint value.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].lslack
property name

When the constraint entity is a specific constraint, returns the constraint name.

Returns

Constraint name.

Return type

str

>>> cons = m.getConstraint("Time")
>>> cons[1].name
property num_instances

When the constraint entity is a collection of constraints, returns the number of constraints in the collection. You can also use the len method to get the size of the constraint collection.

Returns

Number of constraints in the collection.

Return type

int

>>> cons = m.getConstraint("Time")
>>> cons.num_instances
property slack

When the constraint entity is a specific constraint, returns the minimum amount of improvement (minimum difference) relative to the upper and lower bounds of the current constraint value.

Returns

Amount of improvement (difference) relative to the upper and lower bounds of the constraint value.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].slack
property ub

When the constraint entity is a specific constraint, returns the upper bound of the constraint.

Returns

Upper bound of the constraint.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].ub
property ubs

When the constraint entity is a specific constraint, returns the upper bound of the constraint.

Returns

Upper bound of the constraint.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].ubs
property uslack

When the constraint entity is a specific constraint, returns the amount of improvement (difference) relative to the upper bound of the current constraint value.

Returns

Amount of improvement (difference) relative to the upper bound of the constraint value.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].uslack
property value

When the constraint entity is a specific constraint, returns the value of the constraint expression calculated based on the current variable values.

Returns

Value of the constraint expression.

Return type

float

>>> cons = m.getConstraint("Time")
>>> cons[1].value
class maplpy.DataFrame

Bases: pybind11_object

The DataFrame class is used to produce data tables in bulk, with a structure similar to excel tables. Data can be added using native methods or initialized using the pandas.DataFrame class.

DataFrame handles data in tabular form. Each column contains a header and multiple data entries of the same type.

Name

Age

Gender

Height

ZhangSan

20

Male

175

LiSi

21

Female

165

Before running the documentation examples of the DataFrame interface, execute the following statements to complete the necessary model initialization.

>>> data = {
        "name": ["Zhang San", "Li Si"],
        "age": [20, 21,],
        "sex": ["Male", "Female"],
        "height": [175, 165]
    }
>>> pd = pandas.DataFrame(data)
>>> pd.set_index(["name", "age"], inplace=True)
>>> md = mp.DataFrame(pd)
>>> print(md)
name   age sex height
Zhang San   20 Male  175
Li Si   21 Female  165
>>> m.setData(df)
>>> m.display("nameage");
Name  : nameage
Type  : set
Index : Pseudo: |0|{}
Entries:
    Multi: |2|{["Zhang San", 20], ["Li Si", 21]}
>>> m.display("height");
Name  : height
Type  : number
Index : Multi: |2|{["Zhang San", 20], ["Li Si", 21]}
Entries:
    ["Zhang San", 20] -> 175
    ["Li Si", 21] -> 165
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: maplpy.DataFrame) -> None

  2. __init__(self: maplpy.DataFrame, dataframe: object) -> None

    Initialize the data table object from a pandas.DataFrame object.

    Parameters:

    dataframe(object): pandas.DataFrame object.

    >>> data = {
            "name": ["Zhang San", "Li Si"],
            "age": [20, 21,],
            "sex": ["Male", "Female"],
            "height": [175, 165]
        }
    >>> pd = pandas.DataFrame(data)
    >>> pd.set_index(["name", "age"], inplace=True)
    >>> md = mp.DataFrame(pd)
    
addRow(self: maplpy.DataFrame, *args) maplpy.DataFrame

Adds a row of data to the data table object.

Parameters

row (list) – A row of data.

Returns

Returns the data table object to support chain calls.

Return type

DataFrame

>>> md.add_row(["Wang Wu", 22, "Female", 180])
>>> print(md)
name   age sex height
Zhao San   20 Male  175
Li Si   21 Female  165
Wang Wu   22 Female  180
property comment

‘’, no comments).

Returns

Data comment character.

Return type

str

>>> md.comment = "#"
>>> print(md.comment)
#
Type

Returns or sets the comment character for data rows when reading a CSV file through the read_csv interface (default

property data_names

Returns or sets the list of names of the current data columns. If not set, the default is to use the list of data column names in the data table.

Returns

List of data column names in the data table.

Return type

list

>>> md.data_names = ["sex", "height"]
>>> print(md.data_names)
['sex', 'height']
property delimiter

‘,’).

Returns

Data delimiter.

Return type

str

>>> print(md.delimiter)
,
>>> md.delimiter = ";"
>>> print(md.delimiter)
;
Type

Returns or sets the delimiter for data rows when reading a CSV file through the read_csv interface (default

property encode

UTF8).

Returns

Data encoding.

Return type

str

>>> print(md.encode)
Encode.UTF8
>>> md.encode = mp.Encode.GBK
>>> print(md.encode)
Encode.GBK
Type

Returns or sets the encoding to be used when reading a CSV file through the read_csv interface (default

getValue(self: maplpy.DataFrame, row: int, col: int) object

Gets the element at the specified position in the data table object.

Parameters
  • row (int) – Row index.

  • column (int) – Column index.

Returns

Element at the specified position in the data table object.

Return type

int|float|str

>>> print(md.get_value(0, 0))
Zhang San
property header

-1, no header).

Returns

Number of header rows.

Return type

int

>>> print(md.header)
-1
>>> md.header = 0
>>> print(md.header)
0
Type

Returns or sets the row number of the table header when reading a CSV file through the read_csv interface (default

property headers

Returns or sets the list of column names of the data table when reading a CSV file through the read_csv interface. When header is set, the column names of the data table are automatically updated when reading the CSV file.

Returns

List of data table column names.

Return type

list

>>> print(md.headers)
['name', 'age', 'sex', 'height']
>>> md.headers = ['nick', 'age', 'sex', 'height']
>>> print(md.headers)
['nick', 'age', 'sex', 'height']
property index_columns

Returns or sets the list of index columns for the data table object.

Returns

List of index columns for the data table object.

Return type

int

>>> print(md.index_columns)
[0, 1]
>>> md.index_columns = [0]
>>> print(md.index_columns)
[0]
property index_name

Returns or sets the current index column name. If not set, the default is to use the concatenated string of index column names in the data table.

Returns

Index column name of the data table.

Return type

str

>>> md.index_name = "id_name"
>>> print(md.index_name)
id_name
property pattern

‘’, automatic matching). - n - Matches numbers - s - Matches strings

Returns

Data matching pattern.

Return type

str

>>> print(md.pattern)
>>> md.pattern = "ns"
>>> print(md.pattern)
ns
Type

Returns or sets the data matching pattern when reading a CSV file through the read_csv interface (default

readCSV(self: maplpy.DataFrame, filepath: str) maplpy.DataFrame

Read csv file. When calling this interface, you need to specify: data matching pattern, table header row number (default: -1), number of rows to skip (default: 0), data delimiter (default: ,), comment character (default: #), encoding type (default: UTF8)

Parameters

filename (str) – CSV file path.

Returns

Returns the data table object to support chaining calls.

Return type

DataFrame

>>> with open("data.csv", "w") as f:
        f.write(
            """Empty X Y Index1 Index2
    1 2 3 4 a
    5 6 7 8 b
    9 10 11 12 cde
    13 14 15 16 fg
    17 18 19 20 hi
    21 22 23 24 gkl
    25 26 27 28 mn"""
        )
>>> md.skip = 1
>>> md.delimiter = " "
>>> md.pattern = "nsnn"
>>> md.read_csv("data.csv")
>>> print(md)
  1  2  3  4  a
  5  6  7  8  b
  9  10 11 12 cde
  13 14 15 16 fg
  17 18 19 20 hi
  21 22 23 24 gkl
  25 26 27 28 mn
readCSVAdvanced(self: maplpy.DataFrame, filepath: str, pattern: str = '', header: int = -1, skip: int = 0, delimiter: str = ', ', comment: str = '', encode: maplpy.Encode = <Encode.UTF8: 1>) maplpy.DataFrame

Read csv file, you can specify at the same time: data matching pattern, table header row number (default: -1), number of rows to skip (default: 0), data delimiter (default: ,), comment character (default: #), encoding type (default: UTF8).

Parameters
  • filename (str) – CSV file path.

  • pattern (str) – Data matching pattern.

  • header (int) – Table header row number.

  • skip (int) – Number of rows to skip.

  • delimiter (str) – Data delimiter.

  • comment (str) – Comment character.

  • encode (str) – Data encoding.

Returns

Returns the data table object to support chaining calls.

Return type

DataFrame

>>> with open("data.csv", "w") as f:
        f.write(
            """Empty X Y Index1 Index2
    1 2 3 4 a
    5 6 7 8 b
    9 10 11 12 cde
    13 14 15 16 fg
    17 18 19 20 hi
    21 22 23 24 gkl
    25 26 27 28 mn"""
        )
>>> md.read_csv("data.csv", "nsnn", skip=1, delimiter=" ", comment="#")
>>> print(md)
  1  2  3  4  a
  5  6  7  8  b
  9  10 11 12 cde
  13 14 15 16 fg
  17 18 19 20 hi
  21 22 23 24 gkl
  25 26 27 28 mn
setValue(self: maplpy.DataFrame, row: int, col: int, value: object) None

Sets the element at the specified position in the data table object.

Parameters
  • row (int) – Row index.

  • column (int) – Column index.

  • value (int|float|str) – Value to set.

Returns

Returns the data table object to support chain calls.

Return type

DataFrame

>>> md.set_value(0, 0, "Zhao San")
>>> print(md.get_value(0, 0))
Zhao San
property shape

Returns the number of rows and columns of the data table object.

Returns

Number of rows and columns of the data table object.

Return type

tuple

>>> print(md.shape)
(2, 4)
property skip

-1, start from the non-header row).

Returns

Number of rows to skip.

Return type

int

>>> md.skip = 1
>>> print(md.skip)
1
Type

Returns or sets the number of rows to skip when reading a CSV file through the read_csv interface (default

property use_columns

Returns or sets the list of column indexes to be used when the data table object is added to the model (column indexes start from 0).

Returns

List of column indexes to be used when the data table object is added to the model.

Return type

list

>>> print(md.use_columns)
[0, 1, 2, 3]
>>> md.use_columns = [0, 3]
>>> print(md.use_columns)
[0, 3]
class maplpy.Encode

Bases: pybind11_object

The Encode enumeration class is used to specify the file encoding type when reading CSV files.

Members:

GBK :

gbk encoding, used to read files encoded in GB2312.

UTF8 :

utf-8 encoding, used to read files encoded in UTF-8.

GBK = <Encode.GBK: 0>
UTF8 = <Encode.UTF8: 1>
__init__(self: maplpy.Encode, value: int) None
__repr__(self: object) str
property name
property value
class maplpy.IntOption

Bases: pybind11_object

The IntOption enum class is used to specify integer type options to configure.

Members:

SOLVER_OUTPUT :

SOLVER_OUTPUT encoding, used to specify whether to output solver logs. 0: do not output solver logs; 1: output solver logs. Default is 1.

SOLVER_OUTPUT = <IntOption.SOLVER_OUTPUT: 1>
__init__(self: maplpy.IntOption, value: int) None
__repr__(self: object) str
property name
property value
class maplpy.Integrality

Bases: pybind11_object

The Integrality enum class is used to specify the type of decision variables.

Members:

CONTINUOUS :

CONTINUOUS - Continuous variable.

INTEGER :

CONTINUOUS - Continuous variable.

BINARY :

BINARY - Binary variable.

IMPLICIT :

IMPLICIT - Implicit variable.

AUXILIARY :

AUXILIARY - Auxiliary variable.

AUXILIARY = <Integrality.AUXILIARY: 4>
BINARY = <Integrality.BINARY: 2>
CONTINUOUS = <Integrality.CONTINUOUS: 0>
IMPLICIT = <Integrality.IMPLICIT: 3>
INTEGER = <Integrality.INTEGER: 1>
__init__(self: maplpy.Integrality, value: int) None
__repr__(self: object) str
property name
property value
class maplpy.MAPL

Bases: pybind11_object

The MAPL class is used to represent a model instance. It manages the model’s parameters, variables, constraints, objectives, options, etc.

>>> import maplpy as mp
>>> m = mp.MAPL("steel")
>>> m.read("steel4.mpl")
>>> m.display("rate")
>>> m.getConstraint("Time")
>>> m.getConstraints()
>>> m.getVariable("Make")
>>> m.getVariables()
>>> m.getObjective("Total_Profit")
>>> m.getOption(mp.StrOption.MODEL_NAME)
>>> m.setOption(mp.StrOption.SOLVER, "mindopt")
>>> m.setOption(mp.StrOption.SOLVER_OPTIONS, "num_threads= 2 print=0")
>>> m.solve()
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: maplpy.MAPL) -> None

    Initialize a model instance.

    >>> m = mp.MAPL()
    
  2. __init__(self: maplpy.MAPL, arg0: str) -> None

    Initialize a model instance.

    Parameters:

    name (str): Name of the model instance.

    >>> m = mp.MAPL("diet_problem")
    
display(self: maplpy.MAPL, name: str = '') None

If no output name is specified, outputs the solutions of all models. If an output name is specified, outputs the information of the specified symbol.

Parameters

name (str) – Output name.

>>> m.display("rate")
eval(self: maplpy.MAPL, text: str) None

Read modeling instructions from a string and execute them.

Parameters

text (str) – Modeling instructions

>>> m.eval()
getConstraint(self: maplpy.MAPL, name: str) mapl::Constraint

Get a constraint entity by the specified name from the model instance.

Parameters

name (str) – Constraint name.

Returns

Constraint entity object.

Return type

Constraint

>>> m.getConstraint("Time")
<Constraint with 2 constraints>
getConstraints(self: maplpy.MAPL) List[mapl::Constraint]

Get all constraint entities from the model instance.

Returns

List of constraint entity objects.

Return type

list

>>> m.getConstraints()
[]
getObjective(self: maplpy.MAPL, name: str) mapl::Objective

Get an objective entity by the specified name from the model instance.

Parameters

name (str) – Objective name.

Returns

Objective entity object.

Return type

Objective

>>> m.getObjective("Total_Profit")
<Objective with 1 objectives>
getObjectives(self: maplpy.MAPL) List[mapl::Objective]

Get all objective entities from the model instance.

Returns

List of objective entity objects.

Return type

list

>>> m.getObjectives()
[]
getVariable(self: maplpy.MAPL, name: str) mapl::Variable

Get a decision variable entity by the specified name from the model instance.

Parameters

name (str) – Decision variable name.

Returns

Decision variable entity object.

Return type

Variable

>>> m.getVariable("Make")
<Variable with 1 variables>
getVariables(self: maplpy.MAPL) List[mapl::Variable]

Get all decision variable entities from the model instance.

Returns

List of decision variable entity objects.

Return type

list

>>> m.getVariables()
[]
read(self: maplpy.MAPL, file_path: str) None

Read a model file.

Parameters

file_path (str) – Path to the model file.

>>> m.read("steel4.mpl")
setData(*args, **kwargs)

Overloaded function.

  1. setData(self: maplpy.MAPL, dataframe: mapl::DataFrame) -> None

    Create corresponding param or set parameters for the model from a maplpy.DataFrame data table.

    Parameters:

    data (mapldy.DataFrame): Data table object.

    >>> m.setData(md)
    
  2. setData(self: maplpy.MAPL, dataframe: object) -> None

    Create corresponding param or set parameters for the model from a pandas.DataFrame data table.

    Parameters:

    data (pandas.DataFrame): pandas.DataFrame object.

    >>> m.setData(pd)
    
setOption(*args, **kwargs)

Overloaded function.

  1. setOption(self: maplpy.MAPL, option: maplpy.IntOption, value: int) -> None

    Set an integer option for the model instance.

    Parameters:

    option (IntOption): Option to be set. value (int): Value for the option.

    >>> m.setOption(mp.IntOption.SOLVER_OUTPUT, 0)
    
  2. setOption(self: maplpy.MAPL, option: maplpy.StrOption, value: str) -> None

    Set a string option for the model instance.

    Parameters:

    option (StrOption): Option to be set. value (str): Value for the option.

    >>> m.setOption(mp.StrOption.MODEL_NAME, "diet_problem")
    >>> m.setOption(mp.StrOption.SOLVER, "mindopt")
    >>> m.setOption(mp.StrOption.SOLVER_OPTIONS, "num_threads= 2 print=0")
    
solve(self: maplpy.MAPL) None

Invoke the solver to solve the model.

>>> m.solve()
startCmd(self: list) int

Execute the interactive command line.

Parameters

args (list) – Command line arguments.

Returns

Return value.

Return type

int

>>> m.startCmd(["-h"])
write(self: maplpy.MAPL, dir_path: str = '') None

Write the model file to the specified directory.

Parameters

dir_path (str) – Directory to write to (default is the current directory).

>>> m.write("model/")
class maplpy.MaplModel

Bases: pybind11_object

__init__(self: maplpy.MaplModel) None
start_cmd(self: int, arg0: list) int

Execute the interactive command line, deprecated interface.

Parameters

args (list) – Command line arguments.

Returns

Return value.

Return type

int

>>> m.startCmd(["-h"])
class maplpy.Objective

Bases: pybind11_object

Objective - The Objective class is used to bind an objective function entity in the optimization model. Use MAPL.getObjective to get and bind the objective function entity. An objective function entity may be a single objective function or a multi-dimensional one; it is a collection of objectives with the same name but different indexes. This entity class allows you to get the properties of the objective function, such as name, expression, and optimal value.

>>> import maplpy as mp
>>> m = mp.MAPL()
>>> m.read("steel4.mpl")
>>> o = m.getObjective("Total_Profit")
>>> o.expr
>>> o.minimization
>>> m.optimize()
>>> o.value
__init__(*args, **kwargs)
__iter__(self: maplpy.Objective) Iterator
__len__(self: maplpy.Objective) int
__repr__(self: maplpy.Objective) str
property exitcode

Returns the exit code of the solver.

Returns

Exit code of the solver.

Return type

int

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].exitcode
property expr

When the objective entity is a specific objective, returns the string representation of the objective expression.

Returns

String representation of the objective expression.

Return type

str

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].expr
property is_scalar

When the objective entity is a specific objective, returns whether the specific objective is a scalar.

Returns

Whether the objective is a scalar.

Return type

bool

>>> objs = m.getObjective("Time")
>>> objs.is_scalar
property message

If the solver logs are turned off, redirects the logs to this location.

Returns

Solver logs.

Return type

str

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].message
property minimization

When the objective entity is a specific objective, returns whether the objective is a minimization objective.

Returns

Whether the objective is a minimization objective.

Return type

bool

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].minimization
property mstatus

When the objective entity is a specific objective, returns the status of the objective.

Returns

Status of the objective.

Return type

str

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].mstatus
property name

When the objective entity is a specific objective, returns the objective name.

Returns

Objective name.

Return type

str

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].name
property sstatus

Returns the status of the solver.

Returns

Status of the solver.

Return type

str

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].sstatus
property value

When the objective entity is a specific objective, returns the value of the objective expression calculated based on the current variable values.

Returns

Value of the objective expression.

Return type

float

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].value
property xref

When the objective entity is a specific objective, returns all variables related to the objective.

Returns

All variables related to the objective.

Return type

list

>>> objs = m.getObjective("Total_Profit")
>>> objs[0].xref
class maplpy.StrOption

Bases: pybind11_object

The StrOption enum class is used to specify string type options to configure.

Members:

MODEL_NAME :

MODEL_NAME is used to name the model.

>>> m.setOption(mp.StrOption.MODEL_NAME, "diet_problem")
SOLVER :

SOLVER is used to specify the solver.

>>> m.setOption(mp.StrOption.SOLVER, "mindopt")
SOLVER_OPTIONS :

SOLVER_OPTIONS is used to specify solver options.

>>> m.setOption(mp.StrOption.SOLVER_OPTIONS, "num_threads= 2 print=0")
SOLVER_PATH :

SOLVER_PATH is used to specify the solver path.

>>> m.setOption(mp.StrOption.SOLVER_PATH, "/usr/local/bin;/usr/bin")
MODEL_NAME = <StrOption.MODEL_NAME: 1>
SOLVER = <StrOption.SOLVER: 2>
SOLVER_OPTIONS = <StrOption.SOLVER_OPTIONS: 4>
SOLVER_PATH = <StrOption.SOLVER_PATH: 3>
__init__(self: maplpy.StrOption, value: int) None
__repr__(self: object) str
property name
property value
class maplpy.Tuple

Bases: pybind11_object

The Tuple class is used to represent indexes, which can be integers, strings, or floats. It is used similarly to a list, except you need to initialize its size first before assigning values. The role of the index is to extract a single variable, constraint, or objective from an entity.

>>> t = Tuple(3) # Initialize with size 3
>>> t[0] = "xiao_ming"
>>> t[1] = 18
>>> t[2] = 175.0
>>> t
<Tuple with ("xiao_ming", 18, 175.0)>
>>> stu = m.getVariable("stu")
>>> xiao_ming = stu.find(t)
__getitem__(self: maplpy.Tuple, index: int) Union[int, float, str]

Get the value at the specified index in the Tuple class. If this built-in function exists, it means you can use the [] method to get the value by position index.

Parameters

index (int) – Position index.

Returns

Value at the specified position.

Return type

int|float|str

>>> t = Tuple(("xiao_ming", 18, 175.0))
>>> t[0]
"xiao_ming"
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: maplpy.Tuple, size: int) -> None

    Initialize the Tuple class.

    Parameters:

    size (int): Size of the Tuple class.

    >>> t = Tuple(3)
    
  2. __init__(self: maplpy.Tuple, values: List[Union[int, float, str]]) -> None

    Initialize the Tuple class.

    Parameters:

    tuple (tuple): Initial values of the Tuple class.

    >>> t = Tuple(("xiao_ming", 18, 175.0))
    
__len__(self: maplpy.Tuple) int

Returns the length of the Tuple class. If this built-in function exists, it means you can use the len method to get the length of the Tuple class.

Returns

Length of the Tuple class.

Return type

int

>>> t = Tuple(("xiao_ming", 18, 175.0))
>>> len(t)
__repr__(self: maplpy.Tuple) str

Returns the string representation of the Tuple class. If this built-in function exists, it means you can directly output the class.

Returns

String representation of the Tuple class.

Return type

str

>>> t = Tuple(("xiao_ming", 18, 175.0))
>>> print(t)
<Tuple with ("xiao_ming", 18, 175.0)>
__setitem__(self: maplpy.Tuple, index: int, value: Union[int, float, str]) None

Set the value at the specified index in the Tuple class. If this built-in function exists, it means you can use the [] method to set the value by position index.

Parameters
  • index (int) – Position index.

  • value (int|float|str) – Value to set.

Returns

None

>>> t = Tuple(("xiao_ming", 18, 175.0))
>>> t[0] = "xiao_ming_new"
>>> t
<Tuple with ("xiao_ming_new", 18, 175.0)>
class maplpy.Variable

Bases: pybind11_object

Variable - The Variable class is used to bind a decision variable entity in the optimization model. Use MAPL.getVariable to get and bind the decision variable entity. A decision variable entity may be a single decision variable or a multi-dimensional one; it is a collection of decision variables with the same name but different indexes. This entity class allows you to get the properties of the decision variable, such as name, bounds, and expression value.

>>> vars = m.getVariable("Make")
>>> x.lb = 10
>>> x.ub = 20
>>> x.name = "x"
>>> m.optimize()
>>> x.value
__getitem__(self: maplpy.Variable, arg0: int) maplpy.Variable

When the decision variable entity is a collection of decision variables, returns the specific decision variable at the specified position. If this built-in function exists, it means you can use the [] method to get the value by position index.

Parameters

position (int) – Position index value.

Returns

Specific decision variable at the specified position in the collection of decision variables.

Return type

Variable

>>> vars = m.getVariable("Make")
>>> var = vars[1]
__init__(*args, **kwargs)
__iter__(self: maplpy.Variable) Iterator

When the decision variable entity is a collection of decision variables, iterates over the collection of decision variables. If this built-in function exists, it means you can use the for in method to iterate.

>>> vars = m.getVariable("Make")
>>> for var in vars:
>>>     print(var.name)
__len__(self: maplpy.Variable) int

When the decision variable entity is a collection of decision variables, returns the size of the collection of decision variables. If this built-in function exists, it means you can use the len method to get the size of the collection of decision variables.

Returns

Size of the collection of decision variables.

Return type

int

>>> vars = m.getVariable("Make")
>>> len(vars)
__next__(self: mapl::Variable::iterator) mapl::Variable::iterator
__repr__(self: maplpy.Variable) str

Returns the string representation of the collection of decision variables. When the decision variable entity is a collection of decision variables, returns the string representation of the collection of decision variables. When the decision variable entity is a specific decision variable, returns the string representation of the specific decision variable. If this built-in function exists, it means you can directly output the class.

Returns

String representation of the collection of decision variables or decision variable.

Return type

str

>>> vars = m.getVariable("Make")
>>> print(vars)
>>> for var in vars:
>>>     print(var)
find(self: maplpy.Variable, *args) maplpy.Variable

When the decision variable entity is a collection of decision variables, returns the specific decision variable indexed by Tuple.

Parameters

index (Tuple) – Tuple index value.

Returns

Specific decision variable indexed by Tuple in the collection of decision variables.

Return type

Variable

>>> vars = m.getVariable("Make")
>>> t = mp.Tuple(["plate"])
>>> var_plate = vars.find(t)
>>> var_bands = vars.find("bands")
fix(*args, **kwargs)

Overloaded function.

  1. fix(self: maplpy.Variable) -> maplpy.Variable

    When the decision variable entity is a specific decision variable, fixes the current decision variable value.

    >>> vars = m.getVariable("Make")
    >>> vars[1].fix()
    
  2. fix(self: maplpy.Variable, value: float) -> maplpy.Variable

    When the decision variable entity is a specific decision variable, fixes the current decision variable value to the specified value.

    Parameters:

    value (float): Specified value.

    >>> vars = m.getVariable("Make")
    >>> vars[1].fix(5000)
    
property index

When the decision variable entity is a specific decision variable, returns the index of the decision variable in the model.

Returns

Index of the decision variable.

Return type

int

>>> vars = m.getVariable("Make")
>>> vars[1].index
property init

When the decision variable entity is a specific decision variable, returns the initial value of the decision variable.

Returns

Initial value of the decision variable.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].init
property integrality

When the decision variable entity is a specific decision variable, returns the type of the decision variable.

Returns

Type of the decision variable.

Return type

Integrality

>>> vars = m.getVariable("Make")
>>> vars[1].integrality
property lb

When the decision variable entity is a specific decision variable, returns the lower bound of the decision variable.

Returns

Lower bound of the decision variable.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].lb
property lrc

When the decision variable entity is a specific decision variable, returns the amount of improvement (difference) relative to the lower bound of the current decision variable value.

Returns

Amount of improvement (difference) relative to the lower bound of the decision variable value.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].lrc
property name

When the decision variable entity is a specific decision variable, returns the decision variable name.

Returns

Decision variable name.

Return type

str

>>> vars = m.getVariable("Make")
>>> vars[1].name
property num_instances

When the decision variable entity is a collection of decision variables, returns the number of decision variables in the collection. You can also use the len method to get the size of the collection of decision variables.

Returns

Number of decision variables in the collection.

Return type

int

>>> vars = m.getVariable("Make")
>>> vars.num_instances
property rc

When the decision variable entity is a specific decision variable, returns the minimum amount of improvement (minimum difference) relative to the upper and lower bounds of the current decision variable value.

Returns

Amount of improvement (difference) relative to the upper and lower bounds of the decision variable value.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].rc
setValue(self: maplpy.Variable, value: float) maplpy.Variable

When the decision variable entity is a specific decision variable, sets the current decision variable value.

Parameters

value (float) – Specified value.

>>> vars = m.getVariable("Make")
>>> vars[1].setValue(2400)
property status

not used).

Returns

Status of the decision variable.

Return type

str

>>> vars = m.getVariable("Make")
>>> vars[1].status
Type

When the decision variable entity is a specific decision variable, returns the status of the current decision variable (in

Type

used; unused

property ub

When the decision variable entity is a specific decision variable, returns the upper bound of the decision variable.

Returns

Upper bound of the decision variable.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].ub
unfix(self: maplpy.Variable) maplpy.Variable

When the decision variable entity is a specific decision variable, unfixes the current decision variable value.

>>> vars = m.getVariable("Make")
>>> vars[1].unfix()
property urc

When the decision variable entity is a specific decision variable, returns the amount of improvement (difference) relative to the upper bound of the current decision variable value.

Returns

Amount of improvement (difference) relative to the upper bound of the decision variable value.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].urc
property value

When the decision variable entity is a specific decision variable, returns the value of the decision variable expression calculated based on the current variable values.

Returns

Value of the decision variable expression.

Return type

float

>>> vars = m.getVariable("Make")
>>> vars[1].value