seminar: embedding optimization in applications with mpl optimax - april 2012
TRANSCRIPT
![Page 1: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/1.jpg)
1Copyright © 2012 Maximal Software, Inc. All rights reserved
Optimization Seminar - OptiMax
Embedding Optimization in Applications
with MPL OptiMax
Presented by
Bjarni Kristjansson
Maximal Software, Inc.
![Page 2: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/2.jpg)
2Copyright © 2012 Maximal Software, Inc. All rights reserved
Presentation Overview
• Tools and Methods for embedding optimization in Applications• Introduction to MPL OptiMax for Visual Basic, CSharp, Python,
and Matlab• Using the Python Command Language Interface (IDLE)• Using the Numpy, Scipy, and MatPlotLib with MPL for Python• Writing GUI Optimization Applications in Python and CSharp• Multi-Threaded support for MPL OptiMax on Servers• Connecting MPL OptiMax with Web API’s and Web Services• Deploying Optimization on Mobile/Tablet Computers
![Page 3: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/3.jpg)
3Copyright © 2012 Maximal Software, Inc. All rights reserved
Building Optimization Applications Development/Deployment Issues
Phase I - Development• Model Development Environment • Formulating the Model• Connecting to External Data• Solver Testing and Tuning
Phase II - Deployment• End-User Applications• Run-times / Component Libraries • Embedding Optimization into Application• Packaging/Distribution
![Page 4: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/4.jpg)
4Copyright © 2012 Maximal Software, Inc. All rights reserved
SolverSolver
ProgramProgram
Callable Libraries for Solvers
![Page 5: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/5.jpg)
5Copyright © 2012 Maximal Software, Inc. All rights reserved
SolverSolver
ModLangModLang
SolverSolver
ProgramProgram
Algebraic Modeling Languages
![Page 6: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/6.jpg)
6Copyright © 2012 Maximal Software, Inc. All rights reserved
SolverSolverDBDB
ModLangModLang
SolverSolverDBDB
ProgramProgram
Connecting to Databases
![Page 7: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/7.jpg)
7Copyright © 2012 Maximal Software, Inc. All rights reserved
SolverSolverDBDB
ProgramModLang
ProgramModLang
Merging Program with Model
![Page 8: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/8.jpg)
8Copyright © 2012 Maximal Software, Inc. All rights reserved
Application Programming• Programming Languages (Procedural)• Application/User-Interface• Database Programming• Most programmers tend to know rather little about
optimization and formulating models
Optimization Modeling• Modeling Languages (Declarative)• Optimization Packages• Most model builders tend to know rather little about
application/GUI and database programming
Programming vs. Optimization Modeling
![Page 9: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/9.jpg)
9Copyright © 2012 Maximal Software, Inc. All rights reserved
DBDBProgramProgram
SolverSolver
ModLangModLang
Application
Optimization
Program Running Model
![Page 10: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/10.jpg)
10Copyright © 2012 Maximal Software, Inc. All rights reserved
DBDB
ProgramProgram
SolverSolver
ModLangModLang
OptiMaxOptiMax
Application
Optimization
Optimization Component Library
![Page 11: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/11.jpg)
11Copyright © 2012 Maximal Software, Inc. All rights reserved
OptiMax Component Library Seamless Integration of Technologies
• Modeling Language• Solver/Optimizers• Database/Data Sources• Programming Languages (COM/ActiveX)
• Visual Basic/VBA• C/C++• Java• Delphi• Web scripting
![Page 12: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/12.jpg)
12Copyright © 2012 Maximal Software, Inc. All rights reserved
Solvers Database
OptiMax
VB C++ Delphi Java
MPLWIN MPLX
MPL Modeling Language
MPL Library (C++)
MPL/OptiMax Design Overview
![Page 13: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/13.jpg)
13Copyright © 2012 Maximal Software, Inc. All rights reserved
Models (Model)
OptiMax
Solvers (Solver)
Matrix
Solution
SolverOptions (SolverOption)
Symbols (Symbol)
Variables (Variable)
Constraints (Constraint)
Variables (Variable)
Constraints (Constraint)
StatusWindow
MplOptions (MplOption)
OptiMax Component Library Object Model Hierarchy
![Page 14: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/14.jpg)
14Copyright © 2012 Maximal Software, Inc. All rights reserved
Models (Model)
OptiMax
DataConstants (DataConstant)
Symbols (Symbol)
DataVectors (DataVector)
Subscripts (Subscript)
VariableVectors (VariableVector)
Subscripts (Subscript)
ConstraintVectors (ConstraintVector)
Subscripts (Subscript)
Macros (Macro)
PlainVariables (Variable)
PlainConstraints (Constraint)
IndexSets (IndexSet)
OptiMax Component Library Object Model - Symbols
![Page 15: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/15.jpg)
15Copyright © 2012 Maximal Software, Inc. All rights reserved
OptiMax Component Library Application Building Features
• General Model Handling • Model Parsing• Model Querying• Modifying Models / Solving Models• Write Standard Output Files• Database Import/Export• Multiple Models in Memory• Multiple Model Runs / Iterations over models• Use Solution Values as Input• Callbacks/User Exits• Access to MPL Internal Indexing Data Structures
![Page 16: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/16.jpg)
16Copyright © 2012 Maximal Software, Inc. All rights reserved
MPL OptiMax for .Net
MPL Release 4.2n:
• Supports both CSharp and Visual Basic (2008/2010 - 32/64 bit)
• Multiple new objects, methods and properties
• Seamless integration with native .Net data structures
• Array
• ArrayList
• Generic List
• ICollection
• IEnumerable
• Iterators for all collection classes to support foreach loops
• ToString() implementation for all classes
• Exception handling with detailed error messages
• Extensive code samples showing the new OptiMax features
• Server version with full multi-threaded support
![Page 17: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/17.jpg)
17Copyright © 2012 Maximal Software, Inc. All rights reserved
MPL OptiMax for Python
MPL Release 4.2n:
• Supports Python 2.6, 2.7, 3.1, and 3.2 in both 32-bit and 64-bit mode• Distributed as compiled PYD files for increased speed• Multiple new objects, methods and properties• Seamless integration with native python data structures
• Lists, List of Lists• Tuples, Tuples of Tuples
• Iterators for all collection classes to support for loops• Implementation of magic methods for all classes
__init__(), __len__(), __iter__(), __getitem__(), __str__()• Dynamic object attributes for model identifiers• Exception handling with detailed error messages• Extensive code samples showing the new OptiMax features• Server version with full multi-threaded support
![Page 18: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/18.jpg)
18Copyright © 2012 Maximal Software, Inc. All rights reserved
MPL OptiMax for Matlab
MPL Release 4.2n:
• Supports Matlab in both 32-bit and 64-bit mode
• Distributed as compiled MEX files
• Utilizes the new C-API Mpllib42.dll library
• Based on the standard interface of Matlab Optimization Toolbox
• mpl_initlibrary()
• mpl_linprog()
• mpl_bintprog()
• mpl_mintprog()
• mpl_freelibrary()
• MPL can be kept in memory through the hmpl library handle
• Solver can be specified through the solverName argument
• Solver options can be set through the option list argument
![Page 19: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/19.jpg)
19Copyright © 2012 Maximal Software, Inc. All rights reserved
MPLLib C-API DLL Library
MPL Release 4.2n:
• Full C-Api library interface with over 600 functions
• Available in both 32-bit and 64-bit versions
• Utilized by MPL OptiMax Component Libraries
• MPL OptiMax for .Net
• MPL OptiMax for Python
• MPL OptiMax for Matlab
• Supports all functionality of MPL OptiMax from the C language
![Page 20: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/20.jpg)
20Copyright © 2012 Maximal Software, Inc. All rights reserved
Simple MPL OptiMax Example in Python
from mplpy import *
def RunModel(modelName):
modelFile = mpl.HomeDir + modelName + ".mpl"
print("MPL file: " + modelFile)
mod.ReadModel(modelFile)
print("Variables = " + str(mod.Matrix.Variables.Count))
print("Constraints = " + str(mod.Matrix.Constraints.Count))
print("")
print("Solving " + modelName + " with " + mpl.Solver.Name + " solver:")
mod.Solve(mpl.coinmp)
print("")
print(mod.Solution.ResultString)
print("ObjectValue = " + str(mod.Solution.ObjectValue))
for var in mod.Solution.Variables:
print(var.Name + " = " + str(var.Activity))
![Page 21: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/21.jpg)
21Copyright © 2012 Maximal Software, Inc. All rights reserved
Planning Example in CSharp
OptiMax mpl = new OptiMax();
mpl.UseExceptions = false;
Solver solver = mpl.Solvers.Add("CPLEX");
Model model = mpl.Models.Add("Planning");
string modelFile = mpl.HomeDirectory + modelName + ".mpl";
Console.WriteLine("Reading MPL file: " + modelFile);
ResultType result = model.ReadModel(modelFile);
if (result != ResultType.Success)
{
Console.WriteLine(model.Error.ToString());
}
Console.WriteLine("Solving " + modelName + " with " + solver.Name + " solver");
result = model.Solve(solver);
foreach (Variable var in model.Solution.Nonzeros)
{
Console.WriteLine(var.Name + " " + var.Activity.ToString());
}
Console.WriteLine(model.Solution.ResultString + " ObjectValue = " + model.Solution.ObjectValue);
Console.WriteLine("Nonzeros = " + model.Solution.Nonzeros.Count.ToString());
![Page 22: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/22.jpg)
22Copyright © 2012 Maximal Software, Inc. All rights reserved
Portfolio Example - Dynamic Attributes
from mplpy import mplfrom numpy import arange, float32from matplotlib import pyplot as plot
modelFilename = mpl.HomeDir + "Portfolio.mpl"result = mpl.model.SolveModel(modelFilename, mpl.cplex)
investVect = mpl.model.InvestinvestVect.ZeroTol = 0.001
count = investVect.Nonzeros.CountinvestAmount = arange(count, dtype=float32)stockNames = range(count)
for i, var in enumerate(investVect.Nonzeros): investAmount[i] = var.Activity stockNames[i] = investVect.stock.ValueStr print((stockNames[i] + ":").ljust(8) + ("%1.1f%%" % (investAmount[i] * 100.0)).rjust(6))
![Page 23: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/23.jpg)
23Copyright © 2012 Maximal Software, Inc. All rights reserved
EFrontier Example - Changing RHS
iterCount = 100deltaPercent = 0.02CPLEX_OPTIMAL = 1targetReturnData = model.TargetReturnvarianceMacro = model.Variance meetTargetCon = model.MeetTargetTargetArray = arange(iterCount, dtype=float32)VarianceArray = arange(iterCount, dtype=float32)
for i in range(iterCount): target = targetReturnData.Value * (1 + i * deltaPercent) meetTargetCon.RHSValue = target model.Solve(mpl.cplex) print(str(i) + ") " + str(target) + ", " + str(varianceMacro.Value)) if model.Solution.ResultCode == CPLEX_OPTIMAL: TargetArray[i] = target VarianceArray[i] = varianceMacro.Value else: solCount = i print(str(solCount + 1) + ") " + model.Solution.ResultString + " (" + str(model.Solution.ResultCode) + ")") break
![Page 24: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/24.jpg)
24Copyright © 2012 Maximal Software, Inc. All rights reserved
CutStock Example – Add() Statements
def LoadModel(self, model, cutNames, patternNames, priceSheet, sheetsAvail, cutDemand, cutsInPattern):
try: model.IndexSets.AddNameSet("cuts", cutNames) model.IndexSets.AddNameSet("patterns", patternNames) model.DataConstants.Add("PriceSheet", priceSheet) model.DataConstants.Add("SheetsAvail", sheetsAvail) model.DataVectors.AddDense("CutDemand[cuts]", cutDemand) model.DataVectors.Add("CutsInPattern[patterns, cuts]", cutsInPattern) model.PlainVariables.Add("SheetsCut", "-> T1") model.PlainVariables.Add("TotalCost", "-> TC") model.VariableVectors.Add("PatternCount[patterns]", "-> \"\"") model.VariableVectors.Add("ExcessCuts[cuts]", "-> X") model.Objectives.Add("TotalCost", ObjectSense.Minimize) model.PlainConstraints.Add("TotCost", "TotalCost = PriceSheet*SheetsCut") model.PlainConstraints.Add("RawAvail", "SheetsCut < SheetsAvail") model.PlainConstraints.Add("Sheets", "SheetsCut = SUM(patterns: PatternCount[patterns])") model.ConstraintVectors.Add("CutReq[cuts]", "SUM(patterns: CutsInPattern[patterns, cuts] * PatternCount[patterns]) CutDemand[cuts] + ExcessCuts[cuts]") except Exception as ex: print(str(ex)) result = model.LastResult return result
![Page 25: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/25.jpg)
25Copyright © 2012 Maximal Software, Inc. All rights reserved
Add Statements - IndexSet / DataVectors
Adding IndexSet:
cutNames = ['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8']
model.IndexSets.AddNameSet("cuts", cutNames)
Adding DataVectors:
cutDemand = [500, 400, 300, 450, 350, 200, 800, 200]
model.DataVectors.AddDense("CutDemand[cuts]", cutDemand)
cutsInPattern = ['p1', 'w1', 1, 'p1', 'w8', 1, 'p2', 'w2', 1, 'p2', 'w7', 1, . . . 'p28', 'w8', 5, 'p29', 'w8', 7]
model.DataVectors.Add("CutsInPattern[patterns, cuts]", cutsInPattern)
![Page 26: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/26.jpg)
26Copyright © 2012 Maximal Software, Inc. All rights reserved
Exceptions with Detailed Syntax Error Messages
Syntax Error:
model.PlainConstraints.Add("TotCost", "TotalCost = PriceSheet */ SheetsCut")
Exception Output:
The PlainConstraints.Add('TotCost', expr='TotalCost = PriceSheet */ SheetsCut') method
returned 'SyntaxError' (2) with the following error message:
**** A minor mistake was found in line 1:
6. I expected to see either a number or a variable, but found instead '/'.
File: C:\MplWin4\OptiMax\Python\CutStock Line: (line=1, col=43)
SUBJECT TO TotCost: TotalCost = PriceSheet */ SheetsCut; ^
![Page 27: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/27.jpg)
27Copyright © 2012 Maximal Software, Inc. All rights reserved
Exceptions with Wrong Data Types
Syntax Error:
cutNames = ['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8']
model.DataConstants.Add("SheetsAvail", cutNames)
Exception Output:
The argument 'dataValue' for the 'DataConstants.Add('SheetsAvail', dataValue, dataAttr)' method
cannot be assigned the value '['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8']' of type 'list'.
Quick fix: Please enter either a numeric or string value.
![Page 28: Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012](https://reader036.vdocument.in/reader036/viewer/2022062418/554f4791b4c905b9508b4636/html5/thumbnails/28.jpg)
28Copyright © 2012 Maximal Software, Inc. All rights reserved
Applications with MPL OptiMax
• Model written in MPL• Data transferred to/from MPL through
• Excel Ranges
• Database Connection
• Text files
• End-User Application Building• User-Interface done in VB/CSharp/C-API/C++/Java/Phyton
• Process Input from User
• Control Solver Runs
• Process Output from Model
• Display graphs
• Model solved with any supported solver
No limits on size, speed, or robustness