【问题标题】:Write objective function and constraints in LP format以 LP 格式编写目标函数和约束
【发布时间】:2019-02-15 20:10:41
【问题描述】:

我正在使用 CVXPY 来求解目标函数。变量是大矩阵。我想创建一个函数来将目标函数和约束写入文本文件,然后可以将其用于其他求解器,例如 CPLEX。一种方法是将每个变量显式写入字符串并将它们连接起来以创建目标函数。但是,我正在寻找替代方法,以使表达式不会变得太大。例如,如果变量如下:

[ w11, w12

  w21,  w22 ]

那么表达式将是 w11+w12+w21+w22。我们可以想象,如果变量矩阵增长,表达式也会增长。

有没有办法将变量初始化为 LP 格式的矩阵?

【问题讨论】:

  • 您是否知道 CPLEX 可用作 CVXPY 中 solve 方法的 options 之一?您可以使用 cplex_filename 选项从 CPLEX 导出 LP 文件。
  • 我明白了。谢谢!我还没有安装 CPLEX,但我现在看到了导出 LP 文件的选项。

标签: cplex cvxpy


【解决方案1】:

编写用于 cplex 求解器的文本文件的 matlab 代码...可能会有所帮助,如果您有更好的东西,可以发布它

clc;
clear all;
close all;
%fileID = fopen('test2_in.txt','wt');

filename = fullfile('"E:\...','ex_1.txt'); 

 fileID = fopen('ex_1.txt','w+'); 


formatSpec = 'MAXIMIZE \n Obj:';
fprintf(fileID,formatSpec);


obj_coeff = [0.12 0.15];
%-------------------------------------------------------------------------
% Writing Objective function for the problem
for ii = 1:length(obj_coeff) 
     if ii==length(obj_coeff)
        formatSpec = '(%d) X_%d';
    else
        formatSpec = ' (%d) X_%d +';
    end
    fprintf(fileID,formatSpec,obj_coeff(ii),ii);
end
formatSpec = '\n\nSUBJECT TO \n';
fprintf(fileID,formatSpec);
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%writing constraints
temp = 1;
for jj = 1:length(obj_coeff) 
    if jj==length(obj_coeff)
        formatSpec = ' X_%d <= %d\n r %d:';
        fprintf(fileID,formatSpec ,jj,temp+1);
        temp = temp+1;
    else
        formatSpec = ' X_%d +';
        fprintf(fileID,formatSpec,jj);
    end        
end
formatSpec = '\n\n binary\n ';
fprintf(fileID,formatSpec);
for ii = 1:1:length(obj_coeff) 
    formatSpec = 'X_%d ';
    fprintf(fileID,formatSpec,ii); 
end

formatSpec = '\n\nEND';
fprintf(fileID,formatSpec);
fclose(fileID);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多