{"record":{"id":"7c238a5aabba5532","repo":"TheAlgorithms/Go","slug":"populationnum-must-be-bigger-than-selectionnum","errorCode":null,"errorMessage":"populationNum must be bigger than selectionNum","messagePattern":"populationNum must be bigger than selectionNum","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"strings/genetic/genetic.go","lineNumber":84,"sourceCode":"// with Conf instance. Empty instance of Conf (&Conf{}) can be provided,\n// then default values would be set.\n//\n// Link to the same algorithm implemented in python:\n// https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py\nfunc GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {\n\tpopulationNum := conf.PopulationNum\n\tif populationNum == 0 {\n\t\tpopulationNum = 200\n\t}\n\n\tselectionNum := conf.SelectionNum\n\tif selectionNum == 0 {\n\t\tselectionNum = 50\n\t}\n\n\t// Verify if 'populationNum' s bigger than 'selectionNum'\n\tif populationNum < selectionNum {\n\t\treturn nil, errors.New(\"populationNum must be bigger than selectionNum\")\n\t}\n\n\tmutationProb := conf.MutationProb\n\tif mutationProb == .0 {\n\t\tmutationProb = .4\n\t}\n\n\tdebug := conf.Debug\n\n\t// Just a seed to improve randomness required by the algorithm\n\trnd := rand.New(rand.NewSource(time.Now().UnixNano()))\n\n\t// Verify that the target contains no genes besides the ones inside genes variable.\n\tfor position, r := range target {\n\t\tinvalid := true\n\t\tfor _, n := range charmap {\n\t\t\tif n == r {\n\t\t\t\tinvalid = false","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/strings/genetic/genetic.go#L66-L102","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Set Conf so that PopulationNum > SelectionNum before calling (e.g., PopulationNum: 200, SelectionNum: 50)","If setting only one field, check it against the other's effective default (PopulationNum default 200, SelectionNum default 50) and adjust","Swap or fix swapped config values if the fields were accidentally reversed"],"exampleFix":"// before\nconf := &genetic.Conf{PopulationNum: 30} // selectionNum defaults to 50 -> error\n// after\nconf := &genetic.Conf{PopulationNum: 200, SelectionNum: 50} // population > selection","handlingStrategy":"validation","validationCode":"func validConf(c *genetic.Conf) bool {\n    p, s := c.PopulationNum, c.SelectionNum\n    if p == 0 { p = 200 }\n    if s == 0 { s = 50 }\n    return p > s\n}","typeGuard":"func hasValidPopulationConfig(conf *genetic.Conf) bool {\n    p := conf.PopulationNum; if p == 0 { p = 200 }\n    s := conf.SelectionNum; if s == 0 { s = 50 }\n    return p > s\n}","tryCatchPattern":"res, err := genetic.GeneticString(target, charmap, conf)\nif err != nil {\n    if strings.Contains(err.Error(), \"populationNum must be bigger\") {\n        conf.PopulationNum = 200\n        res, err = genetic.GeneticString(target, charmap, conf)\n    }\n    if err != nil { return nil, err }\n}","preventionTips":["Always set both PopulationNum and SelectionNum explicitly with population > selection","Remember defaults: PopulationNum 200, SelectionNum 50 — check explicit values against them","Validate Conf in your config-loading code before invoking the algorithm","Watch for swapped fields when constructing Conf from env/config files"],"tags":["go","genetic-algorithm","configuration","invalid-input"],"backgroundTag":"invalid-config-value","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}