Skip to main content

not supported between float and generator

Answered

Comments

5 comments

  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    I think the brackets are not placed properly: \(\texttt{for j in range(..)}\) needs to be placed in the gp.quicksum brackets. Additionally, I think you need \(\texttt{addConstr()}\) instead of \(\texttt{addConstrs()}\).

    0
  • Richard Liu
    Gurobi-versary
    First Comment
    First Question
    model_SP.addConstr(gp.quicksum(P[i,j]*u[j] for j in range(len(P[0])))>=q[i] for i in range(len(q)))
      model_SP.addConstr(gp.quicksum(D.T[i,j]*w1[j] for j in range(len(d)))+
                          gp.quicksum(K.T[i,j]*w2[j] for j in range(len(g)))+
                          gp.quicksum(G.T[i,j]*w3[j] for j in range(len(h)))+
                            gp.quicksum(L.T[i,j]*w4[j] for j in range(len(s)))<=c[i] for i in range(len(c)))
     
    i have rewrited the sentences you mention as above.but gurobi says:
    TypeError: unsupported operand type(s) for -: 'generator' and 'NoneType'.
    whats wrong with it?(the arrow still refers to the first addConstr sentence)
     
    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    If you put the loop over i in the constraint, you need \(\texttt{addConstrs()}\). This is now different from the code you posted before.

    If this is still not working, then please provide a minimal reproducible example, see Tutorial: Preparing a Minimal Reproducible Example.

    0
  • Richard Liu
    Gurobi-versary
    First Comment
    First Question
    here is the MRE:
     
    import matplotlib.pyplot as plt
    import numpy as np
    import gurobipy as gp
    from gurobipy import GRB
    import random
    import seaborn as sns

     

    #parameters predefinition and declaration
    #thermostatically controlled load
    num_tcls=100 # amount of thermostatically controlled units
    Cth=2
    Rth=2
    Pm=5.6
    eta_tcl=2.5
    theta_r=22.5 #temperature setpoint
    #energy storage  
    Pnet_max=1500
    eta=0.95
    Eess0=500
    Eess_min=100
    Eess_max=900
    Pess_max=200
    PLED_max=150
    PLED_all=1000
    #robust coefficient
    gamma=64
    #P_ac=(T-theta_r)/(eta*Rth)
    e_load=np.array([160,150,140,140,130,135,150,180,215,250,275,320,
                     335,290,270,280,280,290,320,360,345,260,180,160])
    #take account of deviation of buy price
    buy_price=np.array([0.30,0.30,0.30,0.30,0.30,0.30,0.60,0.60,0.60,0.60,0.90,0.90,0.90,0.90,0.90,0.60,0.60,0.60,
                        0.90,0.90,0.90,0.30,0.30,0.30])
    ppv=np.array([0,0,0,0,0,40,60,80,90,80,90,100,150,150,130,140,80,30,20,0,0,0,0,0])
    temperature=np.array([-1,1,0,-1,0,-1,0,1,2,3,4,5,8,9,10,8,7,6,5,2,1,1,0,-1])
    #construction of matrix D and vector d
    #upper&lower bound of electricity purchased from utility grid
    D0=np.hstack((np.eye(24),np.zeros((24,96))))
    d0=np.zeros((24,1))
    D1=np.hstack((-np.eye(24),np.zeros((24,96))))
    d1=-Pnet_max*np.ones((24,1))
    #upper&lower bound of LED light-supplement
    D2=np.hstack((np.zeros((24,72)),np.eye(24),np.zeros((24,24))))
    d2=np.zeros((24,1))
    D3=np.hstack((np.zeros((24,72)),-np.eye(24),np.zeros((24,24))))
    d3=-PLED_max*np.ones((24,1))
    D4=np.hstack((np.zeros((24,96)),-np.eye(24)))
    d4=-1*np.expand_dims(ppv,axis=0).T
    #constraints of ESS SoC
    D5=np.hstack((np.zeros((24,24)),eta*np.tri(24),-1/eta*np.tri(24),np.zeros((24,48))))
    d5=-(Eess0-Eess_min)*np.ones((24,1))
    D6=np.hstack((np.zeros((24,24)),-eta*np.tri(24),1/eta*np.tri(24),np.zeros((24,48))))
    d6=-(Eess_max-Eess0)*np.ones((24,1))
    #vertically stack D0-D6,d0-d6 to complete matrix D and d
    D=np.vstack((D0,D1,D2,D3,D4,D5,D6))
    d=np.vstack((d0,d1,d2,d3,d4,d5,d6))
    #construction of matrix K and g
    K0=np.hstack((np.zeros((1,24)),eta*np.ones((1,24)),-1/eta*np.ones((1,24)),np.zeros((1,48))))
    g0=np.zeros((1,1))
    K1=np.hstack((np.zeros((1,72)),np.ones((1,24)),np.zeros((1,24))))
    g1=PLED_all*np.ones((1,1))
    #light supplement restricted to 19:00-6:00
    K2=np.hstack((np.zeros((1,79)),np.ones((1,12)),np.zeros((1,29))))
    g2=np.zeros((1,1))
    K=np.vstack((K0,K1,K2))
    g=np.vstack((g0,g1,g2))
    #cannot charge/discharge simultaneously
    F0=Pess_max*np.eye(24)
    G0=np.hstack((np.zeros((24,24)),-np.eye(24),np.zeros((24,72))))
    h0=np.zeros((24,1))
    F1=-Pess_max*np.eye(24)
    G1=np.hstack((np.zeros((24,48)),-np.eye(24),np.zeros((24,48))))
    h1=-Pess_max*np.ones((24,1))
    F,G,h=np.vstack((F0,F1)),np.vstack((G0,G1)),np.vstack((h0,h1))
    L=np.hstack((np.eye(24),-np.eye(24),np.eye(24),-np.eye(24),np.eye(24)))
    Y=num_tcls/(eta_tcl*Rth)*np.eye(24)
    s=np.expand_dims(e_load,axis=0).T+num_tcls*theta_r/(eta_tcl*Rth)*np.ones((24,1))
    c=np.hstack((np.expand_dims(buy_price,axis=0),np.zeros((1,96))))
    #matrices about uncertainty variable u
    P0=np.eye(24)
    P1=-np.eye(24)
    P2=np.ones((1,24))
    q0=np.expand_dims(temperature-4,axis=0).T
    q1=np.expand_dims(-temperature,axis=0).T
    q2=(sum(temperature)-gamma)*np.ones((1,1))
    P=np.vstack((P0,P1,P2))
    q=np.vstack((q0,q1,q2))
     
    model2=gp.Model('subproblem')
    def SP2(model_SP,x_opt):
        model_SP.Params.Nonconvex=2
        u=model_SP.addVars(range(24),lb=-10,ub=15,vtype=GRB.CONTINUOUS)
        w1=model_SP.addVars(range(len(d)),lb=0,ub=GRB.INFINITY,vtype=GRB.CONTINUOUS)
        w2=model_SP.addVars(range(len(g)),vtype=GRB.CONTINUOUS)
        w3=model_SP.addVars(range(len(h)),lb=0,ub=GRB.INFINITY,vtype=GRB.CONTINUOUS)
        w4=model_SP.addVars(range(len(s)),vtype=GRB.CONTINUOUS)

        for i in range(49):
            model_SP.addConstr(gp.quicksum(P[i,j]*u[j] for j in range(24))>=q[i])
        for i in range(120):
            model_SP.addConstr(gp.quicksum(D.T[i,j]*w1[j] for j in range(168))+
                                gp.quicksum(K.T[i,j]*w2[j] for j in range(3))+
                                gp.quicksum(G.T[i,j]*w3[j] for j in range(48))+
                                gp.quicksum(L.T[i,j]*w4[j] for j in range(24))<=c[i])
           
        model_SP.setObjective((gp.quicksum(d[i]*w1[i] for i in range(len(d)))+
                              gp.quicksum(g[i]*w2[i] for i in range(len(g)))+
                              gp.quicksum(h[i]*w3[i] for i in range(len(h)))+
                              gp.quicksum((s[i]-gp.quicksum(Y[i,j]*u[j] for j in range(24)))*w4[i]) for i in range(len(s))),GRB.MAXIMIZE)
        model_SP.optimize()
        return model_SP.ObjVal,u.X
     
    x1=np.array([0., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
           1., 0., 0., 0., 1., 1., 1.])
    ub1,u1=SP2(model_SP=model2,x_opt=x1)
     
     
    now the problem is :Invalid argument to Model.addConstr.
    the arrow refers to the row below for i in range(120).
     
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Richard,

    The M in MRE stands for "minimal".  You can have this one is for free:

    import numpy as np
    import gurobipy as gp
    from gurobipy import GRB

    #take account of deviation of buy price
    buy_price=np.array([0.30,0.30,0.30,0.30,0.30,0.30,0.60,0.60,0.60,0.60,0.90,0.90,0.90,0.90,0.90,0.60,0.60,0.60,
                        0.90,0.90,0.90,0.30,0.30,0.30])

    c=np.hstack((np.expand_dims(buy_price,axis=0),np.zeros((1,96))))

    model2=gp.Model('subproblem')

    w1=model2.addVars(1)
    model2.addConstr(1.0*w1[0] <=c[0]) # i=0, j=0, ignoring other quicksums

    I'd encourage you to try and debug this yourself now by looking at the expression 1.0*w1[0] <=c[0] in an interactive Python session.

    - Riley

     

    0

Please sign in to leave a comment.