Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012

630 views
511 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
630
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Seminar: Embedding Optimization in Applications with MPL OptiMax - April 2012

  1. 1. Optimization Seminar - OptiMaxEmbedding Optimization in Applications with MPL OptiMax Presented by Bjarni Kristjansson Maximal Software, Inc. Copyright © 2012 Maximal Software, Inc. All rights reserved 1
  2. 2. 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 2
  3. 3. Building Optimization Applications Development/Deployment IssuesPhase I - Development • Model Development Environment • Formulating the Model • Connecting to External Data • Solver Testing and TuningPhase II - Deployment • End-User Applications • Run-times / Component Libraries • Embedding Optimization into Application • Packaging/Distribution Copyright © 2012 Maximal Software, Inc. All rights reserved 3
  4. 4. Callable Libraries for Solvers Program Program Solver Solver Copyright © 2012 Maximal Software, Inc. All rights reserved 4
  5. 5. Algebraic Modeling LanguagesModLangModLang Program ProgramSolver Solver Solver Solver Copyright © 2012 Maximal Software, Inc. All rights reserved 5
  6. 6. Connecting to Databases ModLang ModLang Program ProgramSolver Solver DB Solver Solver DB DB DB Copyright © 2012 Maximal Software, Inc. All rights reserved 6
  7. 7. Merging Program with Model Program Program ModLang ModLang Solver Solver DB DB Copyright © 2012 Maximal Software, Inc. All rights reserved 7
  8. 8. Programming vs. Optimization ModelingApplication Programming• Programming Languages (Procedural)• Application/User-Interface• Database Programming• Most programmers tend to know rather little about optimization and formulating modelsOptimization Modeling• Modeling Languages (Declarative)• Optimization Packages• Most model builders tend to know rather little about application/GUI and database programming Copyright © 2012 Maximal Software, Inc. All rights reserved 8
  9. 9. Program Running Model Program Program DB DBApplicationOptimization ModLang ModLang Solver Solver Copyright © 2012 Maximal Software, Inc. All rights reserved 9
  10. 10. Optimization Component Library Program Program OptiMax OptiMax DB DBApplicationOptimization ModLang ModLang Solver Solver Copyright © 2012 Maximal Software, Inc. All rights reserved 10
  11. 11. 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 11
  12. 12. MPL/OptiMax Design Overview VB C++ Delphi JavaMPLWIN MPLX OptiMax MPL Library (C++) MPL Modeling Language Solvers Database Copyright © 2012 Maximal Software, Inc. All rights reserved 12
  13. 13. OptiMax Component Library Object Model HierarchyOptiMax Solvers (Solver) SolverOptions (SolverOption) MplOptions (MplOption) StatusWindow Models (Model) Matrix Variables (Variable) Constraints (Constraint) Solution Variables (Variable) Constraints (Constraint) Symbols (Symbol) Copyright © 2012 Maximal Software, Inc. All rights reserved 13
  14. 14. OptiMax Component Library Object Model - SymbolsOptiMax Models (Model) Symbols (Symbol) IndexSets (IndexSet) DataConstants (DataConstant) DataVectors (DataVector) Subscripts (Subscript) Macros (Macro) VariableVectors (VariableVector) Subscripts (Subscript) ConstraintVectors (ConstraintVector) Subscripts (Subscript) PlainVariables (Variable) PlainConstraints (Constraint) Copyright © 2012 Maximal Software, Inc. All rights reserved 14
  15. 15. 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 15
  16. 16. MPL OptiMax for .NetMPL 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 16
  17. 17. MPL OptiMax for PythonMPL 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 17
  18. 18. MPL OptiMax for MatlabMPL 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 18
  19. 19. MPLLib C-API DLL LibraryMPL 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 19
  20. 20. Simple MPL OptiMax Example in Pythonfrom 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)) Copyright © 2012 Maximal Software, Inc. All rights reserved 20
  21. 21. Planning Example in CSharpOptiMax 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()); Copyright © 2012 Maximal Software, Inc. All rights reserved 21
  22. 22. Portfolio Example - Dynamic Attributesfrom mplpy import mplfrom numpy import arange, float32from matplotlib import pyplot as plotmodelFilename = mpl.HomeDir + "Portfolio.mpl"result = mpl.model.SolveModel(modelFilename, mpl.cplex)investVect = mpl.model.InvestinvestVect.ZeroTol = 0.001count = 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)) Copyright © 2012 Maximal Software, Inc. All rights reserved 22
  23. 23. EFrontier Example - Changing RHSiterCount = 100deltaPercent = 0.02CPLEX_OPTIMAL = 1targetReturnData = model.TargetReturnvarianceMacro = model.VariancemeetTargetCon = 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 23
  24. 24. CutStock Example – Add() Statementsdef 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 24
  25. 25. Add Statements - IndexSet / DataVectorsAdding 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) Copyright © 2012 Maximal Software, Inc. All rights reserved 25
  26. 26. Exceptions with Detailed Syntax Error MessagesSyntax 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:MplWin4OptiMaxPythonCutStock Line: (line=1, col=43) SUBJECT TO TotCost: TotalCost = PriceSheet */ SheetsCut; ^ Copyright © 2012 Maximal Software, Inc. All rights reserved 26
  27. 27. Exceptions with Wrong Data TypesSyntax 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. Copyright © 2012 Maximal Software, Inc. All rights reserved 27
  28. 28. 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 Copyright © 2012 Maximal Software, Inc. All rights reserved 28

×