[AMPL 24452] Possible differences in AMPL using AMPL-IDE and amplpy

Hi,
I am calling the same AMPL run-files from a python and/or a ampl-program using random values. To do so (in order to be able to reproduce the results), I am using the random seed.

python program:

ampl = initAMPL()
ampl.setOption(‘randseed’, t) # t is an positive integer value

or

ampl-program:

option randseed (t);

Within the called ampl-program, I am using function “Uniform01()” to get random numbers. Now I detected, that the results of the main program are different due to apparently differnt random values using the same seed. Is it this way? Is there any solution to acces the same vector of random data in both instances?

Best regards,
Lothar

In the meantime I wrote two small test programs (a ampl as well a python program). Both programs simply print the random numbers to the screen - showing the same numbers. Thus, I have to perform additional test.

Ok, I performed some other test showing the problem.

Using the AMPL IDE, “randseed” seems to be independent from any problem - this is exactly what I want.

Using amplpy, “randseed” is being allocated to a problem. Thus, it is being changed switching between different problems. Additionally, activating a problem (e.g. once again switching between several problems), the random numbers are reset based on the randseed value which results into the fact that there are exactly the same random numbers every time.

Is there any possibility to get the same behaviour in amplpy than using the AMPL-IDE?

Best regards,
Lothar

Hi Lothar,

In amplpy, do you have just one AMPL object for the entire model or one object for each problem? It terms of randoms amplpy and the IDE should behave very similarly as long as what you do in both sessions is identical. Could you please send us an example so that we can reproduce the issue?

Hi Filipe,

Thank’s for your help.

So far, I only have a single AMPL object for the entire model. Using python, there finally will be several parallel instances using their own individual AMPL object.

Attached please find the two test programs. From my point of view, the result should be the same. Unfortunately, the result is different.

Best regards,
Lothar

(Attachment py_randomTest2.py is missing)

ampl_randomTest2.run (852 Bytes)

Hi Lothar,

Both program returned the same for us:

$ python py_randomTest2.py
First 5 random numbers of randseed 9:

Uniform01() = 0.749333

Uniform01() = 0.430275

Uniform01() = 0.401318

Uniform01() = 0.13195

Uniform01() = 0.48825

Resetting the randseed
Data of active problem - pr2
9
Uniform01() = 0.749333

Data of active problem - pr1
1
Uniform01() = 0.609209

Data of active problem - pr2
9
Uniform01() = 0.749333

Data of active problem - pr1
1
Uniform01() = 0.609209

Data of active problem - pr2
9
Uniform01() = 0.749333

ampl ampl_randomTest2.run
First 5 random numbers of randseed 9:

Uniform01() = 0.749333

Uniform01() = 0.430275

Uniform01() = 0.401318

Uniform01() = 0.13195

Uniform01() = 0.48825

Resetting the randseed
Data of active problem - pr2
option randseed 9;
Uniform01() = 0.749333

Data of active problem - pr1
option randseed 1;
Uniform01() = 0.609209

Data of active problem - pr2
option randseed 9;
Uniform01() = 0.749333

Data of active problem - pr1
option randseed 1;
Uniform01() = 0.609209

Data of active problem - pr2
option randseed 9;
Uniform01() = 0.749333

We just added the following to py_randomTest2.py because the .run file had one more step that the python script:

ampl.eval(“problem pr2;”)
ampl.eval(“printf ‘Data of active problem - pr2\n’;”)
print(ampl.getOption(“randseed”))
ampl.eval(“display Uniform01();”)

Can you send us the outputs you get on your machine?

Hi Filipe,
I get the same output running the python program. Here is the output of the AMPL program (option version 'AMPL Version 20201019 (MS VC++ 10.0, 64-bit)):

First 5 random numbers of randseed 9:

Uniform01() = 0.749333

Uniform01() = 0.430275

Uniform01() = 0.401318

Uniform01() = 0.13195

Uniform01() = 0.48825

Resetting the randseed
Data of active problem - pr2
option randseed 9;
Uniform01() = 0.749333

Data of active problem - pr1
option randseed 9;
Uniform01() = 0.430275

Data of active problem - pr2
option randseed 9;
Uniform01() = 0.401318

Data of active problem - pr1
option randseed 9;
Uniform01() = 0.13195

Data of active problem - pr2
option randseed 9;
Uniform01() = 0.48825

As you can see, there is no reset of the random generator switching between the problems, which is exactly the behaviour I would like to have using the python program.

Best regards,
Lothar

Hi Lothar,

Did you run that on a clean AMPL session or after setting randseed at least once?

In you python script, please add ampl.setOption(“randseed”, 9) right after ampl = amplpy.AMPL()

Here is the output when randseed is set at the beginning:

python py_randomTest2.py

First 5 random numbers of randseed 9:

Uniform01() = 0.749333

Uniform01() = 0.430275

Uniform01() = 0.401318

Uniform01() = 0.13195

Uniform01() = 0.48825

Resetting the randseed
Data of active problem - pr2
9
Uniform01() = 0.749333

Data of active problem - pr1
9
Uniform01() = 0.430275

Data of active problem - pr2
9
Uniform01() = 0.401318

Data of active problem - pr1
9
Uniform01() = 0.13195

Data of active problem - pr2
9
Uniform01() = 0.48825

What was happening, was that randseed was only being set in the python script after problem pr2 being declared, making it the active problem. Since pr2 was the active problem, the option was set just for that problem in particular, and not for pr2 where randseed was still 1.

Hi Filipe,

Yes, that’s exactly what I investigated yesterday. In general both of my programs work the same way - except that the original python program finally uses a single AMPL instance to deal with several seeds (after each other). I am performing a time series calculation using a single AMPL instance, setting seed “1” for the first timestep, performing the optimization (switching between several problems) and -after receiving the result continuing with the second time step setting the seed to “2” and so on. Thus, the gerneral structure of both programs is:

  • getting an AMPL instance
  • performing initialization issues (variable definition, problem definition
  • loop:
  • setting the seed (after defining the problems)
  • performing the optimization switching between the problems and trying to use the same seed

So far, this seemed to work fine for my using the AMPL program using the AMPL-IDE. But a few minutes ago I detected that this happend because I used the same seed to test the program, assuming that a “reset” command leads to a generic seed of “1” for the problems defined afterwards (running the program again in the AMPL-IDE). Based on your feedback, I recognised that this isn’t the case - instead, the last used seed is being used as a generic value. That’s obvious restarting the IDE or the AMPL process within the IDE.
Based on the structure of my programs dealing with time series calculations, it will not be possible to set the seed before defining the problems. Nevertheless, I found out that, defining the same seed to all problems, will not reinitialize the process of calculating a random number switching between the problems. This is the way in case different seeds are used and thus, getting the same random values than before. So, changing the seed, I will simply allocate the same seed to all the defined problems like

option randseed (seed);
problem pr1;
option randseed (seed);
problem pr2;
option randseed (seed);
problem pr3;

to solve my problem.

Again, thank you very much for your time to show me how the “randseed” is being considered by the problems. Is there any description which data is being stored “in a problem”?

Best regards,

Lothar

Hi Filipe,

maybe there is also an description which issues are not effected by the “reset” command - like e.g. the “randseed”.

Best regards,
Lothar

Hi Lothar,

To reset all options you can use reset options;. Essentially to reset everything you need reset; reset options;.

Regarding your question about the data being stored in each problem, it is: variables, constraints, objectives, and options. Sets and parameters are shared across all problems.

Regarding the options when multiple problems are defined, when each problem is defined it receives a copy of the options defined so far. After that point options are set individually for each problem. In your case, as you noticed, setting the same value for randseed for both problems avoids reseting the seed at every switch. When you switch between problems, the options that have different values are updated and if randseed has a different value it it reset.

Hi Filipe,

Again, Thank’s a lot for the information.

Best regards,
Lothar