algorithmtraveling-salesmangenetic

Mlrose TSPOpt Genetic Algorithm own cost function


I want to program my own cost function for my own TSP problem. I do not want to use the mlrose one, because I want to optimize real coords with time.

First of all, I created a coords_List which looks like this: [(49.321,8.213),[50.321,9.124]...)

Then I created my own fitness function which accepts the guess array and returns a float.

fitnessF =  mlrose.CustomFitness(coords_list)

Now I set up mlRose:

problem_fit = mlrose.TSPOpt(length = len(coords_list),fitness_fn =fitnessF, maximize=False)
best_state, best_fitness = mlrose.genetic_alg(problem_fit, random_state = 2)

This returns:

Exception: fitness_fn must have problem type 'tsp'.

Now I set up the code like this:

fitnessF =  mlrose.TravellingSales(check_fitness)

Now it will return:

object of type 'function' has no len()

Thanks in advance

Ps: I would also be ready to share my notebook over Email


Solution

  • I could figure out where the problem was. It is right that I have to define my custom fitness function, but for TSP I have to add the problem type:

    fitnessF = mlrose.CustomFitness(coords_list,"tsp")

    Now it will work.