AMPL Python API Multi Period Optimization with Dynamic Parameter Values

100 views
Skip to first unread message

Aksel Spence

unread,
Feb 25, 2020, 3:38:25 AM2/25/20
to AMPL Modeling Language
Hi, 
I am developing a multiperiod optimiziton model that allocates mentors to students with Python and AMPL. All data is processed and stored in Python, while the optimization model is in AMPL. My problem is that the parameter for "number of students" and "number of mentors" is dynamic and varies from period to period. Each period is processed as an iteration of a for loop and my intention is to update these peremeters for each period. However, I find that if the "number of students" and "number of mentors" of a period does not equal the number in period 1, the program encounters an error. 

Can anyone help me finding a solution on how to dynamically update the parameters for number of students and mentors in AMPL for each iteration? 

Attached is screenshots of the error message, and some of the scripts. 


parameters.PNG
error message.PNG
AMPL parameters.PNG

AMPL Google Group

unread,
Feb 25, 2020, 7:13:32 PM2/25/20
to AMPL Modeling Language
Could you please provide us a minimal example with code so that we can reproduce this issue on our end?

Updating parameters should not be a problem unless the change conflicts with other data set previously. Could you please add ampl.eval('display rating;') right before ampl.getParameter('rating').setValues(rating) and send us the output?


--
Filipe Brandão
am...@googlegroups.com
{#HS:1093012455-68873#}
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ampl/b590cc2d-b061-476d-9e38-a67b58bb7cde%40googlegroups.com.

Aksel Petter Spence

unread,
Feb 26, 2020, 4:15:03 AM2/26/20
to am...@googlegroups.com
error message display rating.PNG
Dear Filipe, 
In the screenshot you can see the output when I run with  ampl.eval('display rating;'). The first batch (period) runs fine, but for the second batch we get an error message. Indeed, we see that the number of values for rating is equal to number of mentors in that batch. Still, we get an error with invalids subscripts.  

In case we are not able to identify the issue from this, I'll also add a version of the AMPL model, the Python script and the datasets for students and mentors. 


Best, Aksel

Python_AMPL_MultiPeriod.py
model4.mod
mentors.npy
students.npy
Error message AMPL-PY.PNG

Aksel Spence

unread,
Feb 26, 2020, 4:34:56 AM2/26/20
to AMPL Modeling Language
By the way, the reason why it is 7 invalid subscripts is that there is 11 mentors in batch 1, and only 4 in batch 2. So I assume AMPL is expecting 11 also in the second batch. However, the length of the set of mentors (set mentor= 1..nMentors;) is supposed to be updated for the second batch.


onsdag 26. februar 2020 10.15.03 UTC+1 skrev Aksel Petter Spence følgende:
error message display rating.PNG
Dear Filipe, 
In the screenshot you can see the output when I run with  ampl.eval('display rating;'). The first batch (period) runs fine, but for the second batch we get an error message. Indeed, we see that the number of values for rating is equal to number of mentors in that batch. Still, we get an error with invalids subscripts.  

In case we are not able to identify the issue from this, I'll also add a version of the AMPL model, the Python script and the datasets for students and mentors. 


Best, Aksel

ons. 26. feb. 2020 kl. 01:13 skrev AMPL Google Group <am...@googlegroups.com>:
Could you please provide us a minimal example with code so that we can reproduce this issue on our end?

Updating parameters should not be a problem unless the change conflicts with other data set previously. Could you please add ampl.eval('display rating;') right before ampl.getParameter('rating').setValues(rating) and send us the output?


--
Filipe Brandão
am...@googlegroups.com
{#HS:1093012455-68873#}
On Tue, Feb 25, 2020 at 8:38 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi,
I am developing a multiperiod optimiziton model that allocates mentors to students with Python and AMPL. All data is processed and stored in Python, while the optimization model is in AMPL. My problem is that the parameter for "number of students" and "number of mentors" is dynamic and varies from period to period. Each period is processed as an iteration of a for loop and my intention is to update these peremeters for each period. However, I find that if the "number of students" and "number of mentors" of a period does not equal the number in period 1, the program encounters an error.

Can anyone help me finding a solution on how to dynamically update the parameters for number of students and mentors in AMPL for each iteration?

Attached is screenshots of the error message, and some of the scripts.


--
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 am...@googlegroups.com.

--
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 am...@googlegroups.com.

AMPL Google Group

unread,
Feb 26, 2020, 3:08:16 PM2/26/20
to AMPL Modeling Language
As a workaround, ampl.getOutput('display rating;') in the same place where you put ampl.eval('display rating;') should allow skipping the error. We are currently working on a bugfix so that this can be avoided but that will require a new release of the API and it may take a few days.


--
Filipe Brandão
am...@googlegroups.com
{#HS:1093012455-68873#}
On Wed, Feb 26, 2020 at 9:35 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
By the way, the reason why it is 7 invalid subscripts is that there is 11 mentors in batch 1, and only 4 in batch 2. So I assume AMPL is expecting 11 also in the second batch. However, the length of the set of mentors (set mentor= 1..nMentors;) is supposed to be updated for the second batch.

onsdag 26. februar 2020 10.15.03 UTC+1 skrev Aksel Petter Spence følgende:

error message display rating.PNG
Dear Filipe,
In the screenshot you can see the output when I run with ampl.eval('display rating;'). The first batch (period) runs fine, but for the second batch we get an error message. Indeed, we see that the number of values for rating is equal to number of mentors in that batch. Still, we get an error with invalids subscripts.

In case we are not able to identify the issue from this, I'll also add a version of the AMPL model, the Python script and the datasets for students and mentors.

Best, Aksel

ons. 26. feb. 2020 kl. 01:13 skrev AMPL Google Group <[url=javascript:]am...@googlegroups.com>:

Could you please provide us a minimal example with code so that we can reproduce this issue on our end?

Updating parameters should not be a problem unless the change conflicts with other data set previously. Could you please add ampl.eval('display rating;') right before ampl.getParameter('rating').setValues(rating) and send us the output?

--
Filipe Brandão
[url=javascript:]am...@googlegroups.com

On Wed, Feb 26, 2020 at 9:15 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
error message display rating.PNG
Dear Filipe,
In the screenshot you can see the output when I run with ampl.eval('display rating;'). The first batch (period) runs fine, but for the second batch we get an error message. Indeed, we see that the number of values for rating is equal to number of mentors in that batch. Still, we get an error with invalids subscripts.

In case we are not able to identify the issue from this, I'll also add a version of the AMPL model, the Python script and the datasets for students and mentors.

Best, Aksel

ons. 26. feb. 2020 kl. 01:13 skrev AMPL Google Group <am...@googlegroups.com>:

On Wed, Feb 26, 2020 at 12:13 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Could you please provide us a minimal example with code so that we can reproduce this issue on our end?

Updating parameters should not be a problem unless the change conflicts with other data set previously. Could you please add ampl.eval('display rating;') right before ampl.getParameter('rating').setValues(rating) and send us the output?


--
Filipe Brandão
am...@googlegroups.com
On Tue, Feb 25, 2020 at 8:38 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi,
I am developing a multiperiod optimiziton model that allocates mentors to students with Python and AMPL. All data is processed and stored in Python, while the optimization model is in AMPL. My problem is that the parameter for "number of students" and "number of mentors" is dynamic and varies from period to period. Each period is processed as an iteration of a for loop and my intention is to update these peremeters for each period. However, I find that if the "number of students" and "number of mentors" of a period does not equal the number in period 1, the program encounters an error.

Can anyone help me finding a solution on how to dynamically update the parameters for number of students and mentors in AMPL for each iteration?

Attached is screenshots of the error message, and some of the scripts.


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

Aksel Petter Spence

unread,
Feb 26, 2020, 5:09:25 PM2/26/20
to am...@googlegroups.com
Great! It seems to be working well. I will do a full check tomorrow.

Aksel

Reply all
Reply to author
Forward
0 new messages