So I am having some trouble with my code.
Essentially, I am creating an initial array (x0x1) of random coordinates (this is successful). Then, I want to generate a candidate coordinate vector (z) from 3 random choice coordinate vectors from my original coordinate array. I then compare f(z) with every pair of coordinates f(x0x1[i]) in my original array. If f(z) is lower, then I take it to a new array of coordinates. The cycle repeats until I find the value of z that minimizes my function.
The error I get is : index 1 is out of bounds for axis 0 with size 1, and it seems to be happening in my calculateFunction method. Not sure why.
Here is the code I am working with:
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()
The error trace is the following:
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
These 2 lines in x0x1Array function do not generate arrays of the same shape.
x0x1 = np.zeros((N-1, 1), dtype=float) // shape is (49, 1)
x0x1 = np.array((x0,x1)).T // shape is (1, 49, 2)
You should revisit the second line to create that array in a proper way.
UPDATE: getting 3 pairs from the list:
x0_ind, x1_ind, x2_ind = np.random.choice(lennewPopulationArray), 3)
x0 = newPopulationArray[x0_ind]
x1 = newPopulationArray[x1_ind]
x2 = newPopulationArray[x2_ind]
Also, in the calculateFunctionValue function add multiplication sign after the 5 so python knows to multiply
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.