4
$\begingroup$

Suppose I want an AMPL set to contain the integers 1 through 5. How can I declare this in the .dat file?

I can do it in the .mod file, like this:

things.mod:

set THINGS := 1..5;

Console:

ampl: model things.mod;
ampl: display THINGS;
set THINGS := 1 2 3 4 5;

ampl: display THINGS diff {2};
set THINGS diff  {2} := 1 3 4 5;

But now suppose I do this:

things.mod:

set THINGS;

things.dat:

set THINGS := 1..5;

Console:

ampl: model things.mod;
ampl: data things.dat;
ampl: display THINGS;
set THINGS := 1..5;

ampl: display THINGS diff {2};
set THINGS diff  {2} := 1..5;

AMPL seems to be treating 1..5 as a string rather than as an sequence of integers. Why, and how do I fix it?

$\endgroup$
  • 1
    $\begingroup$ You read the AMPL documentation. And if that doesn't suffice, you post at the AMPL forum groups.google.com/forum/#!forum/ampl, which I believe to be much better suited than this forum for addressing an AMPL syntax question, which really isn't an O.R. question, despite AMPL being (among other things) an O.R. tool. If your question were comparing optimization modeling tools, that would be a different story. $\endgroup$ – Mark L. Stone Jun 3 '19 at 1:32
  • $\begingroup$ @MarkL.Stone I did read the AMPL documentation, which offers no guidance on this question, as far as I can tell. If my question is on-topic here, then it's not really relevant if there is another site that might be better suited; I'd still like to ask it here. On the other hand, if you (or others) feel my question is off-topic, feel free to vote to close. I believe it is on-topic because AMPL is an OR tool, as you say, but my feelings won't be hurt if the community disagrees. $\endgroup$ – LarrySnyder610 Jun 3 '19 at 1:39
  • $\begingroup$ I'd rather make a comment than downvote or close posts into oblivion. $\endgroup$ – Mark L. Stone Jun 3 '19 at 2:09
  • 1
    $\begingroup$ One of the most useful threads on OR Exchange was a list of support forums and other resources for O.R. and optimization-related software. I suppose that would have to be Community Wiki to please the SE powers that be.If someone coud find that and post on OR SE, that would be a good starting point. $\endgroup$ – Mark L. Stone Jun 3 '19 at 2:16
  • $\begingroup$ @MarkL.Stone That's fine, you can of course make a comment and close-vote if you want. If you're telling me that this question isn't on topic for OR.SE then a close-vote is a perfectly valid mechanism to indicate that. $\endgroup$ – LarrySnyder610 Jun 3 '19 at 2:32
4
$\begingroup$

The .. operator cannot be used in the data section (i.e. it won't be expanded, see the AMPL FAQ). What you can do is to declare the limits in the model, e.g.

param n;
set things := 1..n;

and define them in the data file

param n := 5;
$\endgroup$
  • $\begingroup$ Yeah that is basically the approach I have used but it feels a bit like a workaround and I wondered if my syntax was just wrong in the .dat file. $\endgroup$ – LarrySnyder610 Jun 3 '19 at 2:01
  • 1
    $\begingroup$ True, could be easier, but that's the way it seems to be right now. It just saw this, confirming that .. won't work in the data part: ampl.com/faqs/… $\endgroup$ – Marcus Ritt Jun 3 '19 at 2:04
  • 1
    $\begingroup$ param n default 5; does what you'd expect; you can then change it in the data file, or not. $\endgroup$ – denis Nov 3 '19 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.