MathProg(AMPL) - 由另一个变量调整的可变数组

时间:2012-03-15 06:12:33

标签: ampl mathprog

我正在编写我的第一个GNU MathProg(AMPL)程序,以查找给定基数,主机数和二分带宽的HyperX拓扑(图形)的最小切换(顶点)计数实例。这是一个简单的第一个程序,因为所有方程已在下面的文章中描述:http://cal.snu.ac.kr/files/2009.sc.hyperx.pdf

我已阅读规范和示例程序,但我遇到了一个非常简单的语法错误。我需要有以下两个变量:L,网络中的维数,以及长度为L的数组S,其中S的每个元素是每个维度中的开关数。在我的MathProg程序中,我将其表达为:

var L >= 1, integer;
var S{1 .. L} >= 2, integer;

但是,当我运行$ glpsol --check --math hyperx.mod时,我收到以下错误:

hyperx.mod:28: operand following .. has invalid type
Context: ...isec ; param radix ; var L >= 1 , integer ; var S { 1 .. L }

如果有人可以帮助解释我应该如何恰当地表达这种关系,我将不胜感激。另外,我包括我编写的整个程序以供参考和额外的帮助。我希望我的程序中有很多语法错误,但在我修复第一个之前,我无法找到其余的。

/* 
 * A MathProg linear program to find an optimal HyperX topology of a
 *  given network size, switch radix, and bisection bandwidth.  Optimal
 *  is simplistically defined as minimum switch count network. 
 *
 * A HyperX topology is a multi-dimensional network (graph) where, in
 *  each dimension, the switches are fully connected.  Every switch
 *  (vertex) is a point in an L-dimensional integer lattic.  Each switch
 *  is identified by a multi-index I = (I_1, ..., I_L) where 0 <= I_k <
 *  S_k for each k = 1..L, where S_k is the number of switches in each
 *  dimension.  A switch connects to all others whose multi-index is the
 *  same in all but one coordinate.
 */

/* Network size in number of hosts. */
param hosts;

/* Desired bisection bandwidth. */
param bisec;

/* Maximum switch radix. */
param radix;

/* The number of dimensions in the HyperX. */
var L >= 1, integer;

/* The number of switches in each dimension. */
var S{1 .. L} >= 2, integer;

/* 
 * Relative bandwidth of the dimension, i.e., the number of links in a
 * given dimension. 
 */
var K{1 .. L} >= 1, integer;

/* The number Terminals (hosts) per switch */
var T >= 1, integer;

/* Minimize the total number of switches. */
minimize cost: prod{i in 1..L} S[i];

/* The total number of links must be less than the switch radix. */
s.t. Radix: T + sum{i in 1..L} K[i] * (S[i] - 1) <= radix;

/* There must be enough hosts in the network. */
s.t. Hosts: T * prod{i in 1..L} S[i] >= hosts;

/* There must be enough bandwidth. */
s.t. Bandwidth: min{K[i]*S[i]} / (2 * T) >= bisec;

/* The order of the dimensions doesn't matter, so constrain them */
s.t. SwitchDimen: forall{i in 1..(L-1)} S[i] <= S[i+1];

/* 
 * Bisection bandwidth depends on the smallest S_i * K_i, so we know
 * that the smallest switch count dimension needs the most links.
 */
s.t. LinkDimen: forall{i in 1..(L-1)} K[i] >= K[i+1];

# TODO: I would like to constrain the search such that the number of
# terminals, T, is bounded to T >= (hosts / O), where O is the switch
# count of the smallest switch count topology discovered so far, but I
# don't know how to do this.

/* Data section */
data;

param hosts := 32

param bisec := 0.5

param radix := 64

end;

1 个答案:

答案 0 :(得分:0)

问题中固定数量的变量是求解器和代数建模语言(包括AMPL / MathProg)中的常见假设。因此,您只能在索引表达式中使用常量表达式,特别是参数,而不是变量。一种可能的解决方案是使L成为参数,针对L的不同值解决您的问题,并选择能够提供最佳目标值的问题。这可以通过简单的AMPL脚本完成。