目录

引言

一、问题描述

 二、问题分析

三、问题解决


引言

出现如下报错:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices


一、问题描述

在运行代码时,出现了报错:

代码:

from pypower.api import case39, runopf
import numpy as np

'''第一级故障'''
mpc1 = case39() #打开case39
# #发电机故障
# mpc['gen'][1, 7] = 0 #第二台发电机停运

mpc1['branch'][0, 10] = 0 #第一条线路断线
#运行线路
result = runopf(mpc1)
#判断是否存在问题
#线路负载率
weak = np.zeros(92).reshape((46, 2))
load_pro = abs(result['branch'][:,13] / result['branch'][:,5])
for i in range(46):
    if load_pro[i] >= 0.8:
        weak[i][0] = i #储存存在故障的线路标号
        weak[i][1] = load_pro[i] #储存存在故障的线路负载率
#选出负载率最大的作为第二级故障
weak_max = np.max(weak[:, 1]) #第二级故障的负载率
weakmax_index = np.argmax(weak[:, 1])
weak_branch = weak[weakmax_index, 0] #第二级故障的线路

'''第二级故障'''
mpc2 = case39() #打开case39
mpc2['branch'][0, 10] = 0 #第一条线路断线
mpc2['branch'][weak_branch, 10] = 0 #第二条线路断线
result = runopf(mpc2)










结果 

Traceback (most recent call last):
  File "C:\First semester of Senior year\本科毕设\毕设参考论文学习笔记\fault.py", line 28, in <module>
    mpc2['branch'][weak_branch, 10] = 0 #第二条线路断线
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

 二、问题分析

数组的索引必须是整数,不能是浮点数、字符串等其他类型。

而本代码问题出在这里:

 weakmax_index的类型如图所示:

 均是浮点数。


三、问题解决

将weak_branch转换为整数类型,用int函数:

weak_branch = int(weak[weakmax_index, 0]) #第二级故障的线路

完整代码如下所示:

from pypower.api import case39, runopf
import numpy as np

'''第一级故障'''
mpc1 = case39() #打开case39
# #发电机故障
# mpc['gen'][1, 7] = 0 #第二台发电机停运

mpc1['branch'][0, 10] = 0 #第一条线路断线
#运行线路
result = runopf(mpc1)
#判断是否存在问题
#线路负载率
weak = np.zeros(92).reshape((46, 2))
load_pro = abs(result['branch'][:,13] / result['branch'][:,5])
for i in range(46):
    if load_pro[i] >= 0.8:
        weak[i][0] = i #储存存在故障的线路标号
        weak[i][1] = load_pro[i] #储存存在故障的线路负载率
#选出负载率最大的作为第二级故障
weak_max = np.max(weak[:, 1]) #第二级故障的负载率
weakmax_index = np.argmax(weak[:, 1])
weak_branch = int(weak[weakmax_index, 0]) #第二级故障的线路

'''第二级故障'''
mpc2 = case39() #打开case39
mpc2['branch'][0, 10] = 0 #第一条线路断线
mpc2['branch'][weak_branch, 10] = 0 #第二条线路断线
result = runopf(mpc2)










结果:

C:\Users\Admin\anaconda3\envs\pppp\python.exe "C:\First semester of Senior year\本科毕设\毕设参考论文学习笔记\fault.py" 
PYPOWER Version 5.1.4, 27-June-2018 -- AC Optimal Power Flow
Python Interior Point Solver - PIPS, Version 1.0, 07-Feb-2011
Converged!

Converged in 0.84 seconds
Objective Function Value = 43441.32 $/hr
================================================================================
|     System Summary                                                           |
================================================================================

How many?                How much?              P (MW)            Q (MVAr)
---------------------    -------------------  -------------  -----------------
Buses             39     Total Gen Capacity    7367.0        -160.0 to 2807.0
Generators        10     On-line Capacity      7367.0        -160.0 to 2807.0
Committed Gens    10     Generation (actual)   6296.7            1409.8
Loads             21     Load                  6254.2            1387.1
  Fixed           21       Fixed               6254.2            1387.1
  Dispatchable     0       Dispatchable           0.0 of 0.0        0.0
Shunts             0     Shunt (inj)              0.0               0.0
Branches          46     Losses (I^2 * Z)        42.46           1065.27
Transformers      12     Branch Charging (inj)     -             1042.6
Inter-ties         6     Total Inter-tie Flow   673.6             192.8
Areas              3

                          Minimum                      Maximum
                 -------------------------  --------------------------------
Voltage Magnitude   0.970 p.u. @ bus 38         1.060 p.u. @ bus 19  
Voltage Angle     -22.67 deg   @ bus 1          8.69 deg   @ bus 36  
P Losses (I^2*R)             -                  3.65 MW    @ line 29-38
Q Losses (I^2*X)             -                116.45 MVAr  @ line 10-32
Lambda P            8.76 $/MWh @ bus 2         19.18 $/MWh @ bus 3   
Lambda Q           -0.74 $/MWh @ bus 37         0.27 $/MWh @ bus 3   

================================================================================
|     Bus Data                                                                 |
================================================================================
 Bus      Voltage          Generation             Load          Lambda($/MVA-hr)
  #   Mag(pu) Ang(deg)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)     P        Q   
----- ------- --------  --------  --------  --------  --------  -------  -------
    1  1.052  -22.667       -         -       97.60     44.20    19.127   0.002
    2  1.046   -4.333       -         -         -         -       8.764  -0.330
    3  1.036   -8.286       -         -      322.00      2.40    19.178   0.266
    4  1.025  -10.218       -         -      500.00    184.00    18.721   0.262
    5  1.034   -9.895       -         -         -         -      18.628   0.237
    6  1.038   -9.181       -         -         -         -      18.573   0.229
    7  1.026  -11.916       -         -      233.80     84.00    18.719   0.244
    8  1.024  -12.726       -         -      522.00    176.60    18.775   0.237
    9  1.060  -18.051       -         -        6.50    -66.60    19.004  -0.008
   10  1.049   -6.280       -         -         -         -      18.314   0.205
   11  1.044   -7.260       -         -         -         -      18.403   0.220
   12  1.031   -7.137       -         -        8.53     88.00    18.372   0.251
   13  1.043   -6.891       -         -         -         -      18.334   0.216
   14  1.035   -8.299       -         -         -         -      18.367   0.226
   15  1.028   -8.135       -         -      320.00    153.00    17.791   0.181
   16  1.040   -6.509       -         -      329.00     32.30    17.437   0.113
   17  1.037   -7.630       -         -         -         -      17.267   0.111
   18  1.035   -8.305       -         -      158.00     30.00    18.020   0.176
   19  1.060   -1.771       -         -         -         -      17.183   0.019
   20  0.998   -3.152       -         -      680.00    103.00    17.227   0.032
   21  1.039   -3.887       -         -      274.00    115.00    17.329   0.101
   22  1.057    0.756       -         -         -         -      17.150   0.044
   23  1.049    0.558       -         -      247.50     84.60    17.156   0.060
   24  1.045   -6.321       -         -      308.60    -92.20    17.431   0.099
   25  1.045   -3.707       -         -      224.00     47.20     9.852  -0.725
   26  1.035   -6.816       -         -      139.00     17.00    13.598  -0.145
   27  1.030   -8.373       -         -      281.00     75.50    15.343  -0.023
   28  1.015   -5.339       -         -      206.00     27.60    13.548  -0.066
   29  1.008   -3.092       -         -      283.50     26.90    13.458  -0.033
   30  1.042   -0.183    425.55    140.00       -         -       8.811  -0.318
   31  1.029    0.000*   646.00    300.00      9.20      4.60    18.502   0.209
   32  1.028    1.993    725.00    300.00       -         -      18.256   0.191
   33  1.011    3.477    652.00    141.08       -         -      17.025     -
   34  1.017    1.985    508.00    151.01       -         -      17.068     -
   35  1.060    5.914    687.00    245.67       -         -      17.146     -
   36  1.060    8.693    580.00     71.98       -         -      17.057     -
   37  1.016    2.501    482.73      0.00       -         -       9.955  -0.737
   38  0.970    3.055    650.75    -80.18       -         -      13.315     -
   39  1.054  -21.409    939.66    140.23   1104.00    250.00    19.093     -
                        --------  --------  --------  --------
               Total:   6296.69   1409.80   6254.23   1387.10

================================================================================
|     Branch Data                                                              |
================================================================================
Brnch   From   To    From Bus Injection   To Bus Injection     Loss (I^2 * Z)  
  #     Bus    Bus    P (MW)   Q (MVAr)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)
-----  -----  -----  --------  --------  --------  --------  --------  --------
   0      1      2      0.00      0.00      0.00      0.00     0.000      0.00
   1      1     39    -97.60    -44.20     97.69    -36.86     0.086      2.15
   2      2      3    498.84     33.98   -495.86    -27.22     2.982     34.64
   3      2     25    -73.29     72.60     74.05    -87.62     0.759      0.93
   4      2     30   -425.55   -106.58    425.55    140.00    -0.000     33.42
   5      3      4    170.79     32.23   -170.42    -49.55     0.377      6.18
   6      3     18      3.07     -7.41     -3.07    -15.51     0.000      0.00
   7      4      5    -50.79    -72.31     50.84     58.92     0.052      0.83
   8      4     14   -278.79    -62.14    279.41     57.39     0.615      9.91
   9      5      6   -522.38   -113.49    522.91    115.78     0.534      6.94
  10      5      8    471.54     54.57   -469.84    -46.49     1.694     23.72
  11      6      7    559.16    101.69   -557.35    -86.00     1.807     27.71
  12      6     11   -445.27    -38.42    446.56     38.56     1.295     15.18
  13      6     31   -636.80   -179.05    636.80    295.40    -0.000    116.35
  14      7      8    323.55      2.00   -323.15     -5.63     0.398      4.58
  15      8      9    270.99   -124.49   -269.14    112.33     1.849     29.18
  16      9     39    262.64    -45.73   -262.03    -72.91     0.618     15.45
  17     10     11    443.71     83.85   -442.96    -83.82     0.744      8.00
  18     10     13    281.29     99.70   -280.97   -104.16     0.327      3.51
  19     10     32   -725.00   -183.55    725.00    300.00    -0.000    116.45
  20     12     11      3.63    -44.45     -3.60     45.27     0.030      0.82
  21     12     13    -12.16    -43.55     12.19     44.40     0.031      0.85
  22     13     14    268.77     59.76   -268.14    -71.22     0.637      7.14
  23     14     15    -11.27     13.83     11.29    -52.51     0.021      0.25
  24     15     16   -331.29   -100.49    332.30     92.73     1.006     10.51
  25     16     17    238.10      8.49   -237.73    -18.27     0.369      4.69
  26     16     19   -470.97    -67.15    474.29     74.12     3.322     40.48
  27     16     21   -363.95     20.66    364.94    -31.49     0.989     16.69
  28     16     24    -64.48    -87.04     64.51     80.26     0.031      0.61
  29     17     18    155.09      2.17   -154.93    -14.49     0.157      1.84
  30     17     27     82.64     16.10    -82.54    -49.18     0.096      1.28
  31     19     20    174.66      5.11   -174.45     -0.90     0.214      4.21
  32     19     33   -648.95    -79.24    652.00    141.08     3.049     61.84
  33     20     34   -505.55   -102.10    508.00    151.01     2.445     48.91
  34     21     22   -638.94    -83.51    642.00    108.91     3.061     53.57
  35     22     23     45.00     69.02    -44.95    -88.78     0.045      0.71
  36     22     35   -687.00   -177.92    687.00    245.67    -0.000     67.75
  37     23     24    375.93     -6.53   -373.11     11.94     2.827     44.98
  38     23     36   -578.48     10.71    580.00     71.98     1.520     82.69
  39     25     26    183.32    -11.94   -182.33    -35.47     0.994     10.03
  40     25     37   -481.38     52.36    482.73      0.00     1.354     52.36
  41     26     27    198.98      6.24   -198.46    -26.32     0.522      5.48
  42     26     28    -52.51      9.04     52.72    -88.65     0.214      2.36
  43     26     29   -103.14      3.19    103.88   -102.43     0.747      8.19
  44     28     29   -258.72     61.05    259.71    -75.90     0.985     10.62
  45     29     38   -647.09    151.43    650.75    -80.18     3.654     71.25
                                                             --------  --------
                                                    Total:    42.457   1065.27

================================================================================
|     Generation Constraints                                                   |
================================================================================
 Gen   Bus                Active Power Limits
  #     #    Pmin mu    Pmin       Pg       Pmax    Pmax mu
----  -----  -------  --------  --------  --------  -------
   1    31      -        0.00    646.00    646.00    5.282
   2    32      -        0.00    725.00    725.00    3.456
   3    33      -        0.00    652.00    652.00    3.685
   4    34      -        0.00    508.00    508.00    6.608
   5    35      -        0.00    687.00    687.00    3.106
   6    36      -        0.00    580.00    580.00    5.157

Gen  Bus              Reactive Power Limits
 #    #   Qmin mu    Qmin       Qg       Qmax    Qmax mu
---  ---  -------  --------  --------  --------  -------
  0   30   0.318    140.00    140.00    400.00      -  
  1   31     -     -100.00    300.00    300.00    0.209
  2   32     -      150.00    300.00    300.00    0.191
  7   37   0.737      0.00      0.00    250.00      -  

================================================================================
|     Branch Flow Constraints                                                  |
================================================================================
Brnch   From     "From" End        Limit       "To" End        To
  #     Bus   |Sf| mu    |Sf|     |Smax|     |St|    |St| mu   Bus
-----  -----  -------  --------  --------  --------  -------  -----
   2      2    11.853   500.00    500.00    496.61      -        3
PYPOWER Version 5.1.4, 27-June-2018 -- AC Optimal Power Flow
Python Interior Point Solver - PIPS, Version 1.0, 07-Feb-2011
Converged!

Converged in 1.02 seconds
Objective Function Value = 44766.34 $/hr
================================================================================
|     System Summary                                                           |
================================================================================

How many?                How much?              P (MW)            Q (MVAr)
---------------------    -------------------  -------------  -----------------
Buses             39     Total Gen Capacity    7367.0        -160.0 to 2807.0
Generators        10     On-line Capacity      7367.0        -160.0 to 2807.0
Committed Gens    10     Generation (actual)   6321.4            1614.1
Loads             21     Load                  6254.2            1387.1
  Fixed           21       Fixed               6254.2            1387.1
  Dispatchable     0       Dispatchable           0.0 of 0.0        0.0
Shunts             0     Shunt (inj)              0.0               0.0
Branches          46     Losses (I^2 * Z)        67.13           1217.69
Transformers      12     Branch Charging (inj)     -              990.7
Inter-ties         6     Total Inter-tie Flow   498.9             192.2
Areas              3

                          Minimum                      Maximum
                 -------------------------  --------------------------------
Voltage Magnitude   0.971 p.u. @ bus 38         1.060 p.u. @ bus 2   
Voltage Angle     -19.47 deg   @ bus 1         18.07 deg   @ bus 37  
P Losses (I^2*R)             -                 11.16 MW    @ line 2-25
Q Losses (I^2*X)             -                119.98 MVAr  @ line 10-32
Lambda P            8.31 $/MWh @ bus 2         21.18 $/MWh @ bus 1   
Lambda Q           -0.89 $/MWh @ bus 2          0.20 $/MWh @ bus 12  

================================================================================
|     Bus Data                                                                 |
================================================================================
 Bus      Voltage          Generation             Load          Lambda($/MVA-hr)
  #   Mag(pu) Ang(deg)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)     P        Q   
----- ------- --------  --------  --------  --------  --------  -------  -------
    1  1.058  -19.471       -         -       97.60     44.20    21.184   0.002
    2  1.060   13.733       -         -         -         -       8.309  -0.895
    3  1.007  -11.202       -         -      322.00      2.40    20.828   0.146
    4  1.005  -11.224       -         -      500.00    184.00    20.831   0.196
    5  1.018  -10.232       -         -         -         -      20.782   0.180
    6  1.022   -9.450       -         -         -         -      20.734   0.173
    7  1.012  -12.049       -         -      233.80     84.00    20.878   0.194
    8  1.012  -12.773       -         -      522.00    176.60    20.929   0.189
    9  1.060  -16.189       -         -        6.50    -66.60    21.087  -0.000
   10  1.032   -6.524       -         -         -         -      20.534   0.148
   11  1.027   -7.515       -         -         -         -      20.605   0.163
   12  1.014   -7.410       -         -        8.53     88.00    20.598   0.198
   13  1.026   -7.178       -         -         -         -      20.584   0.161
   14  1.017   -8.692       -         -         -         -      20.695   0.174
   15  1.013   -7.754       -         -      320.00    153.00    20.630   0.156
   16  1.026   -5.753       -         -      329.00     32.30    20.477   0.094
   17  1.015   -6.553       -         -         -         -      20.523   0.084
   18  1.010   -8.764       -         -      158.00     30.00    20.669   0.116
   19  1.060   -1.016       -         -         -         -      20.185   0.039
   20  1.000   -2.400       -         -      680.00    103.00    20.238   0.049
   21  1.030   -3.088       -         -      274.00    115.00    20.352   0.088
   22  1.052    1.607       -         -         -         -      20.144   0.030
   23  1.044    1.406       -         -      247.50     84.60    20.152   0.049
   24  1.032   -5.564       -         -      308.60    -92.20    20.471   0.081
   25  1.024   12.324       -         -      224.00     47.20     8.802  -0.663
   26  1.007    1.588       -         -      139.00     17.00    13.490  -0.090
   27  1.002   -3.422       -         -      281.00     75.50    20.344  -0.012
   28  1.001    2.989       -         -      206.00     27.60    13.438  -0.041
   29  0.999    5.243       -         -      283.50     26.90    13.345  -0.020
   30  1.056   17.595    406.39    140.00       -         -       8.428  -0.863
   31  1.015    0.000*   646.00    300.00      9.20      4.60    20.679   0.157
   32  1.013    2.011    725.00    300.00       -         -      20.491   0.137
   33  1.019    4.168    652.00    199.35       -         -      20.000     -
   34  1.022    2.694    508.00    166.87       -         -      20.051   0.006
   35  1.060    6.789    687.00    282.10       -         -      20.142     -
   36  1.060    9.577    580.00     92.38       -         -      20.038     -
   37  0.996   18.069    429.49      0.00       -         -       8.890  -0.672
   38  0.971   11.359    645.13    -18.84       -         -      13.203     -
   39  1.060  -18.225   1042.36    152.20   1104.00    250.00    21.147     -
                        --------  --------  --------  --------
               Total:   6321.36   1614.06   6254.23   1387.10

================================================================================
|     Branch Data                                                              |
================================================================================
Brnch   From   To    From Bus Injection   To Bus Injection     Loss (I^2 * Z)  
  #     Bus    Bus    P (MW)   Q (MVAr)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)
-----  -----  -----  --------  --------  --------  --------  --------  --------
   0      1      2      0.00      0.00      0.00      0.00     0.000      0.00
   1      1     39    -97.60    -44.20     97.69    -37.73     0.085      2.13
   2      2      3      0.00      0.00      0.00      0.00     0.000      0.00
   3      2     25    406.39    110.00   -395.23   -112.15    11.159     13.71
   4      2     30   -406.39   -110.00    406.39    140.00    -0.000     30.00
   5      3      4      2.25     -3.36     -2.25    -19.03     0.001      0.01
   6      3     18   -324.25      0.96    325.39     -8.89     1.142     13.81
   7      4      5   -144.04    -97.23    144.27     87.17     0.229      3.67
   8      4     14   -353.71    -67.73    354.73     70.05     1.020     16.44
   9      5      6   -554.68   -117.07    555.30    120.60     0.619      8.05
  10      5      8    410.41     29.90   -409.10    -26.75     1.311     18.36
  11      6      7    515.70     82.20   -514.13    -69.79     1.572     24.10
  12      6     11   -434.20    -26.98    435.47     27.22     1.266     14.83
  13      6     31   -636.80   -175.83    636.80    295.40    -0.000    119.57
  14      7      8    280.33    -14.21   -280.02      9.76     0.307      3.53
  15      8      9    167.12   -159.61   -166.05    135.65     1.069     16.87
  16      9     39    159.55    -69.05   -159.32    -60.06     0.227      5.66
  17     10     11    433.55     73.53   -432.82    -73.42     0.728      7.83
  18     10     13    291.45    106.50   -291.08   -110.30     0.365      3.92
  19     10     32   -725.00   -180.02    725.00    300.00    -0.000    119.98
  20     12     11      2.68    -45.32     -2.64     46.20     0.032      0.88
  21     12     13    -11.21    -42.68     11.24     43.52     0.031      0.83
  22     13     14    279.85     66.78   -279.13    -76.70     0.718      8.06
  23     14     15    -75.60      6.65     75.71    -43.00     0.111      1.34
  24     15     16   -395.71   -110.00    397.17    107.51     1.464     15.29
  25     16     17    172.67    110.73   -172.38   -121.02     0.290      3.69
  26     16     19   -470.60   -135.54    474.19    146.09     3.581     43.64
  27     16     21   -363.80     -8.78    364.81     -1.18     1.005     16.97
  28     16     24    -64.44   -106.22     64.48     99.83     0.042      0.82
  29     17     18    485.00     26.40   -483.39    -21.11     1.606     18.81
  30     17     27   -312.62     94.61    314.01   -108.83     1.389     18.49
  31     19     20    174.68    -10.33   -174.47     14.56     0.214      4.23
  32     19     33   -648.87   -135.76    652.00    199.35     3.135     63.59
  33     20     34   -505.53   -117.56    508.00    166.87     2.466     49.32
  34     21     22   -638.81   -113.82    641.96    141.25     3.155     55.21
  35     22     23     45.04     70.66    -44.99    -90.19     0.046      0.74
  36     22     35   -687.00   -211.91    687.00    282.10    -0.000     70.20
  37     23     24    375.95     14.46   -373.08     -7.63     2.876     45.75
  38     23     36   -578.47     -8.87    580.00     92.38     1.535     83.50
  39     25     26    599.60     21.85   -588.55     34.92    11.050    111.53
  40     25     37   -428.37     43.10    429.49      0.00     1.115     43.10
  41     26     27    599.98     -5.32   -595.01     33.33     4.969     52.18
  42     26     28    -50.23    -20.48     50.36    -56.79     0.122      1.35
  43     26     29   -100.19    -26.13    100.79    -70.76     0.602      6.60
  44     28     29   -256.36     29.19    257.30    -43.90     0.943     10.17
  45     29     38   -641.59     87.76    645.13    -18.84     3.534     68.92
                                                             --------  --------
                                                    Total:    67.133   1217.69

================================================================================
|     Generation Constraints                                                   |
================================================================================
 Gen   Bus                Active Power Limits
  #     #    Pmin mu    Pmin       Pg       Pmax    Pmax mu
----  -----  -------  --------  --------  --------  -------
   1    31      -        0.00    646.00    646.00    7.459
   2    32      -        0.00    725.00    725.00    5.691
   3    33      -        0.00    652.00    652.00    6.660
   4    34      -        0.00    508.00    508.00    9.591
   5    35      -        0.00    687.00    687.00    6.102
   6    36      -        0.00    580.00    580.00    8.138

Gen  Bus              Reactive Power Limits
 #    #   Qmin mu    Qmin       Qg       Qmax    Qmax mu
---  ---  -------  --------  --------  --------  -------
  0   30   0.863    140.00    140.00    400.00      -  
  1   31     -     -100.00    300.00    300.00    0.157
  2   32     -      150.00    300.00    300.00    0.137
  4   34     -        0.00    166.87    167.00    0.006
  7   37   0.672      0.00      0.00    250.00      -  

================================================================================
|     Branch Flow Constraints                                                  |
================================================================================
Brnch   From     "From" End        Limit       "To" End        To
  #     Bus   |Sf| mu    |Sf|     |Smax|     |St|    |St| mu   Bus
-----  -----  -------  --------  --------  --------  -------  -----
  39     25     4.264   600.00    600.00    589.59      -       26
  41     26     6.519   600.00    600.00    595.94      -       27

进程已结束,退出代码0

跑通啦

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐