遗传算法染色体编码疑问:是否需用编码算法?冗余二进制值如何处理?
Great questions about genetic algorithm encoding—let's break them down one by one!
Short answer: You absolutely can create your own encoding—there’s no hard requirement to use off-the-shelf encoding schemes.
Pre-defined encodings (like binary, real-number, or permutation encoding) are popular because they’re well-tested and work for many common problems. But genetic algorithms are inherently flexible: the only hard rules for your encoding are that it needs to accurately represent solutions in your problem’s search space and play nicely with the core GA operations (selection, crossover, mutation).
For example, if you’re solving a traveling salesman problem, a custom permutation encoding (where each chromosome is a sequence of city indices) is way more intuitive and efficient than forcing a binary encoding. As long as you can define how to cross two chromosomes and mutate them without breaking the validity of the solution, custom encoding is not just allowed—it’s often the best approach for tailoring the GA to your specific problem.
11 when encoding 3 teachers ({harry, sam, bran}) with 2-bit binary codes (harry=00, sam=01, bran=10)? This is a super common edge case when binary encoding doesn’t map perfectly to your problem’s discrete options. Here are a few practical, widely-used ways to handle the unused 11 value:
- Re-generate valid values: Whenever
11pops up (during initialization, crossover, or mutation), discard it and randomly pick one of the valid codes (00,01,10). This keeps your entire chromosome pool valid at all times, with minimal extra logic. - Map to an existing gene: Assign
11to one of your teachers—either statically (e.g., permanently map11tobran/10) or randomly each time it appears. This avoids discarding values entirely and keeps the GA process running smoothly. - Penalize invalid solutions: If you don’t want to modify the encoding directly, add a heavy penalty to the fitness score of any individual that has
11in its chromosome. This way, these invalid individuals are far less likely to be selected for reproduction, and they’ll eventually be filtered out of the population naturally. - Adjust the encoding (if feasible): While 2 bits is the smallest binary length that can fit 3 distinct values, if you’re open to it, you could use a ternary (base-3) encoding instead of binary. This eliminates unused values entirely, though it might complicate your crossover and mutation logic compared to standard binary operations.
内容的提问来源于stack exchange,提问作者dips

