'"x" dimension is equal to "none" of binary decision variables using Python-MIP
I’m trying to optimise a model in an optimization problem with Python-MIP.
I made a binary decision variables and stored the reference in x.
Based on articles, the reference of binary decision variables is expected to have an “x” dimension for each index. Then I should be able to use it like x[1][1][1].x. But I get it none.
There are some logs of the code behaviours:
x = [[[model.add_var(var_type=BINARY) for i in V] for j in V] for k in K]
print(x[1][1][1]) # var(73)
print(type(x[1][1][1]) # <class 'mip.entities.Var'>
print(x[1][1][1].x) # none
It should be a number then I can use it in a condition of this line:
DifferentLocation = [(i, j, k) for i in V for j in V for k in K if x[k][i][j].x > 0.99]
Then I get this error:
DifferentLocation = [(i, j, k) for i in V for j in V for k in K if x[k][i][j].x > 0.99]
TypeError: '>' not supported between instances of 'NoneType' and 'float'
Please take a look at my code:
import numpy as np
import matplotlib.pyplot as plt
import ConstantData as cData
import CommonFunctions as cFunc
Data = [[ 140 , 30],
[ 0 , 60],
[ 10 , 0],
[-100, 100],
[-110, 90],
[ 30, 40]]
V = set(range(len(Data) + 2))
V2 = V - {0} - {1}
n = len(Data) + 2
K = set(range(6))
D = set(range(2))
Demand = [4., 3., 2., 2., 9., 5.]
BigConstraint = 1000
Demand1 = np.zeros((n))
Demand1[2:n] = Demand
Data1 = np.ones((n, 2))
Data1[0][0] = cData.Depot1x
Data1[1][0] = cData.Depot2x
Data1[0][1] = cData.Depot1y
Data1[1][1] = cData.Depot2y
Data1[2:n] = Data
distance = np.ones((n, n))
for i in range(n):
for j in range(n):
distance[i][j] = cFunc.Euclidean(Data1[i], Data1[j])
from itertools import product
from mip import Model, xsum, minimize, BINARY
from time import time
startTime = time()
model = Model()
x = [[[model.add_var(var_type=BINARY) for i in V] for j in V] for k in K]
y = [model.add_var() for i in V]
Sk = [model.add_var(var_type=BINARY) for k in K]
Totalcosts = xsum(distance[i][j] * x[k][i][j] * cData.CostPtud[k] for i in V for j in V for k in K) \
+ xsum(Sk[k] * cData.Rprice[k] for k in K)
model.objective = minimize(Totalcosts)
for i in V2:
model += xsum(x[k][i][j] for j in V for k in K) == 1
for h in V:
for k in K:
model += xsum(x[k][i][h] for i in V) - xsum(x[k][h][i] for i in V) == 0
for k in K:
model += xsum(Demand1[i] * x[k][i][j] for i in V for j in V) <= cData.Capacity[k]
for k in K:
model += xsum(distance[i][j] * x[k][i][j] for i in V for j in V) <= cData.MaxRout[k]
for k in K: # Return to Depot
model += xsum(x[k][i][j] for i in D for j in V2) <= 1
for k in K:
model += Sk[k] * BigConstraint >= xsum(x[k][i][j] for i in V for j in V)
model += xsum(Sk[k] for k in K) <= 3
for (i, j) in product(V2, V2):
for k in K:
if i != j and len(K) >= k:
model += y[i] - y[j] + (n * x[k][i][j]) <= n - 1
model.optimize()
solution = model.num_solutions
temp = x[1][1][1]
print("log system")
print(temp.x)
print(type(temp))
DifferentLocation = [(i, j, k) for i in V for j in V for k in K if x[k][i][j].x > 0.99]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
