[AMPL 24724] set declaration

Hello everyone,
I have an optimization problem described in AMPL and would like to write it using amplpy in google colab.

In my .mod ample file I have two sets as:

set I ordered;
set p;
set H{I} within p;

in AMPL I have a .txt file that determines the relationship between I and H{I}.
How should I define this mapping from I to H{I} in amplpy?

Could you please send us the content of the .txt file? To load the data for H and I you can use for instance a pandas DataFrame or python lists and dictionaries.

Example using pandas dataframes: https://colab.research.google.com/github/ampl/amplcolab/blob/master/authors/fdabrandao/quick-start/pandasdiet.ipynb

Example using lists and dictionaries: https://colab.research.google.com/github/ampl/amplcolab/blob/master/authors/fdabrandao/quick-start/nativediet.ipynb

In my ampl code I have:

Set I ordered;
Set P;
Set H{I} within P;

The problem is that both I and H are sets. In the instances you provided, the values of the dictionary or column of the dataframe are a parameter and not another set.

here is the .csv file containing I and H:

I H

0 1001
1 1002
2 1003 [572]
3 1004 [1]
4 1005
… … …
1995 8156
1996 8157
1997 8158 [543, 544]
1998 8159
1999 8160

2000 rows × 2 columns

Could you tell me how I can describe the mapping of sets H and I?

Sincerely,
Fargol

You can take the data from the csv file and convert it into a dictionary of the form {key: list of values for the set} and then you can load the data as follows:

ampl = AMPL()
ampl.eval(“”"
set I ordered;
set P;
set H{I} within P;
“”")
data = {
1001: ,
1002: ,
1003: [572],
1004: [1],
1005: ,
8156: ,
8157: ,
8158: [543, 544],
8159: ,
8160: ,
}
ampl.set[“I”] = data.keys()
ampl.display(“I”)
ampl.set[“P”] = sum(data.values(), )
ampl.display(“P”)
for i in data:
ampl.set[“H”][i] = data[i]
ampl.display(“H”)

It will produce the following output:

set I := 1001 1002 1003 1004 1005 8156 8157 8158 8159 8160;

set P := 572 1 543 544;

set H[1001] := ; # empty
set H[1002] := ; # empty
set H[1003] := 572;
set H[1004] := 1;
set H[1005] := ; # empty
set H[8156] := ; # empty
set H[8157] := ; # empty
set H[8158] := 543 544;
set H[8159] := ; # empty
set H[8160] := ; # empty

Thanks. It works.
If I have a parameters wind that depends on three sets of I, T, and S in wind.csv file like below, how should I read wind parameter values using ampl.get_parameter(“wind”).set_values(wind_df)?

If wind_df has I, S, and T as index, ampl.get_parameter(“wind”).set_values(wind_df) should work? Is it giving some error? You may need to do wind_df.set_index([“I”, “S”, “T”]) before passing the values in case the index is not set.

Hello,
I’ve modeled my problem in amplpy with the exact model declaration and input as in my ampl code. However, I’m receiving different results.
The only difference between my ampl code and amplpy code is in the following lines which I have in my ampl code but didn’t know how to include in my amplpy code.

Could you tell me if this can be a source for the different results?
If yes, can you provide the equivalent of those codes in amplpy?

option display_1col 100000000000000;

option display_width 10000;

option display_round 10;

option omit_zero_rows 0;

Thank you for all your support.

Sincerely,
Fargol

Hi Fargol,

In amplpy, you can set these options as follows:

ampl.option[“display_1col”] = 100000000000000
ampl.option[“display_width”] = 10000
ampl.option[“display_round”] = 10
ampl.option[“omit_zero_rows”] = 0

This last option “omit_zero_rows” may need to be set in eval statements (e.g., ampl.eval(“option omit_zero_rows 0; display parameter_name;”)) as the API may switch it on for some operations.

Thanks! I still get different results from running the same optimization model in colab using amplpy and ampl on my system.

The gurobi version used in amplpy is 10.0.2 and the version on my ampl is 9.5.2.
Is there any way I can change the version of gurobi used in amplpy to the one my ampl uses to make sure other things are working fine?

Sincerely,
Fargol

Hi Fagol,

Did you install that version of Gurobi as an amplpy module? We don’t have Gurobi 9.5 available as a module, but instead of using modules you can use the binaries from the AMPL installation directory as follows:

from amplpy import AMPL, add_to_path
add_to_path(r"full path to the AMPL installation directory")
ampl = AMPL()

This will ensure that you will be using exactly the same version of AMPL and Gurobi.

I didn’t quite get your hint; when I solve my problem in amplpy it says:

Hi Fargol,

You should only include:

from amplpy import AMPL, add_to_path
add_to_path(r"full path to the AMPL installation directory")
ampl = AMPL()

You should replace “full path to the AMPL installation directory” by the complete path to the directory where you have AMPL installed. This will ensure that the version of AMPL and Gurobi used is the same. In case you are using %%ampl_eval magics, you may also need to register magics:

from amplpy import AMPL, add_to_path, register_magics
add_to_path(r"full path to the AMPL installation directory")
ampl = AMPL()
register_magics(ampl_object=ampl)

Thanks Filipe,
I still get the same result though.It says Gurobi 10.0.2 ….

Other suggestions why there’s a difference between results when using amplpy and ampl?

Could you please send us the output of:

from amplpy import AMPL, add_to_path
add_to_path(r"full path to the AMPL installation directory")
import os
print(os.environ[“PATH”])

Here it is:

full path to the AMPL installation directory:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/usr/local/lib/python3.10/dist-packages/ampl_module_base/bin:/usr/local/lib/python3.10/dist-packages/ampl_module_gurobi/bin:/Users/fargolnem/Desktop/ampl.macos64 11.54.38 AM:/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin

“full path to the AMPL installation directory” needs to be replaced by the path do where you have AMPL installed.

From that output the path may be “/Users/fargolnem/Desktop/ampl.macos64” so you should use the following:

add_to_path(r"/Users/fargolnem/Desktop/ampl.macos64")

I’d replaces it with the folder on my desktop where ampl is installed. What else should I do?

you’re right. I had not included the path. Here is the output:

Is “11.54.38 AM” part of the directory name? If that is the case it would be better to rename it and remove that part to avoid possible issues due to the spaces.