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)
    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 :
Mip

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

Advertisements

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

where

  \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(userLocation.lat - warehouseLocation.lat)
    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(userLocation.lat))
        * Math.cos(Math.toRadians(warehouseLocation.lat))
        * sinLng * sinLng)
    val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
    (AVERAGE_RADIUS_OF_EARTH_KM * c).toInt
  }
}

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

Distance

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 😀

References:
1. Wikipedia : Haversine Formula
2. National Hurricane Center

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 scala.concurrent.ExecutionContext.Implicits.global

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.map(f)(Left(_)), f => F.map(f)(Right(_)))

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

 

You can also see it on the sbt Console

CatsConsole

 

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

eitherOfFutures.bisequence

biseq

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.map(Left(_)), f => f.map(Right(_)))

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

logom

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

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.

                                                                                                                -Cloudera

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.

Why HADOOP?

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

Scalafmt – Styling The Beast

Styling your Scala code !

Knoldus

“I have never seen elegance go out of style” – SONYA TECLAI

Scala (The Beast) has imported efficiency and elegance to the programming world by combining coziness of object oriented programming with the magic of functional programming. This blog illuminates the work of Ólafur Páll Geirsson who has done a great job of styling the beast. In the bulk below I will discuss my experience of using Scalafmt, its installation process and some of its cool code styling features. Stuff we would be using in this blog

  • Editor -> Intellij (v 2017)
  • Build Tool -> sbt (v 0.13.15)
  • Language -> Scala (v 2.12.1)

One of the most important aspect of good code is its readability which comes with good and standard formatting. Ever wondered how an entire project having around 1000 Scala files of poorly formatted code could be formatted without having a headache? Well going shft + ctrl + alt…

View original post 634 more words

Basic of The Gherkin Language

Introduction to Gherkin ! New way of testing !

Knoldus

Hello Everyone ,

In this blog we will discuss about Gherkin Language  which we used in BDD for writing test cases.we will take a look on below topic.

Introduction:

Gherkin’s grammar is defined in the parsing expression grammars. It is Business Readable, DSL created specifically for behavior descriptions without explaining how that behaviour is implemented. Gherkin is a plain English text language.

Gherkin serves two purposes — documentation and automated tests. It is a whitespace-oriented language that uses indentation to define structure.

The Gherkin includes 60 different spoken languages so that we can easily use our own language.The parser divides the input into features, scenarios and steps.

Here is a simple example of Gherkin:

Screenshot from 2017-04-18 11-31-45

When we run this feature this gives us a step definition.In Gherkin, each line is start with a Gherkin keyword, followed by any text you like.

The main keywords are:

  • Feature
  • Scenario
  • Given
  • When
  • Then

Feature:

View original post 198 more words

Getting Started with Apache Cassandra

Cassandra101

Knoldus

Why Apache Cassandra?

Apache Cassandra is a free, open source, distributed data storage system that differs sharply from relational database management systems.
Cassandra has become so popular because of its outstanding technical features. It is durable, seamlessly scalable, and tuneably consistent.
It performs blazingly fast writes, can store hundreds of terabytes of data, and is decentralized and symmetrical so there’s no single point of failure.
It is highly available and offers a schema-free data model.

Installation:

Cassandra is available for download from the Web here. Just click the link on the home page to download the latest release version and Unzip the downloaded cassandra  to a local directory.

Starting the Server:

View original post 221 more words

Neo4j Apoc : A Blessing For Developer

Knoldus

Hello Folks,

As we know about Neo4j, it pulls out developers life from the trouble and black & white screen of the databases. It doesn’t give freedom from the old databases also provides best support with it’s predefined procedures.

As we know that in the Relational Database, Procedure provide advantages of better performance, scalability, productivity, ease of use and security and Neo4j also provides some amazing tool which can perform as mention above.

Yes, I am talking about the Apoc and using of Apoc with Neo4j, is a blessing for the developers. It provides many predefined procedures or user defined functions/views so that we can easily use it and improve our productivity in very simple manner.

APOC is stands for ‘Awesome Procedure On Cypher‘. APOC is a library of procedure for the various areas. It is introduce with the Neo4j 3.0

There are many areas where we use…

View original post 898 more words