# \[AMPL 24724\] set declaration

**URL:** <https://discuss.ampl.com/t/ampl-24724-set-declaration/670>\
**Category:** Google Group Mirror\
**Created:** [August 2, 2023, 9:52pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670 "2023-08-02T21:52:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 2, 2023, 9:52pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/1 "2023-08-02T21:52:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 3, 2023, 9:13am UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/2 "2023-08-03T09:13:30Z")

</div>

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](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](https://colab.research.google.com/github/ampl/amplcolab/blob/master/authors/fdabrandao/quick-start/nativediet.ipynb)

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 7, 2023, 1:34am UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/3 "2023-08-07T01:34:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 7, 2023, 5:11pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/4 "2023-08-07T17:11:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 7, 2023, 9:23pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/5 "2023-08-07T21:23:36Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 8, 2023, 11:49am UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/6 "2023-08-08T11:49:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 22, 2023, 8:56pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/7 "2023-08-22T20:56:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 23, 2023, 3:29pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/8 "2023-08-23T15:29:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 8:04pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/9 "2023-08-24T20:04:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 8:20pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/10 "2023-08-24T20:20:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 8:38pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/11 "2023-08-24T20:38:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 8:55pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/12 "2023-08-24T20:55:49Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 9:36pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/13 "2023-08-24T21:36:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 9:40pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/14 "2023-08-24T21:40:26Z")

</div>

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”])

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 9:47pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/15 "2023-08-24T21:47:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 10:04pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/16 "2023-08-24T22:04:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 10:06pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/17 "2023-08-24T22:06:30Z")

</div>

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")

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 10:06pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/18 "2023-08-24T22:06:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 10:13pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/19 "2023-08-24T22:13:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [August 24, 2023, 10:16pm UTC](https://discuss.ampl.com/t/ampl-24724-set-declaration/670/20 "2023-08-24T22:16:37Z")

</div>

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.

[Next page](https://discuss.ampl.com/t/ampl-24724-set-declaration/670.md?page=2)
