pythonoptimizationmathematical-optimizationgurobiindices

Fixing indices in a constraint of an optimization model


I am solving a Mixed Integer Problem with Gurobi package in Python. The problem is I do not know how to fix indices i for a below constraint:

w(t) = x(t,i=1) - s(t,i=1) ∀t ∈ N

t and i are indices from different sets. I write the following code but it does not work. Could you please help me with it?

d =[18,7,6,5,10,1,8]
sh[1,2,3,4,5]

m=len(sh)
p=len(d)

for t in range(p):
    model.addConstr(w[t] == x[t, 1] - s[t,1]) 

I am going to write a code for the part i=1 but I can not. Writing even index.sh('1') does not help me.

I tried the written code but it shows an error. I am new to Python and need help to solve this issue


Solution

  • Please look at the below listing. I have assumed that index ihas only one element.

    import gurobipy as gp
    from gurobipy import GRB
    
    
    d =[18,7,6,5,10,1,8]
    sh = [1,2,3,4,5]
    
    m=len(sh)
    p=len(d)
    
    model = gp.Model("")
    w = model.addVars(p)
    
    # assuming that length of the second index is 1
    x = model.addVars(p, 1)
    s = model.addVars(p, 1)
    
    for t in range(p):
        model.addConstr(w[t] == x[t, 0] - s[t, 0])