AMPL 中已定义变量

问题描述 投票:0回答:1

`我是 AMPL 新手,我很难理解为什么我会收到有关已定义变量的反馈。

我不断收到消息说: 饮食0.mod 第 2 行偏移 16 Xbread 已经定义了 上下文:var >>> Xbread <<< >= 0; 接下来也已经定义了相应的变量。

Error message I'm getting

这是我正在使用的代码:

# Install dependencies
%pip install -q amplpy

# Google Colab & Kaggle integration
from amplpy import AMPL, ampl_notebook

ampl = ampl_notebook(
modules=\["coin"\],  # modules to install
license_uuid="default",  # license to use
)  # instantiate AMPL object and register magics

%%writefile diet0.mod

# Variables

var Xbread >= 0;
var Xmeat >= 0;
var Xpotatoes >= 0;
var Xcabbage >= 0;
var Xmilk >= 0;
var Xgelatin >= 0;

minimize cost:
0.30*Xbread + 1*Xmeat + 0.05*Xpotatoes + 0.08*Xcabbage + 0.23*Xmilk + 0.48*Xgelatin;

# Constraints
subject to A:
1254*Xbread+1457*Xmeat+318*Xpotatoes+46*Xcabbage+309*Xmilk+1725*Xgelatin>=3000;

subject to B:
39*Xbread+73*Xmeat+8*Xpotatoes+4*Xcabbage+16*Xmilk+43*Xgelatin >=70;

subject to C:
418*Xbread+41*Xmeat+42*Xpotatoes+141*Xcabbage+536*Xmilk+0*Xgelatin>=800;

subject to D:
70*Xpotatoes+860*Xcabbage+720*Xmilk>=500;

%%ampl_eval
model diet0.mod;
option solver cbc;
solve;
display Xbread; Xmeat; Xpotatoes; Xcabbage; Xmilk; Xgelatin;```

     

I have tried resetting the variables but it didn't fix the issue. I didn't find the answer in similar questions here. Any help/hints would be helpful.
python ampl
1个回答
0
投票

您需要在

reset;
之前添加
model diet0.mod;
,以便在加载模型之前清除会话。否则,如果您多次运行该单元,您将收到错误,因为先前的模型定义仍然存在。

另一个问题:在

display
命令中,您需要在参数之间使用逗号:
display Xbread, Xmeat, Xpotatoes, Xcabbage, Xmilk, Xgelatin;

以下应该有效:

%%ampl_eval
reset;
model diet0.mod;
option solver cbc;
solve;
display Xbread, Xmeat, Xpotatoes, Xcabbage, Xmilk, Xgelatin;
© www.soinside.com 2019 - 2024. All rights reserved.