|
OR/MS Today - December 2003 Software Review Mathematica 5 'Mathematical system' offers wealth of features for operations research professionals By ManMohan S. Sodhi Mathematica from Wolfram Research is well known as a "mathematical system" for mathematicians, but it is perhaps not as well known for its wealth of features for OR professionals. The new version 5 is even more compelling for OR professionals who have been intrigued by Mathematica software in the past but did not consider it to be a bread-and-butter package for their profession. This version of the software introduces advanced numerical analysis, linear programming interior point as well as simplex method variants and sparse matrix manipulation with fast algorithms that compete with dedicated numerical software tools. And this is on top of what the software already had in previous versions: probabilistic and statistical functions and calculations, nonlinear algorithms and combinatorial calculations. I was a doctoral student when Stephen Wolfram, founder and CEO of Wolfram Research, came to UCLA in 1991 to demonstrate version 2 of the software. It was fascinating to see mathematical functions being played as music and to see that he used Mathematica itself for both word-processing and presentation. It also was clear that you could use the software close to the way you think and represent equations and symbols in your writing with Greek letters, shorthand notation, functions and operations. I was intrigued by the software but not enough to buy it then or to learn how to use it because I did not make the connection between the software and what I did: linear programming, statistics or general-purpose programming to code new algorithms. My perception changed having explored Mathematica 4 recently as a programming tool to write and to graph some simulation programs with arrays. I was pleasantly surprised by the compactness, and at least relative to C++ or even higher level languages, the readability of the resulting computer programs. But, continuing to have the same perception of Mathematica as a "symbolic mathematics" package with some matrix and other numeric capabilities, I switched to a numerical package known for its speedy matrix manipulations and built-in functions. I knew that Mathematica has tons of built-in features besides matrices graph-theoretic, combinatorial, statistical, and financial features for instance as standard or as add-on packages, but I did not connect it with "serious" number crunching. The new version of Mathematica, version 5, blows away such stereotyping with its OR-related features.
Linear Programming Mathematica 5 can read an MPS file and solve the corresponding LP model with the "default" option (Mathematica chooses the appropriate method based on the size and precision of the problem), or the user can set simplex or interior point to be used by default if desired. To run an example, I first downloaded (and uncompressed) the problem "boeing1" from Netlib (www.netlib.org) in MPS format. Then, within Mathematica 5, I converted the data in the MPS file to a sparse coefficient matrix and the other arrays that Mathematica 5 (but not earlier versions) uses as default and solved it first with the built-in interior point method and then with the default simplex method (Figure 1). On 1.2 GHz Windows 2000 laptop with 512Meg RAM, it took about the same time to solve with either method, about 0.24 seconds. ![]() Figure 1: Solving the "boeing1" LP problem from Netlib in Mathematica 5. Mathematica 5 does not have integer programming, but you could write your own branch-and-bound code within Mathematica or, perhaps less efficiently, in C++ or Java, and use the Mathematica 5 LP solvers for the LP part via Mathlink that is a part of Mathematica 5. To run dual simplex, you can modify the input matrices and arrays by transposing the coefficient array you can manipulate sparse arrays the same way as their dense equivalents and invoke the LinearProgramming[.] call as before or use DualLinearProgramming[.] directly. There are a variety of options that you can use as well by invoking SetOptions[LinearProgramming[.]. A number of warnings and errors exist for LinearProgramming[.] as well. Moreover, you can create your own sparse matrices and use those for LP or for matrix manipulation in algorithms. If you create your own matrices and you are solving real-world problems that invariably have structure, you can use sparse matrices that are handled in Mathematica just as other matrices but stored compactly. For linear programming problems, you can optionally provide a "tolerance" parameter (10-6 default) to determine whether or not we want the solver to consider two numbers equal to each other. I could not find any parameter to clean "dirty" zeros (any number with absolute value smaller than this parameter is set to zero by the solver in question). I expect there will be updates regarding linear programming in the "help" section with more examples and notes regarding LP-related warning and error messages in the near future. Maximize[{x y, x2 + y2 < 150 && (x | y) _ Integers}, {x, y}] If you had a small mixed integer linear programming problem, you can use Maximize/Minimize to solve it in this manner as well. There are two other functions, NMinimize[.] and NMaximize[.] for smooth functions with only a few local optima, the "N" standing for numeric. These functions cannot guarantee to find absolute global minima and maxima. As a guess on my part, I would say that the first two functions work in the symbolic and numeric space whereas the latter two use numerical procedures that we are taught in mathematical programming courses, e.g., Newton-Raphson. Another challenge was trying to understand that computations are done in a Mathematica file or "notebook" that it is divided into a hierarchy of "cells." A cell can be (re)computed by itself, taking the state of the program from the other cells. This is a blessing when you have a large time-consuming program, allowing you to focus only on the cell being modified without having to rerun the entire notebook. But it takes getting used to because we are used to rerunning entire programs. It can also be confusing if you go back and forth to re-valuate cells not keeping the values of all the variables in mind. Finally, it took some getting used to thinking of programming constructs like the "for" loop and the "if" statement to be used as functions. Once you have figured out the programming environment, you can make good progress. Like other good programming environments, Mathematica provides you with much help. For instance, you can hit Cntrl-K (in Windows) to "complete" the name of a function you are typing or you can right click and ask for help on that specific function. I wrote a few small programs and got useful results using such constructs as For[.], If[.] and Min[.]; these are functions in Mathematica (Figure 2) and are used as such. Still, expect progress to be slow initially; you will be tempted to go back and do your programming "quickly" in C++ or Java. I should mention here that Mathematica allows two-way linking with programs in these languages using Mathlink. The help system was wonderful when I wanted to write a simulation program in version 4 of the software. I did so without first reading the hefty but well-written book [2] that comes with the software. Much has been written in OR/MS Today about the benefits even the necessity of using spreadsheets in teaching business students (see e.g., [3]). Mathematica supports Excel with third-party software called Mathematica Link for Excel on the Windows and Mac platforms, but not for the latest versions of Excel. This software allows you to call Mathematica functions from within Excel. There are specific features, again, not tested by me, for presentation. An advantage over Microsoft Powerpoint is that you can run programs as part of your presentation. Finally, you can share your files easily because free reader software, called MathReader, can be downloaded from www.wolfram.com/products/mathreader/ to read Mathematica files, including from within a Web browser.
References
ManMohan Sodhi is a member of the operations management faculty at Cass Business School in London. OR/MS Today copyright © 2003 by the Institute for Operations Research and the Management Sciences. All rights reserved. Lionheart Publishing, Inc. 506 Roswell Rd., Suite 220, Marietta, GA 30060 USA Phone: 770-431-0867 | Fax: 770-432-6969 E-mail: lpi@lionhrtpub.com URL: http://www.lionhrtpub.com Web Site © Copyright 2003 by Lionheart Publishing, Inc. All rights reserved. |