GithubHelp home page GithubHelp logo

khaledashrafh / curve-fitting Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 272 KB

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

License: MIT License

CMake 17.50% C 36.82% C++ 45.68%
chromosome crossover crossover-operator curve-fitting elitist-genetic-algorithm genetic-algorithm genetic-algorithms mutation mutation-operator soft-computing

curve-fitting's Introduction

Curve Fitting Genetic Algorithm

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.

Input Format

The input file should have the following format:

Number of datasets
Number of data points and polynomial degree for each dataset
x-coordinate and y-coordinate for each data point

For example:

1

4 2

1 5
2 8
3 13
4 20

This 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).

Chromosome Encoding and Objective Function

The solution is encoded as a chromosome using a floating-point representation of the polynomial coefficients. The genes represent the coefficients of the polynomial equation.

The 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.

Genetic Algorithm Parameters

The following parameters are used in the genetic algorithm:

  • Population size (populationSize): The number of chromosomes in each population.
  • Number of iterations (numOfIterations): The maximum number of generations the algorithm will run.
  • Crossover probability (Pc): The probability of performing crossover during reproduction.
  • Mutation probability (Pm): The probability of performing mutation on a chromosome.

Genetic Algorithm Steps

The genetic algorithm follows these steps:

  1. Initialization: Initialize the population of chromosomes with random coefficients within the range [-10, 10].
  2. 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.
  3. Selection: Select the best chromosomes from the population using tournament selection.
  4. Crossover: Apply crossover between selected chromosomes with a probability of Pc.
  5. 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.
  6. Reproduction: Replace the current population with the offspring chromosomes, preserving the best solutions using elitism.
  7. Repeat steps 2 to 6 for the specified number of iterations.
  8. Sort the final population based on fitness and output the best solution for each dataset.

Output Format

The 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:

Testcase <dataset index>:
The Coefficients: [ <coefficient1> , <coefficient2> , ... ]
Mean Square Error: <mean square error>

For example:

Testcase 1:
The Coefficients: [ 2.48 , 0.95 , 0.05 ]
Mean Square Error: 0.32456789012345678900

Running the Program

To 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 <bits/stdc++.h>.

Compile and run the program, and the results will be written to the output file specified in the code (output.txt in this case).

Conclusion

The 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.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.

License

This program is licensed under the MIT License.

curve-fitting's People

Contributors

khaledashrafh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

chiefstone

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.