Dina Sheweita
Dina Sheweita

Reputation: 13

How can debug this code on spyde IDE/python

I have this code:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr 02 19:36:07 2017

@author: Ahmed
"""

from gurobipy import*
try:
    m = Model('operating_room')
    #Data
    b= [1,2,3]
    n= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
    j= [1,2,3,4,5,6,7,8,9,10,11,12,13]
    t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]
    W=0.5
    p[b]= 15
    h[b]= [1,2,3]
    m.update()
    #create variables
    X[j] =m.addVar(vtype=GRB.BINARY, name="X[j]")
    Y[n] =m.addVar(vtype=GRB.BINARY, name="Y[n]")
    x[b,j,t] = m.addVar(vtype=GRB.BINARY,name="x[b,j,t]") 
    y[n,b] = m.addVar(vtype=GRB.BINARY,name="y[n,b]")
    m.update()
    #set objective
    m.setObjective(quicksum(X[j] for j in J )+ W*(quicksum(Y[n] for n in N)),GRB.MINIMIZE)  #eqution 1
    
    for b in range(1,3):
    #create constraints
    m.addConstr(quicksum(x[b,j,t] <=1 for j in J for t in range(r[b],(d[b]-p[b]+1))),name="block_allocation")    #eqution 2 
    #equation 3
    for b in range(1,b):
          for j in J:
              for t in T:
    m.addConstr(x[b,j,t] <= X[j] ,name="allocation_to_open_OR")
    #equation 4
    for j in J:
        for t in T:
    m.addConstr(quicksum(x[b,j,l]<=1 for b in range(1:B) for l in range(t-p[b]+1:t)),name="overlaping_blocks")        
    #equation 5
    for b in range(1:B):
    m.addConstr(quicksum(y[n,b]=h[b],for n in N),name="surgical_nurse_demand")    
    #equation 6
    for n in N:
        for b in range(1:B):
    m.addConstr(y[n,b]<=Y[n],name="used_surgical_nurses")  
    #equation 7
    for b,b1 in range(1:B):
        for n in N:
            for t in T:
    m.addConstr(quicksum(x[b,j,l],for j in J for l in range(t-p[b]+1:t)) + quicksum(x[b1,j,l] <= (3-y[n,b]-y[n,b1]),for j in J
    for l in range(t-p[b1]+1:t)),name="link to scheduling")  
    #equation 8   
    for b in range(1:B):
        for j in J:
            for t in T:
    m.addConstr(x[b,j,t] in {0,1},name="integerity_1")          
    #equation 9
    for j in J:
    m.addConstr(X[j] in {0,1},name="integerity_2")                   
    #equation 10
    for b in range(1:B):
        for n in N:
    m.addConstr(y[n,b] in {0,1},name="integerity_3")            
    #equation 11     
    for n in N:
    m.addConstr(Y[n] in {0,1},name="integerity_4")                   
    #equation 12
    for n in N:
        for b,b1 in range(1:B):
            for o[b]!= o[b1]:
    m.addConstr(y[n,b]+y[n,b1]<=1,name="assignment_of_nurses_in_different_OR")
    #equation 13           
    m.addConstr(max(s[b],s[b1])<min(s[b]+p[b],s[b1]+p[b1]),name="prevent_overlapping_in_diff_OR")
    m.optimize()

But I get an error here:

for b in range(1,3):
    #create constraints
m.addConstr(quicksum(x[b,j,t] <=1 for j in J for t in range(r[b],(d[b]-p[b]+1))),name="block_allocation")    #eqution 2 

The error says:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/Ahmed/AppData/Roaming/Microsoft/Windows/Network Shortcuts/operating_roomintegrated.py", line 31
    m.addConstr(quicksum(x[b,j,t] <=1 for j in J for t in range(r[b],(d[b]-p[b]+1))),name="block_allocation")    #eqution 2 
    ^
IndentationError: expected an indented block

What is wrong, and how do I fix it?

Upvotes: -1

Views: 278

Answers (1)

ZeusInTexas
ZeusInTexas

Reputation: 307

As a general advice for Python programming, always pay attention to the compiler errors, especially to the last lines of the stacktrace :

IndentationError: expected an indented block

Please review your code: you have a few indentation problems, which is critical for Python, as it delimits scopes (you may be interested by this answer).

Also, as you said you're using Spyder, you could try to run: Source -> Fix Indentation, Remove Trailing Spaces.

Upvotes: 2

Related Questions