From 0c39eed5d965edb7de41ab3a89b1a70d9b15c144 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Sun, 12 Apr 2020 12:52:31 +0530 Subject: [PATCH] added logging --- build.sbt | 5 ++- project/metals.sbt | 2 +- src/main/scala/HHCSim.scala | 59 +++++++++++++++++++++++++++++++++- src/main/scala/Main.scala | 3 +- src/main/scala/util/Util.scala | 4 ++- 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index 0c19773..1094105 100644 --- a/build.sbt +++ b/build.sbt @@ -11,5 +11,8 @@ libraryDependencies ++= Seq( "org.typelevel" %% "cats-core" % "2.0.0", "com.softwaremill.quicklens" %% "quicklens" % "1.4.12", "com.softwaremill.macwire" %% "macros" % "2.3.3", - "org.rogach" %% "scallop" % "3.4.0" + "org.rogach" %% "scallop" % "3.4.0", + "ch.qos.logback" % "logback-classic" % "1.2.3", + "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2", + "com.github.valskalla" %% "odin-core" % "0.7.0" ) diff --git a/project/metals.sbt b/project/metals.sbt index 23ee198..a64ca30 100644 --- a/project/metals.sbt +++ b/project/metals.sbt @@ -1,4 +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") +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.0-RC1-190-ef7d8dba") diff --git a/src/main/scala/HHCSim.scala b/src/main/scala/HHCSim.scala index 3632c9a..9a92d61 100644 --- a/src/main/scala/HHCSim.scala +++ b/src/main/scala/HHCSim.scala @@ -1,11 +1,14 @@ import model.Customer +import scala.util.chaining._ import util._ +import com.typesafe.scalalogging.LazyLogging +import scala.collection.mutable class HHCSim( private val epsilonMax: Int, private val iterations: Int, // private val customers: IndexedSeq[Customer], private val WLDMax: Float -) { +) extends LazyLogging { // private val graph: Array[Array[T]] def go( customers: Either[String, IndexedSeq[Customer]] @@ -13,8 +16,14 @@ class HHCSim( customers .flatMap(checkEmpty) .map(Util.formAdjMatrix) + .tap(_ => logger.debug("Edge matrix: ")) + .tap(logGraph) .map(edges => Util.mstUsingPrims(edges)) + .tap(_ => logger.debug("MST: ")) + .tap(logGraph) .map(mst => Util.findCentroids(mst)) + .tap(logCentroids) + .tap(logRemovedEdges) .map( e => e match { @@ -22,6 +31,7 @@ class HHCSim( Util.findClusters(mst, centroids, removed) } ) + .tap(logClusters) .map( e => e match { @@ -38,4 +48,51 @@ class HHCSim( case _ => Right(customers) } + def logGraph(it: Either[String, Array[Array[Double]]]): Unit = + it.map( + mst => { + logger.whenDebugEnabled { + mst.foreach { e => + e.foreach { d => + print(f"$d%.2f, ") + } + println + } + } + } + ) + + def logCentroids( + e: Either[ + String, + (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)]) + ] + ) = e.map(f => logger.debug(s"\nCentroids: \n ${f._2.toString}")) + + def logRemovedEdges( + e: Either[ + String, + (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)]) + ] + ) = + e.map( + f => + logger.whenDebugEnabled { + println(s"Removed Edges: \n${f._3.toString}") + } + ) + + def logClusters( + e: Either[ + String, + ( + IndexedSeq[Int], + Map[Int, mutable.ArrayBuffer[Int]], + IndexedSeq[(Int, Int, Double)] + ) + ] + ) = + e.map( + f => logger.whenDebugEnabled { println(s"Clusters: \n${f._2.toString}") } + ) } diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index f8f9376..dfbcd0f 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -5,12 +5,13 @@ import scala.collection.mutable.ArrayBuffer import util.Util import config.Conf import java.io.File +import scala.io.Source object Main { def main(args: Array[String]): Unit = { val conf = new Conf(args.toIndexedSeq) - val bufferedSource = io.Source.fromFile(conf.infile()) + val bufferedSource = Source.fromFile(conf.infile()) val customers = Util.getCustomers(bufferedSource) bufferedSource.close() diff --git a/src/main/scala/util/Util.scala b/src/main/scala/util/Util.scala index 606c3a2..50c2996 100644 --- a/src/main/scala/util/Util.scala +++ b/src/main/scala/util/Util.scala @@ -7,8 +7,10 @@ import scala.collection.mutable import model.Customer import scala.io.BufferedSource import scala.reflect.ClassTag +import com.typesafe.scalalogging.Logger +import com.typesafe.scalalogging.LazyLogging -object Util { +object Util extends LazyLogging { private val r = 6471.00 // km def toRad(num: Double): Double = {