LpVariable¶
A class representing a linear variable
Attributes¶
low_bound: Union[int, float]
property
writable
¶
Getter for lower bound of variable
name: str
property
readonly
¶
Getter for name of variable
obj_coeff
property
readonly
¶
Getter for the coefficient of the variable in the objective function
up_bound: Union[int, float]
property
writable
¶
Getter for upper bound of variable
value: Union[int, float]
property
readonly
¶
Getter for the value of the variable
var_type: VarType
property
writable
¶
Getter for type of variable
Methods¶
evaluate(self)
¶
Returns the value of the variable if set
Source code in flipy/lp_variable.py
def evaluate(self) -> Numeric:
""" Returns the value of the variable if set """
if self._value is None:
raise ValueError('Value of variable %s is None' % self.name)
return self._value
is_constant(self)
¶
Tells whether the variable is restricted to a constant value
Source code in flipy/lp_variable.py
def is_constant(self) -> bool:
""" Tells whether the variable is restricted to a constant value """
return self.low_bound is not None and self.up_bound == self.low_bound
is_free(self)
¶
Tells whether the variable is unbounded
Source code in flipy/lp_variable.py
def is_free(self) -> bool:
""" Tells whether the variable is unbounded """
return self.low_bound is None and self.up_bound is None
is_positive_free(self)
¶
Tells whether the variable is an unbounded non-negative
Source code in flipy/lp_variable.py
def is_positive_free(self) -> bool:
""" Tells whether the variable is an unbounded non-negative """
return self.low_bound == 0 and self.up_bound is None
set_obj_coeff(self, coeff)
¶
Setter for the objective coefficient of this variable
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coeff |
Union[int, float] |
The coefficient of the variable in the objective function |
required |
Source code in flipy/lp_variable.py
def set_obj_coeff(self, coeff: Numeric):
""" Setter for the objective coefficient of this variable
Parameters
----------
coeff
The coefficient of the variable in the objective function
"""
self._obj_coeff = coeff
set_value(self, value)
¶
Setter for the value of the variable. Raises errors if not in bounds or if mismatched type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Union[int, float] |
The value to set for the variable |
required |
Exceptions:
Type | Description |
---|---|
ValueError |
If value does not meet variable's bounds or variable type |
Source code in flipy/lp_variable.py
def set_value(self, value: Numeric) -> None:
""" Setter for the value of the variable. Raises errors if not in bounds or if mismatched type.
Raises
------
ValueError
If value does not meet variable's bounds or variable type
Parameters
----------
value:
The value to set for the variable
"""
self._value = value
to_lp_str(self)
¶
Converts variable into lp format
Source code in flipy/lp_variable.py
def to_lp_str(self) -> str:
""" Converts variable into lp format """
if self.is_free():
return f'{self.name} free'
if self.is_constant():
return f'{self.name} = {self.low_bound:.12g}'
if self.low_bound is None:
lhs = '-inf <= '
elif self.low_bound == 0 and self.var_type == VarType.Continuous:
lhs = ''
else:
lhs = f'{self.low_bound:.12g} <= '
if self.up_bound is not None:
rhs = f' <= {self.up_bound:.12g}'
else:
rhs = ''
return lhs + self.name + rhs