[AMPL 24503] circular dependencies

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

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:

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

1 Like

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:

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

Thank you a lot!!