簡體   English   中英

索引錯誤有問題 (Python)

[英]Having problems with an index error (Python)

所以我的代碼有一些問題。

本質上,我正在創建一個隨機坐標的初始數組(x0x1)(這是成功的)。 然后,我想從原始坐標數組中的 3 個隨機選擇坐標向量生成一個候選坐標向量 (z)。 然后我將 f(z) 與原始數組中的每對坐標 f(x0x1[i]) 進行比較。 如果 f(z) 較低,那么我將它帶到一個新的坐標數組。 循環重復,直到我找到最小化我的函數的 z 值。

我得到的錯誤是:索引 1 超出了大小為 1 的軸 0 的范圍,這似乎發生在我的 calculateFunction 方法中。 不知道為什么。

這是我正在使用的代碼:

import numpy as np
import numpy.random


def calculateFunctionValue(x):
    
    return (x[0]-1)**2+5(x[1]-x[0]**2)**2


def x0x1Array(N, F, K):
    
    x0x1 = np.zeros((N-1, 1), dtype=float)
    x0 = np.zeros((N-1, 1), dtype=float)
    x1 = np.zeros((N-1, 1), dtype=float)
    
    
    for i in range(0, len(x0)):
        for j in range(0, len(x1)):
                x0[i] = np.random.uniform(-2,2)
                x1[j] = np.random.uniform(-2,2)
                
    x0x1 = np.array((x0,x1)).T
                
    generateCandidateVector(x0x1, N, F, K)
           
            
def generateCandidateVector(newPopulationArray, N, F, K):
    
    x0 = np.zeros((1,2))
    x1 = np.zeros((1,2))
    x2 = np.zeros((1,2))
    populationOneArray = np.zeros((N-1, 1))
    generation = 0
    
    while generation <= K:
        generation = generation + 1
        for i in range(0, N-1):
            x0 = np.random.choice(len(newPopulationArray), 1)
            x1 = np.random.choice(len(newPopulationArray), 1)
            x2 = np.random.choice(len(newPopulationArray), 1)
            vectorZ = x0 + F*(x1-x2)
            
            if(calculateFunctionValue(vectorZ) < calculateFunctionValue(newPopulationArray[i])):
                vectorZ = newPopulationArray[i]
                print(vectorZ)
                return generateCandidateVector(vectorZ)
            
            elif(calculateFunctionValue(vectorZ) > calculateFunctionValue(newPopulationArray[i])):
                vectorZ = populationOneArray[i]
                
               
    
def main():
   
    K = 50
    F = 0.8
    N=50
    x0x1Array(N, F, K)
   
    
main()   

錯誤跟蹤如下:

   runfile('C:/Users/SPNMo/Documents/untitled5.py', wdir='C:/Users/SPNMo/Documents')
[0]
C:\Users\SPNMo\Documents\untitled5.py:17: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  return (x[0]-1)**2+5(x[1]-x[0]**2)**2
Traceback (most recent call last):

  File "C:\Users\SPNMo\Documents\untitled5.py", line 74, in <module>
    main()

  File "C:\Users\SPNMo\Documents\untitled5.py", line 71, in main
    x0x1Array(N, F, K)

  File "C:\Users\SPNMo\Documents\untitled5.py", line 35, in x0x1Array
    generateCandidateVector(x0x1, N, F, K)

  File "C:\Users\SPNMo\Documents\untitled5.py", line 55, in generateCandidateVector
    if(calculateFunctionValue(vectorZ) < calculateFunctionValue(newPopulationArray[i])):

  File "C:\Users\SPNMo\Documents\untitled5.py", line 17, in calculateFunctionValue
    return (x[0]-1)**2+5(x[1]-x[0]**2)**2

IndexError: index 1 is out of bounds for axis 0 with size 1

x0x1Array 函數中的這兩行不會生成相同形狀的數組。

 x0x1 = np.zeros((N-1, 1), dtype=float) // shape is (49, 1)
 x0x1 = np.array((x0,x1)).T // shape is (1, 49, 2)

您應該重新訪問第二行以正確的方式創建該數組。

更新:從列表中獲取 3 對:

x0_ind, x1_ind, x2_ind = np.random.choice(lennewPopulationArray), 3)
x0 = newPopulationArray[x0_ind]
x1 = newPopulationArray[x1_ind]
x2 = newPopulationArray[x2_ind]

另外,在calculateFunctionValue 函數中,在5 之后添加乘號,所以python 知道要乘

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM