# Hello about the last element and the first element in the set

**URL:** <https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830>\
**Category:** Support\
**Created:** [November 27, 2023, 7:35am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830 "2023-11-27T07:35:32Z")\
**Posts on this page:** 8\
**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 27, 2023, 7:35am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/1 "2023-11-27T07:35:32Z")

</div>

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;

s.t. VehicleItoJfr {i in {1…(Nv-1)}, j in {(i+1)…Nv}, kk in I}:

(xf[i,kk] - xr[j,kk])^2 + (yf[i,kk] - yr[j,kk])^2 \>= FourSqR;

Hello, I want i to iterate over the last element of the set large\_cav, and then j to iterate over the element always one ahead of i, since large\_cav is an unordered set, unlike {1… (Nv-1)} is an ordered set that can be written as i in {1… (Nv-1)}, j in {(i+1)… Nv}, what do I do with the unordered set large\_cav? By the way, large\_cav is an increasing set

---

<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:29pm UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/2 "2023-11-27T17:29:03Z")

</div>

You can define large\_cav to be an ordered set:

```auto
set large_cav ordered = A11 union A22 union A33 union B11 union B22 union B33 union C11 union C22 union C33 union D11 union D22 union D33;

```

Then you can use AMPL’s ordered set functions in defining your constraint (see section 5.6 Ordered sets in the [chapter 5 of the AMPL book](https://ampl.com/wp-content/uploads/Chapter-5-Simple-Sets-and-Indexing-AMPL-Book.pdf)). The first member of large\_cav will be given by first(large\_cav), and the last member by last(large\_cav).

---

<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 28, 2023, 1:26am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/3 "2023-11-28T01:26:24Z")

</div>

So if the set is large\_cav, how do we express the following set meaning, i in {1… (Nv-1)}, j in {(i+1)… Nv},  
I should have clarified the matter more clearly.  
large\_cav is an unordered and non-arithmetic increasing set, and i want to traverse it using I and j, similar to the following loop  
for i in large\_cav[finall-1]  
for j in large\_cav[i+1]… large\_cav[final]

large\_cav[finall-1] The second to last element of the set large\_cav, large\_cav[final]

Is the last element, large\_cav[i+1] means j goes one element ahead of i

---

<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 29, 2023, 1:10am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/4 "2023-11-29T01:10:25Z")

</div>

If you want to talk about the last element of a set, the second-to-last element of at set, or the element j that is one after the element i of the set, then there needs to be some ordering defined on the set. Thus, you need to tell AMPL that you want it to define an ordering on large\_cav. Then you can use AMPL’s ordered set functions:

- The element that is one after element i in large\_cav is member(i+1, large\_cav)

- The number of elements in large\_cav is card(large\_cav), and so the next-to-last element of large\_cav is member(card(large\_cav)-1, large\_cav)

- The last element of large\_cav is last(large\_cav).

I am not sure what ordering you want to have for large\_cav, but if you want the elements to be ordered by increasing value, then large\_cav should be defined like this:

```auto
set large_cav ordered by Integers = A11 union A22 union A33 union B11 union B22 union B33 union C11 union C22 union C33 union D11 union D22 union D33;

```

---

<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 29, 2023, 1:52am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/5 "2023-11-29T01:52:43Z")

</div>

I’m sorry, I still didn’t give you a good description.

large\_cav is a union, but  
A11 union A22 union A33 union B11 union B22 union B33 union C11 union C22 union C33 union D11 union D22 union D33; It’s incremental, like:

{1,2} union {3,5}union {7,8,9} union {24,39}, so should I assume that large\_cav is an ordered set?

Finally I can use functions to express constraints as follows

s.t. VehicleItoJfr {i in large\_cav , j in large\_cav , kk in I}:  
(xf[i,kk] - xr[j,kk])^2 + (yf[i,kk] - yr[j,kk])^2 \>= FourSqR;

But i and j should satisfy some constraints;

1. i does not traverse the last element of the set,  
j needs to traverse the elements after i.

I found that using these functions seems to be unable to achieve this, do you have any ideas?

---

<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 30, 2023, 9:28pm UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/6 "2023-11-30T21:28:19Z")

</div>

You can define large\_cav with the words **ordered by Integers** in the definition, like this:

```auto
set large_cav ordered by Integers = A11 union A22 union A33 union B11 union B22 union B33 union C11 union C22 union C33 union D11 union D22 union D33;

```

When the set statement is written in this way, large\_cav is treated by AMPL as an ordered set. Then you can use the ordering in your model. I do not know the details of your model, but I can give the following example that shows how the ordering may be used in constraint VehicleItoJfr :

```auto
param Nv = card(large_cav);
param FourSqR = 77.7;
set I = 1..3;

var xf {large_cav, I};
var xr {large_cav, I};
var yf {large_cav, I};
var yr {large_cav, I};

s.t. VehicleItoJfr {i in 1..Nv-1, j in i+1..Nv, kk in I}:
   (xf[member(i,large_cav),kk] - xr[member(j,large_cav),kk])^2 + 
   (yf[member(i,large_cav),kk] - yr[member(j,large_cav),kk])^2 >= FourSqR;

```

---

<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:** [December 1, 2023, 1:10am UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/7 "2023-12-01T01:10:59Z")

</div>

ok thanks ,  
I thought of another way to write it.Do you think it’s okay?

s.t. VehicleItoJfr\_ll {i in large\_cav, j in large\_cav, kk in I : j\> i}:

(xf[i,kk] - xr[j,kk])^2 + (yf[i,kk] - yr[j,kk])^2 \> = (large\_radius + large\_radius)^2;

---

<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:** [December 4, 2023, 5:15pm UTC](https://discuss.ampl.com/t/hello-about-the-last-element-and-the-first-element-in-the-set/830/8 "2023-12-04T17:15:21Z")

</div>

I think that’s OK. You can check by creating a listing of the constraints that AMPL generates. After reading the model and data, execute

```auto
expand VehicleItoJfr >VIJ.txt;

```

and then look in the file `VIJ.txt`.
