Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_expr.h File Reference
#include <ostream>
#include <string>
#include "absl/container/flat_hash_map.h"

Go to the source code of this file.

Classes

class  operations_research::LinearExpr
 
class  operations_research::LinearRange
 

Namespaces

namespace  operations_research
 In SWIG mode, we don't want anything besides these top-level includes.
 

Functions

std::ostream & operations_research::operator<< (std::ostream &stream, const LinearExpr &linear_expr)
 
LinearExpr operations_research::operator+ (LinearExpr lhs, const LinearExpr &rhs)
 
LinearExpr operations_research::operator- (LinearExpr lhs, const LinearExpr &rhs)
 
LinearExpr operations_research::operator* (LinearExpr lhs, double rhs)
 
LinearExpr operations_research::operator/ (LinearExpr lhs, double rhs)
 
LinearExpr operations_research::operator* (double lhs, LinearExpr rhs)
 
LinearRange operations_research::operator<= (const LinearExpr &lhs, const LinearExpr &rhs)
 
LinearRange operations_research::operator== (const LinearExpr &lhs, const LinearExpr &rhs)
 
LinearRange operations_research::operator>= (const LinearExpr &lhs, const LinearExpr &rhs)
 

Detailed Description

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This file allows you to write natural code (like a mathematical equation) to model optimization problems with MPSolver. It is syntactic sugar on top of the MPSolver API, it provides no additional functionality. Use of these APIs makes it much easier to write code that is both simple and big-O optimal for creating your model, at the cost of some additional constant factor overhead. If model creation is a bottleneck in your problem, consider using the MPSolver API directly instead.

This file contains two classes:

  1. LinearExpr: models offset + sum_{i in S} a_i*x_i for decision var x_i,
  2. LinearRange: models lb <= sum_{i in S} a_i*x_i <= ub, and it provides various operator overloads to build up "LinearExpr"s and then convert them to "LinearRange"s.

Recommended use (avoids dangerous code):

MPSolver solver = ...;
const LinearExpr x = solver.MakeVar(...); * Note: implicit conversion
const LinearExpr y = solver.MakeVar(...);
const LinearExpr z = solver.MakeVar(...);
const LinearExpr e1 = x + y;
const LinearExpr e2 = (e1 + 7.0 + z)/3.0;
const LinearRange r = e1 <= e2;
solver.MakeRowConstraint(r);
IntegerValue y
const Variable x
Definition qp_tests.cc:127

WARNING, AVOID THIS TRAP:

MPSolver solver = ...;
MPVariable* x = solver.MakeVar(...);
LinearExpr y = x + 5;

In evaluating "x+5" above, x is NOT converted to a LinearExpr before the addition, but rather is treated as a pointer, so x+5 gives a new pointer to garbage memory.

For this reason, when using LinearExpr, it is best practice to:

  1. use double literals instead of ints (e.g. "x + 5.0", not "x + 5"),
  2. Immediately convert all MPVariable* to LinearExpr on creation, and only hold references to the "LinearExpr"s.

Likewise, the following code is NOT recommended:

MPSolver solver = ...;
MPVariable* x = solver.MakeVar(...);
MPVariable* y = solver.MakeVar(...);
LinearExpr e1 = LinearExpr(x) + y + 5;

While it is correct, it violates the natural assumption that the + operator is associative. Thus you are setting a trap for future modifications of the code, as any of the following changes would lead to the above failure mode:

  • LinearExpr e1 = LinearExpr(x) + (y + 5);
  • LinearExpr e1 = y + 5 + LinearExpr(x);

Definition in file linear_expr.h.