8.2 Defining Macros

Macros are an important feature of the MPL modeling language. They can be used in several ways that can be helpful in maintaining your model, such as defining a specific part of the model into a macro that will enable you to use it repetitively, thus making the model easier to maintain. Macros can also be used to give complex expressions a distinctive name and then refer to these complex expressions throughout the model by the macro. Finally, you can use them to eliminate unnecessary constraints from the model and thereby reduce the size of the problem to be solved by the LP package.

You define each macro item with a name on the left side and a formula or an ex-pression on the right side. Whenever the macro name is encountered in the model, the formula is used instead. The formula cannot be indexed. However, you can use summations and fixed vector items in the formula. A semicolon is required after each macro definition.


    MACROS
        TotalRevenue  := SUM(product,month: price * Sales) ;
        TotalCost     := SUM(product,month: InventoryCost * Inventory
                                     + ProductionCost * Production);

    MODEL
        MAX  Profit  =  TotalRevenue - TotalCost ;

In this example the macros are used to give the above summations a descriptive name. The macro named Revenues is defined as the sum of the revenues of all three prod-ucts over the whole year. The TotalCost macro is the total cost of the inventory storage plus the total cost of the production. When these macros are encountered in the model, their respective summations they represent will be placed into the model. See Chapter 9.9 Using Macros for more information on how to refer to macros in the model

After the model has been solved, MPL will create a solution file that contains the solution values of the defined macros in the model. For example:


   MACROS

      Macro Name                           Values
    -----------------------------------------------
      TotalRevenue                   1298181.8182
      TotalCost                       753615.1818
    -----------------------------------------------


Back To Top | Maximal Home Page | Table of Contents | Previous Page | Next Page