I was following a solution that I found on a book called Deap by Example in which the author solved the TSP by using GA with the Deap library, so far my code is the following:
import sys
import array
import random
import numpy as np
import matplotlib.pyplot as plt
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
def fitnessTSP(ind,x,y):
diffx=np.diff(x[ind])
diffy=np.diff(y[ind])
dist=np.sum(diffx**2-diffy**2)
return dist
def main():
numCities=10
random.seed(169)
x=np.random.rand(numCities)
y=np.random.rand(numCities)
print (x)
print (y)
plt.plot(x,y)
plt.show
creator.create("fitness_func",base.Fitness,weights=(-1,0))
creator.create("Individual",array.array,typecode='i',fitness=creator.fitness_func)
toolbox=base.Toolbox()
toolbox.register("indices",random.sample,range(numCities),numCities)
toolbox.register("individual",tools.initIterate,creator.Individual,toolbox.indices)
toolbox.register("population",tools.initRepeat,list,toolbox.individual)
toolbox.register("mate",tools.cxOrdered)
toolbox.register("mutate",tools.mutShuffleIndexes,indpb=0.05)
toolbox.register("select",tools.selTournament,tournsize=3)
toolbox.register("evaluate",fitnessTSP)
pop=toolbox.population(n=300)
best=tools.HallOfFame(1)
algorithms.eaSimple(pop,toolbox,0.7,0.2,140,halloffame=best)
if __name__=="__main__":
main()
When I run it the error that I get is the following:
fitnessTSP() missing 2 required positional arguments: 'x' and 'y'
I have tried to pass those arguments, but I do not know what to do with the parameter ind that the function should get. For example I have tried this:
toolbox.register("evaluate",fitnessTSP("individual",x,y))
How can I fix this code?
Thanks
The problem comes from the way you register the evaluate
function. To pass the arguments 'x'
and 'y'
, you can do
toolbox.register("evaluate", fitnessTSP, x=x, y=y)
You don't mention it in your question, but the fitness function has to return a tuple. To do that, you can make the last line of your fitness function be
return dist,
Otherwise, you will get another error once you fix the first problem.
This will make your full code be
import sys
import array
import random
import numpy as np
import matplotlib.pyplot as plt
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
def fitnessTSP(ind,x,y):
diffx=np.diff(x[ind])
diffy=np.diff(y[ind])
dist=np.sum(diffx**2-diffy**2)
return dist,
def main():
numCities=10
random.seed(169)
x=np.random.rand(numCities)
y=np.random.rand(numCities)
print (x)
print (y)
plt.plot(x,y)
plt.show
creator.create("fitness_func",base.Fitness,weights=(-1,0))
creator.create("Individual",array.array,typecode='i',fitness=creator.fitness_func)
toolbox=base.Toolbox()
toolbox.register("indices",random.sample,range(numCities),numCities)
toolbox.register("individual",tools.initIterate,creator.Individual,toolbox.indices)
toolbox.register("population",tools.initRepeat,list,toolbox.individual)
toolbox.register("mate",tools.cxOrdered)
toolbox.register("mutate",tools.mutShuffleIndexes,indpb=0.05)
toolbox.register("select",tools.selTournament,tournsize=3)
toolbox.register("evaluate",fitnessTSP, x=x, y=y)
pop=toolbox.population(n=300)
best=tools.HallOfFame(1)
algorithms.eaSimple(pop,toolbox,0.7,0.2,140,halloffame=best)
if __name__=="__main__":
main()
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.