Scala : Getting Started with Mathematical optimization in scala.

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)
    println("objective: " + objectiveValue)
    println("x = " + x.value + "y = " + y.value)


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

Scala: Calculating distance between two Locations

Hey Folks,
Recently I came along a use case where I need to find out the distance between two Location co-ordinates. In today’s blog we are going to solve this problem by implementing Haversine formula in Scala.

Just FYI,

The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.

Law of Haversines:

\operatorname {hav} (c)=\operatorname {hav} (a-b)+\sin(a)\sin(b)\,\operatorname {hav} (C).

Haversine Formula :

For any two points on a sphere, the haversine of the central angle between them is given by

{\displaystyle \operatorname {hav} \left({\frac {d}{r}}\right)=\operatorname {hav} (\varphi _{2}-\varphi _{1})+\cos(\varphi _{1})\cos(\varphi _{2})\operatorname {hav} (\lambda _{2}-\lambda _{1})}


  \operatorname {hav} (\theta )=\sin ^{2}\left({\frac {\theta }{2}}\right)={\frac {1-\cos(\theta )}{2}}
  • d is the distance between the two points (along a great circle of the sphere; see spherical distance),
  • r is the radius of the sphere,
  • φ1, φ2: latitude of point 1 and latitude of point 2, in radians
  • λ1, λ2: longitude of point 1 and longitude of point 2, in radians

So , without wasting any more time , let’s get started with the code.

case class Location(lat: Double, lon: Double)

trait DistanceCalcular {
  def calculateDistanceInKilometer(userLocation: Location, warehouseLocation: Location): Int

class DistanceCalculatorImpl extends DistanceCalcular {

  private val AVERAGE_RADIUS_OF_EARTH_KM = 6371

  override def calculateDistanceInKilometer(userLocation: Location, warehouseLocation: Location): Int = {
    val latDistance = Math.toRadians( -
    val lngDistance = Math.toRadians(userLocation.lon - warehouseLocation.lon)
    val sinLat = Math.sin(latDistance / 2)
    val sinLng = Math.sin(lngDistance / 2)
    val a = sinLat * sinLat +
        * Math.cos(Math.toRadians(
        * sinLng * sinLng)
    val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))

new DistanceCalculatorImpl().calculateDistanceInKilometer(Location(10,20),Location(40,20))


As you can see this is the scala implementation of Haversine formula and it computes the distance in pretty easy way.

You can see the compiled version of the code here on Scala Fiddle

WhatsApp Image 2017-12-01 at 1.30.31 PM


You can also verify your out put from here: National Hurricane Center


They are approximately same.

Hence in this way we can get the distance between two Locations.

If you have any queries you can contact me here or on twitter:@shiv4nsh

If you liked it, share it among peers !

Just one more thing , we are developing a community of developers and college graduates to help them so they may not face the same problem as we did. So if you liked the idea and want to be a part of this movement .

Come and join us : InternityFoundation

WhatsApp Image 2017-12-01 at 1.46.07 PM

I hope you join it 🙂
Wish you best of the weekend 🙂
Till then ! Happy hAKKAing 😀

1. Wikipedia : Haversine Formula
2. National Hurricane Center



In this blog we are going to understand the need of Machine learning and how it is going to shape our future. Let’s dive deep into this and find out how mesmerizing Machine learning is and how it is making things so much easier for us. So let’s get started 🙂


Machine learning…a term which got popularity in early 20’s. But it has evolved way too much as compared to past. Earlier the potential of machine learning was all about pattern recognition and the theory that computers can learn without being programmed to perform specific tasks; researchers interested in artificial intelligence wanted to see if computers could learn from data.It’s a science that’s not new – but one that has gained fresh momentum.

While building a product there are two different ways to see a feature either it is

View original post 639 more words

Support Vector Machines(SVM)-What are they?

Aarav's Blogs

SVM’s were initially developed in 1960s then they  were refined in 1990s and now they are becoming very popular in machine learning as they are demonstrating that they are very powerful and different from other Machine Learning algorithms.

A Support Vector Machine (SVM) is a supervised machine learning algorithm that can be employed for both classification and regression purposes. They are more commonly used in classification problems.

How SVM’s Work?

Consider some usual points on a 2 dimensional space with two columns x1 & x2.


Now how can we derive a line that will separate these two different points and classify them separately? This separation or decision boundary is compulsory as when we add new points in future that we want to classify haven’t been classified yet.                              We will get to know whether they will fall either…

View original post 694 more words

Gurukul Kangri Docker Workshop – Review of Internity !

internity foundation

He guys , here is a very quick update for you.
Yesterday we visited one one of the best colleges in Uttarakhand i.e. Gurukul Kangri for delivering a workshop on Introduction to Docker
To start with the campus , it was quite good ! There’s a  lot of greenery everywhere and it felt like we eloped in a totally parallel universe …

The faculty is quite overwhelming. They took care of almost everything from food-ing to lodging !  Almost everything …

We started the workshop in College at around 11:00. And we immediately made the connection with the students.

In the whole session we explained about what is Internity ? What we are doing ? And how we are doing it ? And addressed some very good questions from the students.

After a brief introduction we quickly jumped on to the Docker. !

We explained them what is it ?…

View original post 255 more words

Myths about Spark transformations: All transformations are lazily evaluated?


Spark transformations are lazy? Yes, but not all. As most of people knows Spark transformations are lazy evaluated but some of the transformations are not lazily evaluated. In this article we will see what are the spark transformations which are not fully lazily evaluated. Lets dive into the sorted word count example

val textFile=sc.textFile(“”)

val words = textFile.flatMap(line => line.split(” “))

val wordPair =,1))

val wordCount = wordPair.reduceByKey(_+_)

Above code snippet will give us the word count for each word in file, Now its time to sort the result on the basis of words.

val wordCountSort = wordCount.sortByKey()
wordCountSort: org.apache.spark.rdd.RDD[(String, Int)] = ShuffledRDD[7] at sortByKey at :32

So now our result is sorted by words. But till now nothing is calculated because we only use transformations. Let’s confirm we didn’t fire any job with localhost:4040

Screen Shot 2018-03-13 at 00.18.41

Surprise? Yes, sortByKey() is transformation and its not fully lazily evaluated. If you see…

View original post 78 more words

Internity at Empressario 2018

I know this one is not techincal but its worth sharing ! We’ll be writing this series ! Please follow ! 🙂

internity foundation

Hey Guys ,
This had been a great week so far.
Learned a lot ! Slept like nothing ! Won arguments ! lost some But most of all won a lot of hearts 🙂

An update from our side . We presented one of our products in Empressario 2018 in Product and Services track . We wanted to share our knowledge and our experience with you so that we can enroll maximum of you guys  to take part in such events and to just stop what you are doing and think about your startup idea that you are planning to execute for a very long time and not starting it because of money issues, time issues, relationship issues etc.

This time we are gonna talk about Empressario , it is the annual business model competition organized by Entrepreneurship Cell, IIT Kharagpur in association with International Business Model Competition (IBMC).

View original post 231 more words

How to transform Either[Future[A],Future[B]] to Future[Either[A,B]]

Hey folks, I hope all you guys are doing well !
Recently I came across one weird situation in which I was getting an
Future[Either[String , Future[A]]] and it was getting more and more complex so I thought of taking out the Future from the Either and then flattening it out , as most of us do always 😉

But this time this simple thing was not happening and I was getting frustrated and then while searching for it I came across a very good library for scala known as Cats which seems to solve my problem.

Just FYI, Cats is a library which provides abstractions for functional programming in the Scala programming language. The name is a playful shortening of the word category.
(Copied 😉 from Github )

So this is how I got introduced to Cats !
So how I solved my problem is via writing a function using cats.Functor

For using Cats you need to add the cats dependency in your build.sbt

name := "scala-cats"

version := "0.1"

organization :="com.internity"

scalaVersion := "2.12.3"

scalacOptions += "-Ypartial-unification"

libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.0-RC1"


case class A(a:String)

import scala.concurrent.Future


import cats.implicits._

import cats.Functor

val eitherOfFutures:Either[Future[String], Future[A]]=Right(Future.successful(A("Shiv")))

val futureOfEitherOfFuture=Future.successful(eitherOfFutures)

def eitherFmap[F[_], A, B](either: Either[F[A], F[B]])(implicit F: Functor[F]): F[Either[A, B]] = either.fold(f =>, f =>

val futureOfEither=futureOfEitherOfFuture.flatMap(eitherFmap(_))


You can also see it on the sbt Console



So this is how we can convert Either[Future[A],Future[B]] into Future[Either[A,B]].

Method 3: Without Functor’s boilerplate  code.

As per comment from M50d pointed out a easier solution :

You can also do it in a more simpler way by using bisequence in cats.instances._ as pointed out by M50d

import cats.instances._



Edit :

Recently from the comments from Reddit on this post , I have found a new apporach in simple scala  I am adding it here thanks to the user @jangchoe for this code snippet

eitherOfFutures.fold(f =>, f =>

If you have any problems look me up on twitter and let me know !

We are also developing a community of developers for bridging the gap between Corporates and the colleges ! If you want to be a part of this movement ! Signup on InternityFoundation

Till then Happy hAKKAing 🙂

References :

  1. StackOverflow
  2. Herding Cats



Integrating Play app with Lagom application

Playing with play and Lagom !

Knoldus Blogs


What is Lagom?

Simply put, it is a microservices tool.

Lagom is a microservices framework that guides developers in moving from monolithic to a scalable, resilient microservice based systems. It is a platform delivering you not only the microservice framework but also a whole tool set for developing applications along with creating, managing and monitoring your services.

According to Lightbend the focus should not be on how small the services are, but instead they should be just the right size, “Lagom” size services.

Most microservices frameworks focus on making it easy to build individual microservices. Lagom allows developers to run a whole system of microservices from a single command.

Before you begin creating your microservices, you need to make some selections:

View original post 242 more words

Introduction To HADOOP !

Knoldus Blogs

Here I am to going to  write a blog on Hadoop!

“Bigdata is not about data! The value in Bigdata [is in] the analytics. ”

-Harvard Prof. Gary King

So the Hadoop came into Introduction!

Hadoop is an open source, Java-based programming framework that supports the processing and storage of extremely large data sets in a distributed computing environment. It is part of the Apache project sponsored by the Apache Software Foundation.


Hadoop was created by computer scientists Doug Cutting and Mike Cafarella in 2006 to support distribution for the Nutch search engine. It was inspired by Google’s MapReduce.


The problem with RDBMS is , it can not processed semi-structured and unstructured data (text, videos, audios, Facebook posts, clickstream data, etc.). It can only work with structured data(banking transaction, location information, etc.). Both are also different in term of processing data.

RDBMS architecture with ER…

View original post 753 more words