【问题标题】:Incompatible types for operator with Mosel运算符类型与 Mosel 不兼容
【发布时间】:2020-04-25 19:53:40
【问题描述】:

我开始使用 Xpress Fico 工作台。我试图以这种方式在model 文件中定义一个简单的模型:

model ModelName
  options noimplicit
  uses "mmxprs"
  ! uses "mminsight" ! uncomment this line for an Xpress Insight model

  declarations
    ! indici
    indexes = 1..4
    contraints = 1..2

    x: array(indexes) of mpvar
    c: array(indexes) of integer

    A: array(contraints, indexes) of real
    B: array(contraints) of real

    ! Objective:linctr
    profit: linctr
  end-declarations

  !profit:=250*x1+230*x2+110*x3+350*x4
  c::[250, 230, 110, 350]
  profit:=sum(i in indexes) c(i)*x(i)

  ! 2*x1+1.5*x2+0.5*x3+2.5*x4<=100
  ! 0.5*x1+0.25*x2+0.25*x3+x4<=50
  A::[  2,  1.5,  0.5, 2.5,
      0.5, 0.25, 0.25,   1]
  B::[  100,
         50]

  forall(r in contraints) do
    sum(c in indexes) A(r, c) * x(c) <= B(r)! body...
  end-do

  writeln("Begin running model")
    maximise(profit)
    writeln("profit: ", getobjval)

    forall(i in indexes) do
      writeln("x( ", i, ")", getsol(x(i)))
    end-do
  writeln("End running model")
end-model

当我尝试构建文件时,我收到以下错误

Mosel: E-101 at (33,21) of `studio/esL01_01.1.mos': Incompatible types for operator (`array of integer' in `range' not defined).
Mosel: E-151 at (33,31) of `studio/esL01_01.1.mos': Incompatible type for subscript 2 of `A'.

有解决这个问题的建议吗?

【问题讨论】:

    标签: optimization linear-programming mosel


    【解决方案1】:

    对上一个答案的更正:Mosel 对运算符的评估应用标准优先规则(即,乘法优先于加法),因此从语言的角度来看,乘积项周围的括号不是必需的 - 尽管它们可能帮助提高可读性 - 所以你也可以写:

    forall(r in contraints) do
      sum(cc in indexes) A(r, cc) * x(cc) <= B(r)! body...
    end-do
    

    【讨论】:

      【解决方案2】:

      您的代码中有两个问题,

      首先,您在求和中使用 c 作为迭代器,但它在上面声明为数组。因此,Mosel 抱怨说您正在使用范围内的数组,这是您无法做到的。您不应该在 Mosel 中使用已声明的变量作为迭代器。所以我把你的迭代器改成了 cc。

      其次,您的目标是对 A(r, c) * x(c) 求和。您需要在它们周围加上括号,以便 Mosel 知道您的总和在哪里结束。否则,它将假定您只对第一个元素 A(r, cc) 求和。

      所以你的循环应该是这样的:

      forall(r in contraints) do
         sum(cc in indexes) (A(r, cc) * x(cc)) <= B(r)! body...
      end-do
      

      【讨论】:

        猜你喜欢
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-24
        • 2013-12-17
        • 2018-04-09
        • 1970-01-01
        • 2015-09-11
        相关资源
        最近更新 更多