Variable based condition in a constraint - is there a workaround?

542 views
Skip to first unread message

lothar...@googlemail.com

unread,
Aug 28, 2018, 4:23:26 PM8/28/18
to AMPL Modeling Language
Hello,

I am performing some grid calcualtions/optimizations (calculation of voltages, currents, power flow, ...) using AMPL which works fine in general. I have to use solver knitro.

Now, I have to take a special constraint into account which bases on two variables: For each single element, an grid angle (first variable: phi) has to be lower than a limit value IN CASE the current via this element is higher than another limit value (second variable: i).

The angle has to be lower 30° (phi < 30°) if the current value is higher 300 A (i > 300A). In case the current is lower, there is no need to check the angle. The angle as well as the current are variables, effected by other data used by the optimizer.


I know that parameter based conditions can be taken into account easily, but variables are forbidden in general. Thus: Is there a workaround?

So far, I tried several possibilities to put the condition in a mathematical description like

    angle * ((atan((i-300)*5)+pi/2)/pi)   <=  30;      # i >= 0;  here, "5" is used as a "gain-factor"


This leads to the result:  Knitro 11.0.1: Current infeasible solution estimate cannot be improved.

Is there a better way to use a condition within a constaint?


Best regards
Lothar

 

AMPL Google Group

unread,
Aug 28, 2018, 9:13:31 PM8/28/18
to Ampl Modeling Language
You can use AMPL if-then-else in your constraint as discussed in https://ampl.com/resources/logic-and-constraint-programming-extensions/ Conditional Operators section. However, if variable appears in the condition, then AMPL treats the constraint as a nonlinear constraint. Since you are using Knitro, it should be able to handle nonlinear problem. Alternatively, you can enforce your condition by using Mixed Integer Programming. The MIPs are harder to solve than the continuous optimization problem. Knitro can solve Mixed Integer Nonlinear Problem. You can do some research about "enforcing logical condition in optimization" or "enforcing logical condition in ampl".

--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:651647338-21267#}
--
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.



KNITRO support team

unread,
Aug 29, 2018, 7:10:42 AM8/29/18
to AMPL Modeling Language
Dear Lothar,

To complement Paras' answer, let me propose another approach using complementarity constraints
You could introduce new slack variables related to the angle phi and the current i and apply a complementarity condition on them:

i < 300 + slack_i
phi < 30 + slack_phi
0 < slack_i complements slack_phi >0

You may have a look at this link for more information on complementarity constraints.

Knitro can make use of such special structures in its internal algorithm, although it still does not provide a guarantee of convergence to a feasible local optimal solution on non-convex models.

Please let us know if this helps or not.

Kind regards,
Sylvain Mouret

lothar...@googlemail.com

unread,
Aug 31, 2018, 10:16:18 AM8/31/18
to AMPL Modeling Language

Dear Sylvain,

unfortunately
I still have a problem with it, Maybe you can give me some hints to solve it. Let me mention that I am quite new in AMPL. Thus, I don't know the programming style really good.

The variables i and phi are calculated based on electrical conditions. I declared slack_i and slack_phi as variables to be >=0 and entered the complementary condition.
Within the program, I "calculated" the slack-variables as e.g. slack_i = i-300 using a let-command. Of course, now I detected that this approach has been wrong.

Nevertheless, it's seems not to be possible to define a variable as unsigned on the one hand and calculating it at the other side. Am I'm wrong? If no, what's the correct way to implement such an issue?

Best regards
Lothar




 

lothar...@googlemail.com

unread,
Aug 31, 2018, 10:16:19 AM8/31/18
to AMPL Modeling Language
Dear Sylvain,

I implemented it this way and it seems to work really good.

So far, there are some calculations which cannot be solved, but that's due to some strange grid parameter which lead to the fact that there is simply no solution.


Nevertheless, I get a lot of messages:

Nonsquare complementarity system:
    455 complementarities including 455 equations
    563 variables

Is there an option to deactivate this output?

Best regards
Lothar

lothar...@googlemail.com

unread,
Aug 31, 2018, 10:16:19 AM8/31/18
to AMPL Modeling Language
First of all: Thank's for your reply.

Unfortunatelly, I didn't get it. I tried the following code (the term dealing with q and p represents the angle, mentioned before) :


    subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b in BUS: b==k}:
   
         abs(branch_i_12[s,t,l,k,m,b]) > 300 ==> branch_q_12[s,t,l,k,m]^2 <= branch_p_12[s,t,l,k,m]^2 / 3;

This leads to the message: Sorry, knitro cannot handle logical constraints.
 
I also tried to put the "if" command in the conditional definition, but AMPL blaims about the syntax

    subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b in BUS: if ((b==k) && (sqrt(branch_i_12[s,t,l,k,m,b]^2) >= 300))}:
   
        branch_q_12[s,t,l,k,m]^2 <= branch_p_12[s,t,l,k,m]^2 / 3;


AMPL-Message:
syntax error
context:  subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b in BUS: if ((b==k) && (sqrt(branch_i_12[s,t,l,k,m,b]^2) >=  >>> 300))} <<< :

Any hints?

Best regards
Lothar

lothar...@googlemail.com

unread,
Aug 31, 2018, 10:16:19 AM8/31/18
to AMPL Modeling Language
Maybe, I've got it:


    subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b in BUS: b==k}:

        branch_q_12[s,t,l,k,m]^2 <= (if (sqrt(branch_i_12[s,t,l,k,m,b]^2) >= 3000) then branch_p_12[s,t,l,k,m]^2 / 3
                                     else 1e20);

What do you think about this solution?

Best regards
Lothar

AMPL Google Group

unread,
Aug 31, 2018, 6:11:21 PM8/31/18
to Ampl Modeling Language
When I replied to you, I was not aware of complementary constraint in Knitro. I think you should try complementary constraint as described by Sylvain. You can read about it at https://ampl.com/NEW/complement.html and https://www.artelys.com/tools/knitro_doc/2_userGuide/complementarity.html?highlight=complementarity. I have written a small AMPL model based on Sylvain suggestion and it should solve your problem:

var i>=0;
var slack_i;
var slack_phi;
var phi>=0;

subject to c1: i <= 300 + slack_i;
subject to c2: phi <= 30 + slack_phi;
subject to c3: slack_i>=0 complements slack_phi>=0;




--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:651647338-21267#}
On Fri, Aug 31, 2018 at 2:16 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
First of all: Thank's for your reply.

Unfortunatelly, I didn't get it. I tried the following code (the term
dealing with q and p represents the angle, mentioned before) :


subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b
in BUS: b==k}:

abs(branch_i_12[s,t,l,k,m,b]) > 300 ==> branch_q_12[s,t,l,k,m]^2
<= branch_p_12[s,t,l,k,m]^2 / 3;

This leads to the message: Sorry, knitro cannot handle logical constraints.

I also tried to put the "if" command in the conditional definition, but
AMPL blaims about the syntax

subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b
in BUS: if ((b==k) && (sqrt(branch_i_12[s,t,l,k,m,b]^2) >= 300))}:

branch_q_12[s,t,l,k,m]^2 <= branch_p_12[s,t,l,k,m]^2 / 3;


AMPL-Message:
syntax error
context: subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in
BRANCH, b in BUS: if ((b==k) && (sqrt(branch_i_12[s,t,l,k,m,b]^2) >= >>>
300))} <<< :

Any hints?

Best regards
Lothar





Am Dienstag, 28. August 2018 22:23:26 UTC+2 schrieb
lothar...@googlemail.com:



On Fri, Aug 31, 2018 at 2:16 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Maybe, I've got it:

subject to NB_ang_branch_12 {s in SCN, t in TME, (l,k,m) in BRANCH, b
in BUS: b==k}:

branch_q_12[s,t,l,k,m]^2 <= (if (sqrt(branch_i_12[s,t,l,k,m,b]^2)
>= 3000) then branch_p_12[s,t,l,k,m]^2 / 3
else 1e20);

What do you think about this solution?

Best regards
Lothar

--
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.



Best regards
Lothar
--
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



Best regards
Lothar





--
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



On Wed, Aug 29, 2018 at 11:10 AM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Dear Lothar,

To complement Paras' answer, let me propose another approach using complementarity constraints
You could introduce new slack variables related to the angle phi and the current i and apply a complementarity condition on them:


i < 300 + slack_i
phi < 30 + slack_phi
0 < slack_i complements slack_phi >0


You may have a look at this link for more information on complementarity constraints.

Knitro can make use of such special structures in its internal algorithm, although it still does not provide a guarantee of convergence to a feasible local optimal solution on non-convex models.

Please let us know if this helps or not.

Kind regards,
Sylvain Mouret

Le mercredi 29 août 2018 03:13:31 UTC+2, AMPL Google Group a écrit :



On Wed, Aug 29, 2018 at 1:13 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You can use AMPL if-then-else in your constraint as discussed in https://ampl.com/resources/logic-and-constraint-programming-extensions/ Conditional Operators section. However, if variable appears in the condition, then AMPL treats the constraint as a nonlinear constraint. Since you are using Knitro, it should be able to handle nonlinear problem. Alternatively, you can enforce your condition by using Mixed Integer Programming. The MIPs are harder to solve than the continuous optimization problem. Knitro can solve Mixed Integer Nonlinear Problem. You can do some research about "enforcing logical condition in optimization" or "enforcing logical condition in ampl".

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


On Tue, Aug 28, 2018 at 8:23 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Hello,

I am performing some grid calcualtions/optimizations (calculation of voltages, currents, power flow, ...) using AMPL which works fine in general. I have to use solver knitro.

Now, I have to take a special constraint into account which bases on two variables: For each single element, an grid angle (first variable: phi) has to be lower than a limit value IN CASE the current via this element is higher than another limit value (second variable: i).

The angle has to be lower 30° (phi < 30°) if the current value is higher 300 A (i > 300A). In case the current is lower, there is no need to check the angle. The angle as well as the current are variables, effected by other data used by the optimizer.


I know that parameter based conditions can be taken into account easily, but variables are forbidden in general. Thus: Is there a workaround?

So far, I tried several possibilities to put the condition in a mathematical description like

angle * ((atan((i-300)*5)+pi/2)/pi) <= 30; # i >= 0; here, "5" is used as a "gain-factor"


This leads to the result: Knitro 11.0.1: Current infeasible solution estimate cannot be improved.

Is there a better way to use a condition within a constaint?


Best regards
Lothar


lothar...@googlemail.com

unread,
Oct 4, 2018, 8:18:38 AM10/4/18
to AMPL Modeling Language
Dear all,

due to a lot of other work, I paused to work on this problem so far. Now, I try to get it in operation.

First of all, many thanks for your contribution. Unfortunatelly, I doesn't seem to work correctly.

So far, I don't have a grid layout which includes the mentioned problem. Thus, neglecting the variables as well as the constraints defining the problem leads to a correct optimization result ("Knitro 11.0.1: Locally optimal solution.").

Adding the variables as well as the constraints to the problem using the same grid, knitro doesn't find the same solution:

Knitro 11.0.1: Current infeasible solution estimate cannot be improved.
objective 196.0417719; feasibility error 129

Here is the relevant part of my code relating the problem:

# defining slack variables
var branch_Dq2p_12 {(l,k,m) in BRANCH, b in BUS: b==k} >= 0;
var branch_Dq2p_21 {(l,k,m) in BRANCH, b in BUS: b==m} >= 0;
var branch_Di_12   {(l,k,m) in BRANCH, b in BUS: b==k} >= 0;
var branch_Di_21   {(l,k,m) in BRANCH, b in BUS: b==m} >= 0;

param iMAX   = 0.3;       # kA

subject to NB_ang_branch_12a {(l,k,m) in BRANCH, b in BUS: b==k}:    branch_i_12[l,k,m,b] <= iMAX + branch_Di_12[l,k,m,b];
subject to NB_ang_branch_12b {(l,k,m) in BRANCH, b in BUS: b==k}:    branch_q_12[l,k,m]^2 <= branch_p_12[l,k,m]^2 /3 + branch_Dq2p_12[l,k,m,b];      # here, branch_q_12[l,k,m]^2 = branch_p_12[l,k,m]^2 /3  "is equal to" 30°
subject to NB_ang_branch_12  {(l,k,m) in BRANCH, b in BUS: b==k}:    branch_Dq2p_12[l,k,m,b] >= 0 complements branch_Di_12[l,k,m,b] >= 0;

subject to NB_ang_branch_21a {(l,k,m) in BRANCH, b in BUS: b==m}:    branch_i_21[l,k,m,b] <= iMAX + branch_Di_21[l,k,m,b];
subject to NB_ang_branch_21b {(l,k,m) in BRANCH, b in BUS: b==m}:    branch_q_21[l,k,m]^2 <= branch_p_21[l,k,m]^2 /3 + branch_Dq2p_12[l,k,m,b];      # here, branch_q_21[l,k,m]^2 = branch_p_21[l,k,m]^2 /3  "is equal to" 30°
subject to NB_ang_branch_21  {(l,k,m) in BRANCH, b in BUS: b==m}:    branch_Dq2p_21[l,k,m,b] >= 0 complements branch_Di_21[l,k,m,b] >= 0;

problem  prb_Test: Q_max, ... branch_Dq2p_12, branch_Di_12, branch_Dq2p_21, branch_Di_21, ... NB_ang_branch_12, NB_ang_branch_12a, NB_ang_branch_12b, NB_ang_branch_21, NB_ang_branch_21a, NB_ang_branch_21b;

let {(l,k,m) in BRANCH, b in BUS: b==k}  branch_Di_12  [l,k,m,b] := 0;
let {(l,k,m) in BRANCH, b in BUS: b==m}  branch_Di_21  [l,k,m,b] := 0;
let {(l,k,m) in BRANCH, b in BUS: b==k}  branch_Dq2p_12[l,k,m,b] := 0;
let {(l,k,m) in BRANCH, b in BUS: b==m}  branch_Dq2p_21[l,k,m,b] := 0;


Knitro has to solve problem prb_Test.


Only deactivating the constraints NB_ang_branch_12 as well as NB_ang_branch_21 defing both as "0=0;" and displaying the following using the dummy variables out1 and out2, all values are zero:


var out1 {(l,k,m) in BRANCH, b in BUS: b==k} = if ((branch_i_12[l,k,m,b] > 0.3) and (branch_q_12[l,k,m]^2 > branch_p_12[l,k,m]^2 /3)) then 1 else 0;
var out2 {(l,k,m) in BRANCH, b in BUS: b==m} = if ((branch_i_21[l,k,m,b] > 0.3) and (branch_q_21[l,k,m]^2 > branch_p_21[l,k,m]^2 /3)) then 1 else 0;

display {(l,k,m) in BRANCH, b in BUS: b==k} out1[l,k,m,b];
display {(l,k,m) in BRANCH, b in BUS: b==m} out2[l,k,m,b];


In this case, I get the correct result - the locally optimal solution, initially calculated.

Any ideas how to get a solution activating the complementary constraints?

AMPL Google Group

unread,
Oct 4, 2018, 6:24:28 PM10/4/18
to Ampl Modeling Language
You need to examine your constraints and figure out what constraints is contributing towards the infeasibility. AMPL provides the command 'expand' to examine the constraints. Issue following command at AMPL command prompt

expand NB_ang_branch_12a;

You can see all the constraints generated by AMPL. It seems that NB_ang_branch_12a and NB_ang_branch_21a are two different set of complementary constraints. You can comment all three NB_ang_branch_21a and study the impact of NB_ang_branch_12a on your problem. If you get infeasible message with NB_ang_branch_12a, then both of your branch_Dq2p_21 and branch_Di_12 must be nonzero to satisfy the constraints. Now, you can use expand NB_ang_branch_12a; and expand NB_ang_branch_12b; to examine individual constraints generated by AMPL. You can compute the value of the expression by fixing the value of branch_Di_12 and branch_Dq2p_21. This should help you to understand the constraints and identify the infeasibility in your problem.

Thank You,
Paras


--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:677886369-24982#}

lothar...@googlemail.com

unread,
Jun 4, 2019, 10:02:10 AM6/4/19
to AMPL Modeling Language
Again, many Thank's for your replay, Paras. After facing (and solving) several other problems, I still have to deal with this one.

In the meantime I checked the contraints using the expand function. Unfortunately, they seems to be ok from my pont of view.


Having a look at an actual calculation, maybe I disunderstand the complementary function:

It seems to be ok in case of a actual problem. Thus, either slack_i or slack_phi is greater than zero. Here, the optimization finds a solution.
In case there isn't any problem, which means that slack_i as well as slack_phi is zero, AMPL (KNITRO) tells me that it convergence to an infeasible point. Does the complementary function exclude the possibility that both variables are zero?

Today I "shifted" the constaint to the objective funktion ("To" and "From" relating both sides of a line) simply using the product of both variables:

minimize Q_GCP + 1e6*sum ( (slack_i_From * slack_phi_From)^2  +  (slack_i_To * slack_phi_To)^2 );

and the result looks quite promising. Here, Q_GCP is the main objective. Using a power function (squared) seems to be necessary to avoid an error message(s)

Error: Knitro-AMPL failed to add quadratic objective structure.
Error: Knitro-AMPL could not load linear or quadratic structures.

So far, I don't know why KNITRO blaims about it. Of course, doing so, it isn't mandatory to hold the limits (current as well as angle) strictly. Thus, I used facor 1e6.

Best regards,
Lothar




lothar...@googlemail.com

unread,
Jun 4, 2019, 5:42:50 PM6/4/19
to AMPL Modeling Language
I think, I've got it:

So far, I am writing

slack_i >= 0 complements slack_phi >= 0;

I guess I simply have to write

slack_i > 0 complements slack_phi > 0;

Sylvain mentioned this in his reply, but I didn't recognized this detail. I will try it tomorrow in the office.

Best regards
Lothar

lothar...@googlemail.com

unread,
Jun 5, 2019, 9:58:40 AM6/5/19
to AMPL Modeling Language
Unfortunately it seems not to be possible to use a single ">" instead of ">=". So it doesn't work.

Any ideas?

Best regards
Lothar

AMPL Google Group

unread,
Jun 5, 2019, 10:32:15 AM6/5/19
to Ampl Modeling Language
That is right, you cannot use the > and < operators in defining constraints for an optimization problem. (Technically, the minimum or maximum is not well defined when these operators are used.)

The constraint "slack_i >= 0 complements slack_phi >= 0" says that the following conditions must hold:

slack_i >= 0,
slack_phi >= 0,
either slack_i = 0 or slack_phi = 0

What are the conditions on slack_i and slack_phi that you want in your problem?

--
Robert Fourer
am...@googlegroups.com
{#HS:871306549-45768#}

AMPL Google Group

unread,
Jun 5, 2019, 10:36:06 AM6/5/19
to Ampl Modeling Language
If you have any more questions about error messages from the Knitro solver, post the entire text of all messages from Knitro. That will show the Knitro version and possibly other useful information. Also, attach your files if that is possible.

--
Robert Fourer
am...@googlegroups.com
{#HS:870277478-45684#}

lothar...@googlemail.com

unread,
Jun 5, 2019, 11:14:20 AM6/5/19
to AMPL Modeling Language
Hello Robert,

The following conditions must hold:


slack_i >= 0,
slack_phi >= 0,
either slack_i = 0 or slack_phi = 0 or BOTH can be 0.

Having a look at the KNITRO manual (https://www.artelys.com/docs/knitro/2_userGuide/complementarity.html) it is stated that both may be 0 as well.

Nevertheless, in the meantime I am not shure if this is the problem. I am getting some really strange (extremely different) results at the moment performing only some "neglectable" modifications.

Best regards,
Lothar

AMPL Google Group

unread,
Jun 5, 2019, 11:19:47 AM6/5/19
to Ampl Modeling Language
Yes, "or both" is assumed. If you want


slack_i >= 0,
slack_phi >= 0,
slack_i = 0 or slack_phi = 0 (or both)

then "slack_i >= 0 complements slack_phi >= 0" is the right way to write it.

--
Robert Fourer
am...@googlegroups.com
{#HS:871389145-45778#}
On Wed, Jun 5, 2019 at 2:35 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
If you have any more questions about error messages from the Knitro solver, post the entire text of all messages from Knitro. That will show the Knitro version and possibly other useful information. Also, attach your files if that is possible.

--
Robert Fourer
am...@googlegroups.com

On Wed, Jun 5, 2019 at 2:31 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
That is right, you cannot use the > and < operators in defining constraints for an optimization problem. (Technically, the minimum or maximum is not well defined when these operators are used.)

The constraint "slack_i >= 0 complements slack_phi >= 0" says that the following conditions must hold:


slack_i >= 0,
slack_phi >= 0,
either slack_i = 0 or slack_phi = 0

What are the conditions on slack_i and slack_phi that you want in your problem?

--
Robert Fourer
am...@googlegroups.com

On Wed, Jun 5, 2019 at 1:58 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Unfortunately it seems not to be possible to use a single ">" instead of ">=". So it doesn't work.

Any ideas?

Best regards
Lothar


On Tue, Jun 4, 2019 at 9:42 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
I think, I've got it:
So far, I am writing

slack_i >= 0 complements slack_phi >= 0;

I guess I simply have to write

slack_i > 0 complements slack_phi > 0;

Sylvain mentioned this in his reply, but I didn't recognized this detail. I will try it tomorrow in the office.

Best regards
Lothar


Reply all
Reply to author
Forward
0 new messages