# Unexpected behavior from defined set

**URL:** <https://discuss.ampl.com/t/unexpected-behavior-from-defined-set/2005>\
**Category:** Support\
**Tags:** sets-and-indexing\
**Created:** [December 10, 2024, 7:48pm UTC](https://discuss.ampl.com/t/unexpected-behavior-from-defined-set/2005 "2024-12-10T19:48:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Felt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/andy_felt/32/872_2.png) [@Andy\_Felt](https://discuss.ampl.com/u/Andy_Felt)\
**Post date:** [December 10, 2024, 7:48pm UTC](https://discuss.ampl.com/t/unexpected-behavior-from-defined-set/2005/1 "2024-12-10T19:48:47Z")

</div>

Hi AMPL experts! I’m running into unexpected behavior. I have the following definitions:

```auto
param timeInterval default 5;
set minutes := 0..59 by timeInterval;
set hours := 800..1500 by 100;
set timeSlots := setof{h in hours, m in minutes} (h + m);
set times := {days, timeSlots};
set schedule within {grades, days, st in timeSlots, et in timeSlots: st <= et - timeInterval};
set scheduleTimes{(g,d,st,et) in schedule} := {(d,t) in {days, timeSlots}: t <= et - timeInterval and t >= st}; 

```

In my mind, each indexed set scheduleTimes should be comprised of ordered pairs like (M, 845). (The timeSlots st and et are start times and end times for an interval.)  
But, for some really simple data, I don’t get ordered pairs.

```auto
ampl: display scheduleTimes;
set scheduleTimes[5,M,845,900] := 845 850 855; <--- These are just timeSlots

```

If I manually enter a diplay command like the following, I get the expected behavior:

```auto
ampl: display {(d,t) in {days, timeSlots}: t <= 900 - timeInterval and t >= 845};
set {d in days, t in timeSlots: t <= 900 - timeInterval && t >= 845} :=
(M,845) (M,855) (T,850) (W,845) (W,855) (R,850) (F,845) (F,855)
(M,850) (T,845) (T,855) (W,850) (R,845) (R,855) (F,850); <--- Ordered pairs

```

Any thoughts? Happy to send the whole model and data file, if it would be helpful. They are still quite small.

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![Andy\_Felt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/andy_felt/32/872_2.png) [@Andy\_Felt](https://discuss.ampl.com/u/Andy_Felt)\
**Post date:** [December 10, 2024, 7:56pm UTC](https://discuss.ampl.com/t/unexpected-behavior-from-defined-set/2005/2 "2024-12-10T19:56:25Z")

</div>

Nevermind! It was my intention that the d’s in the definition of scheduleTimes would match, but that was perhaps assuming too much. If I change it to the following, it works:  
set scheduleTimes{(g,d,st,et) in schedule} :=  
{dd in days, t in timeSlots :dd==d and t \<= et - timeInterval and t \>= st};

Thanks! Resolved

P.s. If you have suggestions on how to simplify this messy swamp of definitions, I’m all ears.

---

<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 11, 2024, 3:32pm UTC](https://discuss.ampl.com/t/unexpected-behavior-from-defined-set/2005/3 "2024-12-11T15:32:42Z")

</div>

Because `d` is defined in the indexing expression at the beginning of the `set` statement,

```auto
set scheduleTimes {(g,d,st,et) in schedule} := 
  {(d,t) in {days,timeSlots}: t <= et - timeInterval and t >= st}; 

```

the expression `(d,t) in {days,timeSlots}` defines a one-dimensional “slice” though the first dimension of `{days,timeSlots}`. So when you display `scheduleTimes` you see one-dimensional sets of timeslots. However, you could instead use the more general `setof` operator here to specify a set of pairs:

```auto
set scheduleTimes {(g,d,st,et) in schedule} := 
  setof {(d,t) in {days, timeSlots}: t <= et - timeInterval and t >= st} (d,t); 

```

This can be further simplified to

```auto
set scheduleTimes {(g,d,st,et) in schedule} := 
  setof {t in timeSlots: t <= et - timeInterval and t >= st} (d,t); 

```
