LpObjective¶
A class representing an objective function
Attributes¶
sense: Type[Union[flipy.lp_objective.Minimize, flipy.lp_objective.Maximize]]
  
      property
      writable
  
¶
    Getter for the sense of the objective
Returns:
| Type | Description | 
|---|---|
| Type[Union[flipy.lp_objective.Minimize, flipy.lp_objective.Maximize]] | The sense of the linear objective, i.e. minimize or maximize | 
Methods¶
add_constant(self, const)
  
      inherited
  
¶
    Adds a constant to the expression (adds to existing constant)
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| const | Union[int, float] | The constant to add | required | 
Source code in flipy/lp_objective.py
          def add_constant(self, const: Numeric) -> None:
    """ Adds a constant to the expression (adds to existing constant)
    Parameters
    ----------
    const:
        The constant to add
    """
    self.const += const
add_expression(self, other)
  
      inherited
  
¶
    Adds another expression to this expression (summing coefficients)
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| other | LpExpression | The expression to add | required | 
Source code in flipy/lp_objective.py
          def add_expression(self, other: 'LpExpression') -> None:
    """ Adds another expression to this expression (summing coefficients)
    Parameters
    ----------
    other:
        The expression to add
    """
    for var, coeff in other.expr.items():
        self.expr[var] += coeff
    self.const += other.const
add_variable(self, var)
  
      inherited
  
¶
    Adds a variable to the expression (with coefficient 1)
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| var | LpVariable | The variable to add to the expression | required | 
Source code in flipy/lp_objective.py
          def add_variable(self, var: LpVariable) -> None:
    """ Adds a variable to the expression (with coefficient 1)
    Parameters
    ----------
    var:
        The variable to add to the expression
    """
    self.expr[var] += 1
evaluate(self)
  
      inherited
  
¶
    Gives the value of the expression
Source code in flipy/lp_objective.py
          def evaluate(self) -> Numeric:
    """ Gives the value of the expression """
    return sum(var.evaluate() * coeff for var, coeff in self.expr.items()) + self.const
sorted_keys(self)
  
      inherited
  
¶
    Returns a list of variable in the expression sorted by name
Source code in flipy/lp_objective.py
          def sorted_keys(self) -> List[LpVariable]:
    """ Returns a list of variable in the expression sorted by name """
    return sorted((v for v in self.expr.keys()), key=lambda v: v.name)
to_lp_terms(self, slack=None)
  
      inherited
  
¶
    Returns a list of string that represents the expression in lp format split in terms
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| slack | Optional[Mapping[flipy.lp_variable.LpVariable, Union[int, float]]] | All slack variables being used and their coefficients | None | 
Returns:
| Type | Description | 
|---|---|
| List[str] | List of terms in string | 
Source code in flipy/lp_objective.py
          def to_lp_terms(self, slack: Optional[Mapping[LpVariable, Numeric]] = None) -> List[str]:
    """ Returns a list of string that represents the expression in lp format split in terms
    Parameters
    ----------
    slack:
        All slack variables being used and their coefficients
    Returns
    -------
    list(str)
        List of terms in string
    """
    terms = []
    is_first = True
    slack = slack or {}
    for var in self.sorted_keys():
        coeff = self.expr[var]
        terms.append(self._to_lp_term_str(var.name, coeff, is_first=is_first))
        is_first = False
    for slack_var in sorted(slack.keys(), key=lambda v: v.name):
        coeff = slack[slack_var]
        terms.append(self._to_lp_term_str(slack_var.name, coeff, is_first=is_first))
        is_first = False
    if self.const < 0:
        terms.append(f'- {-self.const}')
    elif self.const > 0:
        terms.append(f'+ {self.const}')
    elif self.const == 0 and not terms:
        terms.append(f'{self.const}')
    return terms