【问题标题】:AMPL minimizing the sum of integers in a set and number of chosen elementsAMPL 最小化集合中整数的总和以及所选元素的数量
【发布时间】:2014-07-04 22:08:16
【问题描述】:

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

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 个目标,但仍然具有相同的功能。

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

【问题讨论】:

  • 为了表示您的问题已解决,请接受解决问题的答案。它可以是你自己的答案。
  • 谢谢 ALi,我试过了,但出现了一条弹出消息,上面写着“我明天可以接受我自己的答案”。我等到明天才能接受。

标签: mathematical-optimization ampl


【解决方案1】:

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

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;

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多