# Hi, question about sets

**URL:** <https://discuss.ampl.com/t/hi-question-about-sets/826>\
**Category:** Support\
**Created:** [November 23, 2023, 2:22am UTC](https://discuss.ampl.com/t/hi-question-about-sets/826 "2023-11-23T02:22:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liangzilu\_liangzilu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/liangzilu_liangzilu/32/408_2.png) [@liangzilu\_liangzilu](https://discuss.ampl.com/u/liangzilu_liangzilu)\
**Post date:** [November 23, 2023, 2:22am UTC](https://discuss.ampl.com/t/hi-question-about-sets/826/1 "2023-11-23T02:22:28Z")

</div>

Hi, question about sets. If I have two sets A1 and A2, I should go through them together. Examples are as follows:

There are sets a1, a2

There are constraints:

```auto
s.t. limit_xr_a1 {i in a1, j in IP}:
   -12 + R <= xr[i,j] <= -R;

s.t. limit_xr_a2 {i in a2, j in IP}:
   -12 + R <= xr[i,j] <= -R;

```

How do I combine these two constraints，Would it be correct to write something like this?

```auto
s.t. limit_xr_a1 {i in a1, a2, j in IP}:
   -12 + R <= xr[i,j] <= -R;

```

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [November 23, 2023, 6:13pm UTC](https://discuss.ampl.com/t/hi-question-about-sets/826/2 "2023-11-23T18:13:55Z")

</div>

AMPL recognizes “i in a1 union a2” to mean all i that are either in a1 or in a2. Thus you can formulate your constraint as follows:

```auto
s.t. limit_xr_a {i in a1 union a2, j in IP}:
   -12 + R <= xr[i,j] <= -R;

```

In general, you can apply the AMPL “union” operator to any two set expressions, to return the set containing all members of both of them.

---

<div class="post-metadata">

**Author:** ![liangzilu\_liangzilu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/liangzilu_liangzilu/32/408_2.png) [@liangzilu\_liangzilu](https://discuss.ampl.com/u/liangzilu_liangzilu)\
**Post date:** [November 27, 2023, 3:35am UTC](https://discuss.ampl.com/t/hi-question-about-sets/826/3 "2023-11-27T03:35:38Z")

</div>

```auto
param cav_index_start{i in {1..36}};
param cav_index_end{i in {1..36}};

set A1 := {cav_index_start[1]..cav_index_end[1]};
set A11 := {cav_index_start[2]..cav_index_end[2]};
set A111 := {cav_index_start[3]..cav_index_end[3]};

set A2 := {cav_index_start[4]..cav_index_end[4]};
set A22 := {cav_index_start[5]..cav_index_end[5]};
set A222 := {cav_index_start[6]..cav_index_end[6]};

set A3 := {cav_index_start[7]..cav_index_end[7]};
set A33 := {cav_index_start[8]..cav_index_end[8]};
set A333 := {cav_index_start[9]..cav_index_end[9]};

set B1 := {cav_index_start[10]..cav_index_end[10]};
set B11 := {cav_index_start[11]..cav_index_end[11]};
set B111 := {cav_index_start[12]..cav_index_end[12]};

set B2 := {cav_index_start[13]..cav_index_end[13]};
set B22 := {cav_index_start[14]..cav_index_end[14]};
set B222 := {cav_index_start[15]..cav_index_end[15]};

set B3 := {cav_index_start[16]..cav_index_end[16]};
set B33 := {cav_index_start[17]..cav_index_end[17]};
set B333 := {cav_index_start[18]..cav_index_end[18]};

set C1 := {cav_index_start[19]..cav_index_end[19]};
set C11 := {cav_index_start[20]..cav_index_end[20]};
set C111 := {cav_index_start[21]..cav_index_end[21]};

set C2 := {cav_index_start[22]..cav_index_end[22]};
set C22:= {cav_index_start[23]..cav_index_end[23]};
set C222 := {cav_index_start[24]..cav_index_end[24]};

set C3 := {cav_index_start[25]..cav_index_end[25]};
set C33 := {cav_index_start[26]..cav_index_end[26]};
set C333 := {cav_index_start[27]..cav_index_end[27]};

set D11 := {cav_index_start[28]..cav_index_end[28]};
set D11 := {cav_index_start[29]..cav_index_end[29]};
set D111 := {cav_index_start[30]..cav_index_end[30]};

set D2 := {cav_index_start[31]..cav_index_end[31]};
set D22 := {cav_index_start[32]..cav_index_end[32]};
set D222 := {cav_index_start[33]..cav_index_end[33]};

set D3 := {cav_index_start[34]..cav_index_end[34]};
set D33 := {cav_index_start[35]..cav_index_end[35]};
set D333 := {cav_index_start[36]..cav_index_end[36]};

set large_cav = A11 union A22 union A33 union B11 union B22 union B33 union C11 union C22 union C33 union D11 union D22 union D33;
set Middle_cav = A1 union A2 union A3 union B1 union B2 union B3 union C1 union C2 union C3 union D1 union D2 union D3;
set Small_cav = A111 union A222 union A333 union B111 union B222 union B333 union C111 union C222 union C333 union D111 union D222 union D333;

```

For the sets large\_cav, Small\_cav, am I writing this correctly?

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [November 27, 2023, 5:13pm UTC](https://discuss.ampl.com/t/hi-question-about-sets/826/4 "2023-11-27T17:13:49Z")

</div>

AMPL will accept your definitions of large\_cav, Middle\_cav, and Small\_cav.

Middle\_cav will be the union of all the sets A1, S2, A3, B1, B2, B3, etc. Small\_cav and large\_cav will also be the union of all the sets indicated.

You can use AMPL statements like “display Middle\_cav;” to check that the result is what you want.
