AMPL First Usage Issues

17 views
Skip to first unread message

Omar El-Menshawy

unread,
Nov 24, 2022, 12:12:46 PM11/24/22
to AMPL Modeling Language
Dear All,

This is my first time to use AMPL, and I'm facing some issues. Would someone help me with them?

I'm used the AMPL official book to create the attached model, and trying to figure out the problems.

This is my first issue which i couldn't solve.
i is not defined
context:  var y =if {(i  >>> in  <<< A,j in A)}> 0 then 1 else 0;

Thanks in advance
Omar
Thesis Model Replication.mod

AMPL Google Group

unread,
Nov 25, 2022, 1:27:50 PM11/25/22
to AMPL Modeling Language
Your "var y = . . ." does not conform to the AMPL syntax for defined variables. But you do not need to define y in this way. Instead you should simply write

var y {i in A, j in A} binary;

Then your Arc_Capacities constraint will insure that x[i,j] > 0 implies y[i,j] = 1. Because your minimization objective includes the term "Arc_Fixed_Cost[i,j] * y[i,j]", any optimal solution that has x[i,j] = 0 (and Arc_Fixed_Cost[i,j] > 0) will also have y[i,j] = 0, so you do not need a "subject to" constraint for that.

You will need to make a similar correction for var v, which should not be hard to figure out.

Note that to say a variable is binary, your model should "binary" in the definition of the variable. Thus you should delete the "subject to Binary" constraints from your model.


--
Robert Fourer
am...@googlegroups.com
{#HS:2077776189-112961#}

Omar El-Menshawy

unread,
Nov 25, 2022, 10:43:10 PM11/25/22
to am...@googlegroups.com
Thanks a lot for your feedback

Now, the model is giving me the below error, and in everytime it changes the set. Am I defining the sets in the wrong way? or what's the problem exactly?
"S is already defined
context:  set  >>> S; <<<         #Set of Input Nodes (S)"

And would you please advise me with an example model which i can use to check my model. As I'm always facing a different error.

Thanks in advance for your support


--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/WmUuyOd0S9M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-2077776189-6181125277-1669400866-1507244607%40helpscout.net.

AMPL Google Group

unread,
Nov 25, 2022, 11:42:44 PM11/25/22
to AMPL Modeling Language
After you make changes to your model file, be sure to reset AMPL before trying to read the file again. For example:

reset;
model "Thesis Model Replication.mod";


--
Robert Fourer
am...@googlegroups.com
{#HS:2077776189-112961#}
On Sat, Nov 26, 2022 at 3:43 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thanks a lot for your feedback

Now, the model is giving me the below error, and in everytime it changes the set. Am I defining the sets in the wrong way? or what's the problem exactly?
"S is already defined
context: set >>> S; <<< #Set of Input Nodes (S)"

And would you please advise me with an example model which i can use to check my model. As I'm always facing a different error.

Thanks in advance for your support

On Fri, Nov 25, 2022 at 6:27 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Your "var y = . . ." does not conform to the AMPL syntax for defined variables. But you do not need to define y in this way. Instead you should simply write

var y {i in A, j in A} binary;

Then your Arc_Capacities constraint will insure that x[i,j] > 0 implies y[i,j] = 1. Because your minimization objective includes the term "Arc_Fixed_Cost[i,j] * y[i,j]", any optimal solution that has x[i,j] = 0 (and Arc_Fixed_Cost[i,j] > 0) will also have y[i,j] = 0, so you do not need a "subject to" constraint for that.

You will need to make a similar correction for var v, which should not be hard to figure out.

Note that to say a variable is binary, your model should "binary" in the definition of the variable. Thus you should delete the "subject to Binary" constraints from your model.


--
Robert Fourer
am...@googlegroups.com

Omar El-Menshawy

unread,
Nov 27, 2022, 7:23:36 PM11/27/22
to am...@googlegroups.com
Yes. I did that before but still got the same error message.

Any other suggestions?

Thanks in advance

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/WmUuyOd0S9M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.

AMPL Google Group

unread,
Nov 28, 2022, 1:10:44 PM11/28/22
to AMPL Modeling Language
This error can also happen if you have a model file and a data file, but you use the "model" command to read both of them. For example, where you are supposed to enter

model sched1.mod;
data sched.dat;

if instead you enter

model sched1.mod;
model sched.dat;

(and sched1.dat does not begin with a "data" statement) then you will get an "already defined" error:

ampl: model sched1.mod;
ampl: model sched.dat;

..\sched/sched.dat, line 2 (offset 6):

SHIFTS is already defined
context: set >>> SHIFTS <<< := Mon1 Tue1 Wed1 Thu1 Fri1 Sat1

If this is not your problem, however, then to get more help, send the files that you are using. It is best to include them as attachments to your email, but if that won't work then you can copy and paste them into your email's text.


--
Robert Fourer
am...@googlegroups.com
{#HS:2077776189-112961#}
On Mon, Nov 28, 2022 at 12:23 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Yes. I did that before but still got the same error message.

Any other suggestions?

Thanks in advance

On Sat, Nov 26, 2022 at 4:42 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
After you make changes to your model file, be sure to reset AMPL before trying to read the file again. For example:

reset;
model "Thesis Model Replication.mod";


--
Robert Fourer
am...@googlegroups.com
sched.dat
sched1.mod
Reply all
Reply to author
Forward
0 new messages