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
how to model abs(variable) linearly Topic is solved
 Gideon Kruseman
 User
 Posts: 15
 Joined: 4 months ago
Re: how to model abs(variable) linearly
Gideon Kruseman
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData
Re: how to model abs(variable) linearly
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
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 8 times
 Gideon Kruseman
 User
 Posts: 15
 Joined: 4 months ago
Re: how to model abs(variable) linearly
Hi Steve,
May I suggest changing equation f into:
f .. sqr(xcx) + sqr(ycy) =L= 1;
This way you y will be negative and it shows better that the abs tricks work
best,
Gideon
May I suggest changing equation f into:
f .. sqr(xcx) + sqr(ycy) =L= 1;
This way you y will be negative and it shows better that the abs tricks work
best,
Gideon
Gideon Kruseman
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData

 User
 Posts: 85
 Joined: 7 months ago
Re: how to model abs(variable) linearly
is it possible that
Suppose Q takes on value 10
then due to the equation Q =e= Qpos Qneg;
Qpos takes on value 20 and Qneg takes on value 10;
Suppose Q takes on value 10
then Qpos takes on value 10 and Qneg talkes on value 20;
in either case the objective variable Z because of Z =g= 2*(Qpos + Qneg);
takes on value 60;
Suppose Q takes on value 10
then due to the equation Q =e= Qpos Qneg;
Qpos takes on value 20 and Qneg takes on value 10;
Suppose Q takes on value 10
then Qpos takes on value 10 and Qneg talkes on value 20;
in either case the objective variable Z because of Z =g= 2*(Qpos + Qneg);
takes on value 60;

 User
 Posts: 85
 Joined: 7 months ago
Re: how to model abs(variable) linearly
what is the difference whether y is positive or negative ?

 User
 Posts: 85
 Joined: 7 months ago
Re: how to model abs(variable) linearly
I tried your code, and it worked well.
one question is for the second method, if we use Gideon's trick,can we make the problem linear or MIP?
In the third method, I like the way you use macro. is it possible if you define a macro outside of your code, could you still call that macro function?
one question is for the second method, if we use Gideon's trick,can we make the problem linear or MIP?
In the third method, I like the way you use macro. is it possible if you define a macro outside of your code, could you still call that macro function?
Re: how to model abs(variable) linearly
Gideon Kruseman wrote:Hi Steve,
May I suggest changing equation f into:
f .. sqr(xcx) + sqr(ycy) =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

 User
 Posts: 85
 Joined: 7 months ago
Re: how to model abs(variable) linearly
I think the definition should be as below:
free variable W;
positive variable Qpos,Qneg;
Absolute_definition_of_Q.. Q = Qpos+Qneg;
objective Z;
W =E=(QposQneg);
minimize Z;
free variable W;
positive variable Qpos,Qneg;
Absolute_definition_of_Q.. Q = Qpos+Qneg;
objective Z;
W =E=(QposQneg);
minimize Z;
Gideon Kruseman wrote: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 = QposQneg;
equation Objective;
Objective.. Z =g= 2*(Qpos+Qneg);
 Gideon Kruseman
 User
 Posts: 15
 Joined: 4 months ago
Re: how to model abs(variable) linearly
Yanzhiping,
Thet way I initially formulated the response to your question is that you have some sort of variable Q that can be either positive or negative. I assumed you wanted to have the absolute value of Q.
If this is not the case I misunderstood your question. If thgis is the case, the code I provided is the correct way to model this problem.
best,
Gideon
Thet way I initially formulated the response to your question is that you have some sort of variable Q that can be either positive or negative. I assumed you wanted to have the absolute value of Q.
If this is not the case I misunderstood your question. If thgis is the case, the code I provided is the correct way to model this problem.
best,
Gideon
Gideon Kruseman
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData
exante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socioecnomic data @CGIAR_BigData
Who is online
Users browsing this forum: No registered users and 1 guest