liveBook · Manning

Binary-coded genetic algorithm

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A binary-coded genetic algorithm is a genetic algorithm whose candidate solutions use binary encoding.

A binary-coded genetic algorithm (BGA) is a population-based search method in which each candidate solution is a string of 0s and 1s. The bits encode decision variables; an objective function scores each decoded candidate. Selection, crossover, and mutation then produce candidates for another generation. Ticket pricing is a simple application: search for a price that maximizes profit while respecting a permitted price range. section 7.0 section 7.3.2 section 7.4

Five possible ticket prices

Suppose the permitted price is $75–$235 and profit is −20x² + 6200x − 350000, where x is the ticket price. With whole-dollar prices, an eight-bit chromosome can hold each candidate: 77 becomes 01001101, while 145 becomes 10010001. Score the decoded prices by profit rather than by their bit patterns. Precision matters: representing steps of $0.10 across this range calls for more bits than the eight used for whole-dollar prices. section 7.3.2 section 7.4

Table 7.2 Initial population (view table figure)

Candidate solutions x

Values of x in the solution space

Candidate solutions in the binary coding space

Objective function f(x)

x1

77

01001101

8,820

x2

203

11001011

84,420

x3

110

01101110

90,000

x4

145

10010001

128,500

x5

230

11100110

18,000

The initial population pairs five prices with their eight-bit chromosomes and profit scores, making the encoding-to-fitness step concrete. section 7.3.2

A small Python implementation follows this loop:

  • Generate a population of prices within the permitted range, convert each to binary, and pad chromosomes to a common length. section 7.4
  • Decode each chromosome, calculate profit, and select parents. The implementation shows random selection and a fitness-weighted roulette-wheel alternative. section 7.4
  • With a chosen probability, cross two parents at a point; mutation flips selected bits. Repeat for a set number of generations, recording the best score. section 7.4

In the worked Python run, a population of five evolves for 1,000 generations with crossover probability 0.7 and mutation probability 0.3; the reported best price is $155, yielding $130,500 profit. Treat those settings as an experiment, not a universal recipe. Binary encoding also has a locality drawback: changing a significant bit can cause a large change in the decoded value, a Hamming cliff. Gray-code or real-valued encoding offers alternatives when that disruption matters. section 7.4 section 8.1 section 8.9

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Works through encoding, fitness evaluation, selection, crossover, mutation, and a Python generation loop; does not establish general parameter settings.
Examples
Uses encoded ticket prices, a five-candidate initial population, and a Python run reporting $155 as the best price.
What it assumes
Introduces binary encoding and bit-count calculation while using Python functions and lists for implementation.
What you can do after
Build it from scratch in Python to search binary-encoded ticket prices and inspect the best profit found.
How it connects
Connects genetic algorithms to fitness functions, chromosomes, crossover, mutation, Hamming cliffs, Gray code, and real-valued encoding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.