Running OR-Tools Programs

The following sections explain how to compile and run or-tools programs.

Compiling and running an example program

In the following sections, we'll show how to compile and run a short example program for each of the supported languages.

To begin, click one of the tabs below to display an example program in your preferred programming language. Then copy and paste the code into new file, and save it as my_program, with the appropriate language extension.

Note: We'll assume you save the program at the top level of the OR-Tools directory. If you save it in a different directory, you'll need to add the path to the directory in the commands shown in the sections below.

C++ example
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"

namespace operations_research {
  void RunTest(
    MPSolver::OptimizationProblemType optimization_problem_type) {
    MPSolver solver("Glop", optimization_problem_type);
    MPVariable* const x = solver.MakeNumVar(0.0, 1, "x");
    MPVariable* const y = solver.MakeNumVar(0.0, 2, "y");
    MPObjective* const objective = solver.MutableObjective();
    objective->SetCoefficient(x, 1);
    objective->SetCoefficient(y, 1);
    objective->SetMaximization();
    solver.Solve();
    printf("\nSolution:");
    printf("\nx = %.1f", x->solution_value());
    printf("\ny = %.1f", y->solution_value());
  }

  void RunExample() {
    RunTest(MPSolver::GLOP_LINEAR_PROGRAMMING);
  }
}

int main(int argc, char** argv) {
  operations_research::RunExample();
  return 0;
}
Python example
from __future__ import print_function
from ortools.linear_solver import pywraplp

def main():
  solver = pywraplp.Solver('SolveSimpleSystem',
                           pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
  x = solver.NumVar(0, 1, 'x')
  y = solver.NumVar(0, 2, 'y')
  objective = solver.Objective()
  objective.SetCoefficient(x, 1)
  objective.SetCoefficient(y, 1)
  objective.SetMaximization()
  solver.Solve()
  print('Solution:')
  print('x = ', x.solution_value())
  print('y = ', y.solution_value())

if __name__ == '__main__':
  main()
Java example
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;

public class my_program {
  static { System.loadLibrary("jniortools"); }

  private static MPSolver createSolver (String solverType) {
    return new MPSolver("my_program",
                        MPSolver.OptimizationProblemType.valueOf(solverType));
  }

  private static void runmy_program(String solverType,
                                                  boolean printModel) {
    MPSolver solver = createSolver(solverType);
    MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
    MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
    MPObjective objective = solver.objective();
    objective.setCoefficient(y, 1);
    objective.setMaximization();
    solver.solve();
    System.out.println("Solution:");
    System.out.println("x = " + x.solutionValue());
    System.out.println("y = " + y.solutionValue());
  }

  public static void main(String[] args) throws Exception {
    runmy_program("GLOP_LINEAR_PROGRAMMING", false);
  }
}
C# example
using System;
using Google.OrTools.LinearSolver;

public class my_program
{
  private static void RunLinearProgrammingExample(String solverType)
  {
    Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
    Variable x = solver.MakeNumVar(0.0, 1.0, "x");
    Variable y = solver.MakeNumVar(0.0, 2.0, "y");
    Objective objective = solver.Objective();
    objective.SetCoefficient(x, 1);
    objective.SetCoefficient(y, 1);
    objective.SetMaximization();
    solver.Solve();
    Console.WriteLine("Solution:");
    Console.WriteLine("x = " + x.SolutionValue());
    Console.WriteLine("y = " + y.SolutionValue());
  }

  static void Main()
  {
    RunLinearProgrammingExample("GLOP_LINEAR_PROGRAMMING");
  }
}

Below, you'll find instructions for running the example program in each of the supported languages.

Running the C++ example

To compile a C++ program, you must first create a file named Makefile.user and add an entry corresponding to the program. You can compile and run the C++ example my_program.cc as follows:

  1. Create a new file named Makefile.user at the top level of the or-tools directory (if you haven't already done so), and add the following build rule to the file:
    $(OBJ_DIR)$Smy_program.$O: $(EX) $(CVRPTW_DEPS) $(DIMACS_DEPS) $(FAP_DEPS)
    	$(CCC) $(CFLAGS) -c $(EX) $(OBJ_OUT)$(OBJ_DIR)$S$(basename $(notdir $(EX))).$O
    
    $(BIN_DIR)/my_program$E: $(OR_TOOLS_LIBS) $(CVRPTW_LIBS) $(DIMACS_LIBS) $(FAP_LIBS) \
    	$(OBJ_DIR)$Smy_program.$O
    	$(CCC) $(CFLAGS) $(OBJ_DIR)$Smy_program.$O $(OR_TOOLS_LNK) $(CVRPTW_LNK) $(DIMACS_LNK) \
    	$(FAP_LNK) $(OR_TOOLS_LD_FLAGS) $(EXE_OUT)$(BIN_DIR)$Smy_program$E
    Then save the file.
  2. Open a command window and change to the top level of the or-tools directory.
  3. Compile and run the program as follows:
    make rcc EX=my_program.cc
    Note: This also returns the command for running the executable for the program.

    To just compile the program without running it, enter:

    make ccc EX=my_program.cc

For each C++ program you create, you must add a new build rule like the one above to Makefile.user, replacing each instance of my_program with the program's name.

Running the C++ executable

When you compile a C++ program, the executable is created in the bin directory. You can run the executable for the example program as follows:

bin/my_program

If you make changes to the program, you'll need to re-compile it as shown above.

Running the Python example

You can run the Python example my_program.py as follows:

  1. Open a command window and change to the top level of the or-tools directory.
  2. At the command prompt, enter
    make rpy EX=my_program.py

This first checks whether you have all the necessary Python dependencies, described in Installing OR-Tools, and if so, runs the program.

You can also run the program by entering

python my_program.python
However, this doesn't check the dependencies.

Running the Java example

You can compile and run the Java example my_program.java as follows:

  1. Open a command window and change to the top level of the or-tools directory.
  2. Compile and run the program as follows:
    make rjava EX=my_program.java
    Note: This also returns the syntax for running the executable for the program.

    To just compile the program without running it, enter:

    make cjava EX=my_program.java
Running the Java executable

You can run the executable file for the example program as follows:

java -Djava.library.path=lib -cp objs:lib/com.google.ortools.jar my_program

If you make changes to the program, you'll need to re-compile it as shown above.

Running the C# example

You can compile and run the C# example my_program.cs as follows:

  1. Open a command window and change to the top level of the or-tools directory.
  2. Compile and run the program as follows:
    make rcs EX=my_program.cs
    Note: This also returns the syntax for running the executable for the program.

    To just compile the program without running it, enter:

    make ccs EX=my_program.cs
Running the C# executable

When you compile a C# program, the executable is created in the bin directory. You can run the executable for the example program as follows:

LD_LIBRARY_PATH=lib: mono bin/my_program.exe

If you make changes to the program, you'll need to re-compile it as shown above.

Examples included with OR-Tools

OR-Tools includes a number of example programs that illustrate how to solve various types of optimization problems. The following shows how to compile and run an example in each of the supported languages.

C++

You can run a C++ example as follows:

make rcc EX=examples/cpp/linear_programming.cc
Note: It isn't necessary to create build rules for the C++ examples, as the rules are part of the main Makefile.

Python

You can run a Python example as follows:

make rpy EX=examples/python/linear_programming.py
Java

You can run a Java example as follows:

make rjava EX=examples/com/google/ortools/samples/LinearProgramming.java
C#
make rcs EX=examples/csharp/cslinearprogramming.cs

You can compile all the examples at once by entering

make all

Send feedback about...

Optimization
Optimization