Hello guys ,

Today we are gonna talk about the problem of solving the mathematical optimization. Lets take a quick example,

Have you ever been in a condition where you have inequalities and you have to find out the maximum or minimum values of the variables for which a particular equation is maximum.

For example : There are two equations basically , one equation that you need to maximize and the other equation for which acts as a constraint

Equation to be maximized : **-2 * x + 5 * y
**Additional Constraint :

**y >:= 200 – x**

Range of Values:

For X: **100<x<=200**

For Y: **80<y<=170**

So the value of x and y for which this equation would be maximum will be x=100 and y=170 (Checkout your mathematical skills :D)

Now we need to solve such equations programatically. Where comes in the mathematical optimization libraries.

In our case we are going to use Optimus

Now let’s get started with the programming:

Firstly you need to add the optimus dependency in your build.sbt So your buid.sbt should look like this.

name := "scala-mip" version := "0.1" scalaVersion := "2.12.4" organization := "com.foobar" // The necessary dependencies can be added here libraryDependencies ++= Seq( "com.typesafe" % "config" % "1.3.1", //Mathematical Programming Dependencies "com.github.vagmcs" %% "optimus" % "2.1.0", "com.github.vagmcs" %% "optimus-solver-oj" % "2.1.0", "org.scalatest" %% "scalatest" % "3.0.0" % "test" )

Now we have to make a bounded variable i.e to define this expression:

For X: **100<x<=200**

For Y: **80<y<=170**

In Optimus we do it using this.

val x = MPFloatVar("x", 100, 200) val y = MPFloatVar("y", 80, 170)

The whole code something looks like this :

import optimus.optimization._ implicit val problem = LQProblem(SolverLib.ojalgo) val x = MPFloatVar("x", 100, 200) val y = MPFloatVar("y", 80, 170) maximize(-2 * x + 5 * y) add(y >:= 200 - x) start() println("objective: " + objectiveValue) println("x = " + x.value + "y = " + y.value) release()

**Explanation:**

**LQProblem:** defines its a Linear equation Problem , there can be other problems like Quadratic equation problem , Mixed Integer problem.

**maximize():** it takes the expression that needs to me maximize there are other functions also like minimize(), subjectTo() etc.

**add():** It takes a constraint expression that needs to be keep in consideration for maximizing that expression

**start(): **To start the computaion

**release()**: To release all the resources.

And it results in this output :

Hence for these value of X and Y this equation is maximum.

Currently I am working on a use case in which I am looking forward to use MIP for solving the problem. The problem is optimal Order fullfillment problem which is described here : Optimal Order Fullfillment.

If someone has any suggestions on solving this problem. Ping me ! I can also use some help 🙂

I hope you enjoyed this and find it interesting !

If you have any queries , let me know on here or on Twitter 🙂

Happy Reading 🙂

One last thing : If you are interested in community based learning , please Join us @InternityFoundation

**References :**

1. Optimus

2. Optimal Order fullfillment