Script in AMPL

114 views
Skip to first unread message

Alessandro Oscar Gilardino Arias

unread,
Nov 23, 2015, 10:29:44 PM11/23/15
to AMPL Modeling Language
Dear Bob and Victor,

I have a question.

In my model in AMPL I have a parameter called generation and I have to run my model in a .run file for 5 times, this means using a for {1..5} but in every loop I have to change the values of all the parameter generation in different values on every iteration, also I have to let the value of a binary variable calles z[j,m] this means that if in any iteration any variable z[j,m] has a value 1, in the following iteration must take a value of 1.

I write the code I think I should write.

set Location := 1..10;
set Arrange:= 1..5;

variable z[Location, Arrange];

the variable z has to accomplish the following constraint:

subject to Containers_in_one_point {j in Location}:
                sum {m in Arrange} z[j,m] <=1;


I think that the .run file should have the following code:

param T := 1..5;

for {T}

if T = 1 then
solve;
let generation := generation * 0.08;
if sum {m in Arrange} z[j,m] = 1 then sum {m in Arrange} z[j,m] = 1 else sum {m in Arrange} z[j,m] <=1;

elseif T = 2 then
solve;
let generation := generation * 0.10;
if sum {m in Arrange} z[j,m] = 1 then sum {m in Arrange} z[j,m] = 1 else sum {m in Arrange} z[j,m] <=1;

elseif T = 3 then
solve;
let generation := generation * 0.12;
if sum {m in Arrange} z[j,m] = 1 then sum {m in Arrange} z[j,m] = 1 else sum {m in Arrange} z[j,m] <=1;

And so on, I am not very sure if that would work in a script of AMPL or if I have to connect Matlab with AMPL.

Also I would like to ask you something about the AMPL.api, when I use the following command: 

ampl = AMPL;

I have the following error:

Undefined variable "com" or class "com.ampl.AMPL".

Error in AMPL (line 21)
                a = com.ampl.AMPL();.


Thanks a lot in advance, and sorry for give you such a long post.

Thanks a lot again!!
 

Robert Fourer

unread,
Nov 24, 2015, 3:17:08 PM11/24/15
to am...@googlegroups.com
If you are going to write out each case individually, then there is no need to a loop ("for") statement or a long series of "if" statements. Also to say that "if in any iteration any variable z[j,m] has a value 1, in the following iteration must take a value of 1" you should use a statement like

for {j in Location, m in Arrange}
if z[j,m] = 1 then fix z[j,m];

which can be written in an AMPL script.

Bob Fourer
am...@googlegroups.com

=======

Alessandro Oscar Gilardino Arias

unread,
Nov 24, 2015, 5:36:47 PM11/24/15
to AMPL Modeling Language, 4...@ampl.com
Thanks a lot for your answer Bob

Alessandro Oscar Gilardino Arias

unread,
Nov 25, 2015, 11:22:15 PM11/25/15
to AMPL Modeling Language, 4...@ampl.com
Thanks again Bob, but I still have a question, for the same script in previous post.

You told me to use 

 for {j in Location, m in Arrange} 
      if z[j,m] = 1 then fix z[j,m]; 

but here is my question, the index m can take value 1,2 or 3. This means that I can have a variable z[1,1], z[1,2] or z[1,3] what I am trying to do is that if z[1,2] take value 1, for the next iteration I have two options, that z[1,2] or z[1,3] can take value 1, for no reason the variable z[1,1] can take value 1 .

I am trying with this code

 for {j in Location, m in Arrange} 
      if z[j,m] = 1 then fix sum{l in Arrange:l>=m} z[j,m]=1;

but when I try to run in AMPL returns me a syntax error.

The other code I was trying is the following

 for {j in Location, m in Arrange, l in Arrange:l>=m} 
      if z[j,m] = 1 then fix z[j,l];  

but in this case, I feel that is happening the following: if z[1,2] = 1, for the next iteration z[1,2]=1 and z[1,3]=1 

or this last is the correct code I should use???

Thanks a lot in advance for all your help

Victor Zverovich

unread,
Nov 27, 2015, 7:08:46 PM11/27/15
to am...@googlegroups.com
You can't use sum in a fix statement (thus the error), but you can use it in a constraint:

  s.t. c{j in Location, m in Arrange}:
    z[j,m] = 1 ==> sum{l in Arrange:l>=m} z[j,m]=1;

This uses indicator constraints and assumes that z is binary, but a more general formulation is possible.

HTH,
Victor

--
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 http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Alessandro Oscar Gilardino Arias

unread,
Nov 27, 2015, 11:36:53 PM11/27/15
to AMPL Modeling Language
Dear Victor, thank you for the answer.

I tried to use what you told me

s.t. c{j in P_POTENCIAL, m in C_GENERAL}:
    z[j,m] = 1 ==> sum{l in C_GENERAL:l>=m} z[j,m]=1; 

but I have the followng message

localizacion.run, line 16 (offset 437):
Abandoning compound command to accept declaration of Fijacion_Z.

context:  if z[j,m]=1 then sum{l in C_GENERAL:l>=m}  >>> z[j,l]=1; <<< 

Also I tried with the following constraint:

subject to Fijacion_Z {j in P_POTENCIAL, m in C_GENERAL}:
if z[j,m]=1 then sum{l in C_GENERAL:l>=m} z[j,l]=1

But i have the following message:

localizacion.run, line 16 (offset 437):
Abandoning compound command to accept declaration of Fijacion_Z.

context:  if z[j,m]=1 then sum{l in C_GENERAL:l>=m}  >>> z[j,l]=1; <<< 

Please, could you give me a hand?

Thanks in advance

Victor Zverovich

unread,
Nov 30, 2015, 6:54:52 PM11/30/15
to am...@googlegroups.com
The error "Abandoning compound command to accept declaration of Fijacion_Z" means that you declare Fijacion_Z in a for loop or other compound statement. To fix this move the declaration outside of the loop.

HTH,
Victor

Alessandro Oscar Gilardino Arias

unread,
Dec 11, 2015, 10:09:35 AM12/11/15
to AMPL Modeling Language
Dear all, thanks for your answers.

In our private emailing you told me to use the following:

for {j in P_POTENTIAL, l in 1..min {m in C_GENERAL: z[j,l]=1} - 1} fix z[j,l];

I tried either this and also I tried the following modifications:

I being trying the following:

for {j in P_POTENTIAL, l in 1..(min {m in C_GENERAL: z[j,m]=1} - 1)-1} fix z[j,l]

(min {m in C_GENERAL: z[j,m]=1} - 1) - 1 this will allow me to, if for example, z[1,2] = 1 then for the variable z[1,1] = 0 because any arrange with a smaller capacity than the arrange number 2 shouldn't be activated. Then I am using the statement lines above.

Also I think I can do that with the followin statement:

for {j in P_POTENTIAL, l in ((min {m in C_GENERAL: z[j,m]=1}-1) -1 )..card(C_GENERAL) } fix z[j,l];

but in both cases I get the following message:

Error at _cmdno 226 executing "for" command
(file localizacion.run, line 15, offset 338):
range with infinite lower bound

Also, I being trying to do the following:

for {j in P_POTENTIAL, m in C_GENERAL}
      if z[j,m] = 1 then if (min {m in C_GENERAL: z[j,l]=1}) = 1 then fix (z[j.1] + z[j,2] + z[j,3]) else if (min {m in C_GENERAL: z[j,l]=1}) = 2 then fix (z[j,2] + z[j,3]) else if (min {m in C_GENERAL: z[j,l]=1}) = 3 then fix z[j,3];
    
but I have the following message:

ocalizacion.run, line 16 (offset 414):
syntax error
context:  if z[j,m] = 1 then if (min {m  >>> in  <<< C_GENERAL: z[j,l]=1}) = 1 then fix (z[j.1] + z[j,2] + z[j,3]) else if (min {m in C_GENERAL: z[j,l]=1}) = 2 then fix (z[j,2] + z[j,3]) else if (min {m in C_GENERAL: z[j,l]=1}) = 3 then fix z[j,3];


Thanks in advance for all your help and I am very sorry for being so annoying 😁

Robert Fourer

unread,
Dec 12, 2015, 9:56:32 PM12/12/15
to am...@googlegroups.com
Actually it should be

for {j in P_POTENTIAL, l in 1..min {m in C_GENERAL: z[j,m]=1} m - 1} fix z[j,l];

But this still has a problem if it is possible to have a case where z[j,m] = 0 for all m. In that case the set {m in C_GENERAL: z[j,m]=1} is empty and the minimum is Infinity -- hence the error message about a range with an infinite bound. If you need to handle this case then I think you can write

for {j in P_POTENTIAL, l in 1..min ({m in C_GENERAL: z[j,m]=1} m, card(C_GENERAL)+1) - 1} fix z[j,l];

though I haven't been able to try this out on a real case. Similarly in your other example you should write

if (min {l in C_GENERAL: z[j,l]=1} l) = 1

since the AMPL "min" operator requires an operand.

Alessandro Oscar Gilardino Arias

unread,
Dec 19, 2015, 6:53:13 PM12/19/15
to AMPL Modeling Language, 4...@ampl.com
Dear Bob, thanks a lot for your answer, and I am very sorry for my late response but I've been very busy.

I would like to ask you if you could please explain me what means the following statement:

for {j in P_POTENTIAL, l in 1..min ({m in C_GENERAL: z[j,m]=1} m, card(C_GENERAL)+1) - 1} fix z[j,l];

Thanks a lot again for your answer

Victor Zverovich

unread,
Dec 21, 2015, 11:27:55 AM12/21/15
to am...@googlegroups.com
The expression

  min ({m in C_GENERAL: z[j,m]=1} m, card(C_GENERAL)+1)

returns the minimum index m such that z[j,m] = 1 or, if there is no such index, the number of elements in C_GENERAL + 1. So the statement


  for {j in P_POTENTIAL, l in 1..min ({m in C_GENERAL: z[j,m]=1} m, card(C_GENERAL)+1) - 1} fix z[j,l];

fixes variables z[j,l] for all j in P_POTENTIAL and l ranging from 1 to the minimum index m such that z[j,m] = 1 (see above) to their current values.

HTH,
Victor

Alessandro Oscar Gilardino Arias

unread,
Dec 21, 2015, 11:35:16 AM12/21/15
to am...@googlegroups.com

Thanks a lot for your help Victor and Bob.

Best regards

Reply all
Reply to author
Forward
0 new messages