AMPL最小化集合中的整数和所选元素的数量

时间:2014-07-04 22:08:16

标签: mathematical-optimization ampl

之前,我发布了一个问题,我询问如何从一组中选择最小数量的整数,并且总和> =常数。 我的代码如下所示:

option solver cplex;
set x:= {4, 5, 7, 1};
param c:= 10;
var u{x} binary;
minimize output : sum {i in x} u[i];
subject to constraint: sum {i in x} u[i]*i >= c;
solve;
display u;

我决定添加一个最小化总和的新目标。 在前面的例子中,cplex产生12(7和5)。我想要它产生11(7和4)。 为此,我添加了这个目标:

minimize output : sum {i in x} u[i]*i;

不幸的是,我有AMPL的学生版,所以我不能使用2个目标。现在我的新代码将解决问题,但我想询问是否有解决方法或将两个目标合并为1但仍然具有相同功能的技巧。

编辑:我更感兴趣的是最小化元素数量,然后最小化总和。

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我想和你们分享一下。

option solver cplex;
set x:= {4, 5, 7, 1};                                                #this is the set where I want to chose the integers from.
param c:= 10;                                                        #this is the constant i want the sum to be >= to.
param MinNumberOfELements;                                           #this will be used later. Explanation will be added then.
var u{x} binary;                                                     #binary array, indicates the corresponding elements in x is used.  u[4] = 1 -->  4 is taken in the output
minimize output : sum {i in x} u[i];                                 #here we minimize number of taken elements without caring of minimizing the sum
subject to constraint: sum {i in x} u[i]*i >= c;                     # the sum must be >= to the constant c.
solve;                                                               #this will cause the objective "output" to have some value

let MinNumberOfELements := output;                                   # take the value of "output" and store it in this param.
drop output;                                                         #since we can have only 1 objective, we drop the old one.

minimize output2: sum {i in x} u[i]*i;                               # in this step, we minimize the sum
subject to constraint2: sum {i in x} u[i] = MinNumberOfELements;     #number of elements chosen must be = to MinNumberOfELements
solve;
display output2,u;

我希望你觉得它很有帮助。