Cross-recursively defined variables

53 views
Skip to first unread message

at

unread,
Dec 15, 2009, 11:37:25 AM12/15/09
to AMPL Modeling Language
Here's a stripped down version of what I need to do:

var vs {t in 1..T)} =
an expression involving rw[t]

var rw {t in 1..(T-1)} =
an expression involving vs[t+1]

The problem of course is that when AMPL hits var vs it says that rw
isn't defined. But I can't define rw without reference to vs.

This seems like it should be a fairly standard problem (formulations
like this are encountered frequently in dynamic economic models) but I
haven't been able to determine how to deal with it cleanly in AMPL.
Any suggestions? Thanks!

at

unread,
Dec 15, 2009, 2:32:31 PM12/15/09
to AMPL Modeling Language
I could reformulate one of the var definitions as a constraint. Is
that a bad idea?

Paul

unread,
Dec 15, 2009, 4:39:00 PM12/15/09
to AMPL Modeling Language


at wrote:
>
> I could reformulate one of the var definitions as a constraint. Is
> that a bad idea?

That's the correct approach, and in fact I would typically put both
relationships in as constraints (rather than in the variable
declarations).

/Paul

at

unread,
Dec 16, 2009, 10:27:33 AM12/16/09
to AMPL Modeling Language
Thanks, but I'm surprised at the general rule you mentioned. In my
case, rw[t] is a linear function of vs[t+1], but vs[t] is a very
nonlinear function of rw[t]. My plan was to make the linear
relationship a constraint and leave the nonlinear one as a variable
declaration, since I was under the impression that nonlinear
constraints should generally be avoided. Does that seem correct?

I suppose it ultimately depends on what the presolve does and how the
problem gets presented to the solver...

Paul

unread,
Dec 16, 2009, 4:08:29 PM12/16/09
to AMPL Modeling Language
at wrote:

>
> Thanks, but I'm surprised at the general rule you mentioned.

More personal practice than "rule". :-)

> In my
> case, rw[t] is a linear function of vs[t+1], but vs[t] is a very
> nonlinear function of rw[t]. My plan was to make the linear
> relationship a constraint and leave the nonlinear one as a variable
> declaration, since I was under the impression that nonlinear
> constraints should generally be avoided. Does that seem correct?
>
> I suppose it ultimately depends on what the presolve does and how the
> problem gets presented to the solver...

I live in a highly linear world, so I rarely deal with NLPs (and I
don't know that I've ever modeled one using AMPL). My impression,
which could be wrong, is that if you specify an expression (linear or
nonlinear) in a variable declaration, AMPL substitutes that expression
anywhere the variable appears (essentially turning the variable into a
"macro" for the expression). If I'm right, you're getting a nonlinear
constraint either way.

/Paul

at

unread,
Dec 16, 2009, 5:03:33 PM12/16/09
to AMPL Modeling Language
Ah, well greetings from the highly NONlinear universe :). (Must be
nice over there!)

What you said sounds pretty plausible. But then it would seem then
that the tradeoff is between a more complicated objective function and
more constraints entering additively into Lagrangian. And so if that's
true it would probably be better to add them as constraints since
automatic differentiation should work more smoothly then...

I guess I'll have to just try it both ways. Thanks again for the
advice.

Robert Fourer

unread,
Dec 16, 2009, 5:07:49 PM12/16/09
to am...@googlegroups.com, at

AMPL does some clever things with defined variables, but not (as yet) clever
enough for the situation you are considering. One thing you could try is to
write

var vs {1..T};
var rw {1..T-1};

subj to vsDefn {t in 1..T-1}: vs[t] = <expr in rw[t]>;
subj to rwDefn {t in 1..T-1}: rw[t] = <expr in vs[t+1]>;

(I have not shown vsDefn[T] since there's no variable rw[T].) Then if you
set "option substout 1;" the vs variables will be substituted out of the
problem and the solver will only see the rw variables and the rwDefn
constraints. On the other hand, if the rwDefn statement is placed first,
the rw variables will be substituted out of the problem and the solver will
only see the vs variables and the vsDefn constraints. And if instead you
leave option substout at its default of zero, then the solver will see all
variables and constraints. In general some experimentation with the solver
is necessary to determine which of these possibilities enables the problem
to be solved fastest and most reliably.

Depending on your expressions it might be possible in principle to
substitute all of the variables except vs[T] out of the problem. However
it's not something you can do in AMPL without rewriting the model somewhat.

Bob Fourer
4...@ampl.com

P.S.: If either vs or rw is defined by a nonlinear function of the other,
you can expect that the solver will see nonlinear constraints.

Paul

unread,
Dec 16, 2009, 7:44:11 PM12/16/09
to AMPL Modeling Language
at wrote:

>
> Ah, well greetings from the highly NONlinear universe :). (Must be
> nice over there!)

It's like Kansas: pleasant but flat.

at

unread,
Dec 17, 2009, 10:27:01 AM12/17/09
to AMPL Modeling Language

Thanks, Bob, that helped a lot. It actually also solves another
question I had as well. I see now that you have a good discussion of
this in Appendix A.8.1, also helpful reading for anyone who may be
following this thread.

goyal198...@gmail.com

unread,
Aug 10, 2018, 2:35:57 PM8/10/18
to AMPL Modeling Language
from where did you find that..can you provide some references

AMPL Google Group

unread,
Aug 11, 2018, 2:41:35 AM8/11/18
to Ampl Modeling Language
Are you asking the reference of defined variable? You can refer to https://ampl.com/BOOK/CHAPTERS/24-refman.pdf at pp 467.

--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:639042554-18829#}
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



prateek goyal

unread,
Aug 13, 2018, 11:11:26 AM8/13/18
to am...@googlegroups.com
I am asking about  the way vs has been defined : keyword   ---     'Defn'  used after vs
subj to vsDefn {t in 1..T-1}: vs[t] 

I can't find it in the manual 

To unsubscribe from this group and stop receiving emails from it, send an email to ampl+unsubscribe@googlegroups.com.

To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



--
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/za0vKtBFy78/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+unsubscribe@googlegroups.com.

AMPL Google Group

unread,
Aug 14, 2018, 11:07:30 AM8/14/18
to Ampl Modeling Language
vsDefn is a user defined constraint name. Defn is not a keyword in AMPL. User could have used any other name for the constraint than vsDefn.

Thanks,
Paras


--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:639042554-18829#}
On Mon, Aug 13, 2018 at 3:11 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
I am asking about the way vs has been defined : keyword --- 'Defn' used after vs
subj to vsDefn {t in 1..T-1}: vs[t]

I can't find it in the manual



On Sat, Aug 11, 2018 at 6:41 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Are you asking the reference of defined variable? You can refer to https://ampl.com/BOOK/CHAPTERS/24-refman.pdf at pp 467.

--
Dr. Paras Tiwari
am...@googlegroups.com


To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages