TheAlgorithms/Go · error

populationNum must be bigger than selectionNum

Error message

populationNum must be bigger than selectionNum

What it means

GeneticString returns errors.New("populationNum must be bigger than selectionNum") (strings/genetic/genetic.go:83-85) when the Conf passed to GeneticString specifies a SelectionNum greater than or equal to the effective PopulationNum. The genetic algorithm needs a breeding pool (selection) strictly smaller than the population; note the defaults (population 200, selection 50) apply only when the fields are zero, so explicitly-set conflicting values trigger the error.

Source

Thrown at strings/genetic/genetic.go:84

// with Conf instance. Empty instance of Conf (&Conf{}) can be provided,
// then default values would be set.
//
// Link to the same algorithm implemented in python:
// https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py
func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
	populationNum := conf.PopulationNum
	if populationNum == 0 {
		populationNum = 200
	}

	selectionNum := conf.SelectionNum
	if selectionNum == 0 {
		selectionNum = 50
	}

	// Verify if 'populationNum' s bigger than 'selectionNum'
	if populationNum < selectionNum {
		return nil, errors.New("populationNum must be bigger than selectionNum")
	}

	mutationProb := conf.MutationProb
	if mutationProb == .0 {
		mutationProb = .4
	}

	debug := conf.Debug

	// Just a seed to improve randomness required by the algorithm
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

	// Verify that the target contains no genes besides the ones inside genes variable.
	for position, r := range target {
		invalid := true
		for _, n := range charmap {
			if n == r {
				invalid = false

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Set Conf so that PopulationNum > SelectionNum before calling (e.g., PopulationNum: 200, SelectionNum: 50)
  2. If setting only one field, check it against the other's effective default (PopulationNum default 200, SelectionNum default 50) and adjust
  3. Swap or fix swapped config values if the fields were accidentally reversed

Example fix

// before
conf := &genetic.Conf{PopulationNum: 30} // selectionNum defaults to 50 -> error
// after
conf := &genetic.Conf{PopulationNum: 200, SelectionNum: 50} // population > selection
Defensive patterns

Strategy: validation

Validate before calling

func validConf(c *genetic.Conf) bool {
    p, s := c.PopulationNum, c.SelectionNum
    if p == 0 { p = 200 }
    if s == 0 { s = 50 }
    return p > s
}

Type guard

func hasValidPopulationConfig(conf *genetic.Conf) bool {
    p := conf.PopulationNum; if p == 0 { p = 200 }
    s := conf.SelectionNum; if s == 0 { s = 50 }
    return p > s
}

Try / catch

res, err := genetic.GeneticString(target, charmap, conf)
if err != nil {
    if strings.Contains(err.Error(), "populationNum must be bigger") {
        conf.PopulationNum = 200
        res, err = genetic.GeneticString(target, charmap, conf)
    }
    if err != nil { return nil, err }
}

Prevention

When it happens

Trigger: Calling genetic.GeneticString(target, charmap, &genetic.Conf{PopulationNum: P, SelectionNum: S}) with S > P (e.g., PopulationNum: 10, SelectionNum: 50); setting one field explicitly while the other keeps a default larger than it, e.g. PopulationNum: 30 with SelectionNum unset (defaults to 50).

Common situations: Config values loaded from env/config file where PopulationNum is small or misread; typos swapping the two fields; assuming defaults fill in per-field when only one is set (0 is replaced by the default, which may exceed the explicit value).

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/7c238a5aabba5532. Report an issue: GitHub.