sum over set with index

531 views
Skip to first unread message

Janina Sand

unread,
May 8, 2021, 8:35:06 AM5/8/21
to AMPL Modeling Language
Hello all,

I am getting an error message by defining a constraint.

I would like to model the following constraint:

Bildschirmfoto 2021-05-08 um 12.54.45.png

I tried to model it like below:

Bildschirmfoto 2021-05-08 um 12.55.57.png

Can somebody please help me where my mistake is?

Thanks a lot in advance!!

BR

Janina

AMPL Google Group

unread,
May 9, 2021, 2:45:00 PM5/9/21
to AMPL Modeling Language
The indexing expression has two errors. First, l in 1..l should be l in 1..L. Second, you need to define l before you write k in 1..K_l[l]. Thus you should have

{l in 1..L, k in 1..K_l[l]}

You can use the AMPL command "expand XYC;" to see the constraints that AMPL is generating and check whether they are what you want.


--
Robert Fourer
am...@googlegroups.com
{#HS:1507525618-104189#}

Janina Sand

unread,
May 11, 2021, 11:41:27 AM5/11/21
to AMPL Modeling Language
Dear Robert, 

thank you for your help!

Does it work the same way for  constraint (17)? So it would be :

subj to C17 {l in 1..L}: sum{l in 1..L, k in 1..K_l[l], i in N} x_oik[i,k] = K_l[l]; 

Thanks in advance for help

BR
Janina



Janina Sand

unread,
May 11, 2021, 3:30:18 PM5/11/21
to AMPL Modeling Language
I would also need some help how to build the objective function. 
Bildschirmfoto 2021-05-11 um 21.23.39.png
I tried like this but it does not seem correct:

Bildschirmfoto 2021-05-11 um 21.27.10.png

I know that I have to define h in C_h[h] somehow? 
C_max is supposed to be the maximum out of the complecion times of products h =1..H

I appreciate any help!

Thanks

Janina

AMPL Google Group

unread,
May 11, 2021, 10:18:04 PM5/11/21
to AMPL Modeling Language
If you index constraint C17 over {l in 1..L}, you can't also sum over {l in 1..L} in the constraint's expression. Maybe you just want to leave "l in 1..L" out of the summation:

subj to C17 {l in 1..L}: sum{k in 1..K_l[l], i in N} x_oik[i,k] = K_l[l];

Your objective "minimize Makespan: C_max;" is good, but you should define just "var C_max;" and then add a constraint "C_max >= C_h[h]" for every h in 1..H.


--
Robert Fourer
am...@googlegroups.com
{#HS:1507525618-104189#}
On Tue, May 11, 2021 at 7:30 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
I would also need some help how to build the objective function.
Bildschirmfoto 2021-05-11 um 21.23.39.png
I tried like this but it does not seem correct:

Bildschirmfoto 2021-05-11 um 21.27.10.png

I know that I have to define h in C_h[h] somehow?
C_max is supposed to be the maximum out of the complecion times of products h =1..H

I appreciate any help!

Thanks

Janina

On Tue, May 11, 2021 at 3:41 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,

thank you for your help!

Does it work the same way for constraint (17)? So it would be :

subj to C17 {l in 1..L}: sum{l in 1..L, k in 1..K_l[l], i in N} x_oik[i,k] = K_l[l];

Thanks in advance for help

BR
Janina

On Sun, May 9, 2021 at 6:44 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
The indexing expression has two errors. First, l in 1..l should be l in 1..L. Second, you need to define l before you write k in 1..K_l[l]. Thus you should have

{l in 1..L, k in 1..K_l[l]}

You can use the AMPL command "expand XYC;" to see the constraints that AMPL is generating and check whether they are what you want.


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

Janina Sand

unread,
May 12, 2021, 7:28:14 AM5/12/21
to AMPL Modeling Language
Dear Robert, 

thank you so much for your help!

now I tried to solve my model but it is say for the data file that e.g H is not a parameter:
Bildschirmfoto 2021-05-12 um 13.22.17.png
and also when I try to solve it it shows:

Bildschirmfoto 2021-05-12 um 13.23.20.png

I do not know where my mistake is. When I check the mod file for mistakes by AMPL it is not showing me any error.

BR
Janina

Janina Sand

unread,
May 12, 2021, 7:32:14 AM5/12/21
to AMPL Modeling Language


Bildschirmfoto 2021-05-12 um 13.29.52.png


Bildschirmfoto 2021-05-12 um 13.29.43.png

maybe my mod and data file make it more clear

AMPL Google Group

unread,
May 12, 2021, 10:59:10 AM5/12/21
to AMPL Modeling Language
You need to read a model file before the data file. For example, if you have a model file named FinalMod.mod in the same folder as the data file, you would give these commands:

ampl: reset;
ampl: model '/Users/Jani/Desktop/AMPL Beispiele/FinalMod.mod';
ampl: data '/Users/Jani/Desktop/AMPL Beispiele/FinalDat.dat';



--
Robert Fourer
am...@googlegroups.com
{#HS:1507525618-104189#}
On Wed, May 12, 2021 at 11:32 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
[image: Bildschirmfoto 2021-05-12 um 13.29.52.png]


[image: Bildschirmfoto 2021-05-12 um 13.29.43.png]

maybe my mod and data file make it more clear
Janina Sand schrieb am Mittwoch, 12. Mai 2021 um 13:28:14 UTC+2:

On Wed, May 12, 2021 at 11:28 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,

thank you so much for your help!

now I tried to solve my model but it is say for the data file that e.g H is not a parameter:
Bildschirmfoto 2021-05-12 um 13.22.17.png
and also when I try to solve it it shows:

Bildschirmfoto 2021-05-12 um 13.23.20.png

I do not know where my mistake is. When I check the mod file for mistakes by AMPL it is not showing me any error.

BR
Janina

On Wed, May 12, 2021 at 2:17 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
If you index constraint C17 over {l in 1..L}, you can't also sum over {l in 1..L} in the constraint's expression. Maybe you just want to leave "l in 1..L" out of the summation:

subj to C17 {l in 1..L}: sum{k in 1..K_l[l], i in N} x_oik[i,k] = K_l[l];

Your objective "minimize Makespan: C_max;" is good, but you should define just "var C_max;" and then add a constraint "C_max >= C_h[h]" for every h in 1..H.


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

Janina Sand

unread,
May 12, 2021, 2:21:12 PM5/12/21
to AMPL Modeling Language
yes, this worked now.
But something is still wrong since i do not get any results...
Ich checked more closely the constraints and i am wondering why it computes e.g C29 differently that stated in the mod file?

Bildschirmfoto 2021-05-12 um 20.19.05.png

AMPL Google Group

unread,
May 13, 2021, 1:57:29 PM5/13/21
to AMPL Modeling Language
AMPL's "expand" command moves all of the terms involving variables to the left of the relational operator (= or <= or >=) and puts the constant term to the right. Thus for a constraint whose general expression is F_h[h] + A_h[h] <= C_h[h] with A_h being a param, it's normal that expand would show you F_h[1] - C_h[1] <= -9 and F_h[2] - C_h[2] <= -7.

You are not getting any results because AMPL's presolve phase has determined -- without even sending the problem to the solver -- that no feasible solution is possible. That is, there is no way to assign values to the variables so that all the conditions on the variables and all the constraints are satisfied.

In your particular case the presolve messages suggest that, in constraints such as C19[2,2], presolve was able to deduce fixed values for all of the variables, but those values did not satisfy the constraint. So you should look more closely at the constraints mentioned in the presolve messages:
  • Use AMPL commands like "expand C19[2,2];" to see exactly what AMPL generated for the constraints mentioned in the presolve messages. (Normally only the first 5 presolve messages are shown, but you can use "option eexit 0, presolve_warnings 100;" to see all of.)
  • Use commands like "display F_h.lb, F_h.ub;" to show the lower and upper bounds that presolve computed on constraints like C19; variables that were fixed will have the lower bound equal to the upper bound. (You can use "display _varname, _var.lb, _var.ub;" to show the lower and upper bounds on all the variables.)
With this information together with your knowledge of the model and data, you will have a start on figuring out why no feasible solution is possible and what should be done to make your problem feasible.


--
Robert Fourer
am...@googlegroups.com
{#HS:1507525618-104189#}
On Wed, May 12, 2021 at 6:21 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
yes, this worked now.
But something is still wrong since i do not get any results...
Ich checked more closely the constraints and i am wondering why it computes e.g C29 differently that stated in the mod file?

Bildschirmfoto 2021-05-12 um 20.19.05.png

AMPL Google Group schrieb am Mittwoch, 12. Mai 2021 um 16:59:10 UTC+2:

You need to read a model file before the data file. For example, if you have a model file named FinalMod.mod in the same folder as the data file, you would give these commands:

ampl: reset;
ampl: model '/Users/Jani/Desktop/AMPL Beispiele/FinalMod.mod';
ampl: data '/Users/Jani/Desktop/AMPL Beispiele/FinalDat.dat';

--
Robert Fourer
On Wed, May 12, 2021 at 2:58 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You need to read a model file before the data file. For example, if you have a model file named FinalMod.mod in the same folder as the data file, you would give these commands:

ampl: reset;
ampl: model '/Users/Jani/Desktop/AMPL Beispiele/FinalMod.mod';
ampl: data '/Users/Jani/Desktop/AMPL Beispiele/FinalDat.dat';



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

Janina Sand

unread,
May 16, 2021, 2:59:49 PM5/16/21
to AMPL Modeling Language
thank you for you answer, Robert!
I am only wondering how i can write constraints where I have a pram M that is multiplied by a binary variable. 
For e.g I have a constraint :

Bildschirmfoto 2021-05-16 um 20.49.50.png

M is a very gib positive (put it in the data file as 1000) number that is only multiplied by 1 if the binary variable x_ijk is 0. If x_ijk is 1 than M is not supposed to be respected.
AMPL now moves all param on the right side which does not represent the original sense of the constraint anymore since i have M respected on the right side with 1000 if x_ijk is 0. On the left side it the term 1002*x_ijk becomes 0. Is there any way i can set the constraint in the way the AMPL does not move the terms?

Bildschirmfoto 2021-05-16 um 20.59.23.png



Thank you in advance for any help!!

Best regards,
Janina

AMPL Google Group

unread,
May 17, 2021, 12:16:28 PM5/17/21
to AMPL Modeling Language
Moving terms to the left or right does not make the constraint wrong. After moving the terms, the modified constraints are mathematically equivalent to the original constraints, and they give the same result from the solver. In fact, solvers require terms to be moved in this way before anoptimization algorithm is applied.

Note that AMPL breaks the term -(1-x_ijk) * M on the left into two terms, -M and x_ijk * M. The constant -M is moved to the right, which is why you see <= 1000. The term x_ijk * M is combined with the term x_ijk * P_lj on the left -- since they involve the same variable -- resulting in 1002 * x_ijk on the left.


--
Robert Fourer
am...@googlegroups.com
{#HS:1507525618-104189#}
On Sun, May 16, 2021 at 7:00 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
thank you for you answer, Robert! I am only wondering how i can write constraints where I have a pram M that is multiplied by a binary variable. For e.g I have a constraint : Bildschirmfoto 2021-05-16 um 20.49.50.png M is a very gib positive (put it in the data file as 1000) number that is only multiplied by 1 if the binary variable x_ijk is 0. If x_ijk is 1 than M is not supposed to be respected. AMPL now moves all param on the right side which does not represent the original sense of the constraint anymore since i have M respected on the right side with 1000 if x_ijk is 0. On the left side it the term 1002*x_ijk becomes 0. Is there any way i can set the constraint in the way the AMPL does not move the terms? Bildschirmfoto 2021-05-16 um 20.59.23.png
Thank you in advance for any help!!
Best regards, Janina

On Thu, May 13, 2021 at 5:57 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
AMPL's "expand" command moves all of the terms involving variables to the left of the relational operator (= or <= or >=) and puts the constant term to the right. Thus for a constraint whose general expression is F_h[h] + A_h[h] <= C_h[h] with A_h being a param, it's normal that expand would show you F_h[1] - C_h[1] <= -9 and F_h[2] - C_h[2] <= -7.

You are not getting any results because AMPL's presolve phase has determined -- without even sending the problem to the solver -- that no feasible solution is possible. That is, there is no way to assign values to the variables so that all the conditions on the variables and all the constraints are satisfied.

In your particular case the presolve messages suggest that, in constraints such as C19[2,2], presolve was able to deduce fixed values for all of the variables, but those values did not satisfy the constraint. So you should look more closely at the constraints mentioned in the presolve messages:
  • Use AMPL commands like "expand C19[2,2];" to see exactly what AMPL generated for the constraints mentioned in the presolve messages. (Normally only the first 5 presolve messages are shown, but you can use "option eexit 0, presolve_warnings 100;" to see all of.)
  • Use commands like "display F_h.lb, F_h.ub;" to show the lower and upper bounds that presolve computed on constraints like C19; variables that were fixed will have the lower bound equal to the upper bound. (You can use "display _varname, _var.lb, _var.ub;" to show the lower and upper bounds on all the variables.)
With this information together with your knowledge of the model and data, you will have a start on figuring out why no feasible solution is possible and what should be done to make your problem feasible.


--
Robert Fourer
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages