0

`I'm new to AMPL and I'm having a hard time understanding why I'm getting a feedback about my variables being already defined.

I keep getting message stating: diet0.mod line 2 offset 16 Xbread is already defined context: var >>> Xbread <<< >= 0; followed by respective variables being also already defined.

Error message I'm getting

This is the code I'm working with:

# 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.
1
  • 1
    Please post the error as a text, ant an screenshot Commented Feb 1, 2024 at 2:13

1 Answer 1

0

You need to add reset; before model diet0.mod; so that the session is cleared before loading the model. Otherwise you will get errors due to the previous model definition being still present if you run that cell more than once.

Another issue: in the display command you need to use commas between the arguments: display Xbread, Xmeat, Xpotatoes, Xcabbage, Xmilk, Xgelatin;

The following should work:

%%ampl_eval
reset;
model diet0.mod;
option solver cbc;
solve;
display Xbread, Xmeat, Xpotatoes, Xcabbage, Xmilk, Xgelatin;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.