knakamura13 / mlrose-ky

A highly optimized fork of the popular mlrose-hiive package. For Machine Learning, Randomized Optimization and SEarch algorithms.
https://nkapila6.github.io/mlrose-ky/
BSD 3-Clause "New" or "Revised" License
20 stars 8 forks source link

Confusing Behavior of `maximize` Parameter in Optimization Problems #18

Open knakamura13 opened 1 month ago

knakamura13 commented 1 month ago

Issue Description The maximize parameter in the mlrose library is not clearly documented, leading to unexpected behavior in certain problem types. Specifically, setting maximize=True does not properly convert some problems to maximization problems and may inadvertently make them easier to solve.

Example: N-Queens Problem In the case of the N-Queens problem, simply setting the parameter maximize=True can drastically reduce the complexity of the problem. This happens because when the objective is to maximize the number of collisions on the chessboard, there are many trivial solutions (e.g., all queens on the first rank, all queens on the second rank, all queens on the same diagonal, etc.).

To properly convert the N-Queens problem into a maximization problem, additional steps are required. Below is an example of how to manually handle this:

# Improper conversion of N-Queens to maximization problem (DO NOT DO THIS)
# problem = mlrose.QueensOpt(length=size, maximize=True)  (DO NOT DO THIS)

# Proper conversion of N-Queens to maximization problem
queens_fit = lambda state: mlrose.Queens().evaluate(state) * -1
problem = mlrose.DiscreteOpt(length=size, max_val=size, maximize=True, fitness_fn=mlrose.CustomFitness(queens_fit))

Using this approach, the objective is maximized correctly without introducing trivial solutions.

Proposed Documentation Improvements