Dear fellow programmers,
I want to know how to schedule activities based on availability (see input).
There are people available on days where the number of available hours is more than zero. We want our schedule to be as close to zero as possible (efficient planning). This means that when we schedule a new project for a client that takes five activities, we need our algorithm to reduce the number of available hours by the duration of the activity for that particular project day. Some activities take 0,5 hours to complete and others 1 hour to complete. I tried to formulate an initial version of the model with the variables A, B, C, D, and E as the activities in the right sequence (yet) with only one employee (called '0' since I will add more employees in the future, but I do not want to start with a too complex situation in the beginning) and the day that the activity is scheduled. For the first and fifth activity, the patient is allowed to give a preference. For the second, third and fourth activity, the activity can be scheduled any time. The availability variable is here a list of the available hours of employee AW (see input).
days = range(1,32)
preferences = [4,11,18,25]
A = LpVariable.dicts(name = "Intake", indexs = [(0, v) for v in preferences], lowBound = 0, upBound = 1, cat = "Continuous")
B = LpVariable.dicts(name = "Write a transcript of intake", indexs = [(0, d) for d in days], lowBound = 0, upBound = 1, cat = "Continuous")
C = LpVariable.dicts(name = "Create treatment plan", indexs = [(0, d) for d in days], lowBound = 0, upBound = 0.5, cat = "Continuous")
D = LpVariable.dicts(name = "Schedule a meeting", indexs = [(0, d) for d in days], lowBound = 0, upBound = 0.5, cat = "Continuous")
E = LpVariable.dicts(name = "Discuss treatment plan with patiënt", indexs = [(0, v) for v in preferences], lowBound = 0, upBound = 1, cat = "Continuous")
############################## OBJECTIVE ##############################################
# define the model to maximize the workday
schedule = LpProblem(name="Maximize_Schedule", sense=LpMaximize)
schedule += lpSum(A[(0,v)] for v in preferences) + lpSum(B[(0,d)] for d in days) + lpSum(C[(0,d)] for d in days) + lpSum(D[(0,d)] for d in days) + lpSum(E[(0,v)] for v in preferences)
############################## CONSTRAINTS ############################################
# Less than available hours
for v in preferences:
schedule += A[0,v] + B[0,v] + C[0,v] + D[0,v] + E[0,v] <= availability[v-1]
for d in days:
schedule += B[0,d] + C[0,d] + D[0,d] <= availability[d-1]
# There is no more than 4 hours to divide
schedule += lpSum(A[(0,v)] for v in preferences) + lpSum(B[(0,d)] for d in days) + lpSum(C[(0,d)] for d in days) + lpSum(D[(0,d)] for d in days) + lpSum(E[(0,v)] for v in preferences) <= 4
# Every activity must be in the solution
schedule += lpSum(A[(0,v)] for v in preferences) == 1
schedule += lpSum(B[(0,d)] for d in days) == 1
schedule += lpSum(C[(0,d)] for d in days) == 0.5
schedule += lpSum(D[(0,d)] for d in days) == 0.5
schedule += lpSum(E[(0,v)] for v in preferences) == 1
# An activity cannot be less than zero
for v in preferences:
schedule += A[(0,v)] >= 0
schedule += E[(0,v)] >= 0
for d in days:
schedule += B[(0,d)] >= 0
schedule += C[(0,d)] >= 0
schedule += D[(0,d)] >= 0
status = schedule.solve()
The model provides me with the following output:
Create_treatment_plan_(0,_4) 0.5
Discuss_treatment_plan_with_patiënt_(0,_11) 1.0
Intake_(0,_11) 1.0
Schedule_a_meeting_(0,_4) 0.5
Write_a_transcript_of_intake_(0,_4) 1.0
Already a lot of constraints are satisfied, but we can see that there is no precedence in the outcome ('intake' is planned before 'Write a transcript of intake'). I have no idea how to implement a precedence constraint in my LP since I am not working with starting time and ending time but with availability. Could you please help me looking for a solution? On the bottom of this page you can find the full version of the my LP formulation in Pulp.
Kind regards,
Tom
Maximize_Schedule:
MAXIMIZE
1*Create_treatment_plan_(0,_1) + 1*Create_treatment_plan_(0,_10) + 1*Create_treatment_plan_(0,_11) + 1*Create_treatment_plan_(0,_12) + 1*Create_treatment_plan_(0,_13) + 1*Create_treatment_plan_(0,_14) + 1*Create_treatment_plan_(0,_15) + 1*Create_treatment_plan_(0,_16) + 1*Create_treatment_plan_(0,_17) + 1*Create_treatment_plan_(0,_18) + 1*Create_treatment_plan_(0,_19) + 1*Create_treatment_plan_(0,_2) + 1*Create_treatment_plan_(0,_20) + 1*Create_treatment_plan_(0,_21) + 1*Create_treatment_plan_(0,_22) + 1*Create_treatment_plan_(0,_23) + 1*Create_treatment_plan_(0,_24) + 1*Create_treatment_plan_(0,_25) + 1*Create_treatment_plan_(0,_26) + 1*Create_treatment_plan_(0,_27) + 1*Create_treatment_plan_(0,_28) + 1*Create_treatment_plan_(0,_29) + 1*Create_treatment_plan_(0,_3) + 1*Create_treatment_plan_(0,_30) + 1*Create_treatment_plan_(0,_31) + 1*Create_treatment_plan_(0,_4) + 1*Create_treatment_plan_(0,_5) + 1*Create_treatment_plan_(0,_6) + 1*Create_treatment_plan_(0,_7) + 1*Create_treatment_plan_(0,_8) + 1*Create_treatment_plan_(0,_9) + 1*Discuss_treatment_plan_with_patiënt_(0,_11) + 1*Discuss_treatment_plan_with_patiënt_(0,_18) + 1*Discuss_treatment_plan_with_patiënt_(0,_25) + 1*Discuss_treatment_plan_with_patiënt_(0,_4) + 1*Intake_(0,_11) + 1*Intake_(0,_18) + 1*Intake_(0,_25) + 1*Intake_(0,_4) + 1*Schedule_a_meeting_(0,_1) + 1*Schedule_a_meeting_(0,_10) + 1*Schedule_a_meeting_(0,_11) + 1*Schedule_a_meeting_(0,_12) + 1*Schedule_a_meeting_(0,_13) + 1*Schedule_a_meeting_(0,_14) + 1*Schedule_a_meeting_(0,_15) + 1*Schedule_a_meeting_(0,_16) + 1*Schedule_a_meeting_(0,_17) + 1*Schedule_a_meeting_(0,_18) + 1*Schedule_a_meeting_(0,_19) + 1*Schedule_a_meeting_(0,_2) + 1*Schedule_a_meeting_(0,_20) + 1*Schedule_a_meeting_(0,_21) + 1*Schedule_a_meeting_(0,_22) + 1*Schedule_a_meeting_(0,_23) + 1*Schedule_a_meeting_(0,_24) + 1*Schedule_a_meeting_(0,_25) + 1*Schedule_a_meeting_(0,_26) + 1*Schedule_a_meeting_(0,_27) + 1*Schedule_a_meeting_(0,_28) + 1*Schedule_a_meeting_(0,_29) + 1*Schedule_a_meeting_(0,_3) + 1*Schedule_a_meeting_(0,_30) + 1*Schedule_a_meeting_(0,_31) + 1*Schedule_a_meeting_(0,_4) + 1*Schedule_a_meeting_(0,_5) + 1*Schedule_a_meeting_(0,_6) + 1*Schedule_a_meeting_(0,_7) + 1*Schedule_a_meeting_(0,_8) + 1*Schedule_a_meeting_(0,_9) + 1*Write_a_transcript_of_intake_(0,_1) + 1*Write_a_transcript_of_intake_(0,_10) + 1*Write_a_transcript_of_intake_(0,_11) + 1*Write_a_transcript_of_intake_(0,_12) + 1*Write_a_transcript_of_intake_(0,_13) + 1*Write_a_transcript_of_intake_(0,_14) + 1*Write_a_transcript_of_intake_(0,_15) + 1*Write_a_transcript_of_intake_(0,_16) + 1*Write_a_transcript_of_intake_(0,_17) + 1*Write_a_transcript_of_intake_(0,_18) + 1*Write_a_transcript_of_intake_(0,_19) + 1*Write_a_transcript_of_intake_(0,_2) + 1*Write_a_transcript_of_intake_(0,_20) + 1*Write_a_transcript_of_intake_(0,_21) + 1*Write_a_transcript_of_intake_(0,_22) + 1*Write_a_transcript_of_intake_(0,_23) + 1*Write_a_transcript_of_intake_(0,_24) + 1*Write_a_transcript_of_intake_(0,_25) + 1*Write_a_transcript_of_intake_(0,_26) + 1*Write_a_transcript_of_intake_(0,_27) + 1*Write_a_transcript_of_intake_(0,_28) + 1*Write_a_transcript_of_intake_(0,_29) + 1*Write_a_transcript_of_intake_(0,_3) + 1*Write_a_transcript_of_intake_(0,_30) + 1*Write_a_transcript_of_intake_(0,_31) + 1*Write_a_transcript_of_intake_(0,_4) + 1*Write_a_transcript_of_intake_(0,_5) + 1*Write_a_transcript_of_intake_(0,_6) + 1*Write_a_transcript_of_intake_(0,_7) + 1*Write_a_transcript_of_intake_(0,_8) + 1*Write_a_transcript_of_intake_(0,_9) + 0
SUBJECT TO
_C1: Create_treatment_plan_(0,_4) + Discuss_treatment_plan_with_patiënt_(0,_4)
+ Intake_(0,_4) + Schedule_a_meeting_(0,_4)
+ Write_a_transcript_of_intake_(0,_4) <= 3
_C2: Create_treatment_plan_(0,_11)
+ Discuss_treatment_plan_with_patiënt_(0,_11) + Intake_(0,_11)
+ Schedule_a_meeting_(0,_11) + Write_a_transcript_of_intake_(0,_11) <= 4
_C3: Create_treatment_plan_(0,_18)
+ Discuss_treatment_plan_with_patiënt_(0,_18) + Intake_(0,_18)
+ Schedule_a_meeting_(0,_18) + Write_a_transcript_of_intake_(0,_18) <= 5
_C4: Create_treatment_plan_(0,_25)
+ Discuss_treatment_plan_with_patiënt_(0,_25) + Intake_(0,_25)
+ Schedule_a_meeting_(0,_25) + Write_a_transcript_of_intake_(0,_25) <= 6
_C5: Create_treatment_plan_(0,_1) + Schedule_a_meeting_(0,_1)
+ Write_a_transcript_of_intake_(0,_1) <= 0
_C6: Create_treatment_plan_(0,_2) + Schedule_a_meeting_(0,_2)
+ Write_a_transcript_of_intake_(0,_2) <= 5
_C7: Create_treatment_plan_(0,_3) + Schedule_a_meeting_(0,_3)
+ Write_a_transcript_of_intake_(0,_3) <= 7
_C8: Create_treatment_plan_(0,_4) + Schedule_a_meeting_(0,_4)
+ Write_a_transcript_of_intake_(0,_4) <= 3
_C9: Create_treatment_plan_(0,_5) + Schedule_a_meeting_(0,_5)
+ Write_a_transcript_of_intake_(0,_5) <= 0
_C10: Create_treatment_plan_(0,_6) + Schedule_a_meeting_(0,_6)
+ Write_a_transcript_of_intake_(0,_6) <= 0
_C11: Create_treatment_plan_(0,_7) + Schedule_a_meeting_(0,_7)
+ Write_a_transcript_of_intake_(0,_7) <= 6
_C12: Create_treatment_plan_(0,_8) + Schedule_a_meeting_(0,_8)
+ Write_a_transcript_of_intake_(0,_8) <= 0
_C13: Create_treatment_plan_(0,_9) + Schedule_a_meeting_(0,_9)
+ Write_a_transcript_of_intake_(0,_9) <= 6
_C14: Create_treatment_plan_(0,_10) + Schedule_a_meeting_(0,_10)
+ Write_a_transcript_of_intake_(0,_10) <= 1
_C15: Create_treatment_plan_(0,_11) + Schedule_a_meeting_(0,_11)
+ Write_a_transcript_of_intake_(0,_11) <= 4
_C16: Create_treatment_plan_(0,_12) + Schedule_a_meeting_(0,_12)
+ Write_a_transcript_of_intake_(0,_12) <= 0
_C17: Create_treatment_plan_(0,_13) + Schedule_a_meeting_(0,_13)
+ Write_a_transcript_of_intake_(0,_13) <= 0
_C18: Create_treatment_plan_(0,_14) + Schedule_a_meeting_(0,_14)
+ Write_a_transcript_of_intake_(0,_14) <= 4
_C19: Create_treatment_plan_(0,_15) + Schedule_a_meeting_(0,_15)
+ Write_a_transcript_of_intake_(0,_15) <= 0
_C20: Create_treatment_plan_(0,_16) + Schedule_a_meeting_(0,_16)
+ Write_a_transcript_of_intake_(0,_16) <= 4
_C21: Create_treatment_plan_(0,_17) + Schedule_a_meeting_(0,_17)
+ Write_a_transcript_of_intake_(0,_17) <= 7
_C22: Create_treatment_plan_(0,_18) + Schedule_a_meeting_(0,_18)
+ Write_a_transcript_of_intake_(0,_18) <= 5
_C23: Create_treatment_plan_(0,_19) + Schedule_a_meeting_(0,_19)
+ Write_a_transcript_of_intake_(0,_19) <= 0
_C24: Create_treatment_plan_(0,_20) + Schedule_a_meeting_(0,_20)
+ Write_a_transcript_of_intake_(0,_20) <= 0
_C25: Create_treatment_plan_(0,_21) + Schedule_a_meeting_(0,_21)
+ Write_a_transcript_of_intake_(0,_21) <= 4
_C26: Create_treatment_plan_(0,_22) + Schedule_a_meeting_(0,_22)
+ Write_a_transcript_of_intake_(0,_22) <= 0
_C27: Create_treatment_plan_(0,_23) + Schedule_a_meeting_(0,_23)
+ Write_a_transcript_of_intake_(0,_23) <= 4
_C28: Create_treatment_plan_(0,_24) + Schedule_a_meeting_(0,_24)
+ Write_a_transcript_of_intake_(0,_24) <= 7
_C29: Create_treatment_plan_(0,_25) + Schedule_a_meeting_(0,_25)
+ Write_a_transcript_of_intake_(0,_25) <= 6
_C30: Create_treatment_plan_(0,_26) + Schedule_a_meeting_(0,_26)
+ Write_a_transcript_of_intake_(0,_26) <= 0
_C31: Create_treatment_plan_(0,_27) + Schedule_a_meeting_(0,_27)
+ Write_a_transcript_of_intake_(0,_27) <= 0
_C32: Create_treatment_plan_(0,_28) + Schedule_a_meeting_(0,_28)
+ Write_a_transcript_of_intake_(0,_28) <= 4
_C33: Create_treatment_plan_(0,_29) + Schedule_a_meeting_(0,_29)
+ Write_a_transcript_of_intake_(0,_29) <= 0
_C34: Create_treatment_plan_(0,_30) + Schedule_a_meeting_(0,_30)
+ Write_a_transcript_of_intake_(0,_30) <= 5
_C35: Create_treatment_plan_(0,_31) + Schedule_a_meeting_(0,_31)
+ Write_a_transcript_of_intake_(0,_31) <= 7
_C36: Create_treatment_plan_(0,_1) + Create_treatment_plan_(0,_10)
+ Create_treatment_plan_(0,_11) + Create_treatment_plan_(0,_12)
+ Create_treatment_plan_(0,_13) + Create_treatment_plan_(0,_14)
+ Create_treatment_plan_(0,_15) + Create_treatment_plan_(0,_16)
+ Create_treatment_plan_(0,_17) + Create_treatment_plan_(0,_18)
+ Create_treatment_plan_(0,_19) + Create_treatment_plan_(0,_2)
+ Create_treatment_plan_(0,_20) + Create_treatment_plan_(0,_21)
+ Create_treatment_plan_(0,_22) + Create_treatment_plan_(0,_23)
+ Create_treatment_plan_(0,_24) + Create_treatment_plan_(0,_25)
+ Create_treatment_plan_(0,_26) + Create_treatment_plan_(0,_27)
+ Create_treatment_plan_(0,_28) + Create_treatment_plan_(0,_29)
+ Create_treatment_plan_(0,_3) + Create_treatment_plan_(0,_30)
+ Create_treatment_plan_(0,_31) + Create_treatment_plan_(0,_4)
+ Create_treatment_plan_(0,_5) + Create_treatment_plan_(0,_6)
+ Create_treatment_plan_(0,_7) + Create_treatment_plan_(0,_8)
+ Create_treatment_plan_(0,_9) + Discuss_treatment_plan_with_patiënt_(0,_11)
+ Discuss_treatment_plan_with_patiënt_(0,_18)
+ Discuss_treatment_plan_with_patiënt_(0,_25)
+ Discuss_treatment_plan_with_patiënt_(0,_4) + Intake_(0,_11)
+ Intake_(0,_18) + Intake_(0,_25) + Intake_(0,_4) + Schedule_a_meeting_(0,_1)
+ Schedule_a_meeting_(0,_10) + Schedule_a_meeting_(0,_11)
+ Schedule_a_meeting_(0,_12) + Schedule_a_meeting_(0,_13)
+ Schedule_a_meeting_(0,_14) + Schedule_a_meeting_(0,_15)
+ Schedule_a_meeting_(0,_16) + Schedule_a_meeting_(0,_17)
+ Schedule_a_meeting_(0,_18) + Schedule_a_meeting_(0,_19)
+ Schedule_a_meeting_(0,_2) + Schedule_a_meeting_(0,_20)
+ Schedule_a_meeting_(0,_21) + Schedule_a_meeting_(0,_22)
+ Schedule_a_meeting_(0,_23) + Schedule_a_meeting_(0,_24)
+ Schedule_a_meeting_(0,_25) + Schedule_a_meeting_(0,_26)
+ Schedule_a_meeting_(0,_27) + Schedule_a_meeting_(0,_28)
+ Schedule_a_meeting_(0,_29) + Schedule_a_meeting_(0,_3)
+ Schedule_a_meeting_(0,_30) + Schedule_a_meeting_(0,_31)
+ Schedule_a_meeting_(0,_4) + Schedule_a_meeting_(0,_5)
+ Schedule_a_meeting_(0,_6) + Schedule_a_meeting_(0,_7)
+ Schedule_a_meeting_(0,_8) + Schedule_a_meeting_(0,_9)
+ Write_a_transcript_of_intake_(0,_1) + Write_a_transcript_of_intake_(0,_10)
+ Write_a_transcript_of_intake_(0,_11) + Write_a_transcript_of_intake_(0,_12)
+ Write_a_transcript_of_intake_(0,_13) + Write_a_transcript_of_intake_(0,_14)
+ Write_a_transcript_of_intake_(0,_15) + Write_a_transcript_of_intake_(0,_16)
+ Write_a_transcript_of_intake_(0,_17) + Write_a_transcript_of_intake_(0,_18)
+ Write_a_transcript_of_intake_(0,_19) + Write_a_transcript_of_intake_(0,_2)
+ Write_a_transcript_of_intake_(0,_20) + Write_a_transcript_of_intake_(0,_21)
+ Write_a_transcript_of_intake_(0,_22) + Write_a_transcript_of_intake_(0,_23)
+ Write_a_transcript_of_intake_(0,_24) + Write_a_transcript_of_intake_(0,_25)
+ Write_a_transcript_of_intake_(0,_26) + Write_a_transcript_of_intake_(0,_27)
+ Write_a_transcript_of_intake_(0,_28) + Write_a_transcript_of_intake_(0,_29)
+ Write_a_transcript_of_intake_(0,_3) + Write_a_transcript_of_intake_(0,_30)
+ Write_a_transcript_of_intake_(0,_31) + Write_a_transcript_of_intake_(0,_4)
+ Write_a_transcript_of_intake_(0,_5) + Write_a_transcript_of_intake_(0,_6)
+ Write_a_transcript_of_intake_(0,_7) + Write_a_transcript_of_intake_(0,_8)
+ Write_a_transcript_of_intake_(0,_9) <= 4
_C37: Intake_(0,_11) + Intake_(0,_18) + Intake_(0,_25) + Intake_(0,_4) = 1
_C38: Write_a_transcript_of_intake_(0,_1)
+ Write_a_transcript_of_intake_(0,_10) + Write_a_transcript_of_intake_(0,_11)
+ Write_a_transcript_of_intake_(0,_12) + Write_a_transcript_of_intake_(0,_13)
+ Write_a_transcript_of_intake_(0,_14) + Write_a_transcript_of_intake_(0,_15)
+ Write_a_transcript_of_intake_(0,_16) + Write_a_transcript_of_intake_(0,_17)
+ Write_a_transcript_of_intake_(0,_18) + Write_a_transcript_of_intake_(0,_19)
+ Write_a_transcript_of_intake_(0,_2) + Write_a_transcript_of_intake_(0,_20)
+ Write_a_transcript_of_intake_(0,_21) + Write_a_transcript_of_intake_(0,_22)
+ Write_a_transcript_of_intake_(0,_23) + Write_a_transcript_of_intake_(0,_24)
+ Write_a_transcript_of_intake_(0,_25) + Write_a_transcript_of_intake_(0,_26)
+ Write_a_transcript_of_intake_(0,_27) + Write_a_transcript_of_intake_(0,_28)
+ Write_a_transcript_of_intake_(0,_29) + Write_a_transcript_of_intake_(0,_3)
+ Write_a_transcript_of_intake_(0,_30) + Write_a_transcript_of_intake_(0,_31)
+ Write_a_transcript_of_intake_(0,_4) + Write_a_transcript_of_intake_(0,_5)
+ Write_a_transcript_of_intake_(0,_6) + Write_a_transcript_of_intake_(0,_7)
+ Write_a_transcript_of_intake_(0,_8) + Write_a_transcript_of_intake_(0,_9)
= 1
_C39: Create_treatment_plan_(0,_1) + Create_treatment_plan_(0,_10)
+ Create_treatment_plan_(0,_11) + Create_treatment_plan_(0,_12)
+ Create_treatment_plan_(0,_13) + Create_treatment_plan_(0,_14)
+ Create_treatment_plan_(0,_15) + Create_treatment_plan_(0,_16)
+ Create_treatment_plan_(0,_17) + Create_treatment_plan_(0,_18)
+ Create_treatment_plan_(0,_19) + Create_treatment_plan_(0,_2)
+ Create_treatment_plan_(0,_20) + Create_treatment_plan_(0,_21)
+ Create_treatment_plan_(0,_22) + Create_treatment_plan_(0,_23)
+ Create_treatment_plan_(0,_24) + Create_treatment_plan_(0,_25)
+ Create_treatment_plan_(0,_26) + Create_treatment_plan_(0,_27)
+ Create_treatment_plan_(0,_28) + Create_treatment_plan_(0,_29)
+ Create_treatment_plan_(0,_3) + Create_treatment_plan_(0,_30)
+ Create_treatment_plan_(0,_31) + Create_treatment_plan_(0,_4)
+ Create_treatment_plan_(0,_5) + Create_treatment_plan_(0,_6)
+ Create_treatment_plan_(0,_7) + Create_treatment_plan_(0,_8)
+ Create_treatment_plan_(0,_9) = 0.5
_C40: Schedule_a_meeting_(0,_1) + Schedule_a_meeting_(0,_10)
+ Schedule_a_meeting_(0,_11) + Schedule_a_meeting_(0,_12)
+ Schedule_a_meeting_(0,_13) + Schedule_a_meeting_(0,_14)
+ Schedule_a_meeting_(0,_15) + Schedule_a_meeting_(0,_16)
+ Schedule_a_meeting_(0,_17) + Schedule_a_meeting_(0,_18)
+ Schedule_a_meeting_(0,_19) + Schedule_a_meeting_(0,_2)
+ Schedule_a_meeting_(0,_20) + Schedule_a_meeting_(0,_21)
+ Schedule_a_meeting_(0,_22) + Schedule_a_meeting_(0,_23)
+ Schedule_a_meeting_(0,_24) + Schedule_a_meeting_(0,_25)
+ Schedule_a_meeting_(0,_26) + Schedule_a_meeting_(0,_27)
+ Schedule_a_meeting_(0,_28) + Schedule_a_meeting_(0,_29)
+ Schedule_a_meeting_(0,_3) + Schedule_a_meeting_(0,_30)
+ Schedule_a_meeting_(0,_31) + Schedule_a_meeting_(0,_4)
+ Schedule_a_meeting_(0,_5) + Schedule_a_meeting_(0,_6)
+ Schedule_a_meeting_(0,_7) + Schedule_a_meeting_(0,_8)
+ Schedule_a_meeting_(0,_9) = 0.5
_C41: Discuss_treatment_plan_with_patiënt_(0,_11)
+ Discuss_treatment_plan_with_patiënt_(0,_18)
+ Discuss_treatment_plan_with_patiënt_(0,_25)
+ Discuss_treatment_plan_with_patiënt_(0,_4) = 1
_C42: Intake_(0,_4) >= 0
_C43: Discuss_treatment_plan_with_patiënt_(0,_4) >= 0
_C44: Intake_(0,_11) >= 0
_C45: Discuss_treatment_plan_with_patiënt_(0,_11) >= 0
_C46: Intake_(0,_18) >= 0
_C47: Discuss_treatment_plan_with_patiënt_(0,_18) >= 0
_C48: Intake_(0,_25) >= 0
_C49: Discuss_treatment_plan_with_patiënt_(0,_25) >= 0
_C50: Write_a_transcript_of_intake_(0,_1) >= 0
_C51: Create_treatment_plan_(0,_1) >= 0
_C52: Schedule_a_meeting_(0,_1) >= 0
_C53: Write_a_transcript_of_intake_(0,_2) >= 0
_C54: Create_treatment_plan_(0,_2) >= 0
_C55: Schedule_a_meeting_(0,_2) >= 0
_C56: Write_a_transcript_of_intake_(0,_3) >= 0
_C57: Create_treatment_plan_(0,_3) >= 0
_C58: Schedule_a_meeting_(0,_3) >= 0
_C59: Write_a_transcript_of_intake_(0,_4) >= 0
_C60: Create_treatment_plan_(0,_4) >= 0
_C61: Schedule_a_meeting_(0,_4) >= 0
_C62: Write_a_transcript_of_intake_(0,_5) >= 0
_C63: Create_treatment_plan_(0,_5) >= 0
_C64: Schedule_a_meeting_(0,_5) >= 0
_C65: Write_a_transcript_of_intake_(0,_6) >= 0
_C66: Create_treatment_plan_(0,_6) >= 0
_C67: Schedule_a_meeting_(0,_6) >= 0
_C68: Write_a_transcript_of_intake_(0,_7) >= 0
_C69: Create_treatment_plan_(0,_7) >= 0
_C70: Schedule_a_meeting_(0,_7) >= 0
_C71: Write_a_transcript_of_intake_(0,_8) >= 0
_C72: Create_treatment_plan_(0,_8) >= 0
_C73: Schedule_a_meeting_(0,_8) >= 0
_C74: Write_a_transcript_of_intake_(0,_9) >= 0
_C75: Create_treatment_plan_(0,_9) >= 0
_C76: Schedule_a_meeting_(0,_9) >= 0
_C77: Write_a_transcript_of_intake_(0,_10) >= 0
_C78: Create_treatment_plan_(0,_10) >= 0
_C79: Schedule_a_meeting_(0,_10) >= 0
_C80: Write_a_transcript_of_intake_(0,_11) >= 0
_C81: Create_treatment_plan_(0,_11) >= 0
_C82: Schedule_a_meeting_(0,_11) >= 0
_C83: Write_a_transcript_of_intake_(0,_12) >= 0
_C84: Create_treatment_plan_(0,_12) >= 0
_C85: Schedule_a_meeting_(0,_12) >= 0
_C86: Write_a_transcript_of_intake_(0,_13) >= 0
_C87: Create_treatment_plan_(0,_13) >= 0
_C88: Schedule_a_meeting_(0,_13) >= 0
_C89: Write_a_transcript_of_intake_(0,_14) >= 0
_C90: Create_treatment_plan_(0,_14) >= 0
_C91: Schedule_a_meeting_(0,_14) >= 0
_C92: Write_a_transcript_of_intake_(0,_15) >= 0
_C93: Create_treatment_plan_(0,_15) >= 0
_C94: Schedule_a_meeting_(0,_15) >= 0
_C95: Write_a_transcript_of_intake_(0,_16) >= 0
_C96: Create_treatment_plan_(0,_16) >= 0
_C97: Schedule_a_meeting_(0,_16) >= 0
_C98: Write_a_transcript_of_intake_(0,_17) >= 0
_C99: Create_treatment_plan_(0,_17) >= 0
_C100: Schedule_a_meeting_(0,_17) >= 0
_C101: Write_a_transcript_of_intake_(0,_18) >= 0
_C102: Create_treatment_plan_(0,_18) >= 0
_C103: Schedule_a_meeting_(0,_18) >= 0
_C104: Write_a_transcript_of_intake_(0,_19) >= 0
_C105: Create_treatment_plan_(0,_19) >= 0
_C106: Schedule_a_meeting_(0,_19) >= 0
_C107: Write_a_transcript_of_intake_(0,_20) >= 0
_C108: Create_treatment_plan_(0,_20) >= 0
_C109: Schedule_a_meeting_(0,_20) >= 0
_C110: Write_a_transcript_of_intake_(0,_21) >= 0
_C111: Create_treatment_plan_(0,_21) >= 0
_C112: Schedule_a_meeting_(0,_21) >= 0
_C113: Write_a_transcript_of_intake_(0,_22) >= 0
_C114: Create_treatment_plan_(0,_22) >= 0
_C115: Schedule_a_meeting_(0,_22) >= 0
_C116: Write_a_transcript_of_intake_(0,_23) >= 0
_C117: Create_treatment_plan_(0,_23) >= 0
_C118: Schedule_a_meeting_(0,_23) >= 0
_C119: Write_a_transcript_of_intake_(0,_24) >= 0
_C120: Create_treatment_plan_(0,_24) >= 0
_C121: Schedule_a_meeting_(0,_24) >= 0
_C122: Write_a_transcript_of_intake_(0,_25) >= 0
_C123: Create_treatment_plan_(0,_25) >= 0
_C124: Schedule_a_meeting_(0,_25) >= 0
_C125: Write_a_transcript_of_intake_(0,_26) >= 0
_C126: Create_treatment_plan_(0,_26) >= 0
_C127: Schedule_a_meeting_(0,_26) >= 0
_C128: Write_a_transcript_of_intake_(0,_27) >= 0
_C129: Create_treatment_plan_(0,_27) >= 0
_C130: Schedule_a_meeting_(0,_27) >= 0
_C131: Write_a_transcript_of_intake_(0,_28) >= 0
_C132: Create_treatment_plan_(0,_28) >= 0
_C133: Schedule_a_meeting_(0,_28) >= 0
_C134: Write_a_transcript_of_intake_(0,_29) >= 0
_C135: Create_treatment_plan_(0,_29) >= 0
_C136: Schedule_a_meeting_(0,_29) >= 0
_C137: Write_a_transcript_of_intake_(0,_30) >= 0
_C138: Create_treatment_plan_(0,_30) >= 0
_C139: Schedule_a_meeting_(0,_30) >= 0
_C140: Write_a_transcript_of_intake_(0,_31) >= 0
_C141: Create_treatment_plan_(0,_31) >= 0
_C142: Schedule_a_meeting_(0,_31) >= 0
VARIABLES
Create_treatment_plan_(0,_1) <= 0.5 Continuous
Create_treatment_plan_(0,_10) <= 0.5 Continuous
Create_treatment_plan_(0,_11) <= 0.5 Continuous
Create_treatment_plan_(0,_12) <= 0.5 Continuous
Create_treatment_plan_(0,_13) <= 0.5 Continuous
Create_treatment_plan_(0,_14) <= 0.5 Continuous
Create_treatment_plan_(0,_15) <= 0.5 Continuous
Create_treatment_plan_(0,_16) <= 0.5 Continuous
Create_treatment_plan_(0,_17) <= 0.5 Continuous
Create_treatment_plan_(0,_18) <= 0.5 Continuous
Create_treatment_plan_(0,_19) <= 0.5 Continuous
Create_treatment_plan_(0,_2) <= 0.5 Continuous
Create_treatment_plan_(0,_20) <= 0.5 Continuous
Create_treatment_plan_(0,_21) <= 0.5 Continuous
Create_treatment_plan_(0,_22) <= 0.5 Continuous
Create_treatment_plan_(0,_23) <= 0.5 Continuous
Create_treatment_plan_(0,_24) <= 0.5 Continuous
Create_treatment_plan_(0,_25) <= 0.5 Continuous
Create_treatment_plan_(0,_26) <= 0.5 Continuous
Create_treatment_plan_(0,_27) <= 0.5 Continuous
Create_treatment_plan_(0,_28) <= 0.5 Continuous
Create_treatment_plan_(0,_29) <= 0.5 Continuous
Create_treatment_plan_(0,_3) <= 0.5 Continuous
Create_treatment_plan_(0,_30) <= 0.5 Continuous
Create_treatment_plan_(0,_31) <= 0.5 Continuous
Create_treatment_plan_(0,_4) <= 0.5 Continuous
Create_treatment_plan_(0,_5) <= 0.5 Continuous
Create_treatment_plan_(0,_6) <= 0.5 Continuous
Create_treatment_plan_(0,_7) <= 0.5 Continuous
Create_treatment_plan_(0,_8) <= 0.5 Continuous
Create_treatment_plan_(0,_9) <= 0.5 Continuous
Discuss_treatment_plan_with_patiënt_(0,_11) <= 1 Continuous
Discuss_treatment_plan_with_patiënt_(0,_18) <= 1 Continuous
Discuss_treatment_plan_with_patiënt_(0,_25) <= 1 Continuous
Discuss_treatment_plan_with_patiënt_(0,_4) <= 1 Continuous
Intake_(0,_11) <= 1 Continuous
Intake_(0,_18) <= 1 Continuous
Intake_(0,_25) <= 1 Continuous
Intake_(0,_4) <= 1 Continuous
Schedule_a_meeting_(0,_1) <= 0.5 Continuous
Schedule_a_meeting_(0,_10) <= 0.5 Continuous
Schedule_a_meeting_(0,_11) <= 0.5 Continuous
Schedule_a_meeting_(0,_12) <= 0.5 Continuous
Schedule_a_meeting_(0,_13) <= 0.5 Continuous
Schedule_a_meeting_(0,_14) <= 0.5 Continuous
Schedule_a_meeting_(0,_15) <= 0.5 Continuous
Schedule_a_meeting_(0,_16) <= 0.5 Continuous
Schedule_a_meeting_(0,_17) <= 0.5 Continuous
Schedule_a_meeting_(0,_18) <= 0.5 Continuous
Schedule_a_meeting_(0,_19) <= 0.5 Continuous
Schedule_a_meeting_(0,_2) <= 0.5 Continuous
Schedule_a_meeting_(0,_20) <= 0.5 Continuous
Schedule_a_meeting_(0,_21) <= 0.5 Continuous
Schedule_a_meeting_(0,_22) <= 0.5 Continuous
Schedule_a_meeting_(0,_23) <= 0.5 Continuous
Schedule_a_meeting_(0,_24) <= 0.5 Continuous
Schedule_a_meeting_(0,_25) <= 0.5 Continuous
Schedule_a_meeting_(0,_26) <= 0.5 Continuous
Schedule_a_meeting_(0,_27) <= 0.5 Continuous
Schedule_a_meeting_(0,_28) <= 0.5 Continuous
Schedule_a_meeting_(0,_29) <= 0.5 Continuous
Schedule_a_meeting_(0,_3) <= 0.5 Continuous
Schedule_a_meeting_(0,_30) <= 0.5 Continuous
Schedule_a_meeting_(0,_31) <= 0.5 Continuous
Schedule_a_meeting_(0,_4) <= 0.5 Continuous
Schedule_a_meeting_(0,_5) <= 0.5 Continuous
Schedule_a_meeting_(0,_6) <= 0.5 Continuous
Schedule_a_meeting_(0,_7) <= 0.5 Continuous
Schedule_a_meeting_(0,_8) <= 0.5 Continuous
Schedule_a_meeting_(0,_9) <= 0.5 Continuous
Write_a_transcript_of_intake_(0,_1) <= 1 Continuous
Write_a_transcript_of_intake_(0,_10) <= 1 Continuous
Write_a_transcript_of_intake_(0,_11) <= 1 Continuous
Write_a_transcript_of_intake_(0,_12) <= 1 Continuous
Write_a_transcript_of_intake_(0,_13) <= 1 Continuous
Write_a_transcript_of_intake_(0,_14) <= 1 Continuous
Write_a_transcript_of_intake_(0,_15) <= 1 Continuous
Write_a_transcript_of_intake_(0,_16) <= 1 Continuous
Write_a_transcript_of_intake_(0,_17) <= 1 Continuous
Write_a_transcript_of_intake_(0,_18) <= 1 Continuous
Write_a_transcript_of_intake_(0,_19) <= 1 Continuous
Write_a_transcript_of_intake_(0,_2) <= 1 Continuous
Write_a_transcript_of_intake_(0,_20) <= 1 Continuous
Write_a_transcript_of_intake_(0,_21) <= 1 Continuous
Write_a_transcript_of_intake_(0,_22) <= 1 Continuous
Write_a_transcript_of_intake_(0,_23) <= 1 Continuous
Write_a_transcript_of_intake_(0,_24) <= 1 Continuous
Write_a_transcript_of_intake_(0,_25) <= 1 Continuous
Write_a_transcript_of_intake_(0,_26) <= 1 Continuous
Write_a_transcript_of_intake_(0,_27) <= 1 Continuous
Write_a_transcript_of_intake_(0,_28) <= 1 Continuous
Write_a_transcript_of_intake_(0,_29) <= 1 Continuous
Write_a_transcript_of_intake_(0,_3) <= 1 Continuous
Write_a_transcript_of_intake_(0,_30) <= 1 Continuous
Write_a_transcript_of_intake_(0,_31) <= 1 Continuous
Write_a_transcript_of_intake_(0,_4) <= 1 Continuous
Write_a_transcript_of_intake_(0,_5) <= 1 Continuous
Write_a_transcript_of_intake_(0,_6) <= 1 Continuous
Write_a_transcript_of_intake_(0,_7) <= 1 Continuous
Write_a_transcript_of_intake_(0,_8) <= 1 Continuous
Write_a_transcript_of_intake_(0,_9) <= 1 Continuous
With a time-indexed variable x[t], y[t] ∈ {0,1}, I often also keep scalar variables xt, yt ≥ 0 around which hold the time for x,y:
xt = sum{ t*x[t] }
yt = sum{ t*y[t] }
Of course this assumes we have:
sum{ x[t] } = 1
sum{ y[t] } = 1
i.e. x,y happen at exactly one time (it also works with "at most one time" as long as the first t is greater than zero).
With this you can easily impose a precedence constraint:
xt ≤ yt-1
I am programming a vehicle routing problem in Python with PuLP. I got all my code in it, but for some reason I get a negative value for one of my decision variables, even though I restricted all of them to be nonnegative.
My code is as follows (Traveltimes is a two dimensional np array, with travel times between each pair of customers (i,j), where c(i,j) = c(j,i) and c(i,i) = 0.):
My code:
numVehicles = 2
numCustomers = 2
prob = LpProblem("DSP", LpMinimize)
var = [[[0 for k in range(numVehicles)] for j in range(numCustomers+1)] for i in range(numCustomers+1)]
for i in range(numCustomers+1):
for j in range(numCustomers+1):
for k in range(numVehicles):
var[i][j][k] = LpVariable("x"+str(i)+","+str(j)+","+str(k), 0,1, cat='Binary')
# ADD OBJECTIVE
obj = ""
for i in range(numCustomers+1):
for j in range(numCustomers+1):
for k in range(numVehicles):
obj += traveltimes[i][j]*var[i][j][k]
prob += obj
# ADD CONSTRAINTS
# All customers visited
for j in range(numCustomers+1):
for k in range(numVehicles):
nr = ""
for i in range(numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Enter each customer exactly once
for i in range(numCustomers+1):
nr = ""
for k in range(numVehicles):
for j in range(1, numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Leave each customer exactly once
for j in range(numCustomers+1):
nr = ""
for k in range(numVehicles):
for i in range(1, numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Per vehicle only one customer can be visited as first
nrFirst = ""
for k in range(numVehicles):
for j in range(numCustomers+1):
nrFirst += var[0][j][k]
prob += nrFirst <= 1
# Max num vehicles
nrOut = ""
for k in range(numVehicles):
for j in range(numCustomers+1):
nrOut += var[0][j][k]
prob += nrOut <= numVehicles
# Restrict x(0,j,k) to be nonpositive
for j in range(numCustomers+1):
for k in range(numVehicles):
prob += var[0][j][k] >= 0
print(prob)
# Solve LP
prob.solve()
for v in prob.variables():
print(v.name, "=", v.varValue)
print("objective=", value(prob.objective))
The first output is the formulation printed
MINIMIZE
1.731*x0,1,0 + 1.731*x0,1,1 + 2.983*x0,2,0 + 2.983*x0,2,1 + 1.731*x1,0,0 + 1.731*x1,0,1 + 9.375*x1,2,0 + 9.375*x1,2,1 + 2.983*x2,0,0 + 2.983*x2,0,1 + 9.375*x2,1,0 + 9.375*x2,1,1 + 0.0
SUBJECT TO
_C1: x0,0,0 + x1,0,0 + x2,0,0 = 1
_C2: x0,0,1 + x1,0,1 + x2,0,1 = 1
_C3: x0,1,0 + x1,1,0 + x2,1,0 = 1
_C4: x0,1,1 + x1,1,1 + x2,1,1 = 1
_C5: x0,2,0 + x1,2,0 + x2,2,0 = 1
_C6: x0,2,1 + x1,2,1 + x2,2,1 = 1
_C7: x0,1,0 + x0,1,1 + x0,2,0 + x0,2,1 <= 1
_C8: x1,1,0 + x1,1,1 + x1,2,0 + x1,2,1 <= 1
_C9: x2,1,0 + x2,1,1 + x2,2,0 + x2,2,1 <= 1
_C10: x0,0,0 + x0,1,0 + x0,2,0 <= 1
_C11: x0,0,0 + x0,0,1 + x0,1,0 + x0,1,1 + x0,2,0 + x0,2,1 <= 1
VARIABLES
0 <= x0,0,0 <= 1 Integer
0 <= x0,0,1 <= 1 Integer
0 <= x0,1,0 <= 1 Integer
0 <= x0,1,1 <= 1 Integer
0 <= x0,2,0 <= 1 Integer
0 <= x0,2,1 <= 1 Integer
0 <= x1,0,0 <= 1 Integer
0 <= x1,0,1 <= 1 Integer
0 <= x1,1,0 <= 1 Integer
0 <= x1,1,1 <= 1 Integer
0 <= x1,2,0 <= 1 Integer
0 <= x1,2,1 <= 1 Integer
0 <= x2,0,0 <= 1 Integer
0 <= x2,0,1 <= 1 Integer
0 <= x2,1,0 <= 1 Integer
0 <= x2,1,1 <= 1 Integer
0 <= x2,2,0 <= 1 Integer
0 <= x2,2,1 <= 1 Integer
It can clearly be observed that all variables are restricted to be an integer between 0 and 1 (thus binary). However, for some reason, I do get negative values for some variable(s), as can be seen below
x0,0,0 = 0.0
x0,0,1 = -1.0
x0,1,0 = 0.0
x0,1,1 = 1.0
x0,2,0 = 0.0
x0,2,1 = 1.0
x1,0,0 = 1.0
x1,0,1 = 1.0
x1,1,0 = 1.0
x1,1,1 = 0.0
x1,2,0 = 0.0
x1,2,1 = 0.0
x2,0,0 = 0.0
x2,0,1 = 1.0
x2,1,0 = 0.0
x2,1,1 = 0.0
x2,2,0 = 1.0
x2,2,1 = 0.0
objective= 11.159
Really looking forward to any suggestions on how to solve this problem, since I clearly do not want negative values!
As a few others have suggested you should write a Minimum Complete and Verifiable Example.
That said, if you are getting constraints violated, and you are sure you've implemented them correctly, I reckon you have an infeasible problem (i.e. if you looked at your constraints carefully you would find there is a combination which makes solving impossible).
To check this add:
print (("Status:"), LpStatus[prob.status])
Just after you do prob.solve(). I reckon you'll find it's infeasible.
prob += nr == 1
"+=" is for assignment
"==" is checking for equivalence, and belongs in an "if" statement or a "while".
For instance:
if prob + nr == 1: #execute what follows if prob + nr is equal to 1