patternMinor
Simple load balancer model in Scala
Viewed 0 times
simplescalaloadbalancermodel
Problem
For one of my cases in algorithms I am required to create a model of a load balancer model for multiple processors. Lately I became interested in Scala so I decided that it would be great to create this program in Scala. I'm not really familiar with the concept of functional programming so any feedback will be welcome. Please note that I am not asking about the correctness of my algorithm as this is my homework.
object HelloWorld {
def randomElements(arg: Integer): Double = {
val r = scala.util.Random
return r.nextDouble()
}
// used for debugging
def return1Elements(arg: Integer): Double ={
return 1
}
def fillJobs(size: Integer, fillingFunction: (Integer) => Double): Array[Double] = {
val array = new Array[Double](size);
for (el _)
var sortedProcessors = processorsArray
for(processorIndex Double): Double = {
val processors = processorsArray.clone()
val jobs = jobsArray.clone()
return solvingFunction(processors, jobs)
}
def main(args: Array[String]) {
val JOB_NUMBER = 1024
val PROCESSOR_NUMBER = 8
val jobs = fillJobs(JOB_NUMBER, randomElements)
val processors = fillProcessors(PROCESSOR_NUMBER)
println("Job times")
println("============================")
for (element <- jobs) {
println(element)
}
println("============================")
println("Results")
println("Find max solver " + solve(processors, jobs, findMaxSolver))
}
}Solution
Below are some of the changes I would make to your code. In my opinion one of the nice aspects of Scala is that it gives you all sorts of ways to reduce the amount of work your mind has to do in order to decipher code. As an example, one of the first things I did was declare a type
As the program is set up right now you don't need to pass in a
Next note that
And finally (for now) checkout how I initialized
Cheers.
ArrD that is equivalent to Array[Double]. I then just substitued where necessary and the code (to my mind) became more readable. The choice of ArrD was arbitrary on my part, you could if you wanted use ArrayDouble instead. Along these lines I shortened all of you variable and value names.As the program is set up right now you don't need to pass in a
procs array, but I left it in anyway. As you mentioned this is a homework assignment so I'll leave a bit of mystery as to why this is. And really you don't need the take and the drop.Next note that
jobs.sorted is equivalent to jobs.sortWith(_ > _).And finally (for now) checkout how I initialized
jobs and procs. If you still would like to use your randomElements function you should look into the method called tabulate.Cheers.
object O {
type ArrD = Array[Double]
def findMaxSolver(procs: ArrD, jobs: ArrD): Double = {
val sJobs = jobs.sorted
var sProcs = sJobs.take(procs.size)
for(job Double) = f(procs, jobs)
def run = {
val jobNum = 1024
val procNum = 8
val jobs = Array.fill(jobNum)(scala.util.Random.nextDouble())
val procs = Array.fill(procNum)(0.0)
println(s"RESULTS:\nFind max solver ${solve(procs, jobs, findMaxSolver)}")
}
}Code Snippets
object O {
type ArrD = Array[Double]
def findMaxSolver(procs: ArrD, jobs: ArrD): Double = {
val sJobs = jobs.sorted
var sProcs = sJobs.take(procs.size)
for(job <- sJobs.drop(procs.size)){
sProcs = sProcs.sorted
sProcs(0) += job
}
sProcs.max
}
def solve(procs: ArrD, jobs: ArrD, f: (ArrD, ArrD) => Double) = f(procs, jobs)
def run = {
val jobNum = 1024
val procNum = 8
val jobs = Array.fill(jobNum)(scala.util.Random.nextDouble())
val procs = Array.fill(procNum)(0.0)
println(s"RESULTS:\nFind max solver ${solve(procs, jobs, findMaxSolver)}")
}
}Context
StackExchange Code Review Q#88593, answer score: 2
Revisions (0)
No revisions yet.