以下のプログラムでインデックスのサイズが足りていないのはわかるのですがそれをどう直したらよいのかわかりません。
サイズは1~9までの大きさにしたいと思っています。
どう修正したらよいでしょうか。
python
1#プログラム 2>>> (opt_i,opt_j)=(0,0) 3>>> for (i,j) in E_hat: 4 for (u,v) in pi: 5 (opt_i,opt_j) = np.argmax(sum(max(0,war[u,v]-(war[u,i]+dist_x[i][j]+war[j,v]),war[u,v]-(war[u,j]+dist_x[i][j]+war[i,v])))) 6 7#エラーメッセージ 8Traceback (most recent call last): 9 File "<pyshell#119>", line 3, in <module> 10 (opt_i,opt_j) = np.argmax(sum(max(0,war[u,v]-(war[u,i]+dist_x[i][j]+war[j,v]),war[u,v]-(war[u,j]+dist_x[i][j]+war[i,v])))) 11 File "C:\Users\stuart\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\matrixlib\defmatrix.py", line 195, in __getitem__ 12 out = N.ndarray.__getitem__(self, index) 13IndexError: index 9 is out of bounds for axis 1 with size 9
次のような方法も試してみましたがやはりエラーの根本は同じでした。
python
1#プログラム 2>>> wa=0 3>>> (opt_i,opt_j)=(0,0) 4>>> for (i,j) in E_hat: 5 for (u,v) in pi: 6 wa=wa+max(0,war[u,v]-(war[u,i]+dist_x[i][j]+war[j,v]),war[u,v]-(war[u,j]+dist_x[i][j]+war[i,v])) 7 a=wa 8 (opt_i,opt_j)=np.argmax(a) 9 10 11#エラーメッセージ 12Traceback (most recent call last): 13 File "<pyshell#116>", line 3, in <module> 14 wa=wa+max(0,war[u,v]-(war[u,i]+dist_x[i][j]+war[j,v]),war[u,v]-(war[u,j]+dist_x[i][j]+war[i,v])) 15 File "C:\Users\stuart\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\matrixlib\defmatrix.py", line 195, in __getitem__ 16 out = N.ndarray.__getitem__(self, index) 17IndexError: index 9 is out of bounds for axis 1 with size 9 18
プログラム内で出てくるE_hat,pi,war,dist_xはそれぞれこのようになっています
Python
1>>> E_hat 2{(7, 3), (4, 8), (2, 8), (9, 8), (6, 2), (1, 6), (3, 7), (8, 5), (7, 6), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (3, 9), (1, 9), (7, 5), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (6, 8), (3, 1), (1, 7), (7, 8), (2, 4), (8, 4), (5, 9), (9, 1), (2, 1), (9, 4), (5, 1), (7, 2), (4, 9), (2, 9), (8, 1), (6, 3), (1, 5), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (3, 8), (7, 4), (1, 8), (4, 3), (9, 5), (5, 2)} 3>>> pi 4{(5, 9), (4, 7), (1, 3), (6, 9), (4, 8), (5, 6), (2, 8), (8, 9), (1, 6), (3, 7), (2, 5), (5, 8), (1, 2), (4, 9), (6, 7), (2, 9), (1, 5), (3, 6), (2, 6), (4, 5), (1, 4), (3, 9), (2, 3), (1, 9), (3, 5), (2, 7), (7, 8), (7, 9), (4, 6), (6, 8), (5, 7), (3, 8), (1, 8), (1, 7), (3, 4), (2, 4)} 5>>> war 6matrix([[ 0. , 6.32455532, 4. , 5. , 14.38681307, 7 9. , 13. , 18.85730076, 13.47213595], 8 [ 6.32455532, 0. , 6.32455532, 10.44766095, 8.06225775, 9 11.32455532, 15.79669128, 14.46538199, 15.79669128], 10 [ 4. , 6.32455532, 0. , 4.12310563, 12.07106781, 11 5. , 9.47213595, 14.85730076, 9.47213595], 12 [ 5. , 10.44766095, 4.12310563, 0. , 16.19417344, 13 9.12310563, 8. , 16.21359193, 10.82842712], 14 [14.38681307, 8.06225775, 12.07106781, 16.19417344, 0. , 15 7.07106781, 11.54320377, 6.40312424, 11.54320377], 16 [ 9. , 11.32455532, 5. , 9.12310563, 7.07106781, 17 0. , 4.47213595, 9.85730076, 4.47213595], 18 [13. , 15.79669128, 9.47213595, 8. , 11.54320377, 19 4.47213595, 0. , 8.21359193, 2.82842712], 20 [18.85730076, 14.46538199, 14.85730076, 16.21359193, 6.40312424, 21 9.85730076, 8.21359193, 0. , 5.38516481], 22 [13.47213595, 15.79669128, 9.47213595, 10.82842712, 11.54320377, 23 4.47213595, 2.82842712, 5.38516481, 0. ]]) 24>>> dist_x 25array([[ 0. , 6.32455532, 4. , 5. , 12.20655562, 26 9. , 11.70469991, 15.29705854, 13.15294644], 27 [ 6.32455532, 0. , 6.32455532, 10.04987562, 8.06225775, 28 9.21954446, 13.45362405, 13.34166406, 13.60147051], 29 [ 4. , 6.32455532, 0. , 4.12310563, 9.21954446, 30 5. , 8.06225775, 11.40175425, 9.21954446], 31 [ 5. , 10.04987562, 4.12310563, 0. , 13.03840481, 32 7.21110255, 8. , 13.89244399, 10.19803903], 33 [12.20655562, 8.06225775, 9.21954446, 13.03840481, 0. , 34 7.07106781, 11.04536102, 6.40312424, 9.48683298], 35 [ 9. , 9.21954446, 5. , 7.21110255, 7.07106781, 36 0. , 4.47213595, 6.70820393, 4.47213595], 37 [11.70469991, 13.45362405, 8.06225775, 8. , 11.04536102, 38 4.47213595, 0. , 8.06225775, 2.82842712], 39 [15.29705854, 13.34166406, 11.40175425, 13.89244399, 6.40312424, 40 6.70820393, 8.06225775, 0. , 5.38516481], 41 [13.15294644, 13.60147051, 9.21954446, 10.19803903, 9.48683298, 42 4.47213595, 2.82842712, 5.38516481, 0. ]])