2 Solving Math Programs with LINGO

2
Solving Math Programs with
LINGO
2.1 Introduction
The process of solving a math program requires a large number of calculations and is, therefore, best
performed by a computer program. The computer program we will use is called LINGO. The main
purpose of LINGO is to allow a user to quickly input a model formulation, solve it, assess the
correctness or appropriateness of the formulation based on the solution, quickly make minor
modifications to the formulation, and repeat the process. LINGO features a wide range of commands,
any of which may be invoked at any time. LINGO checks whether a particular command makes sense
in a particular context.
LINGO is available in two versions:
1) a Windows-specific version, and
2) a generic, text-based version.
The text-based version runs under most popular operating systems, including Unix and Linux. For
either version, additional information is available to the user under the Help menu item or Help
command. The Windows commands are covered briefly here. After the commands section, we will
show you how to enter and solve a simple model.
2.2 LINGO for Windows
When LINGO for Windows starts, it opens a blank window known as a Model Window. The Model
Window is where you “do all your work”. Output in LINGO is displayed in a Report Window. LINGO
can generate a number of reports pertaining to your model. The following is a list of all the commands
that are available to you, mainly in the model window.
2.2.1 File Menu
NEW F2
Use the NEW command from the File menu, press F2, or use the button to create a new Model
window. In the Model window, you can enter your model.
21
22
Chapter 2 Solving Math Programs with LINGO
OPEN F3
Use the OPEN command from the File menu, press F3, or use the button to open an existing file,
typically a model file.
SAVE F4
Use the SAVE command from the File menu, press F4, or use the button to save the active (frontmost)
window. You can save any window — Model, Report, or Command— in this way. If the window is a
new Model window, a Report window, or the Command window, SAVE opens the SAVE AS... dialog.
SAVE AS... F5
Use the SAVE AS... command from the File menu or press F5 to save the active (frontmost) window
under the name you enter in the dialog box.
CLOSE F6
Use the CLOSE command from the File menu or press F6 to close the active (frontmost) window. If
the window is a new Model window or you have changed the file, you will be asked whether you want
to save the changes.
PRINT F7
Use the PRINT command from the File menu, use the button, or press F7 to send the active
(frontmost) window to your printer.
PRINT SETUP... F8
Use the PRINT SETUP... command from the File menu or press F8 to select the printer to which you
want output to go.
PRINT PREVIEW Shift +F8
Use the PRINT PREVIEW command from the File menu or press Shift+F8 to display each page of the
active (frontmost) window as it will appear when printed.
LOG OUTPUT... F9
Use the LOG OUTPUT... command from the File menu or press F9 to send all subsequent screen
activity, which would be sent to the Command Window, to a text file. The command opens a standard
Windows file dialog from which you can name the log file and choose to echo output to the screen.
You may also choose either to overwrite an existing file in the Log Output… dialog box or to append
subsequent output to it.
Once you have selected a file to write to, a check will appear in the File menu before the Log
Output… Command. To turn off Log Output, simply select the command again.
TAKE COMMANDS... F11
Use the TAKE COMMANDS... command from the File menu or press F11 to “Take” a LINGO batch
file with commands and model text for automated operation.
IMPORT LINDO FILE... F12
Use the IMPORT LINDO FILE… command from the File menu or press F12 to open a file containing
a LINDO model in LINDO TAKE format.
Solving Math Problems with LINGO Chapter 2
23
EXPORT FILE
Use the MPS FORMAT... or MPI FORMAT... sub-commands from the File | Export File command to
write MPS or MPI format files, respectively. The MPS file format is an industry standard format
developed by IBM and is useful for passing models from one solver or platform to another. The MPI
file format is a special format developed by LINDO Systems for representing all classes of
mathematical programs—linear, integer, and nonlinear.
LICENSE
Use the LICENSE command from the File menu to enter a new password for LINGO. Some versions
of LINGO require the user to input a password. Think of the password as a “key” that unlocks the
LINGO application. If you upgrade your copy of LINGO, you will need to enter a new password.
DATABASE USER INFO
Use the DATABASE USER INFO command from the File menu when linking models directly to
databases through use of the @ODBC() function. Many times the database you link your model to will
require a user id and/or password. To avoid having to enter your user id and password each time your
model is run, you can input them once at the start of your session using this command.
EXIT F10
Use the EXIT command from the File menu or press F10 to quit LINGO.
2.2.2 Edit Menu
UNDO Ctrl+Z
Use the UNDO command from the Edit menu, or press Ctrl+Z, to undo the last action.
CUT Ctrl+X
Use the CUT command from the Edit menu, click the button, or press Ctrl+X to clear the selected text
and place it on the clipboard for pasting.
COPY Ctrl+C
Use the COPY command from the Edit menu, click the button, or press Ctrl+C to copy the selected
text to the clipboard for pasting.
PASTE Ctrl+V
Use the PASTE command from the Edit menu, click the button, or press Ctrl+V to paste clipboard
contents at the insertion point.
PASTE SPECIAL...
Use the PASTE SPECIAL command from the Edit menu to insert a selected item from the Windows
clipboard into the active (frontmost) window at the cursor insertion point, allowing choice as to how
the object is pasted.
SELECT ALL... Ctrl +A
Use the SELECT ALL command from the Edit menu or press Ctrl+A to select the entire contents of the
active (frontmost) window.
24
Chapter 2 Solving Math Programs with LINGO
FIND ... Ctrl+F
Use the FIND command from the Edit menu or press Ctrl+F to search for a desired string of text in
the active (frontmost) window.
FIND NEXT... Ctrl +N
Use the FIND NEXT command from the Edit menu or press Ctrl+N to find the next instance of the text
most recently searched for using the FIND command in the active (frontmost) window.
REPLACE Ctrl +H
Use the REPLACE command from the Edit menu or press Ctrl+H to replace one string of text with
another in the active (frontmost) window.
GO TO LINE... Ctrl+T
Use the GO TO LINE command from the Edit menu, click on the button, or press Ctrl+T to enter a
line number of the active window to which you want to go.
MATCH PARENTHESIS Ctrl+P
Use the MATCH PARENTHESIS command from the Edit menu, click the button, or type Ctrl+P to
find the close parenthesis that corresponds to the open parenthesis you have selected.
In addition to this command, there is one other way to find matching parentheses. LINGO will
highlight matching parentheses in red when the Match Paren option is enabled under the
LINGO|Options command (see below). By placing the cursor immediately after one of the parentheses
of interest, you will notice that the color of the parenthesis changes from black to red. LINGO will
simultaneously display the matching parenthesis in red. These parentheses will remain displayed in red
until you move the cursor to another position.
PASTE FUNCTION
Use the PASTE FUNCTION command from the Edit menu to paste any of LINGO’s built-in functions
at the current insertion point. Choose the category of the LINGO function you want to paste, then
select the function from the cascading menu. LINGO inserts place holders for arguments in the
functions.
SELECT FONT... Ctrl +J
Use the SELECT FONT command from the Edit menu or press Ctrl+J to select a new font in which to
display the currently selected text.
INSERT NEW OBJECT
Use the INSERT NEW OBJECT command from the Edit menu to embed an OLE object into the
LINGO document.
LINKS
Use the LINKS command from the Edit menu to control the links to external objects in your document.
Solving Math Problems with LINGO Chapter 2
25
OBJECT PROPERTIES Alt+Enter
Use the OBJECT PROPERTIES command from the Edit menu or press Alt+Enter to specify the
properties of a selected, embedded object
2.2.3 LINGO Menu
SOLVE Ctrl+S
Use the SOLVE command from the LINGO menu, click on the button, or press Ctrl+S to send the
model currently in memory to the LINGO solver. If you have more than one model open, the
frontmost (or active) window is the one in memory.
SOLUTION… Ctrl+O
Use the SOLUTION command from the LINGO menu, click the button, or press Ctrl+O to open the
Solutions dialog box. Here you can specify the way you want a report of the solution currently in
memory to appear. When you click OK, LINGO writes the report to a Report Window.
RANGE Ctrl+R
Use the RANGE command from the LINGO menu or press Ctrl+R to see a standard range report.
OPTIONS... Ctrl +I
Use the OPTIONS... command from the LINGO menu, click the button, or press Ctrl+I to change a
number of parameters that affect the configuration of the program.
GENERATE… Ctrl+G/Ctrl+Q
Use the DISPLAY MODEL and DON’T DISPLAY MODEL sub-commands from the LINGO | Generate
command or press Ctrl+G or Ctrl+Q, respectively, to create an expanded version of the current model.
The expanded model explicitly lists all the generated constraints and variables in your model.
If you choose to display the model, LINGO will place a copy of the generated model in a new
window, which you may scroll through to examine, print, or save to disk. If you choose not to display
the model, LINDO will generate the model without displaying it, but will store the generated model for
later use by the appropriate solver.
26
Chapter 2 Solving Math Programs with LINGO
PICTURE Ctrl+K
Use the PICTURE command from the LINGO menu or press Ctrl+K to display a model in matrix
form. Viewing the model in matrix form can be helpful in identifying special structure in your model.
MODEL STATISTICS Ctrl+E
Use the MODEL STATISTICS command from the LINGO menu or press Ctrl+E to list summary
statistics for your model. The statistics shown vary slightly depending on whether the model you're
working with is linear or nonlinear.
LOOK… Ctrl+L
Use the LOOK command from the LINGO menu or press Ctrl+L to generate a report containing your
model’s formulation. The LOOK command lets you choose all or selected lines of a model for
viewing. This command is mainly of interest if you are working in command line mode rather than the
more typical Windows environment.
2.2.4 Windows Menu
COMMAND WINDOW Ctrl +1
Use the COMMAND WINDOW command from the Windows menu or press Ctrl+1 to open LINGO’s
Command Window. The Command Window gives you access to LINGO’s command line interface. In
general, Windows users will not need to make use of the Command Window. It is provided for users
who may want to put together application-specific “products” that make use of LINGO through
Command Window scripts to control the program. Please refer to your help file or user’s manual for
more information on the command line commands.
STATUS WINDOW Ctrl +2
Use the STATUS WINDOW command from the Windows menu or press Ctrl+2 to open LINGO’s
Solver Status window.
SEND TO BACK Ctrl+B
Use the SEND TO BACK command from the Windows menu, click on the button, or press Ctrl+B to
send the active (frontmost) window to the back. This command is very useful for switching between a
Model window and a Report Window.
CLOSE ALL Ctrl+3
Use the CLOSE ALL command from the Windows menu, click on the button, or press Ctrl+3 to close
all open model windows and dialog boxes.
TILE Ctrl+4
Use the TILE command from the Windows menu or press Ctrl+4 to arrange all open windows, so they
each occupy the same amount of space within the LINGO program window.
Solving Math Problems with LINGO Chapter 2
27
CASCADE Ctrl+5
Use the CASCADE command from the Windows menu or press Ctrl+5 to arrange all open windows in
a cascade from upper left to lower right with the currently active window on top.
ARRANGE ICONS Ctrl+6
Use the ARRANGE ICONS command from the Windows menu or press Ctrl+6 to move icons
representing any minimized windows, so they are arranged across the bottom of the screen.
LIST OF WINDOWS
A list of all open windows appears at the bottom of the Windows menu, with the currently active
window checked. You may select a window from the list to bring it to the front.
2.2.5 Help Menu
HELP TOPICS F1
Use the HELP TOPICS command from the Help menu, press F1, or click on the first question mark
button to open LINGO help to the Contents section. Press the second button (with the arrow) to invoke
context-sensitive help. Once the cursor has changed to the question mark, selecting any command will
take you to help for that command.
REGISTER
Use the REGISTER command from the Help menu to register your version of LINGO online. You will
need a connection to the internet open for this command to work. Enter your personal information in
the dialog box supplied and select the register button. Your information will be sent directly to LINDO
Systems via the Internet.
LINDO Systems is constantly working to make our products faster and easier to use. Registering
your software with LINDO ensures that you will be kept up-to-date on the latest enhancements and
other product news.
AUTOUPDATE
Use the AUTOUPDATE command from the Help menu to have LINGO automatically check every
time you start the LINGO software whether there is a more recent version of LINGO available for
download on the LINDO Systems website. You will need a connection to the internet open for this
command to work.
ABOUT LINGO…
Use the ABOUT LINGO command from the Help menu to view information about the version of
LINGO you are currently using (e.g., the release number, constraint limit, variable limit, and memory
limit).
2.2.6 Summary
This is not intended to be an exhaustive description of the commands available in the Windows version
of LINGO. Please refer to your help file or user’s manual for a more in-depth analysis.
28
Chapter 2 Solving Math Programs with LINGO
2.3 Getting Started on a Small Problem
When you start LINGO for Windows, the program opens an <untitled> window for you. For purposes
of introduction, let’s enter the Enginola problem we looked at in the previous chapter directly into this
<untitled> window:
MAX = (20 * A) + (30 * C);
!note that the parentheses aren't needed, because LINGO;
!will do multiplication and division first;
A < 60;
C < 50;
A + 2 * C < 120;
Note, even though the strict inequality, “<”, was entered above, LINGO interprets it as the loose
inequality, “≤”. The reason is that typical keyboards have only the strict inequalities, < and >. You
may, and in fact are encouraged to, use the two symbols “<=” to emphasize an inequality is of a
less-than-or-equal-to nature. Also, notice comments are preceded by the exclamation mark (!). A
semicolon (;) terminates a comment.
Click on the Solve/“bullseye” button
, use the Solve command from the Solve menu, or press
Ctrl+S to solve the model. While solving, LINGO will display the Solver Status Window with
information about the model and the solution process. When it’s done solving, the “State” field should
read “Global Optimum”. Then, click on the “Close” button to close the Solver Status Window:
The following solution is now in a Report Window:
Optimal solution found at step:
1
Objective value:
2100.000
Variable
Value
Reduced Cost
A
60.00000
0.0000000
C
30.00000
0.0000000
Row
Slack or Surplus
Dual Price
1
2100.000
1.000000
2
0.0000000
5.000000
3
20.00000
0.0000000
4
0.0000000
15.00000
Editing the model is simply a matter of finding and changing the variable, coefficient, or direction
you want to change. Any changes will be taken into account the next time that you solve the model.
Click on the
work.
button, use the Save command from the File menu, or press F4 to save your
2.4 Integer Programming with LINGO
Fairly shortly after you start looking at problems for which optimization might be applicable, you
discover the need to restrict certain variables to integer values (i.e., 0, 1, 2, etc.). LINGO allows you to
identify such variables. We give an introductory treatment here. It is discussed more thoroughly in
Chapter 11, Formulating and Solving Integer Programs. Integer variables in LINGO can be either 0/1
or general. Variables restricted to the values 0 or 1 are identified with the @BIN specification.
Variables that may be 0, 1, 2, etc., are identified with the @GIN specification.
Solving Math Problems with LINGO Chapter 2
29
In the following model, the variables TOM, DICK, and HARRY are restricted to be 0 or 1:
MAX = 4 * TOM + 3 * DICK + 2 * HARRY;
2.5 * TOM
+ 3.1 * HARRY <= 5;
.2 * TOM + .7 * DICK + .4 * HARRY <= 1;
@BIN(TOM);
@BIN(DICK);
@BIN(HARRY);
After solving, to see the solution, choose Solution from the Reports menu, or click on the
button, and choose All Values. The Report Window displays the following:
Optimal solution found at step:
1
Objective value:
7.000000
Branch count:
0
Variable
Value
Reduced Cost
TOM
1.000000
-4.000000
DICK
1.000000
-3.000000
HARRY
0.0000000
-2.000000
Row
Slack or Surplus
Dual Price
1
7.000000
1.000000
2
2.500000
0.0000000
3
0.1000000
0.0000000
General integers, which can be 0, 1, 2, etc., are identified in analogous fashion by using @GIN
instead of @BIN, for example:
@GIN(TONIC);
This restricts the variable TONIC to 0, 1, 2, 3, ….
The solution method used is branch-and-bound. It is an intelligent enumeration process that will
find a sequence of better and better solutions. As each one is found, the Status Window will be updated
with the objective value and a bound on how good a solution might still remain. After the enumeration
is complete, various commands from the Reports menu can be used to reveal information about the
best solution found.
Let’s look at a slightly modified version of the original Enginola problem and see how the GIN
specification might help:
MAX
A <
C <
A +
= 20 * A + 30 * C;
60;
50;
2 * C < 115;
30
Chapter 2 Solving Math Programs with LINGO
Notice the capacity of 115 on the labor constraint (Row 4):
Optimal solution found at step:
1
Objective value:
2025.000
Variable
Value
Reduced Cost
A
60.00000
0.0000000
C
27.50000
0.0000000
Row
Slack or Surplus
Dual Price
1
2025.000
1.000000
2
0.0000000
5.000000
3
22.50000
0.0000000
4
0.0000000
15.00000
Note that a fractional quantity is recommended for C. If fractional quantities are undesirable,
declare A and C as general integer variables:
MAX = 20 * A + 30 * C;
A < 60;
C < 50;
A + 2 * C < 115;
@GIN( A);
@GIN( C);
Solving results in the following:
Optimal solution found at step:
4
Objective value:
2020.000
Branch count:
1
Variable
Value
Reduced Cost
A
59.00000
-20.00000
C
28.00000
-30.00000
Row
Slack or Surplus
Dual Price
1
2020.000
1.000000
2
1.000000
0.0000000
3
22.00000
0.0000000
4
0.0000000
0.0000000
2.4.1 Warning for Integer Programs
Although the integer programming (IP) capability is very powerful, it requires skill to use effectively.
In contrast to linear programs, just because you can formulate a problem as an integer program, does
not mean that it can be solved in very little time. It is very easy to prepare a bad formulation for an
essentially easy problem. A bad formulation may require intolerable amounts of computer time to
solve. Therefore, you should have access to someone who is experienced in IP formulations if you plan
to make use of the IP capability. Good formulations of integer programs are discussed further in
Chapter 11, Formulating and Solving Integer Programs.
2.5 Solving an Optimization Model
Solving a linear or integer program is a numerically intensive process. We do not discuss the
implementation details of the solution algorithms. Writing an efficient solver requires several
person-years of effort. For a good introduction to some of the algorithms, see Martin (1999) or
Greenberg (1978).
Solving Math Problems with LINGO Chapter 2
31
Even though commercial optimization is quite robust, good practice is to avoid using extremely
small or extremely large numbers in a formulation. You should try to “scale” the model, so there are
no extremely small or large numbers. You should not measure weight in ounces one place and volume
in cubic miles somewhere else in the same problem). A rule of thumb is there should be no nonzero
coefficient whose absolute value is greater than 100,000 or less than 0.0001. If LINGO feels the model
is poorly scaled, it will display a warning. You can usually disregard this warning. However, it is good
practice to choose your units of measure appropriately, so this message does not appear.
2.6 Problems
1.
Recall the Enginola/Astro/Cosmo problem of the previous chapter. Suppose we add the restriction
that only an even number (0, 2, 4…) of Cosmos are allowed. Show how to exploit the @GIN
command to represent this feature. Note, this kind of restriction sometimes arises in the
manufacture of plastic wrap. The product starts out as a long hollow tube. It is flattened and then
two resulting edges are cut off to leave you with two flat pieces. Thus, the number of units
produced is always a multiple of 2.
2.
Using your favorite text editor, enter the Enginola formulation. Save it as a simple, unformatted
text file. Start up LINGO, read the model into LINGO, and solve it.
3.
Continuing from (2), use LINGO to prepare an output file containing both the formulation and
solution. Read this file into your favorite text editor and print it.
32
Chapter 2 Solving Math Programs with LINGO
Download PDF