# R code to accompany HW 2, 36-350, Fall 2013
# Reproduced from slides for lecture 3
  # It's not SUPPOSED to make any sense out of context

factory <- matrix(c(40,1,60,3),nrow=2,
  dimnames=list(c("labor","steel"),c("cars","trucks")))
available <- c(1600,70); names(available) <- rownames(factory)
slack <- c(8,1); names(slack) <- rownames(factory)
output <-c(30,20); names(output) <- colnames(factory)

passes <- 0 # How many times have we  been around the loop?
repeat {
   passes <- passes + 1
   needed <- factory %*% output # What do we need for that output level?
   # If we're not using too much, and are within the slack, we're done
   if (all(needed <= available) &&
       all((available - needed) <= slack)) {
     break()
   }
   # If we're using too much of everything, cut back by 10%
   if (all(needed > available)) {
     output <- output * 0.9
     next()
   }
   # If we're using too little of everything, increase by 10%
   if (all(needed < available)) {
     output <- output * 1.1
     next()
   }
   # If we're using too much of some resources but not others, randomly
   # tweak the plan by up to 10%
   output <- output * (1+runif(length(output),min=-0.1,max=0.1))
}
