windows如何通过Pyomo读取cplex虚拟机配置文件?

How to read cplex virtual machine configuration file through Pyomo in windows?

我正在 Pyomo 中的两台机器(windows 系统)上设置复杂的分布式计算。我怎样才能在 python 脚本中做到这一点?

我已经通过 tcp/ip 设置了 cplex 分布式计算连接,并且可以按照此处的说明使用 Cplex Interactive Optimizer 进行并行计算:https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.cplex.help/CPLEX/UsrMan/topics/parallel_optim/distribMIP/09_tcp_ip.html

我不知道如何在 Pyomo 中做同样的事情 API。根据我的研究,pyomo 的 SolverFactory 只能更改求解器参数的设置。我想做的是通过 Pyomo API.

更改 vmc 设置

可能无法通过 pyomo 执行此操作,但您当然可以直接使用 CPLEX Python API 执行此操作。请参阅 Cplex.copy_vmconfig, Cplex.read_copy_vmconfig 等文档。另外,CPLEX 附带 distmipex1.pydistmipex2.py 示例(您可以在 COS_INSTALL_DIR/cplex/examples/src/distmippython 下找到这些示例)。

当您将 Pyomo 与 CPLEX Interactive Optimizer (--solver cplex) 一起使用时,Pyomo 会将您的问题保存在 .lp 文件中,然后启动 CPLEX 并向其发送一系列命令来读取和解决问题模型,然后保存结果。有一次,我需要为此添加一个步骤来修复 MIP 模型中的整数变量并重新求解它以获得所有约束的对偶值。为此,我在 Pyomo 将其发送到 CPLEX 之前修补了 CPLEX 脚本。它是粗糙和准备好的,但它已经很好地支撑了。您可以在此处查看该代码:https://github.com/switch-model/switch/blob/2.0.6/switch_model/solve.py#L791

您可以根据自己的目的使用类似的代码。例如,您可以 运行 在解决模型之前的某个时间使用此代码:

from pyomo.solvers.plugins.solvers.CPLEX import CPLEXSHELL
old_create_command_line = CPLEXSHELL.create_command_line

def new_create_command_line(*args, **kwargs):
    # call original command
    command = old_create_command_line(*args, **kwargs)
    # alter script
    if hasattr(command, 'script'):
        command.script = 'read configuration.vmc\n' + command.script
        print("changed CPLEX solve script to the following:")
        print(command.script)
    else:
        print ("Unable to patch CPLEX solver script to read configuration.vmc.")
    return command

CPLEXSHELL.create_command_line = new_create_command_line