[英]How to compile the below 4 decision variable arrays in one array using tuple and to be seen in the OPL IDE as a 2D view?
我有 4 個決策變量數組的輸出,我想在二維數組中表示它們。
我的代碼具有以下將輸入到 2D 陣列的輸出: 我沒有 N 束分布在 N 行中的所有 N 個通道(列)上。
int Nbeams=21; //to be distributed on the 2D array
int Nchannels=16; //No of columns
int Nrows=4; //No of rows
dvar int No_beams_in_each_row[rows]=[6,6,6,4];
dvar int first_beam_in_each_row [rows]=[1,7,13,19];
dvar int beam_firstchannel[beams]=[1,3,5,7,9,11,1,3,5,7,9,11,1,3,5,7,9,11,1,5,9,13];
dvar int beam_nomusedchannel[beams]=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4];
dvar int beam_row[beams]=[1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4];
我已經閱讀了下面的代碼,但我無法在我的模型上調整它來獲取二維數組,所以我需要幫助
// 2 ways to display 2D objects in OPL
// 1) Rely on the OPL gantt chart
// 2) Call Python from OPL
int n=6;
int Half=n div 2;
range FirstHalf = 1..Half;
range LastHalf = n-Half+1..n;
range States = 0..1;
range Bord = 0..(n+1);
range Interior = 1..n;
range obj = 0..(n*n);
tuple neighbors {
int row;
int col;
}
{neighbors} Neighbor =
{<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};
dvar int Life[Bord][Bord] in States;
dvar int Obj in obj;
maximize Obj;
subject to {
ct1:
Obj == sum( i , j in Bord )
Life[i][j];
forall( i , j in Interior ) {
ct21:
2*Life[i][j] - sum( nb in Neighbor )
Life[i+nb.row][j+nb.col] <= 0;
ct22:
3*Life[i][j] + sum( nb in Neighbor )
Life[i+nb.row][j+nb.col] <= 6;
forall( ordered n1 , n2 , n3 in Neighbor ) {
ct23:
-Life[i][j]+Life[i+n1.row][j+n1.col]
+Life[i+n2.row][j+n2.col]
+Life[i+n3.row][j+n3.col]
-sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
Life[i+nb.row][j+nb.col] <= 2;
}
}
forall( j in Bord ) {
ct31:
Life[0][j] == 0;
ct32:
Life[j][0] == 0;
ct33:
Life[j][n+1] == 0;
ct34:
Life[n+1][j] == 0;
}
forall( i in Bord : i<n ) {
ct41:
Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
ct42:
Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
ct43:
Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
ct44:
Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
}
ct5:
sum( i in FirstHalf , j in Bord )
Life[i][j] >=
sum( i in LastHalf , j in Bord )
Life[i][j];
ct6:
sum( i in Bord , j in FirstHalf )
Life[i][j] >=
sum( i in Bord , j in LastHalf )
Life[i][j];
}
// Use the Gantt
tuple sequence_like {
int start;
int end;
string label;
int type;
};
{sequence_like} array2[i in 1..n] = {<j-1,j," ",Life[i][j]> | j in 1..n};
execute noname {
array2; // This array2 can be seen in the OPL IDE as a 2D view
}
tuple LifeSolutionT{
int Bord1;
int Bord2;
int value;
};
{LifeSolutionT} LifeSolution = {<i0,i1,Life[i0][i1]> | i0 in Bord,i1 in Bord};
execute DISPLAY
{
var python=new IloOplOutputFile("c:/temp/display.py");
python.writeln("import matplotlib.pyplot as plt");
python.writeln("import numpy as np");
python.writeln("grid=np.array(");
python.writeln("[");
for(var i in Bord)
{
python.writeln("[");
for(var j in Bord) python.write(Life[i][j],",");
python.writeln("],");
;
}
python.writeln("]");
python.writeln(")");
python.writeln("im = plt.imshow(grid, cmap='hot')");
python.writeln("im.axes.get_xaxis().set_visible(False)");
python.writeln("im.axes.get_yaxis().set_visible(False)");
python.writeln("plt.show()");
python.close();
IloOplExec("C:\\Python36\\python.exe c:\\temp\\display.py");
}
您的陣列是一維的,但您可以將它們變成二維的。
int Nbeams=21; //to be distributed on the 2D array
int Nchannels=16; //No of columns
int Nrows=4; //No of rows
range rows=1..Nrows;
int sol[rows]=[6,6,6,4];
dvar int No_beam_in_each_row [rows];
subject to
{
forall(i in rows) No_beam_in_each_row[i]==sol[i];
}
int isBeamThere[i in rows][j in 1..Nchannels]=(j<=No_beam_in_each_row[i]);
execute
{
writeln("No_beam_in_each_row=",No_beam_in_each_row);
writeln("isBeamThere=",isBeamThere);
}
工作正常
並給出
No_beam_in_each_row= [6 6 6 4]
isBeamThere= [[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]]
您可以在 IDE 中看到
如果你想在甘特圖中看到它,那么在 the.mod 中添加
// Use the Gantt
tuple sequence_like {
int start;
int end;
string label;
int type;
};
{sequence_like} array2[i in rows] = {<j-1,j," ",isBeamThere[i][j]> | j in 1..Nchannels};
execute
{
array2;
}
在 IDE 中,當您單擊 array2 時,您將看到
謝謝,但我的問題是還要考慮在其他兩個陣列中表示的每個波束的通道數量和位置:
(1) beam_nomusedchannel[beams]=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4]
(2) beam_firstchannel[beams]=[1,3,5,7,9,11,1,3,5,7,9,11,1,3,5,7,9,11,1,5,9,13];
for example the first three rows each one has 6 beams and each beam has 2 channels and the last row has 4 beams each one has 4 channels so we will have
isBeamThere = [[1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]
以及如何在甘特圖中表示這個數組? 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.