1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Rep Power

    Ruby help, programming noob

    I'm trying to make a program to automatically make a work schedule.

    There's a number of different things I need to implement, such as off days, specific jobs for different people, "on-call" workers, etc.

    I tried it one way yesterday but that didn't work well at all. I think the way I was trying to assign people to jobs was too complicated so I started over today.

    I decided on setting up a class "Soldier" that will include the persons name as a string, and then have a series of priority codes to decide to which of the different available seats at work they will be assigned. There are three possible values for the person's priority on a job: 2, it's the person's main job; 1, qualifed; or 0, unqualified.

    I will put those soldiers in an array called 'team'. I eventually want to write this array to a file at the end of the program, and then import that file the next time it is run, but I'll figure that out later.

    Now I was planning to write a series of statements for each job and have it search my array and look for the person who had the highest priority code on that job, then assign that persons name to that job (represented by an object of class Truck).

    Problem I have is that I can't figure out how to search through each persons entry for that specific value in an array. I would then take the highest one, push him to the end of the array, or copy to another array and take his name out of the list and continue to the next job.

    Also as you can see in the code here, when I define my class Soldier I have to then remap those private variables to public ones. Is there a way I can make those things public in the initialize def, instead of having to define each of them individually?
    Thanks for any help.

    (This is what I have so far on the new one):

    #~ We have 5 trucks, in 4 of them (truck 1, 2, 5, and ASV)there are 3 slots, a driver, tc (passenger), and a gunner.  in the forth truck (wrecker 1), is just a driver and tc.
    #~ The program I'm trying to make needs to do the following:
    #~ * Place qualified people in the truck they are qualified in
    #~ * Take the extra people once the trucks are filled and give them that day off, 
    #~ * Cycle the people that are off so that the first person off isn't off again until everyone else has been off.
    #~ * Two seperate lists of people for off-days: NCO's and lower-enlisted
    #~ * NCO's can only be off wed-sunday, and only one at a time.
    #~ * Thursday truck1 and truck2's gunner are off, but "on-call" so will still be slotted in a truck, but will also take an available day-off.
    #~ * Sunday is like thursday,  but also the team-sergeant (SSG Gib) will be on-call, and will take an nco day-off.
    #~ * Every day we need two people on radio duty.  On days that no NCOs were off the day before, it will be divided into two shifts, with the lower-enlsted that were off the day before taking
    #~ both shifts.  On days where an nco other than SSG Gib were off the day before, that NCO will take the whole shift, and two lower enlisted that were off will take the first and second half of the shift.
    class Truck #generic truck
      def initialize
        @dr = nil #driver
        @tc = nil #tc (passenger)
        @gu = nil #gunner
      def dr
      def dr=(newDr)
        @dr = newDr
      def tc
      def tc=(newTc)
        @tc = newTc
      def gu
      def gu=(newGu)
        @gu = newGu
    class Soldier #personnel and priority
      def initialize(name,gun1dr,gun1tc,gun1gu,gun2dr,gun2tc,gun2gu,gun5dr,gun5tc,gun5gu,wrk1dr,wrk1tc,nco)
        @name = name
        @gun1dr = gun1dr
        @gun1tc = gun1tc
        @gun1gu = gun1gu
        @gun2dr = gun2dr
        @gun2tc = gun2tc
        @gun2gu = gun2gu
        @gun5dr = gun5dr
        @gun5tc = gun5tc
        @gun5gu = gun5gu
        @wrk1dr = wrk1dr
        @wrk1tc = wrk1tc
        @nco = nco
      def name
      def gun1dr
      def gun1tc
      def gun1gu
      def gun2dr
      def gun2tc
      def gun2gu
      def gun5dr
      def gun5tc
      def gun5gu
      def wrk1dr
      def wrk1tc
      def nco
    #add hadRto? for whether they had rto last day off
    #will only do this the first time to generate text file
    team = Array.new
    team.push(Soldier.new("SPC Smi",0,0,0,0,0,0,0,1,0,2,0,false))
    team.push(Soldier.new("SGT Tho",0,0,0,0,2,0,0,0,0,0,0,true))
    team.push(Soldier.new("SPC Sta",0,0,0,0,0,0,1,0,0,2,0,false))
    team.push(Soldier.new("PFC Vuk",0,0,0,0,0,0,0,0,0,0,3,false))
    team.push(Soldier.new("SGT Edd",0,2,0,0,0,0,0,1,0,1,0,true))
    team.push(Soldier.new("PFC Bol",0,0,0,0,0,3,0,0,0,0,0,false))
    team.push(Soldier.new("PFC Sip",0,0,0,3,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("SPC Est",2,0,0,0,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("SPC Fer",2,0,0,0,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("PFC Wil",0,0,3,0,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("PFC Hou",0,0,0,0,0,0,0,0,2,0,0,false))
    team.push(Soldier.new("SPC Wag",0,0,0,0,0,0,1,0,0,0,2,false))
    team.push(Soldier.new("SPC And",2,0,0,2,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("SPC Sap",0,0,0,0,0,0,0,0,2,0,0,false))
    team.push(Soldier.new("PFC Riv",2,0,0,0,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("SGT Eld",0,3,0,0,0,0,0,0,0,0,0,true))
    team.push(Soldier.new("SPC Ten",0,0,0,0,0,0,2,0,0,0,0,false))
    team.push(Soldier.new("SGT Mil",0,1,0,0,0,0,0,2,0,0,0,true))
    team.push(Soldier.new("SSG Gib",0,0,0,0,0,0,0,0,0,0,0,false))
    team.push(Soldier.new("SGT Min",0,1,0,0,2,0,0,0,0,1,0,true))
    #make 2 different arrays for nco and soldier
    #import soldier array
    #import nco array
    #assign off days and RTO duty for nco
      #check day
      #if day is a sunday, add SSG Gib to off days, he will be on-call
      #if day is wednesday through saturday
        #pull nco off the front of the list
        #add him to days off
        #move him to another array
      #put last nco on rto duty
      #add the remaining to the front of the copy in the same order they are in
      #if day is monday or tuesday, add them to main job assignment roster
      #take the copy and export it to text file
    #assign off days and rto for soldier
      #check had rto flag on last soldiers
      #if there are more soldiers that had rto than needed
        #randomly assign rto to them
        #add soldier to rto duty
        #set rto flag
        #add soldier to rto duty
        #set rto flag
    #check day
      #if day is a thursday or sunday
        #add gunners as on-call
        #add remaining off-days
      #elsif day is other
        #pull soldiers off the front of array
        #put them on off list
        #copy array
      #add remaining to soldier off roster
      #export to text file
    #assign remaining workers to main job roster
    #assign jobs
      #check each truck
      #find the person with the highest priority code for each job
      #assign them to that job
      #if there is more than one person that has the same code, randomly assign them
    #export jobs,rto, and off days to csv formatted spread sheet file
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Rep Power
    I'd have a look at the attr_reader attr_writer methods. They can be called statically and be given a list of symbols, all of whose corresponding value are then accessible simply by calling object.attribute, or object.attribute = x for writable variables.

    For (a redundant) example:
    class MyClass
        attr_reader :myvariable1, :myvariable2
        attr_writer :myvariable1, :myvariable2
        def initialize(val1, val2)
            @myvariable1 = val1
            @myvariable2 = val2
    myobj = myClass.new(1,2)
    myobj.myvariable1 = 12
    puts "Val 1: #{myobj.myvariable1}, val 2: #{myobj.myvariable2}"
    ruby test.rb
    Val 1: 12, val 2: 2

    Hope this helps,

  4. #3
  5. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    England, UK
    Rep Power

IMN logo majestic logo threadwatch logo seochat tools logo