Reputation: 1
In my optimization problem, I have four charging stations for mobile power sources and two mobile power sources. At any given time, only two of the charging stations are connected to mobile power sources, while the other two remain unused. After identifying the two charging stations to which mobile power sources are connected through the optimization process, I want to define a set that contains charging stations at which mobile power source get connected. After identifying the mobile power source connected charging stations, I need to use this information to formulate constraints within the same optimization problem.
Challenges:
The set elements depend on the outcome of the optimization problem.
The code that I have tried:
range MEG = 1..N;
range Mobilestation = 1..M;
dvar boolean pos[MEG][Mobilestation];
{int} Mobilestation = {};
execute {
for (var n in MEG) {
for (var i in Mobilestation) {
if (pos[n][i] == 1) {
Msource.add(i);
}
}
}
}
But with this code I am getting null set. Please suggest me correct code in CPLEX (OPL) to define the set which depend on the decision variable of the optimization problem.
Upvotes: 0
Views: 96
Reputation: 10062
If you want to use this set in the model , you should rather use another decision variable and then compute the set in the postprocess block. Let me give you a small example:
int N=10;
int M=4;
range MEG = 1..N;
range Mobilestation = 1..M;
dvar boolean pos[MEG][Mobilestation];
dvar boolean isStationUsed[Mobilestation];
minimize sum(i in MEG,j in Mobilestation) pos[i][j];
subject to
{
// suppose we want at least one station for each Meg
forall(i in MEG) sum(j in Mobilestation) pos[i][j]>=1;
// if you want to use isStationUsed in the model use this instead of the set!
forall(j in Mobilestation) isStationUsed[j]==(1<=sum(i in MEG)pos[i][j]);
}
{int} MSource = {};
execute {
for (var n in MEG) {
for (var i in Mobilestation) {
if (pos[n][i] == 1) {
MSource.add(i);
}
}
}
}
Upvotes: 0