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)
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
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.