# Re: [R] Monte carlo help

From: Mike Marchywka <marchywka_at_hotmail.com>
Date: Sun, 06 Mar 2011 20:23:34 -0500

> Date: Sun, 6 Mar 2011 15:42:11 -0800
> From: ksayho_at_gmail.com
> To: r-help_at_r-project.org
> Subject: [R] Monte carlo help
>
> Hello, I am currently doing my project and I need some help.
> I am trying to schedule tutors for a study room where students come in and
> out for helps. My goal is to schedule the tutors to maximally accomodate to
> the flow of students that need help with math so that each tutor is given an

I'm not sure what "maximally accomodate" means but from passing conversations with IOE folks I think you are talking about cueueing theory and there are at least two parameters such as average and worst case waiting times you may care about ( think about things like traffic light optimization including accidents due to switching etc).
For example, see if any of this helps ( and feel free to tell me I'm wrong here as I'd like to know about it and I admit I know nothing about it anyway).

> appropriate(also consecutive) hours of schedule. I am using monte carlo
> simulation for this, however I have difficulties setting up constraints and

Again, see the above for ideas. If you start assigning costs to things like

tutor and wait times you should end up with non-monotonic merit thingys. Unbounded monotonic things are probably just best presented as a trade offsure  the more you spend the more wait time goes down probably but does each extra dollar buy as much "result" and when do you have other stuff to do etc.

Just from skimming it, you probably want to make more use of R constructs to hide conditionals and loops. Consider your long if-else tree and replace with something like this,

> hours_lut<-1/c(5,6,rep(7,5));
> hours_lut

 0.2000000 0.1666667 0.1428571 0.1428571 0.1428571 0.1428571 0.1428571
> hours_lut

 0.1428571
>

a<-hours_lut[hours]

> appropriate number of tutors for each hours (as the place opens from 8 am to
> 7 pm). I researched how many people are coming in and out for a week and I
> am just going to use those numbers for my probability for each hours
>
> a <- 0; # probability of students coming in
>
> tutors <- 2; # number of tutors
>
>
> cat("week", "weekday", "hours", "tutors", "students", " solved", "
> unsolved","\n");
>
> total_stud <- 0; #total student number
> total_solved <- 0; #total number of questions answered
> total_unsolved <- 0; #total number of questions unanswered
>
>
> for (week in 1:3) {
> for (weekday in 1:5) {
> for (hours in 1:11) {
> solved <- 0;
> unsolved <- 0;
>
> if (hours == 1) {
> a <- 1/5;
> } else if (hours == 2) {
> a <- 1/6;
> } else if (hours == 3) {
> a <- 1/7;
> } else if (hours == 4) {
> a <- 1/7;
> } else if (hours == 5) {
> a <- 1/7;
> } else if (hours == 6) {
> a <- 1/7;
> } else if (hours == 7) {
> a <- 1/7;
> } else if (hours == 8) {
> a <- 1/7;
> } else if (hours == 9) {
> a <- 1/7;
> } else if (hours == 10) {
> a <- 1/7;
> } else if (hours == 11) {
> a <- 1/7;
> }
>
> random_num <- runif(1);
> if (random_num < a)
> students <- 1
> else
> students <- 0;
> if (students == 1) {
> if (tutors > 0) {
> solved <- solved+1;
> tutors <- tutors-1;
> }else{
> unsolved = unsolved+1;
> }
> }
>
>
> total_stud <- total_stud + students;
> total_solved <- total_solved + solved;
> total_unsolved <- total_unsolved + unsolved;
>
> cat(week,"\t", weekday,"\t", hours, "\t", tutors,"\t", students,"\t",
> solved,"\t", unsolved,"\n"); # display results for
> } # end loop on each hours
> } # endloop on weekday
> } # loop on week
>
> # output total statistics now....
> cat("\n totals over entire simulation:\n\n")
> cat(" students solved unsolved\n")
> cat("\t",total_stud,"\t",total_solved,"\t", total_unsolved,"\n")
>
> Please how can I improve this I am completely stuck and how Can i get
> organized result, I tried to use cat however it seems not working, lost.....
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Monte-carlo-help-tp3338196p3338196.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help