简体   繁体   English

Python中的约束整数优化

[英]Constrained integer optimization in Python

I'd like to minimize a function, which takes a 3x8 matrix of non-negative integers as input. 我想最小化一个函数,该函数需要一个3x8的非负整数矩阵作为输入。 Each row specifies a variable, whereas each column specifies a certain time point in the system. 每行指定一个变量,而每列指定系统中的某个时间点。 See the input in CSV format below. 请参阅下面的CSV格式输入。

   ,Time0,Time1,Time2
U_i,0,0,0
U_o,0,0,0
C_i,0,0,0
C_o,0,0,0
T_i,0,0,0
T_o,0,0,0
D_i,0,0,0
D_o,0,0,0

The constraints for each column is: 每列的约束是:

C_i + T_i >= U_i
C_o + T_o >= U_o
D_i <= 15
D_o <= 15
D_i = 0 if C_i == 0
D_o = 0 if C_o == 0

And the overall constraint across rows is C_i + C_o + T_i + T_o = 5 . 行之间的总约束为C_i + C_o + T_i + T_o = 5 I've looked at scipy.optimize, but cannot find a proper method that handles integers. 我看过scipy.optimize,但是找不到合适的方法来处理整数。 Could someone give me a hint or a MWE on how to do this? 有人可以给我一个提示或MWE的方法吗?

For smaller problems the AMPL website has a free tool: http://www.ampl.com/TRYAMPL/startup.html . 对于较小的问题,AMPL网站提供了免费工具: http : //www.ampl.com/TRYAMPL/startup.html The model file (*.mod) to upload would would be: 要上传的模型文件(* .mod)将是:

set T;

var U_i {T} >= 0 integer;
var U_o {T} >= 0 integer;
var C_i {T} >= 0 integer;
var C_o {T} >= 0 integer;
var T_i {T} >= 0 integer;
var T_o {T} >= 0 integer;
var D_i {T} >= 0 integer;
var D_o {T} >= 0 integer;

minimize obj: sum {t in T} (U_i[t] + U_o[t] + C_i[t] + C_o[t] + T_i[t] + T_o[t] + D_i[t] + D_o[t]);
subject to C1 {t in T}: C_i[t] + T_i[t] >= U_i[t];
subject to C2 {t in T}: C_o[t] + T_o[t] >= U_o[t];
subject to C3 {t in T}: D_i[t] <= 15;
subject to C4 {t in T}: D_o[t] <= 15;
subject to C5 {t in T}: D_i[t] <= C_i[t] / 1000;
subject to C6 {t in T}: D_o[t] <= C_o[t] / 1000;
subject to C7 {t in T}: C_i[t] + C_o[t] + T_i[t] + T_o[t] = 5;

The data file (*.dat): 数据文件(* .dat):

set T := 1 2 3;

Since you didn't specify an objective function I put in a linear dummy. 由于您未指定目标函数,因此我放入了线性虚拟对象。 After uploading both files you can select a solver; 上传完两个文件后,您可以选择一个求解器。 I think none of them is an integer solver unfortunately, but some are non-linear. 我认为它们都不是整数求解器,但是有些不是非线性的。 This way you could probably find a lower bound, feasible integer solutions you can get from there by some rounding heuristic (not necessary integer optimal). 这样,您可能可以找到较低的可行整数解决方案,可以通过一些舍入启发法(不一定是整数最优的)从那里得到。

Alternatively: Excel solver? 或者:Excel求解器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM