Example #1
0
def assignNurses(solution, hini, data):

    """
        hini is used as the highest hour at which a nuser can start working.
        If at hini a nuses has not started working, that nuser won't work.

        A nurse can work before hini,(if there is enough demand)
    """

    demand = data["demand"]
    pending = solution["pending"]
    hours = data["hours"]
    sumW = [0] * data["nNurses"]

    z = solution["z"]
    w = solution["w"]

    for h in range(hours):

        # for each hour

        # compute valid candidates
        #  those who must be assigned (rest constraint)
        #  those who can be assigned to work
        #  if h>hini and z[n]== 0 , that nurse cannot work
        mustWork, canWork = computeAssignments(solution, h, data, sumW, hini)

        # print("h=" + str(h))
        # print("mustWork")
        # print(mustWork)
        # print("canWork")
        # print(canWork)
        # print("hini:")
        # print(hini)
        # print("demand")
        # print(data["demand"])
        # print("pending")
        # print(solution["pending"])

  
        #   try to assign if pending[h] > 0 and h >= hini[n]
        for n in mustWork:
            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            w[n][h] = 1
            sumW[n] += 1
            pending[h] -= 1
            if z[n] == 0:
                z[n] = 1
                solution["cost"] += 1
            #print("w[" + str(n) + "," + str(h) + "] = 1")
            # pp.pprint(solution["w"])



        for n in canWork:
            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            if pending[h] > 0:    
                w[n][h] = 1
                sumW[n] += 1
                pending[h] -= 1
                if z[n] == 0:
                    z[n] = 1
                    solution["cost"] += 1
                #print("w[" + str(n) + "," + str(h) + "] = 1")
            # print("w[" + str(n) + "]")
            # pp.pprint(solution["w"])

        # if pending[h] > 0:
        #     print(" h:" + str(h) + " pending[h]=" + str(pending[h]))
        # print(solution["pending"])
        # print("")
        

    # pp.pprint(data)
    # pp.pprint(solution["cost"])
    # exit()

    # compute cost: already updated!

    # compute feasibility: if unfeasible -> fitness should be inf
    if not isFeasible(solution, data):
        # assign the max cost
        solution["cost"] = 200000 * data["nNurses"]
def assignNurses(solution, hini, data):
    """
        hini is used to add extra nurses at each hour
            hini[h]=0.2 -> means we add 0.1*demand[h] in terms of nurse assigments

    """

    demand = data["demand"]
    pending = solution["pending"]
    hours = data["hours"]
    sumW = [0] * data["nNurses"]

    z = solution["z"]
    w = solution["w"]

    for h in range(hours):

        # for each hour

        # compute valid candidates
        #  those who must be assigned (rest constraint)
        #  those who can be assigned to work
        #  if h>hini and z[n]== 0 , that nurse cannot work
        mustWork, canWork = computeAssignments(solution, h, data, sumW)

        # print("h=" + str(h))
        # print("mustWork")
        # print(mustWork)
        # print("canWork")
        # print(canWork)
        # print("hini:")
        # print(hini)
        # print("demand")
        # print(data["demand"])
        # print("pending")
        # print(solution["pending"])

        #   try to assign if pending[h] > 0 and h >= hini[n]
        for n in mustWork:
            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            w[n][h] = 1
            sumW[n] += 1
            pending[h] -= 1
            if z[n] == 0:
                z[n] = 1
                solution["cost"] += 1
            #print("w[" + str(n) + "," + str(h) + "] = 1")
            # pp.pprint(solution["w"])

        for n in canWork:
            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            if pending[h] + hini[h] > 0:
                w[n][h] = 1
                sumW[n] += 1
                pending[h] -= 1
                if z[n] == 0:
                    z[n] = 1
                    solution["cost"] += 1
                #print("w[" + str(n) + "," + str(h) + "] = 1")
            # print("w[" + str(n) + "]")
            # pp.pprint(solution["w"])

    # pp.pprint(data)
    # pp.pprint(solution["cost"])
    # exit()

    # compute feasibility: if unfeasible -> fitness should be inf
    if not isFeasible(solution, data):
        # assign the max cost
        solution["cost"] = 200000 * data["nNurses"]
def assignNurses(solution, hini, data):
    """
        hini is used to add extra nurses at each hour
            hini[h]=0.2 -> means we add 0.1*demand[h] in terms of nurse assigments

    """

    demand = data["demand"]
    pending = solution["pending"]
    hours = data["hours"]
    sumW = [0] * data["nNurses"]

    checkers = initCheckers(data)

    z = solution["z"]
    w = solution["w"]

    for h in range(hours):

        #print(" for loop h="+str(h))

        solution["mustWork_count"] = 0

        mustWork, canWork = computeAssignments(solution,
                                               h,
                                               data,
                                               checkers=checkers,
                                               sumW=sumW)

        # print("h=" + str(h))
        # print("mustWork")
        # print(mustWork)
        # print("canWork")
        # print(canWork)
        # print("hini:")
        # print(hini)
        # print("demand")
        # print(data["demand"])
        # print("pending")
        # print(solution["pending"])

        #   try to assign if pending[h] > 0 and h >= hini[n]
        for n in mustWork:
            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            update_checkers(solution, data, n, h, 1, checkers)
            w[n][h] = 1
            sumW[n] += 1
            pending[h] -= 1
            if z[n] == 0:
                z[n] = 1
                solution["cost"] += 1

            #print("w[" + str(n) + "," + str(h) + "] = 1")
            # pp.pprint(solution["w"])

        for n in canWork:

            # print("nurse :" + str(n) + "  h: " + str(h) + " pending: ")
            # print(pending)
            if pending[h] + hini[h] > 0:
                update_checkers(solution, data, n, h, 1, checkers)
                w[n][h] = 1
                sumW[n] += 1
                pending[h] -= 1
                if z[n] == 0:
                    z[n] = 1
                    solution["cost"] += 1

                #print("w[" + str(n) + "," + str(h) + "] = 1")
            # print("w[" + str(n) + "]")
            # pp.pprint(solution["w"])

    # pp.pprint(data)
    # pp.pprint(solution["cost"])
    # print("")

    # compute feasibility: if unfeasible -> fitness should be inf
    if not isFeasible(solution, data):
        # assign the max cost
        #solution["cost"] = 200000 * data["nNurses"]
        solution["cost"] = 100 * solution["cost"]