Seminar: Data Modeling for Optimization with MPL - Oct 2012

579 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
579
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Seminar: Data Modeling for Optimization with MPL - Oct 2012

  1. 1. Data Modeling for Optimisation The Database Connection in MPLThe database connection in MPL has the ability to access data from manydifferent sources, such as: • Relational Databases • Excel Spreadsheets • External Text Files • InternetThis gives the model developer the flexibility to choose the most efficientand convenient way to incorporate the data into the model. Among thedata formats that are supported by MPL are: • Microsoft Access and Excel • ODBC • FoxPro/DBase • SQL Server • Oracle • SAP Copyright © 2012 Maximal Software, Inc. All rights reserved 1
  2. 2. Data Modeling for Optimisation Import Indexes from DatabaseMPL allows you to import the elements for an index directly from adatabase. In the INDEX section, where you define the index, enter thekeyword DATABASE after the assignment symbol (:=) followed byparentheses containing the table name and the column/field name youwant to import from. INDEX depot := DATABASE("Depots","DepotID");In the above example, MPL will open the database table Depots, locatethe column DepotID, and then read in the entries for the index depot. Copyright © 2012 Maximal Software, Inc. All rights reserved 2
  3. 3. Data Modeling for Optimisation Import Indexes from DatabaseIn most cases the imported indexes are the key fields for the table whichare underlined in the following examples: The Depots Table DepotID Capacity Atlanta 400000 Chicago 50000 New York 70000 Dallas 100000The column name defaults to the name of the index so if it is the same youdo not have to specify it. In the example below the column name in thedatabase table is DepotID which is the same as the index DepotID. INDEX DepotID := DATABASE("Depots"); Copyright © 2012 Maximal Software, Inc. All rights reserved 3
  4. 4. Data Modeling for Optimisation Import Indexes from Database (Cont.)MPL can import from more than one database in the same run. In theexample below, MPL will read in the factory index from Access, insteadof the default database: INDEX factory := DATABASE(Access, "Factory", "FactID"); Copyright © 2012 Maximal Software, Inc. All rights reserved 4
  5. 5. Data Modeling for Optimisation Import Indexes from Database (Cont.)MPL also allows you to import subset indexes from database tables. INDEX FactoryDepot[factory,depot] := DATABASE("FactDep");This statement will open the database table FactDep and locate thecolumns for factory and depot and then read in the entries forFactoryDepot. FactID DepotID TrCost Shipment The FactDep Table: Houston Chicago 3200 0 Houston Dallas 5100 0 Seattle Atlanta 2800 0 Seattle Chicago 6800 0 Seattle New York 4700 0 Seattle Dallas 5400 0Notice that MPL automatically uses the same name as default for thecolumns FactID and DepotID, as in the original tables the indexes weredefined from. Copyright © 2012 Maximal Software, Inc. All rights reserved 5
  6. 6. Data Modeling for Optimisation Import Indexes from Database (Cont.)If an index column does have a different name than in the original table, youcan specify it following the table name by first entering the index name followedby an equal sign and the column name. INDEX FactoryDepot[factory,depot] := DATABASE("FactDep", factory="Factory", depot="Depot");This means if you are consistent in naming the columns in different tables youdo not have to specify them each time you refer to them in MPL. Copyright © 2012 Maximal Software, Inc. All rights reserved 6
  7. 7. Data Modeling for Optimisation Import Data Vectors from DatabaseMPL allows you import the elements for a data vector directly from a database.In the DATA section, where you define the data vector, enter the keywordDATABASE after the assignment symbol (:=), followed by parenthesescontaining the table name and the column/field name you want to import from. DATA FactDepCost[factory,depot] := DATABASE("FactDep", "TrCost");In the above example, MPL will open the database table FactDep, locate thecolumns TRCost, FactID, and DepotID, and then read in the entries for the datavector FactDepCost. The FactDep Table: FactID DepotID TrCost Shipment Houston Chicago 3200 0 Houston Dallas 5100 0 Seattle Atlanta 2800 0 Seattle Chicago 6800 0 Seattle New York 4700 0 Seattle Dallas 5400 0 Copyright © 2012 Maximal Software, Inc. All rights reserved 7
  8. 8. Data Modeling for Optimisation Import Data Vectors from Database (Cont.)If an index column does have a different name than in the original table,you can specify it following the table name by first entering the indexname, followed by an equal sign and the column name. DATA FactDepCost[factory,depot] := DATABASE("FactDep","TrCost",factory="Factory",depot="Depot");The column name defaults to the name of the data vector, so if it is thesame you do not have to specify it. DATA TrCost := DATABASE("FactDep"); Copyright © 2012 Maximal Software, Inc. All rights reserved 8
  9. 9. Data Modeling for Optimisation Import Data Vectors from Database (Cont.)MPL can import from more than one database in the same run. Thedefault database is specified in the Database Options dialog box inthe Options menu. If you need to import a data vector from table in adatabase other than the default, you can do so by specifying thedatabase name before the name of the table.In the example below, MPL will read in the data vector DepotCustCostfrom Access instead of the default database. DATA DepotCustCost := DATABASE(Access, "DepCust", "TrCost"); Copyright © 2012 Maximal Software, Inc. All rights reserved 9
  10. 10. Data Modeling for Optimisation Export Variable Values to DatabaseAfter optimizing the problem, MPL can export the variable values to thedatabase where it can be used to report the solution back to the user. Inthe DECISION VARIABLES section, where you define the variable vector,enter the keyword EXPORT TO after the defined variable, followed by thekeyword DATABASE and parentheses containing the table name and thecolumn/field name you want to export to. DECISION VARIABLES FactDepShip[factory,depot] EXPORT TO DATABASE("FactDep", "Shipment");In the above example, MPL will open the database table FactDep, locatethe columns Shipment, FactID, and DepotID, and then export the solutionvalues for the variable vector FactDepShip. Copyright © 2012 Maximal Software, Inc. All rights reserved 10
  11. 11. Data Modeling for Optimisation Export Variable Values to Database (Cont.)Here is an example of the FactDep table after the solution values havebeen exported. The FactDep Table: FactID DepotID TrCost Shipment Houston Chicago 3200 210 Houston Dallas 5100 0 Seattle Atlanta 2800 455 Seattle Chicago 6800 0 Seattle New York 4700 328 Seattle Dallas 5400 189Notice that MPL automatically uses the same name as the default for theindex columns FactID and DepotID, as in the original tables the indexeswere defined from. Copyright © 2012 Maximal Software, Inc. All rights reserved 11
  12. 12. Data Modeling for Optimisation Export Variable Values to Database (Cont.)MPL also allows you to export variable values other than the activity. Youcan export the reduced costs, the upper and lower ranges for the objectivefunction, as well as the objective function coefficient values. You can changewhich values will be exported by entering one of the following keywords directlyafter the keyword EXPORT: Activity, ReducedCost, ObjectCoeff, ObjectLower,ObjectUpper.For example, if you want to export the reduced cost for a variable enter thefollowing: DECISION VARIABLES FactDepShip[factory,depot] EXPORT ReducedCost TO DATABASE("FactDep", "ReducedCost");If you need to export more than one value for a variable vector you can doso by entering multiple export statements after the variable definition. Copyright © 2012 Maximal Software, Inc. All rights reserved 12
  13. 13. Data Modeling for Optimisation Export Variable Values to Database (Cont.)MPL offers three different options on how the database table is updatedby the EXPORT statement. • UPDATE, which is the default, searches through the database and for each record locates the corresponding value in the solver solution and updates the database entry with it. This option minimizes the changes done to the database table since only the existing values are updated, but can sometimes be slow especially on SQL type databases. • REFILL keyword right after the EXPORT keyword is used to specify that the whole database table should be emptied and then refilled with the entries from the solver solution. Since this takes out the necessity to search the table this can often lead to faster export times for larger tables. • CREATE keyword right after the EXPORT keyword is used to specify that the database table should be created and then filled with the entries from the solver solution. This option is mainly useful when exporting to the database table for the first time. Copyright © 2012 Maximal Software, Inc. All rights reserved 13
  14. 14. Data Modeling for Optimisation Export Constraint Values to DatabaseJust as exporting variable values, MPL can also export the constraint valuesto the database. You will need to define constraint vectors in theCONSTRAINTS section with the keyword EXPORT TO followed by thekeyword DATABASE and parentheses containing the table name and thecolumn/field name you want to export to. SUBJECT TO FactoryCapacity[factory] EXPORT ShadowPrice TO DATABASE("Factory", "ShadowPrice");In the above example, MPL will open the database table Factory, locate thecolumns FactID and ShadowPrice, and then export in the shadow pricevalues for the constraint FactoryCapacity. Copyright © 2012 Maximal Software, Inc. All rights reserved 14
  15. 15. Data Modeling for Optimisation Export Constraint Values to Database (Cont.)Here is an example of the Factory table after the shadow price values havebeen exported. FactID Capacity ShadowPrice 120.938 Houston 320000 4 Seattle 73000 0.0000Notice that MPL automatically uses the same name as the default for theindex column FactID, as in the original tables the factory index was definedfrom. If an index column does have a different name than in the originaltable you can specify it by first entering the index name followed by anequal sign and the column name. SUBJECT TO FactoryCapacity[factory] EXPORT ShadowPrice TO DATABASE("Factory", "ShadowPrice”, factory="Factory”);This means, if you are consistent in naming the columns in different tables,you do not have to specify them each time you refer to them in MPL. Copyright © 2012 Maximal Software, Inc. All rights reserved 15
  16. 16. Data Modeling for Optimisation Export Constraint Values to Database (Cont.)In the example below, MPL will export the shadow price of the constraintvector FactoryCapacity from FoxPro instead of the default database. SUBJECT TO FactoryCapacity[factory] EXPORT ShadowPrice TO DATABASE(FoxPro,"Factory","ShadowPrice");MPL also allows you to export constraint values other than the shadowprice. You can export the slack values, the upper and lower ranges for theright-hand-side as well as the right-hand-side values. You change whichvalues will be exported by entering one of the following keywords directlyafter the keyword EXPORT: Activity, Slack, ShadowPrice, RhsValue,RhsLower, RhsUpper. For example, if you want to export the slack for aconstraint enter the following: SUBJECT TO FactoryCapacity[factory] EXPORT Slack TO DATABASE("Factory","Slack");If you need to export more than one value for a variable vector you can doso by entering multiple export statements after the variable definition. Copyright © 2012 Maximal Software, Inc. All rights reserved 16
  17. 17. Data Modeling for Optimisation SpreadSheet Modeling in MPL• Spreadsheet optimisation allows users to create models that are easy to use, enabling the user to quickly update the data and solve the model.• Spreadsheets are efficient at handling and managing 2-Dimensional dense data and single scalar values. Copyright © 2012 Maximal Software, Inc. All rights reserved 17
  18. 18. Data Modeling for Optimisation Import Indexes from ExcelMPL allows you to import the elements for an index directly from anEXCELRANGE. In the INDEX section, where you define the index, enterthe keyword EXCELRANGE after the assignment symbol (:=) followed byparentheses containing the table name and the column/field name youwant to import from. INDEX depot := EXCELRANGE("Depots");In the above example, MPL will read in the entries for the index depot fromthe Excel range called Depots. Copyright © 2012 Maximal Software, Inc. All rights reserved 18
  19. 19. Data Modeling for Optimisation Import Data Vectors from ExcelMPL allows you import the elements for a data vector directly from EXCEL.One can read in data in tabular format using the keyword EXCELRANGE orin sparse format using the keyword EXCELSPARSE, below is an example ofhow to import a data vector from EXCEL. DATA FactDepCost[factory,depot] := EXCELSPARSE("FactDep", "3");In the above example, MPL will read in the data vector FactDepCost from therange called FactDep, as we have specified the data is in sparse format it willtake the first column to be entries for the Factory index, the second column tobe entries for the Depot index and 3 specifies the value of the data parameterto be taken from the third column. The FactDep Table: FactID DepotID TrCost Shipment Houston Chicago 3200 0 Houston Dallas 5100 0 Seattle Atlanta 2800 0 Seattle New York 4700 0 Seattle Dallas 5400 0 Copyright © 2012 Maximal Software, Inc. All rights reserved 19
  20. 20. Data Modeling for Optimisation Export Variable Values to ExcelAfter optimizing the problem, MPL can export the variable values to theEXCEL where it can be used to report the solution back to the user. In theDECISION VARIABLES section, where you define the variable vector,enter the keyword EXPORT TO after the defined variable, followed by thekeyword EXCELRANGE/ EXCELSPARSE and parentheses containing therange name and the column number if you are using EXCELSPARSE. DECISION VARIABLES FactDepShip[factory,depot] EXPORT TO EXCELRANGE("Shipping");In the above example, MPL will export the solution values for the variablevector FactDepShip to the range called Shipping. Copyright © 2012 Maximal Software, Inc. All rights reserved 20
  21. 21. Data Modeling for Optimisation Sparse Data vs. Dense Data• Dense Data • Standard representation in row and column format • Row and column format preferably when data is highly dense, containing none or low percentage of null or zero values • Can be difficult to deal with for large data sets.• Sparse Data • Includes only non zero values • Highly efficient representation if small percentage of the values are nonzero • Efficient. Generating and Solving models Copyright © 2012 Maximal Software, Inc. All rights reserved 21
  22. 22. Data Modelling for Optimisation Sparse and Dense Data ExamplesDense DataIn the following example shows the datavector Shipcost. The parameter isindexed over 4 plants, 2 warehouses and 3 products. ShipCost[plant, warehouse, product] := [ ! p1 p2 p3 {pl1} 70, 52, 48, !w1 65, 40, 74, !w2 {pl2} 79, 52, 65, !w1 75, 50, 53, !w2 {pl3} 47, 52, 51, !w1 78, 52, 52, !w2 {pl4} 75, 80, 50, !w1 90, 52, 10]; !w2 Copyright © 2012 Maximal Software, Inc. All rights reserved 22
  23. 23. Data Modelling for Optimisation Sparse and Dense Data ExamplesSparse DataThe first entry of each row specifies the first index. In our example thismeans plant, the second and third entries refers to the machine andproduct indexes and the fourth to the actual data value for ProdCost. ProdCost[plant, machine, product] := [ p1, m11, A1, 73.30, p1, m11, A2, 52.90, p1, m12, A3, 65.40, p1, m13, A3, 47.60, p2, m21, A1, 79.00, p2, m21, A3, 66.80, p2, m22, A2, 52.00, p3, m31, A1, 75.80, p3, m31, A3, 50.90, p3, m32, A1, 79.90, p3, m32, A2, 52.10, p4, m41, A1, 82.70, p4, m41, A2, 63.30, p4, m41, A3, 53.80]; Copyright © 2012 Maximal Software, Inc. All rights reserved 23
  24. 24. Data Modelling for Optimisation Text vs. Database vs. SpreadsheetData can be read in at different speeds depending on the data source. • Text Files - Are compact and simple and by far the quickest way to read in data. • Databases - Are built for data management pretty quick to import and export data. • Spreadsheets - Not designed to handle or manage large data sets, inefficient in importing data and is the slowest method to read in and export data. Copyright © 2012 Maximal Software, Inc. All rights reserved 24

×