pymoo is a Python framework for optimization with evolutionary algorithms and other metaheuristics. Despite its multi-objective focus, it can also run a single-objective genetic algorithm (GA). Its available algorithms include GA, differential evolution, evolutionary strategies, NSGA-II, NSGA-III, and particle swarm optimization. section 7.4
A population-based search needs starting candidates. For a two-variable problem bounded between 0 and 1, pymoo can generate 200 candidates with `FloatRandomSampling()` or Latin hypercube sampling through `LHS()`. Calling the sampler with the problem and population size, then retrieving `get("X")`, yields the candidate values. Plotting them helps reveal gaps and dispersion: initialization affects how broadly the search begins. section 7.1
Optimize a ticket price with GA
Define a `Problem` with one variable, bounds of 75–235, and an `_evaluate` method. Because the solver minimizes, set its objective to `20*x*x - 6200*x + 350000`, the negative of the profit function. Configure `GA` with a population of 100, random sampling, point crossover, polynomial mutation with rounding repair, and duplicate elimination. Run `minimize` for 100 generations. The reported price is $155 and its profit is $130,500. This is a single-objective example, not a demonstration of multi-objective optimization. section 7.4
problem = TicketPrice() #1
algorithm = GA(
pop_size=100,
sampling=FloatRandomSampling(),
crossover=PointCrossover(prob=0.8, n_points=2),
mutation = PolynomialMutation(prob=0.3, repair=RoundingRepair()),
eliminate_duplicates=True
) #2
res = minimize(problem, algorithm, ('n_gen', 100), seed=1, verbose=True) #3
print(f"Optimal ticket price ($): {res.X}") #4
print(f"Profit ($): {-res.F}") #5
The configuration listing shows where sampling, crossover, mutation, duplicate elimination, generation count, and seed enter the pymoo run. section 7.4
Use a ready-made GA when the goal is to adapt operators and settings rather than implement selection, crossover, and mutation from scratch. Check that the objective’s sign matches minimization, choose sampling suited to the solution representation, and inspect the returned solution and objective value. section 7.1 section 7.4
