how to model abs(variable) linearly Topic is solved

Problems with syntax of GAMS
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

positive variables Qpos, Qneg;

Q =e= Qpos - Qneg;

* objextive now becomes 2*(Qpos+Qneg)
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: how to model abs(variable) linearly

Post by dirkse »

I was just editing something in the next generation of GAMS documentation about this very example, so I'll comment here that you need to be minimizing abs(Q). If you're maximizing then you'll have to go to a MIP or perhaps a nonlinear formulation, but the nonlinear problem will be nonconvex so you might as well leave it as a MIP.
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

Of course Steve, I forgot to mention that in my reply to the question. My bad.
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

both variables should be defined as positive.

positive variables Qpos, Qneg;

free variables Q, Z "objective variable";

equation Absolute_definition_of_Q;

Absolute_definition_of_Q.. Q = Qpos-Qneg;

equation Objective;

Objective.. Z =g= 2*(Qpos+Qneg);
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

Absolute_definition_of_Q.. Q =e= Qpos-Qneg;
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

If you want to define Qneg as negative variable, you have to rearrange the signs in all the equations so it makes sense. Defining both as positive variables is the common way to do this.
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

You are making things way to complicated,

Suppose Q takes on value 10
then due to the equation Q =e= Qpos -Qneg;
Qpos takes on value 10 and Qneg takes on value 0;

Suppose Q takes on value -10
then Qpos takes on value 0 and Qneg talkes on value 10;

in either case the objective variable Z because of Z =g= 2*(Qpos + Qneg);
takes on value 20;

This is the bais for least absolute deviations calculations (although you don't need the factor 2, it is completely redundant).

read: http://www.amsterdamoptimization.com/pdf/ols.pdf
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: how to model abs(variable) linearly

Post by dirkse »

Yanzhiping,

I am attaching a little example that has 3 versions of the same model. A variant of this will be in the updated docs so I thought I'd give it a trial run here.

The first uses abs() and therefore solves as a DNLP. This is not the recommended way. It is what you want to avoid.

The second reformulates abs() using the trick Gideon mentioned in his first post. The objective is linear, but since we have a nonlinear constraint we solve using NLP. A nice, smooth NLP. This works since the objective is min(abs(x)). This is the way to go.

The third formulation approximates the abs function via a nonlinear smoothing function. You don't need this in your case so you can ignore this if you find it confusing, but if the objective was working against you rather than for you, this might be useful.

HTH,

-Steve
Attachments
absReform.gms
(1.04 KiB) Downloaded 301 times
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: how to model abs(variable) linearly

Post by Gideon Kruseman »

Hi Steve,

May I suggest changing equation f into:
f .. sqr(x-cx) + sqr(-y-cy) =L= 1;

This way you y will be negative and it shows better that the abs tricks work

best,

Gideon
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: how to model abs(variable) linearly

Post by dirkse »

Gideon Kruseman wrote:Hi Steve,

May I suggest changing equation f into:
f .. sqr(x-cx) + sqr(-y-cy) =L= 1;

This way you y will be negative and it shows better that the abs tricks work

best,

Gideon
Gideon,

Thanks for the suggestion. Indeed, it helps to show the reform tricks are working when the abs() function is "evaluated" correctly on both branches.

-Steve
Post Reply