{"id":15137008,"url":"https://github.com/rafagaitan/crsga","last_synced_at":"2025-10-28T23:37:35.595Z","repository":{"id":94078924,"uuid":"367821203","full_name":"rafagaitan/crsga","owner":"rafagaitan","description":"C++ Genetic Algorithm library","archived":false,"fork":false,"pushed_at":"2021-06-07T06:45:40.000Z","size":492,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T18:38:46.850Z","etag":null,"topics":["conan","genetic-algorithm","onemax"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafagaitan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-16T08:13:20.000Z","updated_at":"2022-04-24T17:00:09.000Z","dependencies_parsed_at":"2023-04-19T19:16:31.981Z","dependency_job_id":null,"html_url":"https://github.com/rafagaitan/crsga","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafagaitan%2Fcrsga","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafagaitan%2Fcrsga/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafagaitan%2Fcrsga/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafagaitan%2Fcrsga/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafagaitan","download_url":"https://codeload.github.com/rafagaitan/crsga/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237821720,"owners_count":19371815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conan","genetic-algorithm","onemax"],"created_at":"2024-09-26T06:42:44.870Z","updated_at":"2025-10-23T11:32:04.893Z","avatar_url":"https://github.com/rafagaitan.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# crsGA: C++ Genetic Algorithm library\n\ncrsGA is a C++ template library for developing genetic algorithms, plus some other utilities (Logger and Threading).\n\n---\n\n**NOTE**\n\nThe library was designed to be easy to use, but knowledge of Genetic Algorithm concepts is required. It might not be well suited for high performance and it will always depend on how you define your data structures.\n\n---\n\nThe library allows to implement each of the concepts of a Genetic Algorithm, for instance the following example shows how to implement the **OneMax** genetic algorithm.\n\nFirst we define what is a `Gen`:\n\n```cpp\n/**\n * @brief Represents the smallest piece of information.\n * \n * A bit of the bitstring of the onemax algorithm\n * \n */\nclass Gen : public crsGA::IGen\n{\n  public:\n    // Gen data (1 bit)\n    uint8_t bit = 0;\n\n  public:\n    Gen() = default;\n    virtual ~Gen() = default;\n    // How to mutate this gen \n    virtual void mutate(const crsGA::UserData *) override\n    {\n        bit = 1 - bit;\n    }\n    // Random creation, used for initialization\n    virtual void random(const crsGA::UserData *) override\n    {\n        if(computeProbability() \u003e= 50.0)\n            bit = 1 - bit;\n    }\n    // Just some friendly way of serializing the information\n    friend std::ostream \u0026operator\u003c\u003c(std::ostream \u0026os, const Gen \u0026gen)\n    {\n        os \u003c\u003c static_cast\u003cint\u003e(gen.bit);\n        return os;\n    }\n};\n```\n\nThen we need to define a `Chromosome`, which is composed of a set of `Gens`. In order to know which `Chromosome` is the best one (the fittest one), we also need to define the `ComputeFitnessPolicy`:\n\n```cpp\n/**\n * @brief Defines how to compute the fitness of a chromosome\n * \n * We return -sum(bitstring), since the algorithm tries to \n * minimize the fitness value\n */\nclass ComputeFitness : public crsGA::ComputeFitnessPolicy\u003cGen\u003e\n{\n  public:\n    virtual float computeFitness(const std::vector\u003cGen\u003e \u0026genes,\n                                 const crsGA::UserData *) const override\n    {\n        float fitness = 0.0f;\n        for (const auto \u0026g : genes)\n        {\n            fitness += g.bit;\n        }\n        return -fitness;\n    }\n};\n```\n\nAnd the `Chromosome` definition:\n\n```cpp\n// Chromosome definition, internally a std::vector\u003cGen\u003e\nusing Chromosome = crsGA::Chromosome\u003cGen, ComputeFitness\u003e;\n```\n\nThe next step is to define the `Population` and how it will be initialized:\n\n```cpp\n/**\n * @brief Defines the initialization policy for the Population\n * \n * @tparam ChromosomeT Type of Chromosome\n */\ntemplate \u003ctypename ChromosomeT\u003e\nclass PopulationInitializationPolicy\n{\n  public:\n    void initialize(std::vector\u003cChromosomeT\u003e \u0026chromosomes, const crsGA::UserData *data) const\n    {\n        for (auto \u0026c : chromosomes)\n        {\n            for(auto \u0026g: c.getGenes())\n            {\n                g.random(data);\n            }\n        }\n    }\n};\n\n// Population definition\nusing Population = crsGA::Population\u003cChromosome, PopulationInitializationPolicy\u003cChromosome\u003e\u003e;\n```\n\nFinally we need to define the `GeneticAlgorithm` that will put all together:\n\n```cpp\nusing OneMaxGA = crsGA::GeneticAlgorithm\u003cGen, Chromosome, Population\u003e;\n```\n\nFor running the genetic algorithm needs a few more steps:\n\n```cpp\nint main(int, char **)\n{\n    // Number of bits of the onemax bitstring\n    auto numGenes = 20u;\n    auto populationSize = 100u;\n    // the fittest one, will be all genes to 1 (111111 ....11)\n    auto fitnessGoal = -static_cast\u003cfloat\u003e(numGenes);\n    // mutation rate\n    float mutationFactor = 0.5f;\n    OneMaxGA ga(populationSize, numGenes, fitnessGoal);\n    ga.setMutationFactor(mutationFactor);\n    ga.reset();\n    // run up to 10 seconds\n    ga.run(10.0);\n    return 0;\n}\n```\n\nThis was an example using most of the default functionality. The API allows also to define the `CrossoverPolicy`, the `SelectionPolicy` and the `ReplacementPolicy`, although a default implementation has been provided. See `GeneticAlgorithm.hpp` and `Common.hpp` headers for more details.\n\n## Applications\n\nThe code also provides a couple of more advanced applications, which allows to visualize the genetic algorithm progress and results in 3D using [OpenSceneGraph](https://github.com/openscenegraph/OpenSceneGraph).\n\nThe applications try to automatic detect the power lines using Genetic and Simulated Annealing algorithms on point cloud data:\n\n![Power lines with ground](images/power_lines_and_ground.png)\n\n## Build\n\nFor building, it uses [**CMake**](https://cmake.org/) and optionally [**Conan**](https://conan.io/) if you want to build the applications.\n\n```bash\nmkdir build\ncd build\ncmake .. -DCMAKE_BUILD_TYPE=Release\nmake -j 8\n # Optional, you may want to change the install with\n # -DCMAKE_INSTALL_PREFIX in cmake command\nmake install\n```\n\nFor running the **OneMax** example:\n\n```bash\ncd build/bin\n./onemax\n```\n\nFor building the applications and download thirtparty dependencies with **Conan**, make sure you have installed [**Conan**](https://conan.io/) on your system.\n\n---\n**Note**\n\nA `Pipfile` for **pipenv** file is provided with the source code, so you can install **Conan** using it if you are familiar with it.\n\n---\n\n```bash\nmkdir build\ncd build\ncmake .. -DCMAKE_BUILD_TYPE=Release -DCRSGA_BUILD_APPLICATIONS=ON\nmake -j 8\n```\n\nFor running the GA application:\n\n```bash\ncd build/bin\n./ga --txt ../../data/electric_lines_and_ground.xyz --numGenes 6 --mutationFactor 20 --show\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafagaitan%2Fcrsga","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafagaitan%2Fcrsga","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafagaitan%2Fcrsga/lists"}