# Scala: Mathematical Optimization

# Scala: Mathematical Optimization

### Time for a math lesson! Let's see how to calculate minimum or maximum values of equations in Scala with some help from the Optimus library.

Join the DZone community and get the full member experience.

Join For Free**Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!**

Hello, folks!

Today we are going to talk about the problem of solving the mathematical optimization. Let's look at 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 a particular equation?

For example: There are two equations: one equation that you need to maximize and another equation that 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 (Check out your mathematical skills!).

Now we need to solve such equations programmatically. This is where mathematical optimization libraries come in. In our case, we are going to use Optimus.

Now let’s get started with the programming.

First, 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 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 looks something like this 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 a linear equation problem. There can be other problems like a quadratic equation problem or a mixed integer problem.**maximize():**takes the expression that needs to be maximized. There are other functions like minimize(), subjectTo(), etc.**add():**takes a constraint expression that needs to be kept in consideration for maximizing that expression.**start():**to start the computation.**release()**: to release all the resources.

And it results in this output:

Hence for the values of X and Y, this equation is maximum.

Currently, I am working on a use case in which I am looking forward to using MIP to solve the problem. The problem is optimal order fulfillment, which is described here: Optimal Order Fulfillment.

If someone has any suggestions for solving this problem, feel free to ping me! I can also use some help.

I hope you enjoyed this and found it interesting! If you have any questions, let me know!

Happy reading!

**Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. **

Published at DZone with permission of Shivansh Srivastava , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}