1.1 What Is This Book About?
Artificial intelligence is a wide field covering diverse techniques, objectives, and measures of success. One branch is concerned with finding provably optimal solutions to some well-defined problems.
This book is an introduction to the art and science of implementing mathematical models of optimization problems .
An optimization problem is almost any problem that is, or can be, formulated as a question starting with What is the best ? For instance,
What is the best route to get from home to work?
What is the best way to produce cars to maximize profit?
What is the best way to carry groceries home: paper or plastic?
Which is the best school for my kid?
Which is the best fuel to use in rocket boosters?
What is the best placement of transistors on a chip?
What is the best NBA schedule?
These questions are rather vague and can be interpreted in a multitude of ways. Consider the first: by best do we mean fastest, shortest, most pleasant to ride, least bumpy, or least fuel-guzzling? Besides, the question is incomplete. Are we walking, riding, driving, or snowboarding? Are we alone or accompanied by a screaming toddler?
To help us formulate solutions to optimization problems, optimizers have established a frame into which we mould the questions; its called a model . The most crucial aspect of a model is that it has an objective and it has constraints. Roughly, the objective is what we want and the constraints are the obstacles in our way. If we can reformulate the question to clearly identify both the objective and the constraints, we are closer to a model.
Lets consider in more detail the best route problem but with an eye to clarify objective and constraints. We could formulate it as
Given a map of the city, my home address, and the address of the daycare of my two-year-old son, what is the best route to take on my bike to bring him to daycare as fast as possible?
The goal is to find among all the solutions that satisfy the requirements (that is, paths following either streets or bike lanes, also known as the constraints) one path that minimizes the time it takes to get there (the objective).
Objectives are always quantities we want to maximize or minimize (time, distance, money, surface area, etc.), although you will see examples where we want to maximize something and minimize something else; this is easily accommodated. Sometimes there are no objectives. We say that the problem is one of feasibility (i.e. we are looking for any solution satisfying the requirements). From the point of view of the modeler, the difference is minimal. Especially since, in most practical cases, a feasibility model is usually a first step. After noticing a solution, one usually wants to optimize something and the model is modified to include an objective function.
1.2 Features of the Text
As this text is an introduction, I do not expect the reader to be already well versed in the art of modeling. I will start at the beginning, assuming only that the reader understands the definition of a variable (both in the mathematical sense and in the programming sense), an equation, an inequality, and a function . I will also assume that the reader knows some programming language, preferably Python , although knowing any other imperative language is enough to be able to read the Python code displayed in the text.
Note that the code in this book is an essential component . To get the full value, the reader must, slowly and attentively, read the code. This book is not a text of recipes described from a birds-eye view, using mathematical notation, with all the nitty-gritty details left as an exercise for the reader. This is implemented, functional, tested, optimization code that the reader can use and moreover is encouraged to modify to fully understand. The mathematics in the book has been reviewed by mathematicians, like any mathematical paper. But the code has been subjected to a much more stringent set of reviewers with names Intel, AMD, Motorola, and IBM.
The book is the fruit of decades of consulting and of years teaching both an introductory modeling class (MOR242 Intro to Operation Research Models) and a graduate class (APM568 Mathematical Modeling in Industry) at Oakland University. I start at the undergraduate level and proceed up to the graduate level in terms of modeling itself, without delving much into the attendant theory .
Every model is expressed in Python using Google OR- Tools ).
My intention is to help the reader become a proficient modeler, not a theoretician. Therefore, little of the fascinating mathematical theory related to optimization is covered. It is nevertheless used profitably to create simple yet efficient models.
The associated web site provides all the code presented in the book along with a random generator for many of the problems and variations. The author uses this as a personalized homework generator. It can also be used as a self-guided learning tool .
https://github.com/sgkruk/Apress-AI
1.2.1 Running the Models
There is danger in describing in too much detail installations instructions because software tends to change more often than this text will change. For instance, when I started with Googles OR-Tools , it was hosted on the Google Code repository; now it is on GitHub. Nevertheless, here are a few pointers. All the code presented here has been tested with
The page https://developers.google.com/optimization offers installation instructions for most operating systems. The fastest and most painless way is
pip install --upgrade ortools
Once OR-Tools are installed, the software of this text can be downloaded most easily by cloning the GitHub repo at
git clone https://github.com/sgkruk/Apress-AI.git
where the reader will find a Makefile testing almost all the models detailed in the text. The reader only has to issue a make to test that the installation was completed successfully.
The code of each section of the book is separated into two parts: a model proper, shown in the text, and a main driver to illustrate how to call the model with some data. For instance, the chapter corresponding to the set cover has a file named set_cover.py with the model and a file named test_set_cover.py which will create a random instance, run the model on it, and display the result. Armed with these examples, the reader should be able to modify to suit his needs. It is important to understand that the mainline is in test_set_cover.py and that file needs to be executed.
1.2.2 A Note on Notation
Throughout the book, I will describe algebraic models. These models can be represented in a number of ways. I will use two. I will sketch each model using common mathematical notation typeset with TEX in math mode. I will then express the complete, detailed model in executable Python code. The reader should have no problem seeing the equivalence between the formulations. Table illustrates some of the equivalencies.
Table 1-1
Equivalence of Expression in Math and Python Modes