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]] ): String Either Map[Int, IndexedSeq[(Int, Double)]] = customers .flatMap(checkEmpty) .map(Util.formAdjMatrix) .tap(logGraph) .map(edges => Util.mstUsingPrims(edges)) .tap(logMST) .map( e => Util.findCentroids(mst = e._1, epsilon = e._2) ) .tap(logCentroids) .tap(logRemovedEdges) .map( e => e match { case (mst, centroids, removed) => { printGraph(mst) Util.findClusters(mst, centroids, removed) } } ) .tap(logClusters) .map( e => e match { case (centroids, clusters, removed) => Util.groupClusters(centroids, clusters, removed) } ) def checkEmpty( customers: IndexedSeq[Customer] ): Either[String, IndexedSeq[Customer]] = customers.length match { case 0 => Left("Error input was empty") case _ => Right(customers) } def printGraph(graph: Array[Array[Double]]) = graph.foreach { e => e.foreach { d => print(f"$d%.2f, ") } println } def logGraph(it: Either[String, Array[Array[Double]]]): Unit = it.map( graph => { logger.whenDebugEnabled { println("Graph: ") printGraph(graph) } } ) def logMST(it: Either[String, (Array[Array[Double]], Double)]): Unit = it.map( t => { logger.whenDebugEnabled { println(s"Epsilon = ${t._2}") println("MST: ") printGraph(graph = t._1) } } ) 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}") } ) }