Skip to content

Fixing stop_fitness for QueensOpt with custom fitness fn#33

Open
eko-wibowo wants to merge 1 commit intoknakamura13:mainfrom
eko-wibowo:custom_stop_fitness
Open

Fixing stop_fitness for QueensOpt with custom fitness fn#33
eko-wibowo wants to merge 1 commit intoknakamura13:mainfrom
eko-wibowo:custom_stop_fitness

Conversation

@eko-wibowo
Copy link

@eko-wibowo eko-wibowo commented Feb 17, 2025

Fixing the issue described here by allowing custom stop_fitness instead of using the default for custom fitness function

#32

Repro problem

def get_queens_problem(N) -> mlrose.QueensOpt:
    def queens_max(state):
        # Initialize counter
        fitness = 0

        # For all pairs of queens
        for i in range(len(state) - 1):
            for j in range(i + 1, len(state)):

                # Check for horizontal, diagonal-up and diagonal-down attacks
                if (state[j] != state[i]) and (state[j] != state[i] + (j - i)) and (state[j] != state[i] - (j - i)):
                    # If no attacks, then increment counter
                    fitness += 1

        return fitness

    fitness_fn = mlrose.CustomFitness(queens_max, problem_type="discrete")
    return mlrose.QueensOpt(length=N, maximize=True, fitness_fn=fitness_fn, stop_fitness=N * (N - 1) / 2)

calls RHCRunner with
iteration_list=np.arange(0, 1001, 100),
restart_list=[10, 25, 100],
max_attempts=500

The plots below show restart as a drop in the fitness function, 3 values in the restarts list are overlapping

  • Without stop iter param (never hit the convergence point, took ~20mins). I think some good "path" might have been stopped due to it's hitting fitness 24 (3 * (10-2))
    fitness_without_stop_iter

  • With stop iteration param Hit convergence point (took 2 mins)
    with_stop_iteration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant