{"id":22537858,"url":"https://github.com/khaledashrafh/curve-fitting","last_synced_at":"2025-10-17T02:30:42.944Z","repository":{"id":191343023,"uuid":"684432005","full_name":"KhaledAshrafH/Curve-Fitting","owner":"KhaledAshrafH","description":"This program implements a genetic algorithm for curve fitting using a polynomial equation. The goal is to find the best coefficients for the polynomial equation that minimize the distance between the curve and a given set of data points. The genetic algorithm is used to search for the optimal solution by evolving a population of candidate solutions","archived":false,"fork":false,"pushed_at":"2023-08-29T05:53:58.000Z","size":272,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-07T11:08:58.665Z","etag":null,"topics":["chromosome","crossover","crossover-operator","curve-fitting","elitist-genetic-algorithm","genetic-algorithm","genetic-algorithms","mutation","mutation-operator","soft-computing","soft-computing-course","soft-computing-techniques","tournament-selection"],"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/KhaledAshrafH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-29T05:39:24.000Z","updated_at":"2024-06-06T23:10:15.000Z","dependencies_parsed_at":"2023-08-29T11:57:49.778Z","dependency_job_id":null,"html_url":"https://github.com/KhaledAshrafH/Curve-Fitting","commit_stats":null,"previous_names":["khaledashrafh/curve-fitting"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KhaledAshrafH%2FCurve-Fitting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KhaledAshrafH%2FCurve-Fitting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KhaledAshrafH%2FCurve-Fitting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KhaledAshrafH%2FCurve-Fitting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KhaledAshrafH","download_url":"https://codeload.github.com/KhaledAshrafH/Curve-Fitting/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236765488,"owners_count":19201281,"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":["chromosome","crossover","crossover-operator","curve-fitting","elitist-genetic-algorithm","genetic-algorithm","genetic-algorithms","mutation","mutation-operator","soft-computing","soft-computing-course","soft-computing-techniques","tournament-selection"],"created_at":"2024-12-07T11:09:02.066Z","updated_at":"2025-10-17T02:30:37.433Z","avatar_url":"https://github.com/KhaledAshrafH.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Curve Fitting Genetic Algorithm\n\nThis program implements a genetic algorithm for curve fitting using a polynomial equation. The goal is to find the best coefficients for the polynomial equation that minimize the distance between the curve and a given set of data points. The genetic algorithm is used to search for the optimal solution by evolving a population of candidate solutions.\n\n## Input Format\n\nThe input file should have the following format:\n\n```\nNumber of datasets\nNumber of data points and polynomial degree for each dataset\nx-coordinate and y-coordinate for each data point\n```\n\nFor example:\n\n```\n1\n\n4 2\n\n1 5\n2 8\n3 13\n4 20\n```\n\nThis example represents one dataset with 4 data points and a polynomial degree of 2. The data points are (1, 5), (2, 8), (3, 13), and (4, 20).\n\n## Chromosome Encoding and Objective Function\n\nThe solution is encoded as a chromosome using a floating-point representation of the polynomial coefficients. The genes represent the coefficients of the polynomial equation.\n\nThe objective function is the mean square error between the curve defined by the polynomial equation and the given data points. The fitness of a chromosome is calculated as the inverse of the total error, with higher fitness indicating a better solution.\n\n## Genetic Algorithm Parameters\n\nThe following parameters are used in the genetic algorithm:\n\n- Population size (`populationSize`): The number of chromosomes in each population.\n- Number of iterations (`numOfIterations`): The maximum number of generations the algorithm will run.\n- Crossover probability (`Pc`): The probability of performing crossover during reproduction.\n- Mutation probability (`Pm`): The probability of performing mutation on a chromosome.\n\n## Genetic Algorithm Steps\n\nThe genetic algorithm follows these steps:\n\n1. **Initialization**: Initialize the population of chromosomes with random coefficients within the range \\[-10, 10\\].\n1. **Individual Evaluation**: Calculate the fitness of each chromosome by evaluating the mean square error between the curve defined by the polynomial equation and the data points.\n1. **Selection**: Select the best chromosomes from the population using tournament selection.\n1. **Crossover**: Apply crossover between selected chromosomes with a probability of `Pc`.\n1. **Mutation**: Perform mutation on the offspring chromosomes with a probability of `Pm`. The mutation is non-uniform, and the range of the mutation depends on the current iteration.\n1. **Reproduction**: Replace the current population with the offspring chromosomes, preserving the best solutions using elitism.\n1. Repeat steps 2 to 6 for the specified number of iterations.\n1. Sort the final population based on fitness and output the best solution for each dataset.\n\n## Output Format\n\nThe program outputs the results for each dataset, including the dataset index, the coefficients of the polynomial equation, and its mean square error. The output format is as follows:\n\n```\nTestcase \u003cdataset index\u003e:\nThe Coefficients: [ \u003ccoefficient1\u003e , \u003ccoefficient2\u003e , ... ]\nMean Square Error: \u003cmean square error\u003e\n```\n\nFor example:\n\n```\nTestcase 1:\nThe Coefficients: [ 2.48 , 0.95 , 0.05 ]\nMean Square Error: 0.32456789012345678900\n```\n\n## Running the Program\n\nTo run the program, make sure you have the input file in the specified format. You can modify the input file directly or create a new file. Additionally, ensure that the necessary libraries are included, such as `\u003cbits/stdc++.h\u003e`.\n\nCompile and run the program, and the results will be written to the output file specified in the code (`output.txt` in this case).\n\n## Conclusion\n\nThe curve fitting genetic algorithm implemented in this program utilizes tournament selection, 2-point crossover, non-uniform mutation, and elitist replacement to find the best coefficients for a polynomial equation that minimizes the distance to given data points. By adjusting the parameters of the genetic algorithm, such as population size and number of iterations, you can explore different trade-offs between exploration and exploitation in the search for the optimal solution. The genetic algorithm provides an effective approach for solving curve fitting problems by iteratively evolving a population of candidate solutions to improve the fitness and convergence towards the optimal solution.\n\n## Contributing\n\nContributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.\n\n## License\n\nThis program is licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledashrafh%2Fcurve-fitting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhaledashrafh%2Fcurve-fitting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledashrafh%2Fcurve-fitting/lists"}