1

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

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 个目标。现在我的新代码将解决这个问题,但我想问是否有解决方法或技巧将 2 个目标合并为 1 个但仍然具有相同的功能。

编辑:我更感兴趣的是尽量减少元素的数量,然后尽量减少总和。

4

1 回答 1

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;

我希望你觉得这对你有帮助。

于 2014-07-05T14:24:05.240 回答