Ejemplo n.º 1
0
def RLS(prefix):

    solutions = []
    solution_number = 0

    tasks = su.create_tasks()
    doctors = su.create_doctors(tasks)
    su.construct_feasible_solution(doctors, tasks)

    while time.time() - start_time <= 1800:
        tasks = su.create_tasks()
        doctors = su.create_doctors(tasks)
        su.construct_feasible_solution(doctors, tasks)
        stuck = 0

        while True:
            last = su.update_objective_function(doctors, tasks)

            local_search(doctors, tasks, 1000)

            if abs(last - su.return_objective_function(doctors, tasks)) < 0.5:
                stuck +=1000
            else:
                stuck = 0

            if stuck >= 5000:
                solutions.append(copy.deepcopy(doctors))
                #su.write_solution(doctors, tasks, '{}solution{}'.format(prefix, solution_number)) # option to write each solution
                solution_number += 1
                break
    return solutions
Ejemplo n.º 2
0
def main():

    PH = 84  # planning horizon
    mo.PH = PH
    su.PH = PH
    cc.PH = PH

    tasks = su.create_tasks()
    doctors = su.create_doctors(tasks)
    su.construct_feasible_solution(doctors, tasks)
    best_solution = copy.deepcopy(doctors)
    
    start_time = time.time()
    last = su.update_objective_function(doctors, tasks)
    solution_number = 0

    while True:
        i = 0
        tasks = su.create_tasks()
        doctors = su.create_doctors(tasks)
        su.construct_feasible_solution(doctors, tasks)
        su.update_objective_function(doctors, tasks)
        for i in range(30000):

            move = random.choice([
                            mo.schedule_task_random,
                            mo.remove_task_random,
                            mo.swap_task_random,
                            mo.swap_between_oncall,
                            mo.swap_morning_oncall,
                            mo.swap_afternoon_oncall,
                            mo.swap_weekend_oncall
            ])


            original_doctors, original_tasks, good_move = move(doctors, tasks)

            for t in original_tasks:
                tasks[t].update_all_penalties(doctors)
            for d in original_doctors:
                doctors[d].update_all_penalties(tasks)


            if 'POK' in original_tasks:
                good_move = mo.repair_POK(doctors, tasks, original_doctors, original_tasks)


            old_cost = sum([d.all_penalties for d in original_doctors.values()]) \
                        + sum([t.all_penalties for t in original_tasks.values()])

            new_cost = sum([doctors[d].all_penalties for d in original_doctors]) \
                        + sum([tasks[t].all_penalties for t in original_tasks])


            diff = abs(su.return_objective_function(doctors, tasks)-su.update_objective_function(doctors,tasks))
            if diff > 0.5:
                print diff
                print move.__name__
                assert False

            if new_cost <= old_cost and good_move:
                pass
            else:
                for t in original_tasks:
                    tasks[t] = original_tasks[t]
                for d in original_doctors:
                    doctors[d] = original_doctors[d]
                    
            diff = abs(su.return_objective_function(doctors, tasks)-su.update_objective_function(doctors,tasks))
            if diff > 0.5:
                print diff
                print move.__name__
                assert False
from __future__ import division
import copy
import random
import lib.column_combine as cc
import lib.move_operators as mo
import lib.startup as su
import csv

PH = 84  # planning horizon
mo.PH = PH
su.PH = PH
cc.PH = PH

tasks = su.create_tasks()
doctors = su.create_doctors(tasks)

with open('solution.csv', 'r') as x:
    y = csv.reader(x, delimiter=',')
    for i, schedule in enumerate(zip(*y)):
        doctors[i].schedule = list(schedule)

for d in doctors:
    for s in range(PH):
        if d.schedule[s] == '-':
            d.schedule[s] = None

su.print_solution(doctors)
        

print 'Solution cost: ', su.update_objective_function(doctors, tasks)
Ejemplo n.º 4
0
def main():

    PHweeks = 12
    PH = 21 * PHweeks  # planning horizon
    mo.PH = PH
    su.PH = PH
    cc.PH = PH

    changes = []

    tasks = su.create_tasks()
    doctors = su.create_doctors(tasks)
    su.construct_feasible_solution(doctors, tasks)
    print 'Initial Solution cost: ', su.update_objective_function(doctors, tasks)
    
    iterations = 215000
    temp = 300
    cool = 0.93
    costs = [su.return_objective_function(doctors, tasks)]

    start_time = time.time()

    for i in range(1, iterations):

        if i%(iterations/100)==0:
            print '{}%'.format(int((i/iterations)*100))
            temp *= cool
            costs.append(su.return_objective_function(doctors, tasks))

        move = su.weighted_choice([
                        mo.schedule_task_random,
                        mo.remove_task_random,
                        mo.swap_task_random,
                        mo.swap_between_oncall,
                        mo.swap_morning_oncall,
                        mo.swap_afternoon_oncall,
                        mo.swap_weekend_oncall
        ], [1,0.75,1,1,1,1,1])


        original_doctors, original_tasks, good_move = move(doctors, tasks)

        for t in original_tasks:
            tasks[t].update_all_penalties(doctors)
        for d in original_doctors:
            doctors[d].update_all_penalties(tasks)

        if 'POK' in original_tasks:
            good_move = mo.repair_POK(doctors, tasks, original_doctors, original_tasks)

        old_cost = sum([d.all_penalties for d in original_doctors.values()]) \
                    + sum([t.all_penalties for t in original_tasks.values()])

        new_cost = sum([doctors[d].all_penalties for d in original_doctors]) \
                    + sum([tasks[t].all_penalties for t in original_tasks])

        if (new_cost <= old_cost and good_move):
            pass
        elif math.exp(-(new_cost - old_cost) / temp) >= random.random() and good_move:
            pass
        else:
            for t in original_tasks:
                tasks[t] = original_tasks[t]
            for d in original_doctors:
                doctors[d] = original_doctors[d]

    end_time = time.time()
    costs.append(su.return_objective_function(doctors, tasks))

    print 'Best objective function: ', su.return_objective_function(doctors,tasks)
    time_taken = abs(start_time - end_time)
    print 'Time taken: ', time_taken
    print 'time per iteration: ', time_taken/iterations
    su.write_solution(doctors, tasks, 'SAsol')
Ejemplo n.º 5
0
def local_search_lambda(doctors):
    tasks = su.create_tasks()
    return local_search(doctors, tasks, 100000)