From 67c7dd11d58afc644190625b8f7d95c55a696c2c Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Thu, 2 Apr 2020 22:08:21 +0530 Subject: [PATCH] first commit --- .gitignore | 5 +++++ .scalafmt.conf | 1 + build.sbt | 19 +++++++++++++++++ project/build.properties | 1 + project/metals.sbt | 4 ++++ src/main/scala/Main.scala | 32 +++++++++++++++++++++++++++++ src/main/scala/model/Coord.scala | 5 +++++ src/main/scala/model/Customer.scala | 3 +++ src/main/scala/model/HHCEdge.scala | 10 +++++++++ 9 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 .scalafmt.conf create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 project/metals.sbt create mode 100644 src/main/scala/Main.scala create mode 100644 src/main/scala/model/Coord.scala create mode 100644 src/main/scala/model/Customer.scala create mode 100644 src/main/scala/model/HHCEdge.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66a7545 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target/ +.metals +.vscode +.idea +.bloop \ No newline at end of file diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..78155c2 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1 @@ +version = "2.0.1" diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..e6948e5 --- /dev/null +++ b/build.sbt @@ -0,0 +1,19 @@ +name := "scala-hhc" +version := "1.0" +scalaVersion := "2.13.1" + + +resolvers ++= Seq( + Resolver.sonatypeRepo("releases"), + Resolver.sonatypeRepo("snapshots") +) + + +// https://mvnrepository.com/artifact/org.projectlombok/lombok +libraryDependencies += "org.projectlombok" % "lombok" % "1.18.10" % "provided" +libraryDependencies ++= Seq( + "org.typelevel" %% "cats-core" % "2.0.0", + "com.softwaremill.quicklens" %% "quicklens" % "1.4.12", + "com.softwaremill.macwire" %% "macros" % "2.3.3", + "me.shadaj" %% "scalapy-numpy" % "0.1.0+5-ad550211" +) \ No newline at end of file diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..6624da7 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.3.5 diff --git a/project/metals.sbt b/project/metals.sbt new file mode 100644 index 0000000..23ee198 --- /dev/null +++ b/project/metals.sbt @@ -0,0 +1,4 @@ +// DO NOT EDIT! This file is auto-generated. +// This file enables sbt-bloop to create bloop config files. + +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.0-RC1-105-118a551b") diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala new file mode 100644 index 0000000..8209be7 --- /dev/null +++ b/src/main/scala/Main.scala @@ -0,0 +1,32 @@ +import model.Coord +import model.Customer +import model.HHCEdge +object Main { + def main(args: Array[String]): Unit = { + + val coord1 = Coord(3, 4) + println(s"Distance from origin = ${coord1.distance}") + + val cust1 = Customer(coord1, 5) + val cust2 = Customer(Coord(2, 5), 7) + + println(s"Customer 1 = ${cust1}") + println(s"Customer 2 = ${cust2}") + + val cust3 = Customer(Coord(3, 5), 6) + val cust4 = Customer(Coord(6, 8), 9) + + val edge1 = HHCEdge(cust1, cust2) + val edge2 = HHCEdge(cust3, cust4) + + println(s"Edge 1 = ${edge1}") + println(s"Edge 1 weight = ${edge1.weight}") + + val V = Array(cust1, cust2, cust3, cust4) + val E = Array(edge1, edge2) + + V.foreach(println(_)) + E.foreach(println(_)) + + } +} diff --git a/src/main/scala/model/Coord.scala b/src/main/scala/model/Coord.scala new file mode 100644 index 0000000..8a0981f --- /dev/null +++ b/src/main/scala/model/Coord.scala @@ -0,0 +1,5 @@ +package model + +case class Coord(latitude: Long, longitude: Long) { + lazy val distance = math.sqrt(math.pow(latitude, 2) + math.pow(longitude, 2)) // distance is automatically calculated +} diff --git a/src/main/scala/model/Customer.scala b/src/main/scala/model/Customer.scala new file mode 100644 index 0000000..7b7208c --- /dev/null +++ b/src/main/scala/model/Customer.scala @@ -0,0 +1,3 @@ +package model + +case class Customer(location: Coord, workload: Int) \ No newline at end of file diff --git a/src/main/scala/model/HHCEdge.scala b/src/main/scala/model/HHCEdge.scala new file mode 100644 index 0000000..4299b6a --- /dev/null +++ b/src/main/scala/model/HHCEdge.scala @@ -0,0 +1,10 @@ +package model + +import scala.math.{sqrt, pow} + +case class HHCEdge(v1: Customer, v2: Customer) { + val weight = sqrt( //automatically compute the distance between customers + pow(v1.location.latitude - v2.location.latitude, 2) + + pow(v1.location.longitude - v2.location.longitude, 2) + ) +}