我目前正在使用Ilog Cplex
库在Java中开发项目,而我正在使用Intellij-Idea IDE。我在从MPS
文件导入模型时遇到了麻烦,这是给我带来麻烦的代码段
IloCplex iloCplexInstance = new IloCplex();
iloCplexInstance.importModel(fileName);
它会引发此异常:
ilog.cplex.CpxException: CPLEX Error 1423: Could not open file 'models\20_70_45_05_100.mps' for reading.
at ilog.cplex.CplexI.CALL(CplexI.java:5204)
at ilog.cplex.CplexI._readModel(CplexI.java:5584)
at ilog.cplex.CplexI.importModel(CplexI.java:1032)
at ilog.cplex.IloCplex.importModel(IloCplex.java:902)
at heuristics.ziround.LPUtils.fromMPS(LPUtils.java:34)
at heuristics.test.LPUtilsTestCompile.main(LPUtilsTestCompile.java:13)
我曾尝试在使用junit4
,junit.runners.Parameterized
的单元测试中以及在其主要方法的简单类中运行它。在每种情况下,结果相同。我也尝试设置文件的完整路径,它给出了相同的结果。我知道我使用的文件还可以,我可以使用cplex终端命令读取它,我还尝试了其他文件。
使用Ilog
库的代码可以编译,但是我不确定它是否可以运行,因为我无法导入模型,因此无法解决。
我正在使用Windows,以管理员身份启动IDE不会产生任何效果,并且文件不会被阻止读取(或写入)。
在官方支持页面上,我发现有关此错误的信息:http://www-eio.upc.es/lceio/manuals/cplex-11/html/refcallablelibrary/html/macros/CPXERR_FAIL_OPEN_READ.html
但是我找不到任何有用的东西。
而且,IBM论坛当前关闭,而且似乎没有人遇到这种问题:(
有人知道这可能是什么麻烦吗?我能做什么?您知道其他任何选择吗?
感谢任何会停下来的人!
异常消息说:
ilog.cplex.CpxException:CPLEX错误1423:无法打开文件'models \ 20_70_45_05_100.mps'进行阅读。
尝试改用绝对路径,例如c:\path\to\your\models\20_70_45_05_100.mps
。
您还可以将代码添加到程序中,以确保路径存在。类似于:
import java.nio.file.Files;
...
IloCplex iloCplexInstance = new IloCplex();
if (!Files.exists(fileName))
throw new AssertionError("path does not exist: " + fileName);
iloCplexInstance.importModel(fileName);
跟随@rkersh回答,我做到了:
String modelsPath = "absolute\\folder\\path";
Collection<Object[]> models = new ArrayList<>();
File folder = new File(modelsPath);
for (final File fileEntry : Objects.requireNonNull(folder.listFiles())) {
if (fileEntry.isFile())
models.add(new String[]{fileEntry.getAbsolutePath()});
}
return models;
这可以确保绝对路径正确,并且现在iloCplexInstance.importModel(fileName);
可以接受它