PyPSA / linopy

Linear optimization with N-D labeled arrays in Python
https://linopy.readthedocs.io
MIT License
165 stars 51 forks source link

in run_xpress: xpress.problem class does not have attribute "tofile" #361

Closed nifroehling closed 1 month ago

nifroehling commented 1 month ago

Dear pypsa/linopy-Team!

I'm currently in the process of integrating linopy into our pypsa-based project, and stumbled upon following problem: In linopy/linopy/solvers.py, in the run_xpress function the lines

if solution_fn is not None:
    try:
        m.tofile(path_to_string(solution_fn), filetype="sol")
    except Exception as err:
        logger.info("Unable to save solution file. Raised error: %s", err)

raise the error

INFO:linopy.solvers:Unable to save solution file. Raised error: 'xpress.problem' object has no attribute 'tofile'

The variable m is an object of the xpress.problem class, which, according to the error message and the fico xpress python interface reference manual, does not have an attribute or function "tofile". The xpress.problem class does have a function problem.writesol, which is there to write out the solution to the problem. This may be an acceptable substitution.

If I replace

m.tofile(path_to_string(solution_fn), filetype="sol")

with

m.writesol(path_to_string(solution_fn))

the error is no longer raised. This, however, does not produce a .sol file, but two files, one in an .hdr and one in the .asc format. I was not able to find an xpress function that would write the solution in the .sol format.

I hope this helps!

Best

Nina Fröhling

nifroehling commented 1 month ago

One additional bit of information: This happens with an older Xpress version (9.0.2) as well as with the latest version (9.4.2). In the latest version, however, the error does not cause the program to abort- in the older version, it does cause the program to abort. I have not tested versions except for 9.0.2 and 9.4.2.

fneum commented 1 month ago

@nifroehling, thanks for investigating! And I agree with your findings. I have implemented writesol in #364, and for now let's accept the alternative file format.

nifroehling commented 1 month ago

Hi Fabian, thank you for the quick response! Best, Nina