2

I have modeled a MIP problem in Python Cplex API. Is there any way to read variable values - variable-wise?

Currently I am reading variable values into a list and mapping those from the LP file"

    for i, j in zip(cpx.variables.get_names(), cpx.solution.get_values()):
        cplex_details_inp.append((i, j))

By this method, I am able to read all variable values at a time, but this method is causing memory issue due to the fact that there are 500k variables.

How to read values variable wise? I don't find any specific document or examples for this.

3 Answers 3

4
for i in cpx.variables.get_names():
           cplex_details_inp.append([i,cpx.solution.get_values(i)])

This code could help.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response.. but this is again the same what I am doing now. My list size won't decrease and will encounter the same memory issue. I am trying to read values variable wise instead of reading all values in a single list.
Also reading values from solution pool(as the above way mentioned) will take a lot of time to get each value.
1

Looking at the reference documentation we can see that there are various options to call this function:

  • solution.get_values() return the values of all variables from the problem.
  • solution.get_values(i) i must be a variable name or index. Returns the value of the variable whose index or name is i.
  • solution.get_values(s) s must be a sequence of variable names or indices. Returns the values of the variables with indices the members of s. Equivalent to [solution.get_values(i) for i in s]
  • solution.get_values(begin, end) begin and end must be variable indices or variable names. Returns the values of the variables with indices between begin and end, inclusive of end. Equivalent to solution.get_values(range(begin, end + 1)).

So you can specify as argument to the function just the variables for which you want to read the variables. Either specify variables one by one or read variables in chunks.

2 Comments

Thanks @Daniel Junglas it worked but by using this method memory issue got resolved but it is taking much time. Earlier model took 75 min but now it is taking more than 3 hours for 5.6L variables.
Two things that may speed this up: 1. Attempt to query as many variables as possible in one call. It may be best to first create a list of all variables for which you want to query the value and then clal get_values() only once with that list. 2. Don't query variables by name. Query them by index.
0

Are all your variables nonzeroes? In case your solution vector is sparse, the following can solve the memory issue:

cplex_details_inp = [[i, cpx.solution.get_values(i)] for i in cpx.variables.get_names() if cpx.solution.get_values(i) > 0]

Compared to appending to a list all the time, it might even speed up the process. In order to treat very small variables as zeroes, you might want to use round():

cplex_details_inp = [[i, cpx.solution.get_values(i)] for i in cpx.variables.get_names() if round(cpx.solution.get_values(i), precision) > 0]

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.