Beispiel #1
0
def CheckValidSolution(solution, data):
    for nurse in solution:
        schedule = nurse['schedule']

        # Try to fill all schedule
        valid = fillScheduleFunctions.FillSchedule(nurse, data)
        if (valid == False):
            return False

        #Max presence
        presence = nurse['end'] - nurse['ini'] + 1
        if (presence > data.maxPresence):
            return False

        #Max hours
        if (nurse['workingHours'] > data.maxHours):
            return False

        #Min hours
        if (nurse['workingHours'] < data.minHours):
            return False

        #Max consec
        if (checkFunctions.CheckMaxConsecutiveHours(nurse['schedule'],
                                                    data.maxConsec) == False):
            return False

        #Check rest
        if (checkFunctions.CheckRest(nurse['schedule']) == False):
            return False
    return True
Beispiel #2
0
def construct(E, alpha, data, seed):
    random.seed(seed)
    # Initialize all schedules to no working
    solution = list(
        map(
            lambda x: {
                'schedule': np.zeros(data.nHours),
                'workingHours': 0,
                'ini': data.nHours,
                'end': 0
            }, [None] * data.nNurses))

    timegreedying = 0
    timeRCL = 0
    timeUpdate = 0
    while E:
        t_start = time.time()
        #Compute greedy cost
        greedy_cost = compute_greedy_cost(E, solution, data)
        t_greedy = time.time()

        #RCL
        max_cost = max(greedy_cost)
        min_cost = min(greedy_cost)
        threshold = min_cost + alpha * (max_cost - min_cost)
        RCL = [e for (e, cost) in zip(E, greedy_cost) if cost >= threshold]

        pick = random.randint(0, len(RCL) - 1)
        pickNurse = RCL[pick][0]
        pickHour = RCL[pick][1]

        #Set nurse-hour to work
        SetWorkingHour(pickHour, solution[pickNurse], data)
        t_rcl = time.time()

        # Update candidates
        E = update(E, solution, pickNurse, data)
        t_update = time.time()

        timegreedying += t_greedy - t_start
        timeRCL += t_rcl - t_greedy
        timeUpdate += t_update - t_rcl

    # Remove unused nurses
    solution = [n for n in solution if n['workingHours'] > 0]

    #Fill schedules (to guarantee all constrains (minHours, rest,...))
    valid = FillAllSchedules(solution, data)

    #Check all constrains
    if valid == True:
        valid = Check.CheckAllConstrains(solution, data, False)

    if valid == False:
        return None, False

    return solution, True
Beispiel #3
0
    obj_hist = [
        0
    ] * MAXITER  #records objective function value of the best so far

    data = DataClass(sourcedata)

    E = get_solution_elements(data)

    t_begin = time.time()
    for i in range(MAXITER):
        sol, valid = construct(E, ALPHA, data, seeds[i])
        if valid:
            sol = local_search(sol, data)
            obj = compute_obj(sol)
            #Check all constrains
            valid = Check.CheckAllConstrains(sol, data, True)

            if valid == True and obj < obj_best:
                sol_best = sol
                obj_best = obj
        obj_hist[i] = obj_best
        print "It" + str(i) + "(" + str(obj_best) + ")",
    print ""
    t_end = time.time()

    #print solution obtained
    #print('---------- SOLUTION -----------')
    print('     - Total time: {:.2f}s'.format(t_end - t_begin))
    if not sol_best:
        print('     - Nurses: Could not find any feasible solution')
    else:
Beispiel #4
0
    bestIndividual = brkga.getBestFitness(population)

    #Print some results
    print('Fitness: ' + str(bestIndividual['fitness']) + ' nNurses: ' +
          str(len(bestIndividual['solution'])))
    print('Time solving: ' + str(t_exec) + ' sec')

    plt.plot(evol)
    plt.xlabel('number of generations')
    plt.ylabel('Fitness of best individual')
    plt.axis([0, len(evol), 0, 1 + int(data['nNurses'])])
    plt.show()
    print("nNurses : " + str(bestIndividual['fitness']))

    #Check all constrains
    if Check.CheckDemand(bestIndividual['solution'], data['demand']) == False:
        print(
            '//////////////########################## ERROR DEMAND ########################'
        )

    i = 1
    for nurse in bestIndividual['solution']:
        if Check.CheckMaxConsecutiveHours(nurse['schedule'],
                                          data['maxConsec']) == False:
            print(
                '//////////////########################## ERROR MAXCONSEC ########################'
            )

        if Check.CheckMaxPresence(nurse['schedule'],
                                  data['maxPresence']) == False:
            print(