Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::LinearExpr Class Reference

#include <linear_expr.h>

Public Member Functions

 LinearExpr ()
 
 LinearExpr (double constant)
 Possible implicit conversions are intentional.
 
 LinearExpr (const MPVariable *var)
 
LinearExproperator+= (const LinearExpr &rhs)
 
LinearExproperator-= (const LinearExpr &rhs)
 
LinearExproperator*= (double rhs)
 
LinearExproperator/= (double rhs)
 
LinearExpr operator- () const
 
double offset () const
 
const absl::flat_hash_map< const MPVariable *, double > & terms () const
 
double SolutionValue () const
 
std::string ToString () const
 

Static Public Member Functions

static LinearExpr NotVar (LinearExpr var)
 static
 

Detailed Description

LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization problem, i.e.

offset + sum_{i in S} a_i*x_i,

where the a_i and offset are constants and the x_i are MPVariables. You can use a LinearExpr "linear_expr" with an MPSolver "solver" to:

  • Set as the objective of your optimization problem, e.g.

    solver.MutableObjective()->MaximizeLinearExpr(linear_expr);

  • Create a constraint in your optimization, e.g.

    solver.MakeRowConstraint(linear_expr1 <= linear_expr2);

  • Get the value of the quantity after solving, e.g.

    solver.Solve(); linear_expr.SolutionValue();

LinearExpr is allowed to delete variables with coefficient zero from the map, but is not obligated to do so.

Definition at line 114 of file linear_expr.h.

Constructor & Destructor Documentation

◆ LinearExpr() [1/3]

operations_research::LinearExpr::LinearExpr ( )

Definition at line 32 of file linear_expr.cc.

◆ LinearExpr() [2/3]

operations_research::LinearExpr::LinearExpr ( double constant)

Possible implicit conversions are intentional.

Definition at line 30 of file linear_expr.cc.

◆ LinearExpr() [3/3]

operations_research::LinearExpr::LinearExpr ( const MPVariable * var)

Definition at line 34 of file linear_expr.cc.

Member Function Documentation

◆ NotVar()

LinearExpr operations_research::LinearExpr::NotVar ( LinearExpr var)
static

static

Returns 1-var.

NOTE(user): if var is binary variable, this corresponds to the logical negation of var. Passing by value is intentional, see the discussion on binary ops.

Definition at line 75 of file linear_expr.cc.

◆ offset()

double operations_research::LinearExpr::offset ( ) const
inline

Definition at line 142 of file linear_expr.h.

◆ operator*=()

LinearExpr & operations_research::LinearExpr::operator*= ( double rhs)

Definition at line 54 of file linear_expr.cc.

◆ operator+=()

LinearExpr & operations_research::LinearExpr::operator+= ( const LinearExpr & rhs)

Definition at line 38 of file linear_expr.cc.

◆ operator-()

LinearExpr operations_research::LinearExpr::operator- ( ) const

Definition at line 72 of file linear_expr.cc.

◆ operator-=()

LinearExpr & operations_research::LinearExpr::operator-= ( const LinearExpr & rhs)

Definition at line 46 of file linear_expr.cc.

◆ operator/=()

LinearExpr & operations_research::LinearExpr::operator/= ( double rhs)

Definition at line 67 of file linear_expr.cc.

◆ SolutionValue()

double operations_research::LinearExpr::SolutionValue ( ) const

Evaluates the value of this expression at the solution found.

It must be called only after calling MPSolver::Solve.

Definition at line 81 of file linear_expr.cc.

◆ terms()

const absl::flat_hash_map< const MPVariable *, double > & operations_research::LinearExpr::terms ( ) const
inline

Definition at line 143 of file linear_expr.h.

◆ ToString()

std::string operations_research::LinearExpr::ToString ( ) const

A human readable representation of this. Variables will be printed in order of lowest index first.

MPSolver gives names to all variables, even if you don't.

Todo
(user): support optionally cropping long strings.

Definition at line 125 of file linear_expr.cc.


The documentation for this class was generated from the following files: