如何在spyde IDE / python

时间:2017-04-29 17:42:39

标签: python ipython anaconda spyder gurobi

逗人,

我是一名研究生,也是spyder软件的新用户。

我正在通过python语言编程使用这个程序,并且我通过很长的路来学习python和spyder以便能够插入我的代码。

但是我遇到了一些错误,我试图解决它并且我成功但其中一些我尝试但我不知道问题是什么。

所以,我很高兴有这个庞大的社区来问像你这样受尊敬的专家,我希望支持我解决这个问题,因为我没有任何导游在我的大学里知道这个软件。

等式#2中的错误

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 

出现的错误是

>>> runfile('C:/Users/Ahmed/AppData/Roaming/Microsoft/Windows/Network Shortcuts/operating_roomintegrated.py', wdir='C:/Users/Ahmed/AppData/Roaming/Microsoft/Windows/Network Shortcuts')

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
  
    
      

    
  

这是我的代码:

# -*- 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()

1 个答案:

答案 0 :(得分:2)

作为Python编程的一般建议,请始终注意编译器错误,尤其是堆栈跟踪的最后几行:

  

IndentationError :预计会出现缩进块

请检查您的代码:您有一些缩进问题,对于Python来说是严重,因为它划分了范围(您可能对this answer感兴趣)。

另外,正如您所说的使用Spyder,您可以尝试运行: 来源 - &gt;修复缩进,删除尾随空格。