MSF ExcelProgrammingPrimer
MSF ExcelProgrammingPrimer
This documentation is provided to you for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright,
no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form
or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express
written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering
subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the
furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual
property.
2006-2010 Microsoft Corporation. All rights reserved.
Microsoft and Excel are registered trademarks or trademarks of Microsoft Corporation in the United States and/or other
countries.
Contents
Contents ........................................................................................................................................................ 2
Introduction .................................................................................................................................................. 3
What does Solver Foundation for Excel look like?........................................................................................ 4
Basic How-To................................................................................................................................................. 7
Creating the Model ................................................................................................................................... 7
Binding Data .............................................................................................................................................. 8
Goals, Constraints, and Directives .......................................................................................................... 11
Checking and Solving the Model ............................................................................................................. 15
Sample Projects........................................................................................................................................... 16
Airline Allocation Sample ..................................................................................................................... 17
Data and data binding ......................................................................................................................... 17
Appendix 1: Directive Options .................................................................................................................... 28
CompactQuasiNewtonDirective ............................................................................................................. 28
ConstraintProgrammingDirective ........................................................................................................... 28
HybridLocalSearchDirective .................................................................................................................... 29
InteriorPointMethodDirective ................................................................................................................ 29
MixedIntegerProgrammingDirective ...................................................................................................... 30
SimplexDirective ..................................................................................................................................... 30
StochasticDirective.................................................................................................................................. 31
Appendix 2: Optimization Modeling Language (OML)................................................................................ 32
Appendix 2: OMLX File Format ................................................................................................................... 33
Root Element Model ........................................................................................................................ 33
Name Element .................................................................................................................................... 33
ModelText Element............................................................................................................................. 33
DataBindings Element ......................................................................................................................... 33
BindingSourceInfo Element ................................................................................................................ 34
InputBindingInfo Element ................................................................................................................... 35
OutputBindingInfo Element ................................................................................................................ 36
Directives Element .............................................................................................................................. 36
Introduction
Solver Foundation is a pure, managed code runtime for mathematical programming, modeling, and
optimization. This .NET/CLR based framework provides a rich set of tools, services, and engines for
Visual Studio and Office. The Solver Foundation for Excel add-in enables the mathematical modeler to
optimize problems easily and within the comfort of the Excel environment. Key features of this
application include the following:
o
o
o
o
Modeling Editor. Simplifies the modeling experience by using a collection of modeling panes
and includes a modeling syntax checker and other similar features exposed through the Solver
Foundation ribbon bar for Excel.
Excel Cell binding. Binds static or dynamic data in a range to and from the model.
Automatic Results and Report generation. Displays all results and reports on separate Excel
sheets for easy viewing, saving, or integration into other workflow systems.
Import/Export capabilities. Supports MPS, QPS, SMPS, and OML formats. You can deploy a
model from Excel directly to C# - avoiding the need for the application developer to re-write any
model code.
Improved Microsoft Office Integration. Deploy models from Excel to SharePoint using our
OMLX file (model + metadata). This provides a script-safe and secure method to transfer models
between client and a server.
This release of the Solver Foundation for Excel add-in provides several improvements:
Support for 18 new OML operators such as Sin, Cos, If, Max, Min.
Nonlinear optimization support using the Compact Quasi Newton and Hybrid Local Search solvers.
This document covers how you can model, solve, and generate reports by using the Solver Foundation
for Excel add-in. Please direct queries and feature suggestions online at www.solverfoundation.com.
Models are entered in the right-hand pane. Models are built from input parameters, decisions, goals (in
the form of objective functions), constraints, and sets. Constraint and goal expressions are specified
using the Optimization Modeling Language (OML), a type safe declarative modeling language. You can
bind input parameters and output decisions to cell ranges in a worksheet.
To get started, enter the model in the modeling pane, and press the Check button on the Ribbon to
make sure the model is valid. Then, press the Solve button, and view the results on the Solver
Foundation Results sheet.
The Solver Foundation ribbon tab includes many features to help build, solve, and analyze models.
Model. Basic functionality for displaying, importing, exporting, and deploying models.
o Model. Hides or shows the modeling pane window.
o Import. Supports importing OML, MPS and QPS files. The add-in supports file sizes up to 10
MB. Larger files can be processed by the command-line tool MSFCLI, or by writing .NET
code to call Solver Foundation Services.
o Export. Save as MPS, QPS (when appropriate) or OML.
o Deploy. A model can be deployed to SharePoint (via OMLX format) or Visual Studio (via C#).
o Check. Performs syntax checking on the model. Results of this check will be displayed below
the model in the Model Validation text box within the modeling pane.
Report Verbosity. These options enable the user to fine tune what details are generated.
o Clear Log On Solve. The Log tab will clear after each Solve.
o Decisions. Display detailed Decision information when available.
o Directives. Display Directive related information.
o Infeasibility. Include solver specific sensitivity information.
o Sensitivity. Include solver-specific feasibility information when available.
o Solver Details. Include details such as solve time, algorithms used, etc.
Simulation. Solver Foundation supports stochastic programming to perform basic MonteCarlo and
Latin Hypercube simulations.
o Method. The sampling method used to solve stochastic models. Choose between Automatic
(the default), Monte Carlo and Latin Hypercube.
o Count. The number of samples. Larger values lead to improved accuracy but require more
time to solve.
o Seed. The seed used to initialize the random number generator. Using the same seed leads
to more predictable solution behavior.
Solve. The main runtime to invoke a solution.
o Solve. Runs the appropriate solver, and generates results in the Solver Foundation Results
sheet.
o (Solve) Next. Iterate through Constraint Programming problems that have multiple
solutions. Multiple solutions will be shown sequentially in the Solver Foundation Results
sheet.
o Stop. Interrupts the Check, Solve or Summary functions mid-stream.
Basic How-To
This section goes through the general process of creating and solving a
model.
Sets. These are collections of objects that are used as indexes in Parameters or Decisions. For
example, a matrix Parameter takes two Sets as indexes.
Parameters. These are the inputs of the solver data is bound from an Excel sheet to the model.
Parameters can be indexed, or singled-valued constants of type Reals, Integers, or Booleans.
Decisions. These are the outputs or results of the model being solved. Data can be bound to a
specific Excel sheet. Supported types for Decisions can be Reals, Integers, or Booleans.
Goals. This is where you define the business goal or goals you are trying to accomplish. These are
used to specify a quantity or equation that should be maximized or minimized.
Constraints. This is where you can add constraints to the model. These are restrictions placed on
Decisions.
Directives. Ability to provide solver hints and other solver specific attributes.
Model. Shows the model in OML format. Advanced users can manually edit OML directly from this
pane.
Log. Solver output details. This is similar to the command line interface output.
Binding Data
Most real world models have an algebraic form but depend on data to be used for a specific problem
instance. For example, a model may use a Demand parameter where product demand values are stored
in a range of cells. Data can be entered manually, or imported from a variety of sources using the Get
External Data button on the Data ribbon tab. Once the sheet contains model data, you can use the
Parameters and Decisions modeling pane tabs to bind the data to and from the model.
Parameters
The Parameters tab allows the user to bind data from an Excel sheet to the model. The following
parameter types are available:
-
Parameters can be single-valued or indexed using Sets. A Set is an unordered list of items that define the
valid set of indexes for a Parameter or Decision. Both random and non-random parameters can be
indexed. You can use the Binding Editor to associate Sets with Parameters. Click on the Binding button
for a parameter to display the Binding Editor. The Binding Editor is shown in the following screenshot:
Set the cell range for the data using the control at the top of the dialog. The Binding Editor supports
three different data layouts.
A single row or column. Uncheck the Range Includes Column Headers and select the range
containing the data. In the first Set dropdown, choose the Set that represents the indexes for
the Parameter.
A table (matrix) containing data values. Uncheck the Range Includes Column Headers and
select the range containing the data. In the first Set dropdown select the Set that represents the
first (row) index. In the second Set dropdown select the second (column) index. The Column
Header controls are disabled.
A table containing rows containing indexes and values. The table has headers that describe the
contents of each column. Check the Range Includes Column Headers checkbox. Typically one
or more columns will represent indexes and one column contains the values. For each column
that represents an index, select the appropriate Set and Column Header. In the Value Field
dropdown, select the dropdown that contains the parameter values.
Decisions
Similar to the Parameters tab, the Decisions pane allows the user to bind Decisions generated from
Solver Foundation directly into an Excel sheet. The following input Decisions are available.
-
Decision. An output value that represents values that the solver should determine.
Recourse Decision. An output value in a stochastic model that is made in response to the
realization of a random parameter.
Decisions, just like Parameters, can be indexed using Sets. In the airline simulation sample below, there
are two decisions:
A doubly-indexed Decision that represents the number of aircraft of each type assigned to each
route.
A Recourse Decision that represents the number of passengers that will be bumped from each flight.
In this example, the customer demand for each route is random, therefore the number of passengers
that will need to be bumped is not known in advance. Since the number of bumped passengers depends
on the realization of the demand random parameter, it is defined as a recourse decision.
10
11
The first step is to specify a unique name for the goal. The Kind of goal is either Maximize or Minimize.
The Expression is text that combines Decisions, Parameters, and constant values using the rich set of
operations provided by OML. This example uses the Sum and Foreach expressions to iterate over the
set of possible routes and airplane types. Goals can contain Recourse Decisions, in which case Solver
Foundation can substitute the expected value over all samples of the random parameters.
12
Constraints
A constraint is simply an expression that restricts possible values for decisions. Some constraints are
logical in nature for example, the sum of demand over all routes is equal to the total demand.
Others represent business constraints, such as do not allow server load to exceed 90%.
The constraint above is indexed over the Routes set using the Foreach expression. The constraint says
that for each route, the demand is the sum of the products of the passenger capacities and airplane
fleet allocation, minus the number of passengers bumped from the route
13
Directives
Parameters, decisions, goals and constraints all describe the content of a model, whereas a directive
specifies how a model should be solved. Directives are not required when building a model; however,
these are useful when you want to customize the behavior of a solver.
A Stochastic Directive in used for this model because it contains random parameters. A complete list of
Directive attributes can be found in the appendix.
14
15
Sample Projects
The Solver Foundation installation includes several samples to help you get started. These samples
demonstrate a variety of solver and application domain types:
Area
Solver Foundation for Excel
XLSX
XLSX
XLSX
XLSX
XLSX
XLSX
XLSX
Domain
Samples
Linear Programming
Constraint Programming
Quadratic Programming
Simulation
Mixed Integer Programming
Nonlinear Programming
Special Ordered Sets (SOS)
These samples span classical problem domains in mathematical programming and operations research:
linear programming (LP), quadratic programming (QP), constraint programming (CP), nonlinear
programming (NLP) and stochastic programming (SP).
16
17
The actions in the Solver Foundation ribbon cover the entire lifecycle of building a model, checking
correctness, solving, and analyzing results. The modeling pane on the right guides the model creation
18
process. The first step is to create Parameters that bind to the data in the worksheet. In the right-hand
pane, there is defined Parameter called Demand that binds to a range in the worksheet. The decisions
are defined in a similar fashion. The most interesting decision is Allocation: the number of planes of each
type to assign to each route. First define Routes and Aircraft sets by switching to the Sets tab, clicking
New, and entering the name for each Set.
Now create the decision by switching to the Decisions tab and clicking New. Use the Binding Editor to
associate the Routes and Aircraft sets:
19
20
The goal is to minimize operational costs. This goal is expressed by combining the inputs and outputs
using OML's expression language. Similarly you can define constraints, such as the constraint that the
sum of the allocations for a plane type over all routes must not exceed inventory.
21
The model definition is complete. The OML tab shows the complete representation of the model in
OML. Click the Solve button, and Solver Foundation automatically interprets the OML, determines the
underlying type of optimization problem, calls the correct underlying solver, and returns the best
allocation, shown in the results:
22
The decision values are data bound to the right-hand portion of the worksheet. These values are used in
a pivot table and chart that provide additional insight:
23
Solver Foundation introduces a simulation framework that extends OML to handle stochastic models. In
this example, the customer demand is an imprecise estimate. Therefore it is more realistic to capture
this uncertainty directly in the model. This is easily done in OML using a random parameter.
24
The demand is modeled using a NormalDistribution parameter. A normal distribution is defined by its
mean and standard deviation. These values are different for each route, so this example uses data
binding to define the mean and standard deviation. The model can now be modified to use the random
parameter. Since the demand is random, it is necessary to have a fallback plan in case demand for a
route exceeds capacity. If a route is overbooked, it is possible to bump passengers and refund them
money. This is a recourse decision that is made once the actual passenger demand is known. Recourse
decisions are defined in the Output tab, similar to how the Allocation decision was defined earlier.
25
Click Solve to use sampling to simulate different values for the random parameters and solve the model,
finding the allocation that works best over the range of possible demands.
The average, minimum, and maximum values for the recourse decisions are shown in the log.
26
Click the Summary button to produce a solution report, which in this case includes additional
information particular to stochastic models, including information about how sampling was performed.
Solver Foundation chooses default values for sampling parameters, but these can be changed in the
Simulation area of the Solver Foundation ribbon. For example, the number of samples can be increased
to 200.
More information about sampling options and stochastic models can be found in the Simulation Primer.
27
CompactQuasiNewtonDirective
ConstraintProgrammingDirective
28
Bool. RestartEnabled: Whether to enable the solver to restart from the beginning if it isn't making
progress
HybridLocalSearchDirective
Bool. RunUntilTimeout: whether to continue to search for improved solutions until aborted.
InteriorPointMethodDirective
Double. GapTolerance: The gap tolerance. If set to 0, the solver can choose
29
MixedIntegerProgrammingDirective
Double. GapTolerance: Gets or sets the tolerance to declare an integer solution optimal
SimplexDirective
30
StochasticDirective
31
32
Name Element
Name element contains the name of the model.
<Name>Supply Chain Planning</Name>
ModelText Element
ModelText element contains the entire OML model text.
<ModelText>Model[
Parameters[
Sets, Products, Factories, Areas, Promotions
],
...
]</ModelText>
DataBindings Element
DataBindings element contains information for binding to different type of data sources. DataBindings
element can contains 0 to n BindingSourceInfo element. Each BindingSourceInfo element contains
information for setting up input and output data binding when solving a model.
<DataBindings>
<BindingSourceInfo>
<Name>XML</Name>
...
</BindingSourceInfo>
<BindingSourceInfo>
<Name>SQL</Name>
...
33
</BindingSourceInfo>
</DataBindings>
BindingSourceInfo Element
In BindingSourceInfo element there are two metadata elements:
Name element contains the name of the binding source. The name should be unique in
DataBindings collection.
Connection element contains the connection information for the data source. For SQL Server it
will be connection string; For XML file it will be the filename; For SharePointSite it will be the Url
to the site collections.
34
...
</DecisionBindings>
</BindingSourceInfo>
InputBindingInfo Element
InputBidingInfo contains binding information to a parameter:
35
OutputBindingInfo Element
OutputBindingInfo contains information for single decision binding:
Directives Element
Directives element contains all directives will be used to solve the model. Directives element contains 0
to n directives. Each directive represent an instance can be created by SFS. There should be only one
instance per type can exist in directives collection.
Here is an example of Directives element:
<Directives>
<DirectiveInfo>
<TypeName>SimplexDirective</TypeName>
<Enabled>true</Enabled>
<Properties>
<DirectivePropertyInfo>
<Name>Algorithm</Name>
<Value xsi:type="xsd:int">2</Value>
</DirectivePropertyInfo>
36
<DirectivePropertyInfo>
<Name>Arithmetic</Name>
<Value xsi:type="xsd:int">2</Value>
</DirectivePropertyInfo>
<DirectivePropertyInfo>
<Name>Basis</Name>
<Value xsi:type="xsd:int">1</Value>
</DirectivePropertyInfo>
<DirectivePropertyInfo>
<Name>GetSensitivity</Name>
<Value xsi:type="xsd:boolean">true</Value>
</DirectivePropertyInfo>
<DirectivePropertyInfo>
<Name>IterationLimit</Name>
<Value xsi:type="xsd:int">200</Value>
</DirectivePropertyInfo>
<DirectivePropertyInfo>
<Name>MaximumGoalCount</Name>
<Value xsi:type="xsd:int">20</Value>
</DirectivePropertyInfo>
<DirectivePropertyInfo>
<Name>TimeLimit</Name>
<Value xsi:type="xsd:int">2000</Value>
</DirectivePropertyInfo>
</Properties>
</DirectiveInfo>
</Directives>
DirectiveInfo Element
DirectiveInfo element contains all information for a SFS directive object. DirectiveInfo element contains
following elements:
DirectivePropertyInfo Element
DirectivePropertyInfo contains name value pair for each directive object property. DirectivePropertyInfo
element contains following elements:
37
38