Skip to Content
Author's profile photo Tobias Trapp

Advanced Business Rules with HANA and R – Linear Optimization

In the last blog entry I introduced business rules using prediction algorithms: https://blogs.sap.com/2016/10/09/analytical-business-rules-hana-r-forecasting-time-series/. Therefore I used R which can be called from HANA using SQLscript which can be called from BRFplus rules. In this blog entry I will present another use case where R is used to solve an optimization problem.

The focus of this blog is on R. you can easily call the R script from an SQLscript procedure that can be called using AMDP or an Database Procedure Proxy. Both are object accessible from BRFplus.
Using R for Workload Management

Five years ago I wrote the following blog about using a solver for linear programs to assign work items to responsible persons: https://blogs.sap.com/2011/10/04/operations-research-abap/. Please let me cite parts of the blog which describe the task to solve:

“Consider the case that we have a huge number of workflows of three different types but only a limited number of people who can work on them. Each workflow of a certain type needs a certain amount of resources/time to get completed and has a certain gain. So the task is to calculate a certain number of workflows so that that they completed according to the resources such that the total benefit is maximized.”

Assigning work items to responsible persons is a classical task for BRFplus. Often the workitem corresponds to a business object whose attributes can be used in decision table. Those kinds of static assigments are one strategy to perform workload management, but there are others like “round robin” or assignment due to effort needed to perform this task.

I recommend to read the first paragraphs of above mentioned blog which introduce the problem:

  • We have a set of tasks which can be performed by a specific person.
  • Each tasks needs a certain time to be performed and will bring a certain benefit (in terms of money for example).
  • The specific person has only a limited amount of time to perform the work, perhaps we works only 50% of his or her time in a special organization unit and has responsibilities in other organizational units.
  • So we have to find a set of tasks for the person that he or she can perform everything in time and the benefit is maximized.

You can easily see the correspondence to a Knapsack problem (see http://en.wikipedia.org/wiki/Knapsack_problem). The tasks to be done correspond to items that have to be stored in a knapsack. The knapsack has a certain capacity which must not be exceeded. Each item has a certain benefit and the overall benefit should be maximized.
In above mentioned blog entry I gave an example for this task and came to following system of linear inequalities for which we are looking for a maximum integer solution according to this linear function 15 x1 + 10 x2 + 40 x3 such that following linear inequalities hold:

  • x1 <= 100, x2 <= 50 and x3 <= 40 and
  • 10 x1 + 50 x2 + 80 x3 <= 2000.

This is in general a difficult problem and I suggested to find a fractional solution and to round it. I showed how to solve linear programs in ABAP but in R it is much simpler:

library(lpsolve)
f.obj <- c(15, 10, 40)
f.con <- matrix (c(10, 50, 80, 1, 0, 0, 0, 1, 0, 0, 0, 1), nrow=4, byrow=TRUE)
f.dir <- c("<=", "<=", "<=", "<=")
f.rhs <- c(2000, 100, 50, 40)
s <- lp ("max", f.obj, f.con, f.dir, f.rhs)

With s and s$solution we can get the result: An optimal solution is (100.0, 0.0, 12.5) and the value of the solution is 2000. Please observe that you call also find an integer solution using s <- lp ("max", obj, con, dir, rhs, all.int=TRUE). In our case the integer solution is (100.0, 0.0, 12) which is here the fractional solution rounded down.
Compared to the ABAP code in my blog von 2011 this is a fast and elegant solution for the problem. More information about lpsolve package can be found on CRAN: https://cran.r-project.org/web/packages/lpSolve/lpSolve.pdf. There is also a list of all libraries for optimization: https://cran.r-project.org/web/views/Optimization.html

Summary

You can perform complex calculations like predictions and even optimization in business rules using HANA and R. In the last blog entry I called those business rules analytical. This means that they don’t operate on a single business object, they operate on sets of objects instead. I gave two examples:

  • work items that have to be assigned using optimization
  • and time dependent data which I discussed in my last blog entry

In my opinion R is the perfect programming environment to use those algorithms. Usually I select data from HANA “Outside-In” with the R studio and write my scripts. After testing them I implement them in HANA and call them using BRFplus. The only drawback is that you can’t use R in ADMP directly. Otherwise the creation of analytical rules using R would me much easier.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Lars Breddemann
      Lars Breddemann

      You're on a roll Tobias! Very good!

      When I read your blog I was reminded of a project that my Custom Development colleagues have built:

      SAP Custom Development optimizes Call Center Workforce to make customers and staff happy

      If I'm not mistaken they also started using R to model the solution and fiddle with some models in a quick and agile fashion. Once the found their solution they implemented it with SAP HANA onboard tools. These tools include (that surprised even me) also a well hidden linear optimisation tool.

      Looks like there is a growing interest in doing integrated optimisation and analytics/prediction. Exciting!

      Author's profile photo Former Member
      Former Member

      Great blog!!!

      Is there any optimization algorithms available directly  in SAP PAL? i have a long list of predictive analysis algorithm in PAL, wondering if there is anything on optimization algorithm.