{"id":15906034,"url":"https://github.com/lucacappelletti94/greedy_grasp_ant","last_synced_at":"2025-04-02T22:43:34.242Z","repository":{"id":98554940,"uuid":"190429177","full_name":"LucaCappelletti94/greedy_grasp_ant","owner":"LucaCappelletti94","description":"Small C project to highlight the similarities of Greedy, GRASP and Ant system algorithms.","archived":false,"fork":false,"pushed_at":"2019-07-19T16:39:26.000Z","size":41029,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T13:14:06.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LucaCappelletti94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-06-05T16:21:47.000Z","updated_at":"2021-03-14T17:17:06.000Z","dependencies_parsed_at":"2023-03-14T05:00:38.838Z","dependency_job_id":null,"html_url":"https://github.com/LucaCappelletti94/greedy_grasp_ant","commit_stats":{"total_commits":72,"total_committers":1,"mean_commits":72.0,"dds":0.0,"last_synced_commit":"12f5ac99b4d0e9599a2ecd138f8f6a3551fe2473"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fgreedy_grasp_ant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fgreedy_grasp_ant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fgreedy_grasp_ant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fgreedy_grasp_ant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LucaCappelletti94","download_url":"https://codeload.github.com/LucaCappelletti94/greedy_grasp_ant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246905830,"owners_count":20852818,"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":[],"created_at":"2024-10-06T13:20:41.610Z","updated_at":"2025-04-02T22:43:34.213Z","avatar_url":"https://github.com/LucaCappelletti94.png","language":"C","readme":"# greedy_grasp_ant\n[![Build Status](https://travis-ci.org/LucaCappelletti94/greedy_grasp_ant.svg?branch=master)](https://travis-ci.org/LucaCappelletti94/greedy_grasp_ant) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/3cdaec8507ce46cd961b2885d2ef9d06)](https://www.codacy.com/app/LucaCappelletti94/greedy_grasp_ant?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=LucaCappelletti94/greedy_grasp_ant\u0026amp;utm_campaign=Badge_Grade)\n\nSmall C project to highlight the similarities of Greedy, GRASP and Ant system algorithms.\n\n![Normalized min max scores](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/scores/scores.png?raw=true)\nTo visualize the scores of the various datasets, scores have been min-max normalized for each dataset column.\n\n## Building and running the program\nTo build the program just run the following:\n\n```bash\nmkdir my_test_dir\ncd my_test_dir\ncmake ..\nmake\n```\nThen to run the software just build just execute the following, where `{my_dataset}` is a valid dataset under `/data`, such as `01matrizn100m10.dat`:\n```bash\n./greedy_grasp_ant --data={my_dataset} --all --verbose\n```\n\nThe output will be something of the following fashion:\n\n```plain\nData file: ../data/01matrizn100m10.dat\nScore average over 10 executions.\ngreedy: 306\ngreedy_bestsum: 322\ngreedy_bestpair: 306\ngreedy_tryall: 333\nuniform_grasp: \n\tmean: 211\n\tstd: 14\n\tvar: 220\n\tmin: 191\n\tmax: 237\nlinear_HBSS_grasp: \n\tmean: 245\n\tstd: 11\n\tvar: 141\n\tmin: 217\n\tmax: 260\nexponential_HBSS_grasp: \n\tmean: 312\n\tstd: 4\n\tvar: 16\n\tmin: 306\n\tmax: 319\nuniform_RCL_grasp: \n\tmean: 287\n\tstd: 5\n\tvar: 28\n\tmin: 278\n\tmax: 295\nlinear_RCL_grasp: \n\tmean: 289\n\tstd: 8\n\tvar: 72\n\tmin: 277\n\tmax: 308\nexponential_RCL_grasp: \n\tmean: 311\n\tstd: 5\n\tvar: 29\n\tmin: 303\n\tmax: 319\nant_system: \n\tmean: 255\n\tstd: 9\n\tvar: 95\n\tmin: 241\n\tmax: 275\nRequired time: 102.841000 ms\n```\n\n## Greedy\nThe [greedy algorithm implementation](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/greedy.c) is a simple loop in which, at every iteration, the best point is selected:\n\n```c\nvoid greedy (data_t *pI, solution_t *px)\n{\n  while (!is_solution_feasible(pI, px))\n    move_point_in(best_additional_point(px,pI),px,pI);\n}\n```\n\n## GRASP\nThe [GRASP algorithm implementation](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/grasp.c) contains multiple implementations for the various distributions:\n\n-   [Uniform](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/53df14aa325d75fd5270075b2880834c8a6c5901/src/distributions.c#L16-L23): the algorithm will be a random search.\n-   [HBSS](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/53df14aa325d75fd5270075b2880834c8a6c5901/src/distributions.c#L35-L43): weight the solutions probabilities on their quality, following either linear or exponential distribution.\n-   [RCL](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/53df14aa325d75fd5270075b2880834c8a6c5901/src/distributions.c#L56-L64): like HBSS, but with a cardinality limit. The solutions above the given cardinality get probability zero.\n\n```c\nvoid grasp(data_t *pI, solution_t *px, int iterations, size_t max, double* (*get_distribution)(int**, size_t, size_t))\n{\n  solution_t best;\n  create_solution(px-\u003ecard_N, \u0026best);\n  for(int i=1; i\u003c=iterations; i++) {\n    while (!is_solution_feasible(pI, px))\n      move_point_in(random_additional_point(px, pI, max, get_distribution), px, pI);\n    if (best.f \u003c px-\u003ef) {\n      copy_solution(px, \u0026best);\n      clean_solution(px);\n    }\n  }\n  copy_solution(\u0026best, px);\n}\n```\n\nThe available distributions can be seen [here](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/distributions.c).\n\n## Ant System\nFinally, the [Ant System Algorithm implementation](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/ant.c) goes a step further than the grasp, introducing a memory, called trace.\n\n```c\nvoid ant_system(data_t *pI, solution_t *px, int iterations, double oblivion, double mu_memory, double mu_data)\n{\n  double* trace = init_trace(pI-\u003en);\n  solution_t best;\n  create_solution(px-\u003ecard_N, \u0026best);\n  for(int i=1; i\u003c=iterations; i++){\n    while (!is_solution_feasible(pI, px))\n      move_point_in(ant_additional_point(px, pI, trace, mu_memory, mu_data), px, pI);\n    if (best.f \u003c px-\u003ef)\n    {\n      copy_solution(px, \u0026best);\n      clean_solution(px);\n      update_trace(\u0026best, oblivion, trace, pI-\u003en);\n    }\n  }\n  copy_solution(\u0026best, px);\n  free(trace);\n}\n```\n\n## Repository structure\nThe repository is structured as follows:\n\n-   [`/data`](https://github.com/LucaCappelletti94/greedy_grasp_ant/tree/master/data): contains the project data, as in the weighted graphs on which the varius algorithms can be tested.\n-   [`/scores`](https://github.com/LucaCappelletti94/greedy_grasp_ant/tree/master/scores): containes the scores obtained by running every algorithm on every available dataset, their summary and heatmap image.\n-   [`/scripts`](https://github.com/LucaCappelletti94/greedy_grasp_ant/tree/master/scripts)\n\t-   [`bayes.py`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/scripts/bayes.py): python script to execute bayesian optimization of the hyper parameters for the ant system algorithm.\n\t-   [`multiproc.py`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/scripts/multiproc.py): python script to run the program on every available dataset in a parallel fashion.\n\t-   [`.gaussian_process`](https://github.com/LucaCappelletti94/greedy_grasp_ant/tree/master/scripts/.gaussian_process): cache folder for the gaussian process, storing the datapoints and their respective scores already explored by the bayesian optimization.\n-   [`/src`](https://github.com/LucaCappelletti94/greedy_grasp_ant/tree/master/src): the project itself, containing the source code. Detailed explanation of the project structure is [available down below](https://github.com/LucaCappelletti94/greedy_grasp_ant#project-structure).\n-   [`/.gitignore`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/.gitignore): git settings file for avoiding to upload unwanted files.\n-   [`/.travis.yml`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/.travis.yml): travis settings file for automated build testing and evaluation.\n-   [`/CMakeLists.txt`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/CMakeLists.txt): cmake settings file for meta-building the project and creating a make file for actually building it. [Instruction for building the program are available here](https://github.com/LucaCappelletti94/greedy_grasp_ant#building-and-running-the-program).\n-   [`/README.md`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/README.md): markdown file containing a short project description.\n\n## Project structure\nThe [project source code](https://github.com/LucaCappelletti94/greedy_grasp_ant) is structured as follows:\n\n-   [`/src/alloc.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/alloc.c): functions for allocating various types of multi-dimensions vectors.\n-   [`/src/data.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/data.c): functions for loading, visualizing and destroying the dataset.\n-   [`/src/utils.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/utils.c): utilities.\n-   [`/src/distances.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/distances.c): functions for alocating the distances vectors, calculating them given a solution and the available points and destroying them.\n-   [`/src/solution.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/solution.c): functions for allocating, updating and destroying solutions.\n-   [`/src/distributions.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/distributions.c): functions for allocating stocastic vectors and calculating distributions given distances scores.\n-   [`/src/greedy.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/greedy.c): implementation of Greedy.\n-   [`/src/grasp.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/grasp.c): implementation of GRASP.\n-   [`/src/ant.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/ant.c): implementation of ant system.\n-   [`/src/main.c`](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/src/main.c): main of the program, which executes every available algorithm on the given data.\n\n\n## Available options\nThere are a number of available options.\n\n### Required options\nThe only required options are the data file path and at least an implementation. The available implementations are listed below, and the data file path can be specified as follows:\n\n```bash\n--data={my_dataset} --{any_available_implementation}\n```\n\nor alternatively:\n\n```bash\n--data {my_dataset} --{any_available_implementation}\n```\n\n### Available implementations\nTo run one or more implementations, just pass the corresponding options. The available options are:\n\n-   `--greedy`\n-   `--greedy_bestsum`\n-   `--greedy_bestpair`\n-   `--greedy_tryall`\n-   `--uniform_grasp`\n-   `--linear_HBSS_grasp`\n-   `--exponential_HBSS_grasp`\n-   `--uniform_RCL_grasp`\n-   `--linear_RCL_grasp`\n-   `--exponential_RCL_grasp`\n-   `--ant_system`\n\nTo run **every** available implementation pass only the option `--all`.\n\n### Custom parameters\nFor every parameters used there is a default parameter but also the possibility to pass the value by option. Here's the available options for setting the parameters:\n\n-   `--executions`: number of the test executions (integer).\n-   `--iterations`: number of iterations for grasp and ant (integer).\n-   `--rcl_max`: maximum number of elements to consider in RCL (integer).\n-   `--mu_memory`: trace coefficient for ant (integer).\n-   `--mu_data`: data coefficient for ant (integer).\n-   `--oblivion`: trace oblivion coefficient for ant (double).\n  \nEvery parameter is used like `data`, as follows:\n\n```bash\n--{parameter}={parameter_value}\n```\n\nor equivalently:\n\n```bash\n--{parameter} {parameter_value}\n```\n\n### Utilities related options\nTwo utilities are available:\n\n#### Verbose    \nWhen specified as `--verbose`, verbose prints output to stdout, otherwise it will remain muted.\n\n#### Log\nWhen specified as `--log={csv_log_file}`, the scripts results will be logged to `csv_log_file` in csv file format.\n\n## Multiprocess bayesian tuning for ant-system hyper-parameters\nUsing a [Gaussian process](https://github.com/LucaCappelletti94/gaussian_process) ([script implementation here](https://github.com/LucaCappelletti94/greedy_grasp_ant/blob/master/scripts/bayes.py)) we can tune the `oblivion`, `mu_memory` and `mu_data` parameters for the ant-system as follows.\n\nFirst we build the project:\n\n```bash\nmkdir gga\ncd gga\ncmake ..\nmake\ncd ..\n```\n\nThen we install the required python packages:\n\n```bash\npython -m pip install -r requirements.txt\n```\n\nand finally we run the gaussian process:\n\n```bash\npython scripts/bayes.py\n```\n\nOther than printing the best parameters at the end, they will also be saved in the `best_parameters.json` file.\n\n## Multiprocess implementations scores determination\nAfter having built the project and installed the python requirements, as explained the gaussian process section, we can run the following:\n\n```bash\npython scripts/multiproc.py\n```\n\nThe cumulative results will be stores in `scores/all_scores.csv` and the single files will be stored in `scores`.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucacappelletti94%2Fgreedy_grasp_ant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucacappelletti94%2Fgreedy_grasp_ant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucacappelletti94%2Fgreedy_grasp_ant/lists"}