Do you wonder if Mathematical Optimization software like MIP Codes from FICO-Xpress or Gurobi can be easily deployed on SAP BTP Runtimes. This is a 2-part blog which will walk through quick and easy steps to deploy on 1. SAP’s AI Core via the AI Launchpad 2. SAP Kyma (this blog post).
Steps to create the docker are the same in the blog post on deploying to SAP AI Core.
Now which deployment option is best suited depends very much on the use-case and how the optimization results results need to be integrated in the business process and the overall technology landscape. I will provide some guidelines at the end of the blog but the choice of deployment option would need a wider consideration depending on the use-case you have in mind.
Background
Mathematical Optimization techniques are widely needed to solve a wide range of problems in decision making and finding optimal solution(s) when many exist.Besides the many real world complex problems that depend on Mathematical Optimization, these techniques are also increasingly being used to enhance Machine Learning solutions to provide explainability and interpretability as shown by for example
- Interpretable AI and develop-more-accurate-machine-learning-models-with-mip
- Use-cases on finding counterfactuals to provide options which could make ML Classifier classify differently Counterfactual Analysis for Functional Data
These techniques are useful when for example it is not sufficient to classify credit scores or health scores into good or bad but also provide what can be changed to make the classification go from bad to good.
SAP Products like SAP Integrated Business Planning incorporate optimization in the supply chain solutions. For other problems where you need to extend the solutions and embed ML or Optimization, this blog post walks through the steps of easily installing third-party libraries on BTP so these solutions can be integrated with the enterprise solutions which needs these capabilities.
Prerequisites
To go through the steps described in the blog make sure you already have the following
- Docker Desktop for building docker images
- Setup BTP Account with SAP Kyma runtime as described here Create Kyma Environment on BTP. If you are new to Kyma you can start here SAP Discovery Mission
SAP Kyma provides many equivalent capabilities via SAP Kyma Cockpit or programmatically via kubectl (as described here).In this blog post I will primarily use SAP Kyma Cockpit as it is easier to get started with minimal coding effort.
The subsequent step is to include the development artifacts into an existing project structure, for which the kubectl way might be better suited. Since this step is highly project dependent it is not covered in this blog post.
Create the Docker Image
Follow the steps to create the docker image as described in blog post on deploying to SAP AI Core.
For example I had previously pushed the docker image to docker.io. I can test the image locally by pulling it from it. I will use the same image to now deploy the optimization example on SAP Kyma which is at sapgcoe/sap-optiml-example1.0.
The docker image has 4 files and the rest are installed via pip, please refer to the blog post on deploying to SAP AI Core for the code snippets for :
- Dockerfile
- requirements.txt
- optimizer.py (Sample code from 3 Optimization Libraries: FICO-Xpress,Gurobi ,Pyomo)
- app.py (Sample Flask server code to call above sample code in optimizer.py)
Create Deployment on Kyma
Here we would like to use our own docker image, hence we will follow through the steps of creating a full deployment and not a Serverless Function in Kyma.
On the Kyma console go to Deployments and press Create Deployment. We will start with Simple from the Create Deployment pop-up
Now click on YAML in the above pop-up and add the commands to specify what should run in the docker (it is the same command we gave in the workflow deployment for SAP AI Core Deployment)
Code snippet to add after image in the yaml file as shown above
command:
- /bin/sh
- '-c'
args:
- > #Specify using gunicorn runtime and calling app flask app from app.py file
set -e && echo "Starting" && gunicorn --chdir /app app:app -b 0.0.0.0:9001
ports:
- containerPort: 9001
protocol: TCP
If the Docker image is public you would not need to specify a docker secret. If it Private you would need to add Docker Secret with kubectl create secret docker-registry. You can either patch to default to above secret when pulling from docker repo or specify the secret in the yaml file above with :
imagePullSecrets:
-name:<your docker repo secret>
Once you click create you will see the deployment running as below
Create Service on Kyma
Once we have a deployment running as above we will now create a service which is then subsequently exposed via API Rule to be used by client code. In the Kyma Cockpit go to Discovery and Network -> Services and click on Create Service. Then edit the pop-up yaml to specify the app, which is the name of the deployment created in Step above, in our example sap-optiml-example. You need to specify the protocol to TCP. You can keep the default port as shown in the Service or specify your own for example 9001
When you click Create you will see a Service like this
Create API Rule on Kyma to expose the Optimization Functionality
Now that we have a Service we are at the last step of deployment which is to create an API Rule. This creates the url that client code can call to run the Optimization. You can Create API Rule from above Service screen or go to Discovery and Network -> API Rules and Click on Create API Rule
For the sub-domain specify a name to make this API uniquon your cluster. Currently our app.py code in the Docker image exposes the functions via GET only. You can specify POST if your functions would need POST.
Test the Deployment
One the API rule is created you can test the API as usual for example via Postman call or since we have only implemented GET and not added authentication mechanism, you can open the url created via the API in the browser and test the endpoints:
- /v1/status : This should return “Container is up & running’ as specified in the app.py
- /v1/callXpress?amount = 42
{"dime":1,"nickel":1,"penny":2,"quarter":1}
More details will be available in the logs on Kyma Cockpit for pods in thisdeployment
- Now if we test /v1/callOptimizer API endpoint it will return an error as the environment variable for default optimizer is not yet defined. This is needed in the app.py.To define the environment variable go to the deployment yaml and Edit it to add the env variable as follows: (here we are Xpress as default, or you can do ‘Gurobi’ or ‘Pyomo’)
You can similarly test the other 2 endpoints /v1/callGurobi or /call/Pyomo. You will get the same answer and the logs will give details of the corresponding execution
When to use SAP Kyma for Optimization Deployment
When to use SAP Kyma for Optimization loads depends heavily on your use-case. Some guidelines on when this can make the overall integration easier :
- If you have to embed optimization loads as part of Events in SAP Applications then you could use the event subscription functionality in SAP Kyma and trigger the workload
- You need to scale Optimization loads dynamically, for example you have periods of time when many optimization runs need to execute
- You need to specify the compute resources for the Optimization jobs
- You have other workloads on Kyma and would like to manage intelligence enabled apps from a central Cockpit
Conclusion
This blog post and the one on deploying on SAP AI Core show how easily one can deploy Optimization loads on SAP BTP Runtimes. The dockerized approach facilitates the testing of optimization functionality specific to your use-case and enabling the deployment with ease into a productive landscape.
Happy Optimizing development for your OptiML enabled applications on SAP BTP!