Thursday 16 August 2018

Linear programming in R

Linear programming is a technique to solve optimization problems whose constraints and outcome are represented by linear relationships.

aaa

Simply put, linear programming allows to solve problems of the following kind:

  • Maximize/minimize $\hat C^T \hat X$
  • Under the constraint $\hat A \hat X \leq \hat B$
  • And the constraint $\hat X \geq 0$

This doesn’t seem much when you glance at it but in practice it is a powerful tool that can be used to make decisions in practical life scenarios.

It is often the case that we need to make decisions based on constraints. Often the invisible and most harsh constraint is time, but generally speaking there are a lot of other constraints that we need to take into account. A simple set of examples would be:

  • I want to change my heating system. I want to minimize the cost of the system and the bills, what kind of heating system should I install? A pellet stove? Electric radiators?
  • I want to obtain the maximum profit from the sale of these two products I produce. In what quantities should I produce each product?
  • And so on…

Linear programming can help you with these kind of decisions where:

  1. The function you are trying to optimize is a linear combination of the decision variables (this might not always be the case).
  2. The constraints you have are a linear combination of the decision variables.

An example of linear optimization

I’m going to implement in R an example of linear optimization that I found in the book “Modeling and Solving Linear Programming with R” by Jose M. Sallan, Oriol Lordan and Vincenc Fernandez.  The example is named “Production of two models of chairs” and can be found at page 57, section 3.5. I’m going to solve only the first point.

The problem text is the following

A company produces two models of chairs: 4P and 3P. The model 4P needs 4 legs, 1 seat and 1 back. On the other hand, the model 3P needs 3 legs and 1 seat. The company has a initial stock of 200 legs, 500 seats and 100 backs. If the company needs more legs, seats and backs, it can buy standard wood blocks, whose cost is 80 euro per block. The company can produce 10 seats, 20 legs and 2 backs from a standard wood block. The cost of producing the model 4P is 30 euro/chair, meanwhile the cost of the model 3P is 40 euro/chair. Finally, the company informs that the minimum number of chairs to produce is 1000 units per month. Define a linear programming model, which minimizes the total cost (the production costs of the two chairs, plus the buying of new wood blocks).

Problem definition

First, we need to translate the problem in a mathematical way. Let’s define the following variables

  • $x_{4p}$ is the number of 4P chairs to be produced.
  • $x_{3p}$ is the number of 3P chairs to be produced.
  • $x_w$ is the number of wood blocks to be bought.

Now we can define $\hat X = \begin{pmatrix} x_{4p} \\ x_{3p}  \\  x_w \end{pmatrix}$ as the decision variable vector. Note that it must be $\hat X \geq 0$.

We would like to minimize the total cost so we must set our objective function as follows

$$cost(x_{4p}, x_{3p}, x_w) = 30 x_{4p} + 40 x_{3p} + 80 x_w = MIN(cost) $$

which means that $\hat C = \begin{pmatrix} 30 \\ 40  \\  80 \end{pmatrix}$.

The constraints can be set in the following way

  1. For the seats $$ x_{4p} + x_{3p} \leq 500 + 10 x_w $$
  2. For the legs $$ 4 x_{4p} + 3 x_{3p} \leq 200 + 20 x_w $$
  3. For the backs $$ x_{4p} \leq 100 + 2 x_w $$
  4. Minimum number of chairs produced $$ x_{4p} + x_{3p} \geq 1000 $$

We can now define the coefficient matrix $A = \begin{pmatrix} 1 & 1 & -10 & \\  4 & 3 & -20 & \\  1 & 0 & -2 & \\  - 1 & - 1 & 0 &  \end{pmatrix} $ and $B = \begin{pmatrix} 500 \\ 200 \\ 100 \\ -1000 \end{pmatrix}$.

R implementation and solution

We are now ready to implement this is in R. There are many different packages which can solve this kind of problems but my favourites are lpSolve and lpSolveAPI which is a kind of API built on top of lpSolve. I will use both.

A nice feature about the lpSolve package is that you can specify the direction of the constraints. Indeed in our case the last constraint of minimum number of chairs produced does not fit in with the mathematical definiton of the problem that we gave in the previous paragraph. Here we can either change the signs (and therefore the inequality direction) or specify the inequality direction in lpSolve. I’ll do this second way.

We can set that all the variables should be integers by setting the argument “all.int=TRUE” in the lpSolve::lp function. Of course, lpSolve can work with both integers and real numbers.

It’s also particularly important to check the status at the end of the execution: if it is 0, then a solution has been found but if it is 2 then this means that there is no feasible solution.

23 comments:

  1. Hi,
    with the intpoint package you can solve lp problemas graphically in two dimensiones, and you can compare the simplex with the interior point (karmarkar) method

    https://www.rdocumentation.org/packages/intpoint/versions/1.0

    ReplyDelete
    Replies
    1. Hello! Thanks for reading and for the package suggestion!

      Delete
  2. I appreciate your effort. Keep motivating us. For the persons who want to join any institute for computer software training courses can join ZENITECH.

    ReplyDelete
  3. Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angularjs training in chennai | angularjs training in omr

    ReplyDelete
  4. Thanks for Sharing a very Informative Post and I Must say its really helpful for us.your blog helps me to know more about the course.
    Python Course in Delhi


    Thanks for Sharing a very Informative Post and I Must say its really helpful for.your blog helps me to know more about the course.
    Python Course in Noida


    Thanks for Sharing a very Informative Post and I Must say its really helpful for.your blog helps me to know more about the course.
    Python Course in Gurgaon

    ReplyDelete
  5. Thanks for sharing such a great blog Keep posting..
    Python Training
    Python Course

    ReplyDelete
  6. Awesome post. You Post is very informative. Thanks for Sharing.
    R programming training in Noida

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.
    cs作业

    ReplyDelete
  9. Thanks! You folks help me and my student A LOT!

    ReplyDelete
  10. Thanks for this informative blog.

    - Casino India

    ReplyDelete
  11. This article is a great article that I have seen in my python programming career so far.

    hire python developers in US

    ReplyDelete
  12. Learn Trending Coding Languages like Java, C++, HTML, Python, Android from here for free !

    ReplyDelete
  13. IntelliMindz is the best IT Training in Bangalore with placement, offering 200 and more software courses with 100% Placement Assistance.
    R Programming Online Course
    R Programming Course in Bangalore
    R Programming Training in Chennai

    ReplyDelete
  14. Queen Casino | Komono Casino
    Play at Queen Casino in Komono, ON. クイーンカジノ Enjoy 24/7 friendly customer bk8 service and online slot 카지노 play! Rating: 5 · ‎7 votes · ‎Free · ‎Game

    ReplyDelete
  15. nice blog I got more knowledge about programing thank you so much for sharing programing language knowledge. and anyone want learn data science course in mumbai visit here

    ReplyDelete
  16. Amazing Blog. thank you for sharing this information. Data analytics course is an interdisciplinary field that combines elements of mathematics, statistics, computer science, and domain knowledge to extract insights and knowledge from structured and unstructured data.

    ReplyDelete
  17. Get ready to enter a new era of mobile brilliance as OnePlus unveils its latest flagship masterpiece, the OnePlus 12. A testament to relentless innovation and unwavering dedication to crafting exceptional user experiences, the OnePlus 12 is poised to redefine the smartphone landscape. Prepare to be captivated by its cutting-edge technology, unparalleled performance, and mesmerizing visuals.

    A Performance Juggernaut
    At the heart of the OnePlus 12 lies the Qualcomm Snapdragon 8 Gen 3 processor, a veritable powerhouse that elevates mobile performance to unprecedented heights. This octa-core beast unleashes raw power, ensuring seamless multitasking, effortless handling of demanding applications, and an unparalleled gaming experience. Whether you’re a multitasking maestro, a graphics aficionado, or simply seeking the smoothest, most responsive smartphone experience, the OnePlus 12 is your answer.

    ReplyDelete
  18. Une entrée très intéressante pleine d’informations inspirantes. Veuillez également visiter le Gowork FR Blog

    ReplyDelete
  19. Are you looking to invest in invoice discounting minimum investment? Falcon makes it easy with low minimum investment options. Get started today with Falcon’s simple and flexible invoice discounting solutions. Ideal for businesses seeking to boost cash flow without big upfront costs.

    ReplyDelete
  20. Support a better future with our NGO in Mathura. We help those in need by providing education, healthcare, and basic needs. Join us in making a positive change in the lives of many. Every small effort counts. Let's build a stronger, caring community together.

    ReplyDelete
  21. If you're looking for a clinic that offers Universal Non-Verbal Intelligence Test 2nd Edition in Islamabad, here Pehchan Clinic is available. They provide more health care services.

    ReplyDelete