1

I am trying to output the solution of one of the array variables into an excel workbook. I am not able to get anything other than the representation of the variables within PULP as opposed to the solved value (which is what I am after).

I have tried a number of different pandas/numpy data frames and arrays but no success. Tried using the excel writer library as well but no dice.

YPER = 30
HE = 24

yearlyhours = []
yearlyhours = [(i,j) for i in range(YPER) for j in range(HE)]

MAHLDIFF = pulp.LpVariable.dicts("MAHLDIFF", (range(YPER), range(HE)), lowBound=0, cat='Continuous')

MAHLDIFFOUTPUT = []
for i,j in yearlyhours:
    var_output = {'MAHLDIFF':MAHLDIFF[i][j]}
    MAHLDIFFOUTPUT.append(var_output)
dfOptResults = pd.DataFrame.from_records(MAHLDIFFOUTPUT)
dfOptResults.to_excel('file.xls')

This is the output I get so far, just a sample of full array:

    MAHLDIFF
0   MAHLDIFF_0_0

2 Answers 2

1

This is not really about exporting to Excel, it's just a general question about how to access the values of the variables in PuLP. To do so, use <var_name>.value(), e.g., MAHLDIFF[i][j].value().

You can also loop through all variables in the model using, e.g.:

for v in prob.variables():
    print(v.name, "=", v.varValue)

where prob is the Python variable that holds your PuLP model.

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

3 Comments

Larry, you are absolutely right! I appreciate the scoping check for the question. Either way, this obtained exactly the values that I needed. Appreciate the help!
Just in case you read this, maybe you could help with one more thing. The output top excel is in a single columnar format. I need it to be based on the array specified in YPER and HE. Takes the 6x5 matrix and outputs as a single column of 0-29 values. I would like it to be 0-5 in column A, 6-10 in B, etc, etc. Any ideas? Using the same code above just added the .value()
That's a Python question, not really a PuLP question. Either way, you should post it as a new question. (I'm not the guy to help with that one.)
0

Have you thought about https://solverstudio.org/. If you want to use Excel to manage your data.

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.