Ampl Rounding

372 views
Skip to first unread message

curti...@elpaso.com

unread,
Feb 20, 2009, 7:26:28 PM2/20/09
to AMPL Modeling Language
I want to round in ampl, so that 2.5 rounds up to 3, and also that
-2.5 rounds to -3. The ampl round function rounds these to 2 and -2.

My plan was to replace each round(x) in ampl with a formula like
trunc(abs(x) + 0.5) * (x/max(abs(x), .00001)).

I use the round function about 700 times in my model, and some of the
expressions are quite complicated, so replacing them all would be
error-prone and lead to confusing code.

I tried to setup a user defined function, but this is not supported by
ilog ampl version.

Does anybody have a better idea to do this?

Robert Fourer

unread,
Feb 22, 2009, 10:29:11 PM2/22/09
to am...@googlegroups.com, curti...@elpaso.com

Is the AMPL round() function basically doing what you want, except that it
rounds n.5 to n and -n.5 to -n when n is even? Then I think you might be
satisfied to replace round(r) by

trunc(r + if r >= 0 then .5 else -.5)

The version of AMPL that ILOG now distributes does support user-defined
functions. I'm not sure what it takes to get them to update your copy to
that version. (If you switch to the AMPL company's license agreement then
you can pay one year's maintenance -- 15% of the relevant purchase price --
and get the latest version including user-defined functions.)

Bob Fourer
4...@ampl.com

Reply all
Reply to author
Forward
0 new messages