You are on page 1of 15

www.solver.

com

SOLVER
Solver Tutorial for Optimization Users

Welcome to our tutorial about Solvers for Excel and Visual Basic -- the easiest way to solve
optimization problems -- from Frontline Systems, developers of the Solvers in Microsoft Excel, Lotus
1-2-3, and Quattro Pro.

After completing this tutorial, you can learn even more about topics such as linearity versus
nonlinearity and sparsity in optimization models by taking our Advanced Tutorial.

What are Solvers Good For?


Solvers, or optimizers, are software tools that help users find the best way to allocate scarce
resources. The resources may be raw materials, machine time or people time, money, or anything
else in limited supply. The "best" or optimal solution may mean maximizing profits, minimizing costs, or
achieving the best possible quality. An almost infinite variety of problems can be tackled this way, but
here are some typical examples:

Finance and Investment


Working capital management involves allocating cash to different purposes (accounts receivable,
inventory, etc.) across multiple time periods, to maximize interest earnings.

Capital budgeting involves allocating funds to projects that initially consume cash but later generate
cash, to maximize a firm's return on capital.

Portfolio optimization -- creating "efficient portfolios" -- involves allocating funds to stocks or bonds
to maximize return for a given level of risk, or to minimize risk for a target rate of return.

Manufacturing
Job shop scheduling involves allocating time for work orders on different types of production
equipment, to minimize delivery time or maximize equipment utilization.

Blending (of petroleum products, ores, animal feed, etc.) involves allocating and combining raw
materials of different types and grades, to meet demand while minimizing costs.

Cutting stock (for lumber, paper, etc.) involves allocating space on large sheets or timbers to be cut
into smaller pieces, to meet demand while minimizing waste.

Distribution and Networks


Routing (of goods, natural gas, electricity, digital data, etc.) involves allocating something to different
paths through which it can move to various destinations, to minimize costs or maximize throughput.

Loading (of trucks, rail cars, etc.) involves allocating space in vehicles to items of different sizes so as
to minimize wasted or unused space.

Scheduling of everything from workers to vehicles and meeting rooms involves allocating capacity to
various tasks in order to meet demand while minimizing overall costs.

Solver_Excel & Visual Basic


Page 1 of 15
www.solver.com

Solver Tutorial - How to Use the Excel Solver or Solver DLL

What Must I Do to Use a Solver?


To use a solver, you must build a model that specifies:

The resources to be used, using decision variables,


The limits on resource usage, called constraints, and
The measure to optimize, called the objective.

The solver will find values for the decision variables that satisfy the constraints while optimizing
(maximizing or minimizing) the objective.

Using Spreadsheets. Spreadsheets such as Microsoft Excel provide a convenient way to build such
a model. Anyone who has used a spreadsheet is already familiar with the process: Cells on a
worksheet can hold numbers, labels, or formulas that calculate new values -- such as the objective of
an optimization. Constraints are simply limits (specified with <=, = or >= relations) on formula cells.
And the decision variables are simply input cells containing numbers. Frontline's Premium Solver
products provide powerful tools for solving, or optimizing, such models.

Using Custom Programs. Another way to build an optimization model is to write code in a
programming language such as Visual Basic or C/C++. Instead of spreadsheet cells, variables or
arrays in the program hold the decision variables and calculate the constraints and objective.
Frontline's Solver DLL products provide equally powerful tools for solving, or optimizing, models in
this form.

The choice of approach is up to you. Spreadsheets offer a quick way to build a model, and you can
easily explain the model and its results to other users who are usually spreadsheet-literate.
Application programs in Visual Basic, C/C++ or other languages may offer higher performance and
may be easier to distribute widely, within a company or to commercial users. No matter which
approach you choose, Frontline Systems' Solver products provide the key optimization technology in
an easy-to-use, reliable form.

Solver Tutorial - Defining a Model


How Do I Define a Model?

A solver deals with numbers, so you'll need to quantify the various elements of your model: decision
variables, constraints, and the objective -- and their relationships.

Decision Variables
Start with the decision variables. They usually measure the amounts of resources, such as money,
to be allocated to some purpose, or the level of some activity, such as the number of products to be
manufactured, the number of pounds or gallons of a chemical to be blended, etc.

If you are shipping goods from, say, 3 different plants to 5 different warehouses, there are 3 x 5 = 15
different routes along which products could be shipped. So, you might have 15 variables, each one
measuring the number of products shipped along that route.

You might also have 4 different product types, and you might want to plan shipments in each of the
next 6 months. So this might lead to 3 x 4 x 5 x 6 = 360 variables. This illustrates how a model can
become large rather quickly! Part of the art of modeling is deciding how much detail is really required;
for example, you might not need to explicitly measure shipments of different product types.

Solver_Excel & Visual Basic


Page 2 of 15
www.solver.com

Objective Function
Once you've defined the decision variables, the next step is to define the objective, which is normally
some function that depends on the variables. For example, suppose you were planning how many
units to manufacture of three products: TV sets, stereos, and speakers. Your objective might be to
maximize profit, so assume that each TV set yields a profit of $75, each stereo $50, and each speaker
$35. Then your objective function might be:

75 * TVsets + 50 * Stereos + 35 * Speakers

On a spreadsheet where the number of TV sets, stereos and speakers are in cells D9, E9 and F9
respectively, the formula would be:

= 75*D9 + 50*E9 + 35*F9

You'd be finished at this point, if the model did not require any constraints. For example, in a curve-
fitting application, the objective is to minimize the sum of squared differences between each actual
data value, or observation, and the corresponding predicted value. This sum has a minimum value of
zero, which occurs only when the actual and predicted values are all identical. If you asked a solver to
minimize this objective function, you would not need any constraints.

In most models, however, constraints play a key role in determining what values can be assumed by
the decision variables, and what sort of objective value can be attained.

Solver Tutorial - Defining Constraints


Constraints reflect real-world limits on production capacity, market demand, available funds, and so
on. To define a constraint, you first compute a value based on the decision variables. Then you place
a limit (<=, = or >=) on this computed value.

General Constraints. For example, if A1:A5 contains the percentage of funds to be invested in each
of 5 stocks, you might use B1 to calculate =SUM(A1:A5), and then define a constraint B1 = 1 to say
that the percentages allocated must sum up to 100%.

Bounds on Variables. Of course, you can also place a limit directly on a decision variable, such as
A1 <= 100. Upper and lower bounds on the variables are efficiently handled by most optimizers and
are very useful in many problems.

Policy Constraints
Some constraints are determined by policies that you or your organization may set. For example, in
an investment portfolio optimization, you might have a limit on the maximum percentage of funds to be
invested in any one stock, or one industry group.

Physical Constraints
Many constraints are determined by the physical nature of the problem. For example, if your decision
variables measure the number of products of different types that you plan to manufacture, producing a
negative number of products would make no sense. This type of non-negativity constraint is very
common. Although it may be obvious to you, constraints such as A1 >= 0 must be stated explicitly,
because the solver has no other way to know that negative values are disallowed.

As another example of a physically determined constraint, suppose you are modeling product
shipments in and out of a warehouse over time. You'll probably need a balance constraint, which
specifies that, in each time period, the beginning inventory plus the products received minus the
products shipped out equals the ending inventory -- and hence the beginning inventory for the next
period.

Solver_Excel & Visual Basic


Page 3 of 15
www.solver.com

Integer Constraints
Advanced optimization software also allows you to specify constraints that require decision variables
to assume only integer (whole number) values at the solution. If you are scheduling a fleet of trucks,
for example, a solution that called for a fraction of a truck to travel a certain route would not be useful.
Integer constraints normally can be applied only to decision variables, not to quantities calculated from
them.

A particularly useful type of integer constraint specifies that a variable must have an integer value with
a lower bound of 0, and upper bound of 1. This forces the variable to be either 0 or 1 -- nothing in
between -- at the final solution. Hence, it can be used to model "yes/no" decisions.

For example, you might use a 0-1 or binary integer variable to represent a decision on whether to
lease a new machine. Your model might then calculate a fixed lease cost per month, but also a lower
cost per item processed with the machine, if it is used. A solver can help determine whether leasing
the machine will yield higher or lower profits.

Solver Tutorial - Interpreting Solutions


A solution (set of values for the decision variables) for which all of the constraints in the Solver model
are satisfied is called a feasible solution. Most solution algorithms first try to find a feasible solution,
and then try to improve it by finding another feasible solution that increases the value of the objective
function (when maximizing, or decreases it when minimizing). An optimal solution is a feasible
solution where the objective function reaches a maximum (or minimum) value.

A globally optimal solution is one where there are no other feasible solutions with better objective
function values. A locally optimal solution is one where there are no other feasible solutions "in the
vicinity" with better objective function values -- you can picture this as a point at the top of a "peak" or
at the bottom of a "valley" which may be formed by the objective function and/or the constraints. The
Solver is designed to find optimal solutions -- ideally the global optimum -- but this is not always
possible. In many cases, though, you may be happy to find a good solution -- one that is better than
the solution you are using now.

Whether the Solver can find a globally optimal solution, a locally optimal solution, or a good solution
depends on the nature of the mathematical relationship between the variables and the objective
function and constraints (and the solution algorithm used). For more information, click on the link
"What Makes a Model Hard to Solve?"

Solver Tutorial - What Makes a Model Hard to Solve?


Solver models can be easy or hard to solve. "Hard" models may require a great deal of CPU time and
random-access memory (RAM) to solve -- if they can be solved at all. The good news is that, with
today's very fast PCs and advanced optimization software from Frontline Systems, a very broad range
of models can be solved.

Three major factors interact to determine how difficult it will be to find an optimal solution to a solver
model:

The mathematical relationships between the objective and constraints, and the decision variables
The size of the model (number of decision variables and constraints) and its sparsity (see
discussion)
The use of integer variables - memory and solution time may rise exponentially as you add more
integer variables

Mathematical Relationships
Linear programming problems
Smooth nonlinear optimization problems
Global optimization problems

Solver_Excel & Visual Basic


Page 4 of 15
www.solver.com

Nonsmooth optimization problems

The types of mathematical relationships in a model (for example, linear or nonlinear, and especially
convex or non-convex) determine how hard it is to solve, and the confidence you can have that the
solution is truly optimal. These relationships also have a direct bearing on the maximum size of
models that can be realistically solved.

A model that consists of mostly linear relationships but a few nonlinear relationships generally must be
solved with more "expensive" nonlinear optimization methods. The same is true of models with mostly
linear or smooth nonlinear relationships, but a few nonsmooth relationships. Hence, a single IF or
CHOOSE function that depends on the variables can turn a simple linear model into an extremely
difficult or even unsolvable nonsmooth model.

A few advanced solvers can break down a problem into linear, smooth nonlinear and nonsmooth parts
and apply the most appropriate method to each part -- but in general, you should try to keep the
mathematical relationships in a model as simple (i.e. close to linear) as possible.

Below are some general statements about solution times on modern Windows PCs (with, say, 2GHz
CPUs and 512MB of RAM), for problems without integer variables. To learn more about the types of
problems described below, click on Optimization Problem Types.

Linear programming problems -- where all of the relationships are linear, and hence convex -- can
be solved up to hundreds of thousands of variables and constraints, given enough memory and time.
Models with tens of thousands of variables and constraints can be solved in minutes (sometimes in
seconds) on modern PCs. You can have very high confidence that the solutions obtained are globally
optimal.

Smooth nonlinear optimization problems -- where all of the relationships are smooth functions (i.e.
functions whose derivatives are continuous) -- can be solved up to tens of thousands of variables and
constraints, given enough memory and time. Models with thousands of variables and constraints can
often be solved in minutes on modern PCs.

If the problem is convex, you can have very high confidence that the solutions obtained are globally
optimal. If the problem is non-convex, you can have reasonable confidence that the solutions
obtained are locally optimal, but not globally optimal.

Global optimization problems -- smooth nonlinear, non-convex optimization problems where a


globally optimal solution is sought -- can often be solved up to a few hundred variables and
constraints, given enough memory and time. Depending on the solution method, you can have
reasonably high confidence that the solutions obtained are globally optimal.

Nonsmooth optimization problems -- where the relationships may include functions like IF,
CHOOSE, LOOKUP and the like -- can be solved up to scores, and occasionally up to hundreds of
variables and constraints, given enough memory and time. You can only have confidence that the
solutions obtained are "good" (i.e. better than many alternative solutions) -- they are not guaranteed to
be globally or even locally optimal.

Solver Tutorial - Size, Sparsity and Integer Variables

Model Size
The size of a solver model is measured by the number of decision variables and the number of
constraints it contains.

Most optimization software algorithms have a practical upper limit on the size of models they can
handle, due to either memory requirements or numerical stability. Frontline's standard Solver engines
have realistic upper limits on model size; our most powerful Solver engines have no fixed limits. You

Solver_Excel & Visual Basic


Page 5 of 15
www.solver.com

can view the limits in a dialog box in Excel, or obtain them by calling a function in Excel VBA, Visual
Basic or C/C++.

For example, the LP/Quadratic Solver engine included with the Premium Solver Platform has an
upper limit of 2,000 decision variables, and the GRG Nonlinear Solver engine has a limit of 500
decision variables. (Other Solver engines have much higher limits.)

Sparsity
Most large solver models are sparse. This means that, while there may be thousands of decision
variables and thousands of constraints, the typical constraint depends on only a small subset of the
variables. For example, a linear programming model with 10,000 variables and 10,000 constraints
could have a "coefficient matrix" with 10,000 x 10,000 = 100 million elements, but in practice, the
number of nonzero elements is likely to be closer to 2 million.

Frontline's Large-Scale Solver engines are designed to exploit sparsity. For example, a solver
engine that created a coefficient matrix for all 100 million elements in the problem above would likely
need 800 megabytes of RAM just to hold the matrix -- more than the memory available on most PCs --
and it would take a great deal of CPU time to process this huge matrix. Instead, Frontline's Large-
Scale LP Solver creates a sparse data structure for a factorized matrix that might require 24
megabytes for the above problem, and would proceed to solve it far more quickly, with greater
numerical accuracy.

Integer Variables
Integer variables (i.e. decision variables that are constrained to have integer values at the solution) in
a model make that model far more difficult to solve. Memory and solution time may rise
exponentially as you add more integer variables. Even with highly sophisticated algorithms and
modern supercomputers, there are models of just a few hundred integer variables that have never
been solved to optimality.

This is because many combinations of specific integer values for the variables must be tested, and
each test requires the solution of a "normal" linear or nonlinear optimization problem. The number of
combinations can rise exponentially with the size of the problem. "Branch and bound" and "branch
and cut" strategies help to cut down on this exponential growth, but even with these strategies,
solutions for even moderately large mixed-integer programming (MIP) problems can require a great
deal of time.

Different formulations of the same model with integer variables can take radically different amounts
of time to solve. With such models, expert consulting help with the formulation may make the
difference between an easily solvable and a practically unsolvable model. With a well-designed
formulation, it is often possible to solve linear programming problems with hundreds, or even
thousands of integer variables in a modest amount of time.

Solver Tutorial - Step by Step - Product Mix Example


This part of our Solver Tutorial takes you step by step through the process of creating a Solver
model, using a Product Mix example. We'll first show you how to define the problem and write out
formulas for the objective and constraints. Then we'll walk you through two ways to define and solve
your model -- in an Excel spreadsheet, or in a Visual Basic program. Follow either or both sets of
links below to learn more.

Essential Steps
To define an optimization model, you'll follow these essential steps:

Solver_Excel & Visual Basic


Page 6 of 15
www.solver.com

1. Choose a spreadsheet cell, or a variable in your program, to hold the value of each decision
variable in your model.

2. Create a spreadsheet formula in a cell, or an assignment statement in a program function, that


calculates the objective function in your model.

3. Similarly, create formulas in cells, or assignment statements in a program function, to


calculate the left hand sides of your constraints.

4. Use the dialogs in Excel, or function calls in your program, to tell the Solver about your
decision variables, objective and constraint calculations, and desired bounds on constraints
and variables.

5. Click Solve in Excel, or call optimize() in your program, to find the optimal solution.

Within this overall structure, you have a great deal of flexibility, either in a spreadsheet or in a custom
program, in how you choose cells or variables to hold your model's decision variables and constraints,
and which formulas and built-in functions you use. Since decision variables and constraints usually
come in groups, you'll want to use cell ranges in your spreadsheet, or arrays in your program to
represent them.

A Sample Model
Imagine that you are managing a factory that is building three products: TV sets, stereos and
speakers. Each product is assembled from parts in inventory, and there are five types of parts:
Chassis, picture tubes, speaker cones, power supplies and electronics units. Your goal is to produce
the mix of products which will maximize profits, given the inventory of products on hand.

From this description, we can see that the decision variables are the number of products to build,
and the objective function will be (gross) profit. Assume that you can sell TV sets for a gross profit of
$75 each, stereos for a profit of $50 each, and speaker cones for $35 each.

To assemble a TV set, you need 1 chassis, 1 picture tube, 2 speaker cones, 1 power supply and 2
sets of electronics. To make a stereo, you need 1 chassis, 2 speaker cones, 1 power supply and 1 set
of electronics. And to build a speaker, all you need is 1 speaker cone and 1 set of electronics. The
parts you have on hand are 450 chassis, 250 picture tubes, 800 speaker cones, 450 power supplies
and 600 sets of electronics. This defines the constraints in this problem: You can build only a limited
number of products from the parts on hand.

Writing the Formulas


Before we implement this problem statement in either Excel or Visual Basic, let's write out formulas
corresponding to the verbal description above. If we temporarily use the symbol x1 for the number of
TV sets assembled, x2 for the number of stereos, and x3 for the number of speakers, our total profit is:

Maximize 75 x1 + 50 x2 + 35 x3 (Profit)

Building each product requires a certain number of parts of each type. For example, TV sets and
stereos each require one chassis, but speakers don't use one. The number of parts used depends on
the mix of products built (the left hand side of each constraint), and we have a limited number of parts
of each type on hand (the corresponding constraint right hand side):

Subject to:
1 x1 + 1 x2 + 0 x3 <= 450 (Chassis)
1 x1 + 0 x2 + 0 x3 <= 250 (Picture tubes)
2 x1 + 2 x2 + 1 x3 <= 800 (Speaker cones)

Solver_Excel & Visual Basic


Page 7 of 15
www.solver.com

1 x1 + 1 x2 + 0 x3 <= 450 (Power supplies)


2 x1 + 1 x2 + 1 x3 <= 600 (Electronics)

Notice that we've expressed each constraint in a uniform way: Where we aren't using any parts of a
particular type, we just use a quantity of 0 parts. This will make it easier to define the constraints in cell
ranges in Excel or in arrays in Visual Basic, and calculate their values using a function like
SUMPRODUCT in Excel or a simple FOR loop in VB.

Since the number of products built cannot be negative, we'll also have lower bounds on the
variables: x1, x2, x3 >= 0.

Now we'll take you step by step through implementing this Solver model, in either Excel using the
Premium Solver or in Visual Basic (or another language) using the Solver DLL. Click the link you want
to follow first!

Solver Tutorial - Step by Step - Product Mix Example - in Excel


Follow these steps to define and solve the Product Mix problem in an Excel spreadsheet:

Creating an Excel Worksheet


Assuming that you have Microsoft Excel, and either the standard Excel Solver or a Premium Solver
product installed, the next step is to create a worksheet where the formulas for the objective
function and the constraints are calculated. In the worksheet below, we have reserved cells D9, E9
and F9 to hold our decision variables x1, x2 and x3: The number of TV sets, stereos and speakers to
build. (Click on the worksheet for a full-size image.)

Notice that the gross profit for each assembled product ($75, $50 and $35) is shown in cells D17, E17
and F17. This allows us to compute the objective function in cell D18 as:

=SUMPRODUCT(D17:F17,D9:F9)

In the table area D11:F15, we've entered the number of parts of each type needed to assemble each
finished product. For example, the 2 at D13 means that we need 2 speaker cones to build a TV set.
These numbers come directly from the formulas for the constraints shown earlier. With these values in
place, we can enter a single formula at cell C11:

=SUMPRODUCT(D11:F11,$D$9:$F$9)

to compute the total number of chassis we will use for any given number of TV sets, stereos and

Solver_Excel & Visual Basic


Page 8 of 15
www.solver.com

speakers. Then we can copy this formula to cells C12:C15 to compute the total number of parts used
of all the other types. (The dollar signs in $D$9:$F$9 specify that this cell range stays constant, while
the cell range D11:F11 becomes D12:F12, D13:F13, etc. in the copied formulas.)

In cells B11:B15, we've entered the number of parts we have in inventory of each type. This allows us
to express the constraints shown earlier as:

C11:C15 <= B11:B15

This is a shorthand for C11 <= B11, C12 <= B12, and so on through C15 <= B15. We can enter this
form directly in the Solver dialogs. We will also enter the constraint:

D9:F9 >= 0

to require the decision variables to have non-negative values.

Using the Solver Dialogs


To let the Solver know which cells on the worksheet represent the decision variables, constraints and
objective function, we select Tools Solver... in Excel (or Range Analyze Solver... in 1-2-3), which
displays the Solver Parameters dialog. In the Set Target Cell edit box, we type or click on cell D18, the
objective function. In the By Changing Cells edit box, we type D9:F9 or select these cells with the
mouse.

To add the constraints, we click on the Add button, select


cells C11:C15 in the Cell Reference edit box (the left hand
side), and select cells B11:B15 in the Constraint edit box (the
right hand side); the default relation <= is OK. (Click on the
dialog to see it full size.)

We choose the Add button again (either from the dialog above, or from the main Solver Parameters
dialog) to define the non-negativity constraint on the decision variables.

When we've completely entered the problem, the Solver


Parameters dialog looks like this (Click on the dialog to see it
full size.) This dialog appears for the Premium Solver
Platform; the dialogs for the standard Excel Solver and the
Lotus 1-2-3 Solver are similar, but they lack the Solver
Engine dropdown list and the Variables button.

Finding and Using the Solution


To find the optimal solution, we simply click on the Solve button. After a moment, the Solver returns
the optimal solution: 200 in cells D9 and E9, and 0 in cell F9. This means that we should build 200 TV
sets, 200 stereos and no speakers, to earn a total profit of $25,000 (shown at cell D18).

The message "Solver found a solution" appears in the Solver


Results dialog, as shown here (Click on the dialog to see it
full size). We now click on "Answer" in the Reports list box to
produce an Answer Report, and click OK to keep the optimal
solution values in cells D9:F9.

After a moment, the Solver creates another worksheet containing an Answer Report, like the one
below, and inserts it to the left of the problem worksheet in the Excel workbook. (Click on the Answer

Solver_Excel & Visual Basic


Page 9 of 15
www.solver.com

Report for a full size image.)

This report shows the original and final values of the objective function (target cell) and the decision
variables (adjustable cells), as well as the status of each constraint at the optimal solution. Notice that
the constraints on speaker cones and electronics are binding and have a slack value of 0: We used
up all of the parts of these two types in building 200 TV sets and 200 stereos, but there were 50 units
of each of the other parts left over. If we could obtain additional speaker cones and electronics, we
could further increase total profits, but extra units of the other parts would not help in the short run.

If you've gotten to this point, congratulations! You've successfully set up and solved a simple
optimization problem using Microsoft Excel. If you like, you can see how to set up and solve the same
Product Mix problem in a Visual Basic program that calls our Solver DLL. If you haven't yet read the
other parts of the tutorial, you may want to return to the Tutorial Start and read the overviews "What
are Solvers Good For?", "How Do I Define a Model?" and "What Makes a Model Hard to Solve?"

This was an example of a linear programming problem. Other types of optimization problems may
involve quadratic programming, mixed-integer programming, constraint programming, smooth
nonlinear optimization, global optimization, and nonsmooth optimization. To learn more, click on
Optimization Problem Types. For a more advanced explanation of linearity and sparsity in
optimization problems, continue with our Advanced Tutorial.

Solver Tutorial - Step by Step - Product Mix Example - in Visual


Basic
Follow these steps to define and solve the Product Mix problem in a Visual Basic program (the steps
in another Windows programming language would be very similar):

Creating a Visual Basic Program


Assuming that you have Visual Basic and a Solver DLL product installed, and you've opened a new or
existing project, the next step is to define a VB function where the formulas for the objective function
and the constraints are calculated. In the VB code module below, we define a function FuncEval()
with the arguments required by the Solver DLL products. (Click on the code module for a full-size
image.)

Solver_Excel & Visual Basic


Page 10 of 15
www.solver.com

In the above code, we've written the formulas to correspond directly to our earlier outline of the
problem (click on Writing the Formulas to see this again). But we could also use Visual Basic arrays
and FOR loops to compute these values. (In fact, we can let the Solver DLL compute the objective
and constraint values for us, using just the coefficients such as 75, 50 and 35 for the objective
function. Read the page Using the LP Coefficient Matrix Directly in our Advanced Tutorial for more
details on this approach.)

Using the Solver DLL Functions


Next, we must tell the Solver DLL about elements of the model that aren't included in the FuncEval()
function above. For example, the left hand sides of the constraints are computed in FuncEval(), but
the constant right hand sides (450 Chassis, 250 Picture tubes, etc.), as well as lower bounds of 0 on
the variables, must be specified separately. To do this, we call the Solver DLL loadnlp() function
and pass appropriate arguments, as shown in the code module below. (Click on the code module for
a full-size image.)

In this code segment, obj, rhs, sense, matval, x, lb and ub are previously defined arrays -- all of type
Double, except for sense which is of type Byte. Other symbols such as NullL and INFBOUND are
predefined. The call to loadnlp() defines a problem and passes these arrays as arguments The obj
and matval arguments are work arrays for the Solver DLL, and need not be initialized when used with
loadnlp().

Finding and Using the Solution


Once the problem is defined, we can simply call the optimize() function to find the optimal solution.
We then call the solution() function to retrieve the solution status, the objective function, and the
decision variable values at the optimal solution, as shown in the code module below. (Click on the
code module for a full-size image.)

Solver_Excel & Visual Basic


Page 11 of 15
www.solver.com

The solution status is an integer code reporting the status of the optimization. For example, 1
(symbolic name PSTAT_OPTIMAL) means that an optimal solution was found, 2 (symbolic name
PSTAT_INFEASIBLE) means that there is no feasible solution (values for the variables that will satisfy
all of the constraints), and so on. All of the possible status codes are defined in the Solver DLL
Programmer's Guide.

If you've gotten to this point, congratulations! You've successfully set up and solved a simple Solver
problem using Visual Basic. If you like, you can see how to set up and solve the same Product Mix
problem in Microsoft Excel. If you haven't yet read the other parts of the tutorial, you may want to
return to the Tutorial Start and read the overviews "What are Solvers Good For?", "How Do I Define a
Model?" and "What Makes a Model Hard to Solve?"

This was an example of a linear programming problem. Other types of optimization problems may
involve quadratic programming, mixed-integer programming, constraint programming, smooth
nonlinear optimization, global optimization, and nonsmooth optimization. To learn more, click on
Optimization Problem Types. For a more advanced explanation of linearity and sparsity in
optimization problems, and use of the Solver DLL loadlp() function, continue with our Advanced
Tutorial.

Solver Advanced Tutorial - Gradients, Linearity, and Sparsity


The Product Mix example we've just covered step by step is the simplest type of optimization problem
-- a linear programming problem. In this Advanced Tutorial, we'll use the Product Mix example to
illustrate several concepts that underlie all optimization problems:

Derivatives, Gradients, and the Jacobian


Recall the formula we wrote for the objective function in the Product Mix example:

Maximize 75 x1 + 50 x2 + 35 x3 (Profit)

Consider how this function changes as we change each variable x1, x2 and x3. If we increase x1 by
1, Profit increases by 75; if we decrease x2 by 1, Profit decreases by 50; and so on. In mathematical
terms, 75 is the partial derivative of the objective function with respect to variable x1, and 50 is its
partial derivative with respect to x2.

A function of several variables has an overall "rate of change" called its gradient, which is simply a
vector of its partial derivatives with respect to each variable:

75 50 35

Similarly, each constraint function has a gradient, which is a vector consisting of the partial derivatives
of that function with respect to each decision variable. The first constraint (Chassis) has a gradient

Solver_Excel & Visual Basic


Page 12 of 15
www.solver.com

vector [ 1 1 0 ] because it increases by 1 if either x1 or x2 is increased by 1, but it doesn't change if x3


is changed:

1 x1 + 1 x2 + 0 x3 <= 450 (Chassis)


1 x1 + 0 x2 + 0 x3 <= 250 (Picture tubes)
2 x1 + 2 x2 + 1 x3 <= 800 (Speaker cones)
1 x1 + 1 x2 + 0 x3 <= 450 (Power supplies)
2 x1 + 1 x2 + 1 x3 <= 600 (Electronics)

We can combine the gradients of all the constraint functions into a matrix, usually called the Jacobian
matrix. The Jacobian for the Product Mix example looks like this:

1 1 0
1 0 0
2 2 1
1 1 0
2 1 1

Almost every "classical" optimization method, for linear and smooth nonlinear problems, makes use of
the objective gradient and the Jacobian matrix of the constraints.

Linearity, Nonlinearity, and Non-Smoothness


Note that each of these partial derivatives is constant in the Product Mix problem. For example, the
objective function changes by 75 units, whether x1 moves from 0 to 1 or from 100 to 101. This is true
of every linear problem: In a linear programming problem, the objective gradient and Jacobian
matrix elements are all constant. In this case, the Jacobian matrix (sometimes with the objective
gradient as an extra row) is often called the LP coefficient matrix.

Consider a simple nonlinear function such as x1 * x2. If x1 increases by 1 unit, how much will the
function change? This change is not constant; it depends on the current value of x2. Even the
"square" function x1 * x1 behaves this way. If, for example, x1 increases from 1 to 2, the function
increases from 1 to 4, a change of 3 units. But if x1 increases from 2 to 3, the function increases from
4 to 9, a change of 5 units. In a nonlinear optimization problem, the objective gradient and
Jacobian matrix elements are not all constant.

In a smooth nonlinear optimization problem, the partial derivatives may be variable, but they change
continuously as the variables change -- they do not "jump" from one value to another. A nonsmooth
function such as ABS(x1) behaves differently: If x1 is positive, it has a partial derivative of 1, but if x1
is negative, it has a partial derivative of -1. The partial derivative is discontinuous at 0, jumping
between 1and -1.

Mixed Linear and Nonlinear Functions and Terms


Many nonlinear (and even nonsmooth) optimization models include some all-linear constraints and /
or some variables that occur linearly in all of the constraints. Advanced Solvers for smooth
nonlinear and nonsmooth problems can gain speed and accuracy by taking advantage of this fact.
The hybrid Evolutionary Solver in the Premium Solver Platform, the Large-Scale GRG Solver engine,
and the OptQuest Solver engine are examples of advanced Solvers that work in this way.

Even if a constraint is nonlinear, it may be separated into nonlinear and linear terms. Similarly,
some variables may occur only in the linear terms of otherwise nonlinear constraints. Some very
advanced Solvers -- including a future large-scale nonlinear Solver from Frontline Systems -- are
designed to exploit these properties of an optimization problem.

Solver_Excel & Visual Basic


Page 13 of 15
www.solver.com

Solver Advanced Tutorial - Using the LP Coefficient Matrix Directly


In our earlier Visual Basic code example for the Product Mix problem, we defined a VB function
FuncEval() that computed the objective and constraint left hand side values for any given values of the
decision variables. We called the Solver DLL loadnlp() function, passing the address of FuncEval().

This approach parallels our Excel spreadsheet example, where the same objective and constraint left
hand side values are computed by Excel formulas. And this general approach can be used for any
type of optimization problem -- linear, smooth nonlinear, or nonsmooth.

But in a linear programming problem -- where the objective gradient and Jacobian matrix elements
are all constant -- we could write a standard FuncEval() function that would work for any model. If we
set up an array obj() holding the objective gradient elements, and a two-dimensional array matval()
that held the quantities of each part needed to build each product, the body of FuncEval() would
contain:

objval = 0
'Compute objective value
For j = 0 To numcols
objval = objval + obj(j) * var(j)
Next j
For i = 0 To numrows
lhs(i) = 0
'Compute each constraint
For j = 0 To numcols
lhs(i) = lhs(i) + matval(i,j) * var(j)
Next j
Next i

In fact, you don't have to write this code -- it is effectively incorporated in the Solver DLL products.
The Simplex method for solving linear programming problems works directly with the objective
gradient and Jacobian matrix. Instead of writing a FuncEval() function and passing work arrays called
obj() and matval() to loadnlp(), you can simply store the constant objective gradient values in obj(), the
constant Jacobian values in matval(), and call the loadlp() function, passing these arrays as
arguments:

'Define the Product Mix problem as an LP


lp = loadlp(PROBNAME, 3, 5, -1, obj, rhs, _
sense, NullL, NullL, NullL, matval, _
x, lb, ub, NullD, 15)
'Find and retrieve the optimal solution
optimize lp
solution lp, stat, objval, x, _
NullD, NullD, NullD

Note that loadlp() does not take an argument such as FuncEval(), and you don't have to write this
function. As shown here, you can use most other Solver DLL functions with problems defined via
calls to either loadlp() or loadnlp().

How is a linear programming model handled in Excel? The default Solver engine in the standard
Excel Solver and the Premium Solver products handles nonlinear problems -- its solution approach
is similar to our earlier Visual Basic example that called loadnlp(). But if a Solver engine specialized
for LP problems -- such as the standard LP/Quadratic Solver -- is selected, the constant LP
coefficients are extracted automatically -- the approach is similar to the one above that called
loadlp().

Solver Advanced Tutorial - Handling Sparsity in the Jacobian


Directly

Solver_Excel & Visual Basic


Page 14 of 15
www.solver.com

When we wrote the constraint functions in the Product Mix example, we included terms such as "0
times x3" in

1 x1 + 1 x2 + 0 x3 <= 450 (Chassis)

so that we could treat the constraints uniformly, use the SUMPRODUCT function in Excel (or
equivalent FOR loops in Visual Basic or another language), and easily describe the Jacobian matrix.
But we recognized that this constraint does not depend on x3, the number of Speakers produced -- it
has a zero partial derivative with respect to x3.

Looking at the entire Jacobian matrix for the Product Mix model, we see that it contains several zero
elements.

1 1 0
1 0 0
2 2 1
1 1 0
2 1 1

In large optimization models, the Jacobian matrix often contains a great many zero elements. In
fact, in LP models with tens of thousands of variables and constraints, it is common for only 2% to 3%
of the elements to be nonzero. A matrix with many zero elements is called sparse; a matrix with
mostly nonzero elements is called dense.

A matrix of a few hundred, or even a thousand rows and columns doesn't take that much memory, by
modern standards. But a 10,000 by 10,000 matrix of double precision numbers (8 bytes each) would
require 800 megabytes of RAM -- more than the memory available on most PCs today -- if all of the
zero elements were stored explicitly. A better alternative is to store only the nonzero elements and
their indices.

Solver engines designed for large-scale problems, such as the Large-Scale LP Solver, Large-Scale
GRG Solver, Large-Scale SQP Solver, KNITRO Solver, MOSEK Solver and XPRESS Solver, are
designed to exploit sparsity in the Jacobian matrix. In the Premium Solver products for Excel, zero
elements in the Jacobian are detected automatically, and only nonzero elements are passed to the
Solver engines.

But when you call the Solver DLL products from your own program in Visual Basic or another
language, you must create arrays that store only the nonzero elements and their indices, and
pass these arrays to the Solver DLL. You do this by creating the matbeg, matcnt and matind arrays
(and a much smaller matval array) and passing these four arguments to either loadlp() or loadnlp().
Further details can be found in the Solver DLL Platform Programmer's Guide.

Solver_Excel & Visual Basic


Page 15 of 15

You might also like