## how to model abs(variable) linearly Topic is solved

Problems with syntax of GAMS
yanzhiping
User
Posts: 85
Joined: 2 years ago

### how to model abs(variable) linearly

excuse me,
I have a variable Q, it can either be positive or negative.
the objective is 2* abs(Q).
but in this way the abs() will make the problem non-linear.
How can I keep the problem linear while getting the absolute value of the variable Q?

Gideon Kruseman
User
Posts: 23
Joined: 1 year ago

### Re: how to model abs(variable) linearly

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

dirkse
Moderator
Posts: 47
Joined: 2 years ago
Location: Fairfax, VA

### Re: how to model abs(variable) linearly

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.

Gideon Kruseman
User
Posts: 23
Joined: 1 year ago

### Re: how to model abs(variable) linearly

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

yanzhiping
User
Posts: 85
Joined: 2 years ago

### Re: how to model abs(variable) linearly

I should mention that my problem is minimization.

I wonder if min(abs(Q)) is available in any version of GAMS now.
dirkse wrote: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.

yanzhiping
User
Posts: 85
Joined: 2 years ago

### Re: how to model abs(variable) linearly

should they be defined as below?
positive variables Qpos;
negative variables Qneg;
Gideon Kruseman wrote:positive variables Qpos, Qneg;

Q =e= Qpos - Qneg;

* objextive now becomes 2*(Qpos+Qneg)

Gideon Kruseman
User
Posts: 23
Joined: 1 year ago

### Re: how to model abs(variable) linearly

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

Gideon Kruseman
User
Posts: 23
Joined: 1 year ago

### Re: how to model abs(variable) linearly

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

Gideon Kruseman
User
Posts: 23
Joined: 1 year ago

### Re: how to model abs(variable) linearly

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

yanzhiping
User
Posts: 85
Joined: 2 years ago

### Re: how to model abs(variable) linearly

what is the relationship between Qpos, Qneg ?
Does it mean Qpos, Qneg cannot be positive at the same time?
Should we define two binary variables to constraint that?

Qpos<=max_Qpos * x_Qpos
Qneg<=max_Qneg * x_Qneg
x_Qpos + x_Qneg<=1