我有一个具有4个特征(名称,价值,成本,人员)的项目表。我想用Minizinc描述一个模型,该模型选择最大化总价值但总预算为255的项目,并且我可以在20个之间选择近9个项目。
我写一个数据文件:data.dzn
% name value cost people
data = [
| PRJ0, 6000, 35, 5
| PRJ1, 4000, 34, 3
| PRJ2, 1000, 26, 4
...
| PRJ20, 1200, 18, 2
|];
PRJ = {PRJ0,...,PRJ20};
FEATURE = {name, value, budget, personnel};
max_budget = 225;
max_prj=9;
所以我的约束是:
choose_project <= 9 /\ budget<=255 s.t solve maximize tot_value;
如何在表格原始项目中选择非任意数量的决策变量(1到9)?到目前为止,这是我的代码:invest.mzn
include "data.dzn";
int: max_budget; %255
int: max_prj; %9
enum FEATURE;
enum PRJ;
array[PRJ,FEATURE] of int: data;
constraint ...
...
solve maximize tot_value;
您可以声明布尔变量数组,例如selected_projects
,该数组编码是否选择了项目PRJ_k
。
然后您可以简单地计算同时选择此数组中的多少个项目。
示例:
enum FEATURE = {name, value, budget, personnel};
enum PRJ = { PRJ0, PRJ1 };
array[PRJ,FEATURE] of int: data =
[| PRJ0, 6000, 35, 5
| PRJ1, 4000, 34, 3
|];
array[PRJ] of var bool: selected_projects;
var int: tot_value;
% The total number of selected projects must be in [1, 9]
constraint let {
var int: tot_selected = sum(prj in PRJ) ( selected_projects[prj] )
} in
1 <= tot_selected /\ tot_selected <= 9;
constraint tot_value = sum(prj in PRJ) ( selected_projects[prj] * data[prj, value] );
% ...
% encoding of budget and personnel constraints
% ...
solve maximize tot_value;