Main Content

Create Python Package to Optimize Cost Equation

This example shows how to use the MATLAB® function fminsearch to solve an optimization problem. You then use MATLAB Compiler SDK™ to create a Python® package and integrate it into a Python application that calls the function.

Create MATLAB Function

In this inventory management example, your company needs to decide on the optimal order quantity to minimize costs, and there are no specific constraints on the order quantity.

Suppose your company's cost function, which represents the total cost associated with managing an inventory, is defined as follows:

C(Q)=D/Q*S+Q/2*H+D*P

  • D is the annual demand

  • Q is the order quantity

  • S is the order cost per order

  • H is the holding cost per unit per year

  • P is the purchase cost per unit

Create a function named fms.m that takes an initial guess for order quantity, annual demand, cost per order, holding cost per unit per year, and purchase cost per unit and outputs the optimal order quantity and cost. You can use the MATLAB function fminsearch to find the order quantity Q that minimizes the total cost C(Q).

function [Q_optimal, cost_optimal] = fms(Q_initial_guess, D, S, H, P)

cost_function = @(Q) D/Q * S + Q/2 * H + D * P;

[Q_optimal, cost_optimal] = fminsearch(cost_function, Q_initial_guess);

end

Generate Python Package

Build a Python package using the compiler.build.pythonPackage function.

buildResults = compiler.build.pythonPackage('fms.m','PackageName','fminsearch');

The compiler.build.Results object buildResults contains information on the build type, generated files, included support packages, and build options.

The function generates the Python files within a folder named fminsearchpythonPackage in your current working directory.

As an alternative, if you want to create a Python package using a graphical interface, see Create Python Application Using Library Compiler App.

Run MATLAB Function in Python

After creating your Python package, you can call it from a Python application. A simple application that calls the function using the specified inputs is provided below.

#!/usr/bin/env python
"""
Sample script that uses the fminsearch package created using
MATLAB Compiler SDK.
Refer to the MATLAB Compiler SDK documentation for more information.
"""

import fminsearch
# Import the matlab module only after you have imported 
# MATLAB Compiler SDK generated Python modules.
import matlab
try:
    my_fminsearch = fminsearch.initialize()
except Exception as e:
    print('Error initializing fminsearch package\\n:{}'.format(e))
    exit(1)
try:
    initial_guess = matlab.double([100], size=(1, 1))
    D = matlab.double([10000], size=(1, 1)); # Annual demand
    S = matlab.double([50], size=(1, 1)); # Order cost per order
    H = matlab.double([0.5], size=(1, 1)); # Holding cost per unit per year
    P = matlab.double([10], size=(1, 1)); # Purchase cost per unit
    Out1, Out2 = my_fminsearch.fms(initial_guess, D, S, H, P, nargout = 2)
    print("Order Quantity:", Out1, "\n", "Order Cost:", Out2)
except Exception as e:
    print('Error occurred during program execution\\n:{}'.format(e))
my_fminsearch.terminate()

This code generates the following output in Python.

Order Quantity: 1414.2135620117188 
Order Cost: 100707.10678118655

See Also

|

Related Topics