# \[AMPL 24503\] circular dependencies

**URL:** <https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355>\
**Category:** Google Group Mirror\
**Created:** [February 2, 2023, 10:55am UTC](https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355 "2023-02-02T10:55:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuele\_Viaro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/samuele_viaro/32/178_2.png) [@Samuele\_Viaro](https://discuss.ampl.com/u/Samuele_Viaro)\
**Post date:** [February 2, 2023, 10:55am UTC](https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355/1 "2023-02-02T10:55:57Z")

</div>

Hello,

I would like to modify a set in AMPL in a while loop in C++…

I created a set SET\_A of numbers to be erased from SET\_B whose size increases at each iteration in the while loop…

Let’s say

_SET\_A = { 1, 3};_  
and I initialize

_SET\_B = {1,2};_

I then have…

_while (condition)_  
_{_  
_//at every iteration I want to remove 1,3 from SET\_B_

_ampl.eval(“redeclare set SET\_B = SET\_B diff SET\_A;”);_

_ampl.solve();_

_//here I increase the SET\_B at the first iteration…then at each iteration I add two more numbers_

_SET\_B = {1,2,3,4}_

_}_

Right now I have a circular dipendency error…is there a way to get around this??

Thank you  
Sam

---

<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:** [February 2, 2023, 11:09pm UTC](https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355/2 "2023-02-02T23:09:03Z")

</div>

When you use `=` in a `set` statement, you are saying that everywhere in the model, the set on the left will have the same value as the expression on the right. You can change your definition using `redeclare`, but AMPL is rejecting your new definition,

```auto
redeclare set SET_B = SET_B diff SET_A;

```

because it is defining `SET_B` by an expression that contains `SET_B`. Instead, you should use AMPL `let` statements to assign values to your sets. Here is an example, just to show the idea:

```auto
set SET_A;
set SET_B;
...
let SET_A := {1,3};
let SET_B := {1,2};

while ...
    let SET_B := SET_B diff SET_A;
    solve;
    ...
    let SET_B := SET_B union {3,4};

```

You can use `let` in similar ways to make whatever assignments you need. (For simplicity, I am just showing the AMPL statements, and you will need to adapt them for use within the C++ API. Also instead of `{3,4}` you would want to use a set expression that is different each time through the loop.)

---

<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:** [February 3, 2023, 4:32pm UTC](https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355/3 "2023-02-03T16:32:50Z")

</div>

When you use = in a set statement, you are saying that everywhere in the model, the set on the left will have the same value as the expression on the right. You can change your definition using redeclare, but AMPL is rejecting your new definition,

`redeclare set SET_B = SET_B diff SET_A;`

because it is defining SET\_B by an expression that contains SET\_B. Instead, you should use AMPL “let” statements to assign values to your sets. Here is an example, just to show the idea:

```auto
set SET_A;
set SET_B;
...
let SET_A := {1,3};
let SET_B := {1,2};

while ...
    let SET_B := SET_B diff SET_A;
    solve;
    ...
    let SET_B := SET_B union {3,4};

```

You can use let in similar ways to make whatever assignments you need. (For simplicity, I am just showing the AMPL statements, and you will need to adapt them for use within the C++ API. Also instead of {3,4} you would want to use a set expression that is different each time through the loop.)

---

<div class="post-metadata">

**Author:** ![Samuele\_Viaro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/samuele_viaro/32/178_2.png) [@Samuele\_Viaro](https://discuss.ampl.com/u/Samuele_Viaro)\
**Post date:** [February 5, 2023, 8:46am UTC](https://discuss.ampl.com/t/ampl-24503-circular-dependencies/355/4 "2023-02-05T08:46:36Z")

</div>

Thank you a lot!!
