Wednesday, June 27, 2012

Genetic Algorithm - To find optimum solutions

Genetic Algorithm is an adaptive method to find an optimum solution for a problem. There are some real world problems for which we cannot find a solution using a particular algorithm. But those problems may have solutions. Genetic Algorithm is a suitable method for finding an optimum solution for such a problem.

Genetic Algorithm belongs to the category of Stochastic methods.  Stochastic methods never operate in the same way twice for a particular problem. They are involved with probabilistic operations.

By applying Genetic Algorithms we expect to achieve the "Survival of Fittest". That is among a population of solutions, Genetic Algorithm works in a way such that the fittest solutions are survived and further being improved.

In Genetic Algorithm terminology, "Chromosome" represents the whole solution for a particular problem. Genetic Algorithm operations start with defining a population of such chromosomes, which represent feasible solutions for the problem. A population is called a generation.

To generate the next generation from the first generation, the fitness of the chromosomes are evaluated. This is done using the "Fitness Function". The most fittest chromosomes become candidates for crossover operation, which creates new chromosomes by including the good features from its parent chromosomes. Then they are undergone through mutation, which introduce new features which are not included in the parent chromosomes. This operation is done to maintain genetic diversity.

Thus, the Genetic Algorithm runs through generations, until it achieves the defined optimum level. In each generation, it creates an improved solution compared to previous generation. So the quality of the solution increases through the generations.

There is a computational overhead due to the lot of operations performed on each and every chromosome in the population. So to achieve a considerable performance level, Genetic Algorithm needs some operations to perform in parallel.

Tuesday, March 6, 2012

Some useful commands in C programming

Recently I have used C for one of my projects. Here are some commands which make it easy for me to compile simple C source files.

Once you save the C source code in source_file_name.c file, you can compile it with gcc command.

gcc source_file_name.c 

This command will compile your source code. The output file name will be a.out. But if you need to give the output file a name you prefer, you can simply give the following command.

gcc source_file_name.c -o output_file_name

Then it will write the object code (compiled code) in to the file "output_file_name".

You can then run the file by just giving its name in the terminal.

$ ./output_file_name

But these are two separate commands. If you need to compile and run your C program using just one command, then you can use the following command.

gcc source_file_name.c -o output_file_name && ./output_file_name

This will compile your source code first and if compilation is successful it will execute the program.

Very basic stuff, but useful. Isn't it :)

note : Why we need to use ./ to execute a program ? 

It is because, in Linux operating systems, to run a program we need to specify the directory where that program file is located. Here ./ means "Current Directory". Otherwise the directory should be listed in the PATH system variable.