Ejemplo n.º 1
0
def powerAvailableEstimation(big_jobs, boosters, hrs):
    '''
      Assume a rating of 0.25, total gain in ratings that can be obtained for different jobs 
    '''

    total_gain              = 0.0;
    ratio_big_jobs_boosters = len(big_jobs) / float( len(big_jobs)  + len(boosters) );

    #Dummy Elf
    dummy_elf               = Elf(0);
    dummy_elf.rating        = 0.25;

    work_start_time         = 540; #All boosters are played with maximum sanctioned time

    shuffle(boosters);

    for job in boosters:

        #Assume the work starts at 9:00 on start of the day
        next_available_time, work_duration = \
            assign_elf_to_toy(work_start_time, dummy_elf, job[1], hrs);

        dummy_elf.update_elf(hrs, Toy(0, '2014 01 01 01 01' ,job[1]), work_start_time, work_duration); #Every toy has default arrival time(irrelevant)

        #Flip a coin and play a big job based on above probability
        if random.random() < ratio_big_jobs_boosters:
            #Big Job Played - Measure the gain
            if dummy_elf.rating > 0.25:
                total_gain += (dummy_elf.rating - 0.25);
            #Reinitialize the dummy_elf
            dummy_elf               = Elf(0);
            dummy_elf.rating        = 0.25;


    return total_gain;
Ejemplo n.º 2
0
def powerRequiredEstimation(big_jobs, hrs):
    """

    :param big_jobs:  List of Big Jobs
    :param hrs:  Hrs Object
    :return:
    """
    power_per_unit   = [];
    #Dummy Elf
    dummy_elf        = Elf(0);
    dummy_elf.rating = 0.25;


    for job in big_jobs:
        #Play the toys at min rating at 9:00 on start of the day
        sanctioned, unsanctioned = breakDownWork(dummy_elf.next_available_time, dummy_elf, job[1], hrs);
        power_per_unit.append(unsanctioned);

    total_power    = sum(power_per_unit);
    power_per_unit = [i/float(total_power) for i in power_per_unit];

    return power_per_unit;
Ejemplo n.º 3
0
def global_optimizer(sub_file, myToys, hrs, NUM_ELVES, output):
    """ Score the submission file, performing constraint checking. Returns the time (in minutes) when
    final present is complete.
    :param sub_file: submission file name. Headers: ToyId, ElfId, Start_Time, Duration
    :param myToys: toys dictionary
    :param hrs: hours object
    :return: time (in minutes) when final present is complete
    """
    file_handler = open(output, "wb")

    hrs = Hours()
    ref_time = datetime.datetime(2014, 1, 1, 0, 0)
    threshold_minute = hrs.convert_to_minute('2450 1 1 0 0')

    basket_toys_above_th = []
    basket_toys_below_th = []
    #Unchanged

    elf_availability = {}
    #Maintains the earliest time when elves finish their last jobs
    for i in range(1, NUM_ELVES + 1):
        elf_availability[i] = [0, 0]

    row_count = 0

    with open(sub_file, 'rb') as f:
        fcsv = csv.reader(f)
        fcsv.next()  # header
        for row in fcsv:
            row_count += 1
            if row_count % 50000 == 0:
                print 'Starting toy: {0}'.format(row_count)

            current_toy = int(row[0])
            current_elf = int(row[1])
            start_minute = hrs.convert_to_minute(row[2])
            duration = int(row[3])

            if start_minute > threshold_minute:
                basket_toys_above_th.append(current_toy)
            else:
                basket_toys_below_th.append((row[0], row[1], row[2], row[3]))

            if elf_availability[current_elf][0] < start_minute:
                elf_availability[current_elf][0] = start_minute
                elf_availability[current_elf][1] = duration

    myelves = []
    for i in elf_availability.keys():
        elf = Elf(i)
        elf.rating = 0.25
        elf.next_available_time = elf_availability[current_elf][0] + int(
            math.ceil(elf_availability[current_elf][1] / elf.rating))
        heapq.heappush(myelves, (elf.next_available_time, elf))

    while len(basket_toys_above_th) > 0:

        #Retrive the toy object
        current_toy_duration = myToys[basket_toys_above_th[0]]

        #Pick the next available elf and execute it
        elf_available_time, current_elf = heapq.heappop(myelves)
        work_start_time = elf_available_time

        current_elf.next_available_time, work_duration = \
                    assign_elf_to_toy(work_start_time, current_elf, current_toy_duration, hrs)




        current_elf.update_elf(hrs, Toy(basket_toys_above_th[0], '2014 01 01 01 01' ,current_toy_duration), \
            work_start_time, work_duration)

        # put elf back in heap
        heapq.heappush(myelves, (current_elf.next_available_time, current_elf))

        tt = ref_time + datetime.timedelta(seconds=60 * work_start_time)
        time_string = " ".join([
            str(tt.year),
            str(tt.month),
            str(tt.day),
            str(tt.hour),
            str(tt.minute)
        ])

        #Add it to the basket below the threshold
        basket_toys_below_th.append((basket_toys_above_th[0], current_elf.id,
                                     time_string, work_duration))

        del basket_toys_above_th[0]

    basket_toys_below_th = sorted(
        basket_toys_below_th,
        key=lambda element: hrs.convert_to_minute(element[2]))

    file_handler.write("ToyId,ElfId,StartTime,Duration\n")
    for obj in basket_toys_below_th:
        file_handler.write(
            str(obj[0]) + "," + str(obj[1]) + "," + str(obj[2]) + "," +
            str(obj[3]) + "\n")
    file_handler.close()