0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Set statementの書き方【GLPK】

Last updated at Posted at 2021-08-02

<--目次へ

マニュアル見てもSetの書き方が分からないことがよくあります。
気が付いた例文を書き留めておきますのでご参考に。

不連続な数値型Setの処理

数値型Setの場合には値の並び替えや最大値などの特定の値を取り出すことで、いろいろな処理に使うことができます。

set A :=  {10, 1, 2, 8, 3, 9, 5};

昇順・降順にソート

# 昇順
set A1 := setof{k in 1..card(A), i in A : card({j in A: i>=j})=k}(i);
# 降順
set A2 := setof{k in 1..card(A), i in A : card({j in A: i<=j})=k}(i);

# 数値範囲がわかってる場合の昇順
set A3:={1..10} inter A;
# 降順
set A4:=setof{i in 1..10}(11-i) inter A;

最小値と最大値、小さい方からn番目

# 最小値
set A5 := {i in A : card({j in A : i>=j})=1};
# 最大値
set A6 := {i in A : card({j in A : i<=j})=1};
# 小さい方から4番目
set A7 := {i in A : card({j in A : i>=j})=4};

隣あう数値のペア

set A8 := setof{k in 1..card(A)-1, i1 in A, i2 in A : card({j1 in A: i1>=j1})=k and card({j2 in A: i2>=j2})=k+1}(i1,i2);
# 結果 {(1,2), (2,3), (3,5), (5,8), (8,9), (9,10)}

1から連番でインデックスをつける

set A9{i in 1..card(A)} := setof{k in A :card({j in A : j<=k})==i}(k);
# 結果 A9[1]:{1}, A9[2]:{2}, A9[3]:{3}, A9[4]:{5}, A9[5]:{8}, A9[6]:{9}, A9[7]:{10}

添字付きのSet

添字以下の値を含むSet

set B := {1, 2, 3, 5, 8, 9, 10};
set B1{i in B} := {j in B : i>=j};
# 結果 B1[1]: {1}, B1[2]: {1, 2}, B1[3]: {1, 2, 3}, B1[5]: {1, 2, 3, 5}
# B1[8]: {1, 2, 3, 5, 8}, B1[9]: {1, 2, 3, 5, 8, 9}, B1[10]: {1, 2, 3, 5, 8, 9, 10}

添字付きのSet(B1)を使った処理

# 最小値
set B2 := setof{i in B: card(B1[i])=1}(i);
# 最大値
set B3 := setof{i in B: card(B1[i])=card(B)}(i);
# 小さい方から4番目
set B4 := setof{i in B : card(B1[i])=4}(i);
# 隣あう数値のペア
set B5 := setof{i in B, j in B1[i]: card(B1[i])=card(B1[j])+1}(j,i);
# 1から連番でインデックスをつける
set B6{i in 1..card(B)} := {j in B : card(B1[j])=I};

タプルのn番目の要素を取り出す

set C1 := {(1,2), (1,3), (2,3), (2,4)};
set C2 := setof{(i,j) in C1}(i); # 1番目の要素
set C3 := setof{(i,j) in C1}(j); # 2番目の要素
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?