Unix, Windows/DOS, and Gurobi Fundamentals, Fall 2018

This page describes some aspects of Unix (or Windows/DOS) and Gurobi needed to get it to work with the examples in class. I will assume that you have a Mac computer; I will later indicate how to modify these commands/remarks to work in Windows/DOS and Linux.
This year we will use the "Gurobi shell" (which is really the Python interpreter/shell with some extras from Gurobi) in classroom discussion/examples.

.lp Files To run Gurobi, we will use a Plain Text file that (1) ends in .lp, and (2) describes an optimization problem (LP, IP, etc.).
We have discussed some sample files .lp files in class, such as the file TV3.lp.
Using TextEdit (Mac) One way to create TV3.lp on a Macbook, is to copy and paste with TextEdit: (1) copy the entire contents of TV3.lp; (2) open TextEdit, create a new document, and paste the entire contents to your document; (3) on the Format menu of TextEdit, select "Make Plain Text" (or put "Plain Text" in your Preferences for TextEdit); (4) save the file as "blah.lp" or something like this (unclick the box that says 'If no exestion is provided, use ".txt"', or else TextEdit will end the file's name with ".txt")
Using MS Word and others Most text editors, including MS Word, allow you to save a file as "Plain Text" (producing a file ending in .txt) via the command "Save As..." or something similar. You can then use the Unix command "cp" or the DOS command "copy" to create a file ending in .lp (or the Unix/DOS command "rename").
Make Sure!! You should make sure that the .lp file that you ask Gurobi to read is really a plain text file.
You can examine the contents of the file by typing "ls blah.lp" in a Unix terminal, or "type blah.lp" in DOS (i.e., in a Windows Command Prompt). [You may not be able to see certain control characters, so "ls" or "type" does not always tell you the whole story...]
Running the Gurobi Shell You can run Gurobi by double clicking on the Gurobi icon or selecting the Gurobi app; I don't recommend, because you won't be able to control the default directory where Gurobi looks to read .lp files; as things stand now, you have to type a longer path name when referring to a file (e.g., you have to type /Users/joel_friedman/MATH441/Gurobi_Directory/TV3.lp or possibly MATH441/Gurobi_Directory/TV3.lp, instead of TV3.lp)
To work around this problem, see the section "Change Directory" below.
A more convenient way to run the Gurobi shell is to open a terminal window (via Terminal, iTerm2, etc., in Unix, or a Command Prompt in Windows), and then navigate to the directory where your .lp files reside. Then type "gurobi.sh" (in Unix, or "gurobi_bat" in DOS) lauch the "Gurobi shell".
A Little Unix Once you open a terminal in a Unix (or Linux) environment, here are some important commands:
  • "cd" (change directory): "cd" alone changes directory to your home directory; "cd MATH441" changes to the subdirectory MATH441 of the current (or "working") directory; "cd .." moves to the directory one level up from the current directory.
  • "ls" (list): lists the contents of your current directory.
  • "pwd" (print working directory): prints the entire the path name of the current directory.
  • Other useful commands are "cp" (copy), "rm" (remove), "mkdir" (make directory), "rmdir" (remove directory).
  • To work in two different working directories at once, you can either open two terminal windows at once or jump between directories using "pushd", "popd", and "dirs".
  • Unix has its own text editors, such as "vi" (or "vim") and "emacs", which are more powerful than TextEdit and work better with Unix.
Linux and Windows/DOS The above Unix commands should function the same in Linux.
The above commands are very similar in Windows (which runs over DOS):
  • Opening a "Command Prompt" in Windows is the equivalent of opening a terminal window in Unix; the Command Prompt runs the operating system "DOS" (disk operating system) instead of "Unix".
  • The DOS commands "cd", "mkdir", and "rmdir" are the rough equivalents of the same Unix commands.
  • The DOS commands "copy", "dir", "del", "type" are the rough equivalent of the respective Unix commands "cp", "ls", "rm","cat".
  • By default, DOS will list the entire path name where you are working; path names in DOS look a little different than they do in Unix. In DOS a typical path name would look like: "C:\Users\Joel\MATH441" (instead of "/Users/Joel/MATH441" in Unix).
Python, In Disguise When you give the command "gurobi.sh" (or, in DOS, "gurobi_bat"), you are really running the Python interpreter with some extras supplied by Gurobi; Gurobi also changes the prompt from ">>> " to "gurobi> ". So aside from running Gurobi software, you can issue any Python commands. For example, if you type 2 ** 10 after the prompt "gurobi> ", you should get "1024" (which is 2^10) as a response.
For this reason the help and manual pages that Gurobi has for the "Gurobi shell" are the same as those pages for "Python".
You can also use Python to modify LP's and IP's, to automate this process for a parametric LP, etc.
Basic Gurobi Here are some basic Gurobi shell commands:
  • mylp = read('TV3.lp') : this creates a class or object named "mylp", initialized on the data in the file TV3.lp [this won't work if you ran the Gurobi app].
  • mylp = read('/Users/jf/MATH441/Gurobi_Run_Here/TV3.lp') : does the same, although we specify the entire path name of TV3.lp [you need to give the entire path if you ran the Gurobi app].
  • mylp.optimize() : tries to solve the LP/IP/QP/etc. This returns the optimum objective value.
  • mylp.printAttr('X') : this prints all the non-zero values in the optimal solution variables.
  • mylp.printAttr(blah), where blah is 'UB', 'LB', 'VarName', etc. : prints these attributes of the problem and optimum solution; see this list of Gurobi Attributes
  • mylp.write('optimumTV.sol') : this creates a file "optimumTV.sol" (in the current working directory) which lists the values of the optimum solution of mylp. The .sol extension is important to get this format of output; the root name "optimumTV" can be whatever you want.
  • See also this Gurobi Python API (application program interface) homepage and this Gurobi Reference Manual homepage.
Change Directory If you launch the "Gurobi app", you can still change the working directory with the following Python interpreter commands: "import os", "os.chdir('blah')" (to change directories), and "os.getcwd()" (print the cwd, i.e., the current working directory).
There are also system "environment variables" you can set (such as PYTHONPATH in Unix) to tell Gurobi to always look for files in a number of different directories. See this Stack Overflow discussion on changing directories in Python.
Doing Anything If you want to do anything of a general nature in the "Gurobi shell" (e.g., change directories, run a number of LP's and print out the results, etc.), you can look for information regarding the "Python shell" (which is much more extensively documented than the "Gurobi shell", which is the Python shell/interpreter with some Gurobi add-ons).
News No news is good news.

UBC Math Home| Joel Friedman Home| Course Materials