{"id":15469134,"url":"https://github.com/epwalsh/csa","last_synced_at":"2025-03-17T21:31:35.082Z","repository":{"id":66216639,"uuid":"133830128","full_name":"epwalsh/CSA","owner":"epwalsh","description":"C++ header-only library for Coupled Simulated Annealing","archived":false,"fork":false,"pushed_at":"2018-05-25T23:04:41.000Z","size":294,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-28T04:58:23.363Z","etag":null,"topics":["coupled-simulated-annealing","cpp","optimization","simulated-annealing"],"latest_commit_sha":null,"homepage":"https://epwalsh.github.io/software/csa","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/epwalsh.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":"2018-05-17T15:04:32.000Z","updated_at":"2024-08-22T12:03:04.000Z","dependencies_parsed_at":"2023-02-24T09:00:44.018Z","dependency_job_id":null,"html_url":"https://github.com/epwalsh/CSA","commit_stats":{"total_commits":27,"total_committers":1,"mean_commits":27.0,"dds":0.0,"last_synced_commit":"3d7154fd35c35fb7297e25bd507c5e3f705b1ad6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epwalsh%2FCSA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epwalsh%2FCSA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epwalsh%2FCSA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epwalsh%2FCSA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/epwalsh","download_url":"https://codeload.github.com/epwalsh/CSA/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243886014,"owners_count":20363649,"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":["coupled-simulated-annealing","cpp","optimization","simulated-annealing"],"created_at":"2024-10-02T01:51:51.482Z","updated_at":"2025-03-17T21:31:35.069Z","avatar_url":"https://github.com/epwalsh.png","language":"C++","readme":"# CSA\n\n**CSA** is a C++ header-only library for\n[Coupled Simulated Annealing](ftp://ftp.esat.kuleuven.be/sista/sdesouza/papers/CSA2009accepted.pdf).\nThe code is derived and modified from the original implementation by the authors of the paper.\n\n## Features\n\n- **Global optimization of arbitrary functions:** CSA represents a class of global optimization\nalgorithms that do not make use of the gradient.\n- **Highly parallel:** The source code is implemented with OpenMP.\n- **Callback interface:** The optimization method recieves the current cost\nand next step via a callback interface. Progress updates can also be given\nthrough a callback interface.\n\n## Requirements\n\nA compiler with OpenMP support.\n\n## Quick example\n\nIn this example we will try to minimize the\n[Schwefel function](https://www.sfu.ca/~ssurjano/schwef.html).\nWe start by defining the function itself:\n\n```.cpp\n#include \u003ccmath\u003e\n\n#define DIM 10\n\n\n// This defines the Schwefel function. The pointer `instance` can be used to\n// pass data to `f`, such as the dimension.\n// In this case we don't need to pass anything, so it will be given as NULL.\ndouble f(void* instance, double* x)\n{\n    double sum = 0.;\n    for (int i = 0; i \u003c DIM; ++i)\n        sum += 500 * x[i] * std::sin(std::sqrt(std::fabs(500 * x[i])));\n    return 418.9829 * DIM - sum;\n}\n```\n\nThe next thing we need is the random step function:\n\n```.cpp\n#define PI 3.14159265358979323846264338327\n\n\n// This function will take a random step from `x` to `y`. The value `tgen`,\n// the \"generation temperature\", determines the variance of the distribution of\n// the step. `tgen` will decrease according to fixed a schedule throughout the\n// annealing process, which corresponds to a decrease in the variance of steps.\nvoid step(void* instance, double* y, const double* x, float tgen)\n{\n    int i;\n    double tmp;\n    for (i = 0; i \u003c DIM; ++i) {\n        tmp = std::fmod(x[i] + tgen * std::tan(PI * (drand48() - 0.5)), 1.);\n        y[i] = tmp;\n    }\n}\n```\n\nOptionally, we can define a progress function, which will print updates\nto the terminal every time a new minimum cost is found:\n\n```.cpp\n// This will receive progress updates from the CSA process and print updates\n// to the terminal.\nvoid progress(\n    void* instance, double cost, float tgen, float tacc, int opt_id, int iter)\n{\n    printf(\n        \"bestcost=%1.3e \\t tgen=%1.3e \\t tacc=%1.3e \\t thread=%d\\n\",\n        cost,\n        tgen,\n        tacc,\n        opt_id);\n    return ;\n}\n```\n\nThe minimization process is then ran like this:\n\n```.cpp\n#include \u003ciostream\u003e\n\n#include \u003ccsa.hpp\u003e\n\n\nint main()\n{\n    srand(0);\n\n    // Create initial solution from uniform distribution.\n    double* x = new double[DIM];\n    for (int i = 0; i \u003c DIM; ++i)\n        x[i] = drand48();\n    double cost = f(nullptr, x);\n    printf(\"Initial cost: %f\\n\", cost);\n\n    // Initialize CSA solver.\n    CSA::Solver\u003cdouble, double\u003e solver;\n    solver.m = 2; // number of threads = number of solvers = 2\n\n    // Start the annealing process.\n    solver.minimize(DIM, x, f, step, progress, nullptr);\n\n    // The array `x` now holds the best solution.\n    cost = f(nullptr, x);\n    printf(\"Best cost: %f\\nx =\\n\", cost);\n    for (int i = 0; i \u003c DIM; ++i)\n        std::cout \u003c\u003c 500 * x[i] \u003c\u003c \" \";\n    std::cout \u003c\u003c std::endl;\n\n    // Clean up.\n    delete[] x;\n\n    return EXIT_SUCCESS;\n}\n```\n\nThe last few lines of output will look something like this:\n\n```\n...\nbestcost=1.338e-04       tgen=1.161e-05          tac=7.006e-44        thread=1\nbestcost=1.332e-04       tgen=9.740e-06          tac=7.006e-44        thread=1\nbestcost=1.314e-04       tgen=9.240e-06          tac=7.006e-44        thread=1\nbestcost=1.285e-04       tgen=5.911e-06          tac=7.006e-44        thread=1\nbestcost=1.283e-04       tgen=3.647e-06          tac=7.006e-44        thread=1\nbestcost=1.280e-04       tgen=3.523e-06          tac=7.006e-44        thread=1\nbestcost=1.279e-04       tgen=3.337e-06          tac=7.006e-44        thread=1\nbestcost=1.277e-04       tgen=2.924e-06          tac=7.006e-44        thread=1\nbestcost=1.276e-04       tgen=2.187e-06          tac=7.006e-44        thread=1\nbestcost=1.275e-04       tgen=2.129e-06          tac=7.006e-44        thread=1\nbestcost=1.275e-04       tgen=1.880e-06          tac=7.006e-44        thread=1\nbestcost=1.275e-04       tgen=1.507e-06          tac=7.006e-44        thread=1\nbestcost=1.275e-04       tgen=1.470e-06          tac=7.006e-44        thread=1\nBest cost: 0.000128\nx =\n420.969 420.969 420.968 420.969 420.968 420.969 420.968 420.969 420.969 420.969\n```\n\nThis is pretty good, since the true minimum is `0 = f(420.9687, ..., 420.9687)`.\nCheck out [example.cpp](https://github.com/epwalsh/CSA/blob/master/example.cpp)\nfor the full source code to this example.\n\n## Documentation\n\nYou can find the full API reference on the project website:\n[epwalsh.github.io/CSA/api/](https://epwalsh.github.io/CSA/api/).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepwalsh%2Fcsa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fepwalsh%2Fcsa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepwalsh%2Fcsa/lists"}