Object type changing to string after being declared as a continuous variable?
Awaiting user inputHi, I'm using Gurobi in Jupyter notebook. I'm running into an issue within one of my functions in an algorithm where I'm getting an "unsupported class type for LinExpr". I'm not having this same error in another function. When I test print(type(X)) in another cell, it says X is not defined, but when I test print(type('X'), it says X is a string.
# Import libraries
from gurobipy import *
import csv
from math import *
import numpy as np
from random import *
import time
# Define Sets and Parameters
start = time.time()
# Time set
timep = list()
Time = 20
for t in range(1, Time + 1):
timep.append(t)
# amount of supply node
supply = open('C:/Users/Emma/Documents/2021-2022/Thesis/Data/supply.csv', 'r', encoding='utf-8-sig')
csv_supply = csv.reader(supply)
mydict_supply = {}
for row in csv_supply:
key_start = row[0:2]
for index, item in enumerate(row[2:21], 1):
key = tuple(key_start + [index])
mydict_supply[key] = item
#print(mydict_supply)
snode = list(mydict_supply.keys())
supply = mydict_supply
snode = tuplelist(snode)
# arc capacity
capacitya = open('C:/Users/Emma/Documents/2021-2022/Thesis/Data/arcs.csv', 'r', encoding='utf-8-sig')
csv_capacitya = csv.reader(capacitya)
mydict_capacitya = {}
for row in csv_capacitya:
key_start = row[0:3]
for index, item in enumerate(row[3:23], 1):
key = tuple(key_start + [index])
mydict_capacitya[key] = item
arc = list(mydict_capacitya.keys())
capacitya = mydict_capacitya
arc = tuplelist(arc)
def RLD (Zednd):
recoveryd = Model()
recoveryd.setParam('OutputFlag', False)
# Definition of decision variables
O = recoveryd.addVars(dnode, timep,counter, vtype=GRB.INTEGER, name="O")
X = recoveryd.addVars(arc, timep,counter, vtype=GRB.CONTINUOUS name="X")
Zn = recoveryd.addVars(attnode,counter, vtype=GRB.BINARY, name="Zn")
Yn = recoveryd.addVars(attnode, vtype=GRB.BINARY, name="Yn")
Fn = recoveryd.addVars(attnode,counter,vtype=GRB.BINARY, name="Fn")
An = recoveryd.addVars(attnode, counter, vtype=GRB.BINARY, name="An")
Apno = recoveryd.addVars(Crewone, attnode, timep,counter, vtype=GRB.BINARY, name="Apno")
Apntw = recoveryd.addVars(Crewtwo, attnode, timep,counter, vtype=GRB.BINARY, name="Apntw")
Apnth = recoveryd.addVars(Crewthree, attnode, timep,counter, vtype=GRB.BINARY, name="Apnth")
Bn = recoveryd.addVars(attnode, timep,counter, vtype=GRB.BINARY, name="Bn")
gama = recoveryd.addVar(vtype=GRB.CONTINUOUS, name="gama")
# (19)
for k,i,g in snode:
for t in timep:
for c in counter:
recoveryd.addConstr(
(sum(X[k,i, j,g, t,c] for k,i, j,g in arc.select(k,i,'*',g)) - sum(X[k,j, i,g, t,c] for k,j, i,g in arc.select(k,'*',i,g)) <= mydict_supply[(k,i,g)]),
name="19")
... # more constraints provided between the above and following sections, all use X in a #similar way to (19)
ZRLD = gama
recoveryd.setObjective(ZRLD, GRB.MINIMIZE)
recoveryd.optimize()
# recovery.printAttr('X')
solutiondnode= recoveryd.getAttr('X', O)
# solutionalphaa = recovery.getAttr('X', Aa)
# print("solutionalphaa", solutionalphaa)
solutionalphan = recoveryd.getAttr('X', An)
# print("solutionalphan", solutionalphan)
solutionfailnode =recoveryd.getAttr('X',Fn)
# print("solutionfailnode", solutionfailnode)
# solutionfailarc = recovery.getAttr('X', Fa)
# print("solutionfailarc", solutionfailarc)
solutionprotect = recoveryd.getAttr('X', Yn)
-
Could you try to isolate the error and produce a minimal working example? It is not possible to reproduce the error with the code snippet you provided.
When I test print(type(X)) in another cell, it says X is not defined, but when I test print(type('X'), it says X is a string.
The error \(\texttt{X is not defined}\) says that in the current cell, the object \(\texttt{X}\) is not known in the given scope. \(\texttt{print(type('X'))}\) works because \(\texttt{'X'}\) is literally a \(\texttt{string}\). Through the use of \(\texttt{''}\), you turned \(\texttt{X}\) into the \(\texttt{string}\) which holds the capital letter \(\texttt{X}\).
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment