help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Constraint on a binary variable


From: Philippe Jugla
Subject: RE: Constraint on a binary variable
Date: Wed, 30 Jun 2021 16:27:02 +0000

Hi Heinrich

That's a really elegant way to model it..
Just finished testing it, and works just fine.

Thanks a lot,
Best regards,
Philippe


De : Heinrich Schuchardt <xypron.glpk@gmx.de>
Envoyé : mercredi 30 juin 2021 17:40
À : Philippe Jugla <philippe.jugla@hotmail.fr>
Cc : help-glpk@gnu.org <help-glpk@gnu.org>
Objet : Re: Constraint on a binary variable
 
On 6/30/21 5:24 PM, Philippe Jugla wrote:
> Hi everyone,
>
> In my model, I have a variable "p" which has an upper bound pmax := 2.5
> and a lower bound pmin := -2.5.
> I would simply like to add a binary variable "sign" which takes the values :
>
> 1 if variable p is positive
> 0 if variable p is negative (or vice-versa)
>
> I thought of something like :
>
> s.t c1 : sign >= p[t] / pmax;
>
> So if p[t] is positive : sign >0 and <1, therefore sign will be forced to 1.
> However, if p[t] is negative, "sign" is greater than a negative value so
> the variable is no longer constrained, and can take values 0 or 1.
>
> How can I work things out ?
>
> Thanks for your help
>
> Best regards,
> Philippe

var sign, binary;
var p, >= -2.5, <= 2.5;
s.t. constraint: p <= 2.5 * sign;
#maximize obj: p;
minimize obj: p;
solve;
display p, sign;
end;

Best regards

Heinrich


reply via email to

[Prev in Thread] Current Thread [Next in Thread]