7.3.7. MdoVar

class mindoptpy.MdoVar(model: MdoModel, index: int)

Bases: object

This object implements the data structure to hold an optimization variable. An empty variable object can be created by calling mindoptpy.MdoModel.add_var(). Once created, users may modify its lower bound, upper bound, or objective coefficient by calling mindoptpy.MdoVar.set_real_attr() with a proper real-valued model attribute.

Examples

from mindoptpy import *

# Create an empty model.
model = MdoModel()
MDO_INFINITY = MdoModel.get_infinity()

model.set_int_attr("MinSense", 1)

# Add variables.
x1 = model.add_var(0, MDO_INFINITY, 1, None, "x1", False)
x2 = model.add_var(0, MDO_INFINITY, 1, None, "x2", False)
x3 = model.add_var(0, 3, 1, None, "x2", False)

# Add constraints
cons1 = model.add_cons(2 * x1 - x2 <= 3, "c1")
cons2 = model.add_cons(3 * x1 + 2 * x2 <= 5, "c2")

# Change attributes
x1.set_int_attr("IsInteger", 0)
x1.get_int_attr("IsInteger")
x1.set_real_attr("UB", 10.0)
x1.get_real_attr("UB")
x1.set_str_attr("ColName", "x_1")
x1.get_str_attr("ColName")

# Check variable
x1.same_as(model.get_var(0))
x1.get_index()

# Solve
model.solve_prob()

# Check primal solution
x1.get_soln_value()
x1.get_real_attr("PrimalSoln")

Methods

get_index()

Function to retrieve the current index of the variable in the optimization model.

get_int_attr()

Function to retrieve the value of an integer-valued column attribute.

get_real_attr()

Function to retrieve the value of a real-valued column attribute.

get_str_attr()

Function to retrieve the value of a string-valued column attribute.

same_as()

Check whether two variable objects refer to the same variable.

set_int_attr()

Function to change the value of an integer-valued column attribute.

set_real_attr()

Function to change the value of a real-valued column attribute.

set_str_attr()

Function to change the value of a string-valued column attribute.

get_soln_value()

Function to retrieve the solution value of this primal variable.

get_index()

This function retrieves the current index of the variable in the optimization model.

Returns

The current index of the variable.

Return type

int

get_int_attr(att: str)

This function retrieves the value of an integer-valued column attribute.

Parameters

att (str) – An integer-valued column attribute to access.

Returns

The current value of the integer-valued column attribute.

Return type

int

get_real_attr(att: str)

This function retrieves the value of a real-valued column attribute.

Parameters

att (str) – A real-valued column attribute to access.

Returns

The current value of the real-valued column attribute.

Return type

float

get_str_attr(att: str)

This function retrieves the value of a string-valued column attribute.

Parameters

att (str) – A string-valued column attribute to access.

Returns

The current value of the string-valued column attribute.

Return type

str

same_as(rhs)

Check whether two variable objects refer to the same variable.

Parameters

rhs (MdoVar) – Another variable object.

Returns

A boolean flag indicates if two variable objects refer to the same variable.

Return type

bint

set_int_attr(att: str, val: int)

This function changes the value of an integer-valued column attribute.

Parameters
  • att (str) – An integer-valued column attribute to access.

  • val (int) – A new value for the integer-valued column attribute.

set_real_attr(att: str, val: float)

This function changes the value of a real-valued column attribute.

Parameters
  • att (str) – A real-valued column attribute to access.

  • val (float) – A new value for the real-valued column attribute.

set_str_attr(att: str, val: str)

This function changes the value of a string-valued column attribute.

Parameters
  • att (str) – A string-valued column attribute to access.

  • val (str) – A new value for the string-valued column attribute.

get_soln_value()

This function returns the solution value of this primal variable.

Returns

The current solution value of this primal variable.

Return type

float