Global Variable using while - python

I am trying to increment a global variable in my code but when I use the key word global it says n has already been used. I am trying to increment n so I can assign each person 1 through 27 to a team.
Thank in advance
my_team = 27 % 4
team_1 = ""
team_2 = ""
team_3 = ""
team_4 = ""
team_5 = ""
team_6 = ""
team_7 = ""
print(my_team)
global n
n = 1
for n in range(1, 28):
while n <= 4 :
global n
team_1 = team_1 + str(n) + " "
n = n + 1
if n == 5:
break
for n in range (4,8):
n= n + 1
team_2 = team_2 + str(n)
while n < 13 and n > 8:
team_3 =team_3 + str(n)
n= n + 1
while n < 17 and n > 12:
team_4 = team_4 + str(n)
n= n + 1
while n < 21 and n > 16:
team_5 = team_5 + str(n)
n= n + 1
while n < 25 and n > 20:
team_6 = team_6 +str(n)
n= n + 1
while n < 28 and n > 24:
team_7 = team_7 + str(n)
n = n+1
print(team_1)

my_team = 27 % 4
team_1 = ""
team_2 = ""
team_3 = ""
team_4 = ""
team_5 = ""
team_6 = ""
team_7 = ""
print(my_team)
n = 1
while n <= 4:
team_1 += str(n) + " "
n += 1
while 4 < n <= 8:
team_2 += str(n) + " "
n += 1
while 8 < n <= 12:
team_3 += str(n) + " "
n += 1
while 12 < n <= 16:
team_4 += str(n) + " "
n += 1
while 16 < n <= 21:
team_5 += str(n) + " "
n += 1
while 21 < n <= 25:
team_6 += str(n) + " "
n += 1
while 25 < n <= 28:
team_7 += str(n) + " "
n += 1
print(team_1)
print(team_2)
print(team_3)
print(team_4)
print(team_5)
print(team_6)
print(team_7)
I've fixed some other problems according to what I think is your goal and made a few other changes which you might be able to learn from.

Related

How to add precedence constraints to LP when time is used as an index in a Pulp variable?

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

How to make a Number/Letter interval in Python

So ...
Am writing a code that makes a Matrix table then calculate how many ( uppercase Letter , Lowercase Letter , Numbers , Symbols )
That's the code i tried :
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if T[i] in ["A","Z"]:
Nb_Maj = Nb_Maj + 1
elif T[i] in ["a","z"] :
Nb_Min = Nb_Min + 1
elif T[i] in range(1,9):
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)
So the Output should be like that :
Nb_Maj= ...
Nb_Min= ...
Nb_chiffre= ...
Nb_symbole= ...
The Problem is on the part of intervals Like ["A","Z"]
Strings have some functions you can use to check what they contain
.isalpha() is true for letters
.isnumeric() is true for numbers
.isalnum() is true for letters and numbers
.isupper() is true for uppercase
Thus you could do something like
if T[i].isalpha():
if T[i].isupper():
Nb_Maj += 1
else:
Nb_Min += 1
elif T[i].isnumeric():
Nb_chiffre += 1
else:
Nb_symbole += 1
Yes it is , here is the whlole code if that would help :
from math import*
def Proc_saisie():
X = -1
while X < 1 or X > 20 :
X = int(input("Donner un entier entre 5 et 20 : "))
return X
def Proc_Remplir(P,X):
T= [[] for i in range(P)]
for i in range(P):
for j in range(X):
d = input("T["+str(i)+","+str(j)+"]=")
T[i].append(d)
return T
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if T[i] in ["A","Z"]:
Nb_Maj = Nb_Maj + 1
elif T[i] in ["a","z"] :
Nb_Min = Nb_Min + 1
elif T[i] in range(1,9):
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)
#---------------------------
L = Proc_saisie()
C = Proc_saisie()
print("L =",L)
print("C =",C)
TAB = []
TAB = Proc_Remplir(L,C)
TAB = Proc_Affiche(TAB,L,C)
I'm not sure I understand what you want 100%, but I think something like follows would fit:
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if "A" <= T[i][j] <= "Z":
Nb_Maj = Nb_Maj + 1
elif "a" <= T[i][j] <= "z" :
Nb_Min = Nb_Min + 1
elif 1 <= T[i][j] <= 9:
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)

Variable takes negative value while it is restricted to be nonnegative

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

Why does my while loop only run once?

I want to make this loop run more than once but it will only run once and then stop. What am I doing wrong?
a = 0
Let = input("Enter a sentence to decrypt : ")
Num = 0
b = 0
while b < 26 :
Num = Num + 1
num = int(Num)
while a < len(Let) :
if ord(Let[a]) > 96 and ord(Let[a]) < 123 and ord(Let[a]) + Num > 96 and ord(Let[a]) + Num < 123 :
print(chr(ord(Let[a]) + Num))
elif ord(Let[a]) > 64 and ord(Let[a]) < 91 and ord(Let[a]) + Num > 64 and ord(Let[a]) + Num < 91 :
print(chr(ord(Let[a]) + Num))
elif ord(Let[a]) > 96 and ord(Let[a]) < 123 and ord(Let[a]) + Num >= 123 :
while ord(Let[a]) + num >= 123 :
num = Num - 26
print(chr(ord(Let[a]) + num))
elif ord(Let[a]) > 64 and ord(Let[a]) < 91 and ord(Let[a]) + Num >= 91 :
while ord(Let[a]) + num >= 91 :
num = Num - 26
print(chr(ord(Let[a]) + num))
a = a + 1
b = b + 1
"a" is not being reset to 0 in the "b" loop:
Let = input("Enter a sentence to decrypt : ")
Num = 0
b = 0
while b < 26 :
Num = Num + 1
num = int(Num)
a = 0
out = ""
while a < len(Let) :
if ord(Let[a]) > 96 and ord(Let[a]) < 123 and ord(Let[a]) + Num > 96 and ord(Let[a]) + Num < 123 :
out += (chr(ord(Let[a]) + Num))
elif ord(Let[a]) > 64 and ord(Let[a]) < 91 and ord(Let[a]) + Num > 64 and ord(Let[a]) + Num < 91 :
out += (chr(ord(Let[a]) + Num))
elif ord(Let[a]) > 96 and ord(Let[a]) < 123 and ord(Let[a]) + Num >= 123 :
while ord(Let[a]) + num >= 123 :
num = Num - 26
out += (chr(ord(Let[a]) + num))
elif ord(Let[a]) > 64 and ord(Let[a]) < 91 and ord(Let[a]) + Num >= 91 :
while ord(Let[a]) + num >= 91 :
num = Num - 26
out += (chr(ord(Let[a]) + num))
a = a + 1
print(out)
b = b + 1
Changed to a "for" loop:
Let = input("Enter a sentence to decrypt : ")
Num = 0
b = 0
while b < 26 :
Num = Num + 1
num = int(Num)
out = ""
for a in Let:
if ord(a) > 96 and ord(a) < 123 and ord(a) + Num > 96 and ord(a) + Num < 123 :
out += (chr(ord(a) + Num))
elif ord(a) > 64 and ord(a) < 91 and ord(a) + Num > 64 and ord(a) + Num < 91 :
out += (chr(ord(a) + Num))
elif ord(a) > 96 and ord(a) < 123 and ord(a) + Num >= 123 :
while ord(a) + num >= 123 :
num = Num - 26
out += (chr(ord(a) + num))
elif ord(a) > 64 and ord(a) < 91 and ord(a) + Num >= 91 :
while ord(a) + num >= 91 :
num = Num - 26
out += (chr(ord(a) + num))
print(out)
b += 1

whats wrong with this statement(count from 0-9)

I am trying to get this code to calculate 5 and print numbers by 5's with a while statement of 7, so I want it to loop through, generating a different number 7 times; however, when it gets to a number over 10, I want it to start back over at 0 and ignore the 10.
This is my code:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
The code prints the first number, but freezes on printing any others. Why isn't this working?
Your code is not in the loop. Python's code blocks are created with indents:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
Is this the result you were trying to achieve:
import random
x = random.randint(1,9)
for i in range(1,8):
print x
x += 5
if x >= 10:
x -= 9
This generates a random number, and adds 5 until it passes 10, then subtracts 9, and it does this seven times. If I understand your question correctly, this is what you were trying to do. Please correct me if I am wrong.
no this is not what I was trying to do here is what I am trying to do.
counts = 0
r = 0
firstpickplusfive = 7
p = firstpickplusfive + 5
while r < 3:
firstpickplusfive = p + counts
if firstpickplusfive > 6:
firstpickplusfive = firstpickplusfive + counts - 10
if p > 9:
p = firstpickplusfive + counts
print firstpickplusfive
counts = counts + 1
r = int(r) + 1
it works alone, but when I add it to the script I am trying to write it doesn't work...if there is a simpler way to do it I would appreciate knowing it.
ie.
number = number + 5 + 0
then
number = number + 5 + 1.....ect which
example 7 + 5 + 0 = 12,
7 + 5 + 1 = 13.........
if the number is equal to 10 then I want it to drop the tens place and keep the 1's place
example 7 + 5 + 0 = 2,
7 + 5 + 1 = 3
Here is an easier method:
for i in range(1,4):
num = 7
num += 5
num +=(i-1)
if num >=10:
num -= 10
print num
i+=1
Try this in your script, does it work?

Categories