I have an implementation where I need to optimize over object variables. I am using the mathematical programming package of docplex. The problem is that all (both) object variables are being considered the same.
from docplex.mp.model import Model
test = Model()
class Node:
def __init__(self, id):
self.id = id
x = test.integer_var(0, 50)
n1 = Node(1)
n2 = Node(2)
test.add_constraint(n1.x + n2.x <= 12)
test.add_constraint(2 * n1.x + n2.x <= 16)
test.maximize(40 * n1.x + 30 * n2.x)
test.solve()
test.print_solution()
The output is as though there is only one variable:
objective: 350
x1=5
The same problem occurs even if I define the variable externally (outside the class) and instantiate it with the class objects:
class Node:
def __init__(self, id):
self.id = id
x = test.integer_var(0, 50)
Node.x = x
However, in a separate test case, when I use variables without docplex, both class and external variables are able to carry distinct values for every object.
class Node:
def __init__(self, id):
self.id = id
cv = 0
ev = 0
Node.ev = ev
o1 = Node(1)
o2 = Node(2)
o1.ev = 5
o2.ev = 10
o1.cv = 6
o2.cv = 7
print(o1.ev, o2.ev)
print(o1.cv, o2.cv)
Output:
5 10
6 7
Process finished with exit code 0
Apologies for the long question. Can someone explain?