1

I would like to get the names of each decision variable after I have run my optimizing programme using Cplex in Python.

I know that there is a function in Matlab for that but I can't find it for Python. Does anybody know if there is such a function?

Thanks!

2 Answers 2

1

let me share a small example:

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve()

print(nbbus40.solution_value);
print(nbbus30.solution_value);

print("display")

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

which gives

6.0
2.0
display
nbBus40  =  6.0
nbBus30  =  2.0
Sign up to request clarification or add additional context in comments.

Comments

0

With the CPLEX Python API (not to be confused with the DOcplex Modeling for Python API shown in Alex's answer), you can use the Cplex.variables.get_names method. The following toy example reads a model file (given on the command line) and prints out all the names:

import sys
import cplex

if len(sys.argv) != 2:
    raise ValueError("usage: {0} <filename>".format(sys.argv[0]))

c = cplex.Cplex(sys.argv[1])

for index, name in enumerate(c.variables.get_names()):
    print("variable[{0}] = {1}".format(index, name))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.