Whats wrong with this code? I am totally new to programming, so please dont hate me


#1



from gurobipy import *

def solve(n,b,p,a,u,l):
    
    #Model
    model = Model("Constrained Knapsack Model")

    x={}
    for i in range (n) :    #1                  2
       x[i] = model.addVar(vtype=GRB.BINARY, obj = p[i], name="x_%s"% i)
        
    #Gewinn maximieren     3
    model.modelSense = GRB.MAXIMIZE
    
    # model aktualisieren, um die Variablen bekannt zu machen
    model.update()
    
    # ergaenzen Sie ab hier die linearen Nebenbedingungen (aka constraints)
                                           #4
    model.addConstr(quicksum(x[i]*a[i] for i in range(n))  <=b,
    name='capacity')

#Maximalanzahl an Gegenstaenden
    model.addConstr(quicksum(x[i] for i in range(n))  <=u,
    name="maximum")

#Minimalanzahl an Gegenstaenden 
    model.addConstr(quicksum(x[i] for i in range(n))  >=1,
    name="minimum")
    
    
    # Solve
    model.update()
    model.optimize()
    
    
    return model


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.