15,846,977 members
See more:
Hello..
I have to write a GA code to find the solution case
there are 10 questions with 4 choices
which means there are 4^10 = 1,048,576 cases and one of them is the solution case.
I have understood that the length of chromosomes must be 10 arrays and
the size of population is also 10.
I know nothing about coding and looked through some code examples and noticed that i have to calculate crossover, mutation, and fitness..

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct Chrom // creating the chrom structure
{
short int bit[10];
int fit;
}chrom;

void *evpop(chrom popcurrent[4]);
int x(chrom popcurrent);
int y(int x);
void *pickchroms(chrom popcurrent[4]);
void *crossover(chrom popnext[4]);
void *mutation(chrom popnext[4]);

This is what I came up with for now. I found this example source on google, and I do not understand why there are popcurrent, popnext..
Thank you very much..
Posted

## Solution 2

Understanding the mechanism of GA is already hard, not to mention trying to interpret other people's code that implements it. That is even harder if you never code before.
Come back to the GA, you can create a model like this:
1. Represent each potential solution by a 40 gene-chromosome, where each gene represents a multiple choice answer.
2. The first question is represented by the first 4 genes, the second question the next 4 genes, and so on...
3. Permutation of the genes will generate different solutions for the population.
4. Put the population through the GA evolution, i.e. selection, crossover, mutation, and fitness evaluation. You should have already learnt it as part of your study. If not, read my articles on Genetic Algorithms Demystified[^] and Genetic Algorithms Implementation[^]
Last but not least, you should learn to code and implement it yourself. Only then will you really understand the working of GA.

v3
Maciej Los 13-Apr-15 1:44am
5ed!
Peter Leow 13-Apr-15 4:28am
Thank you, Maciej.

## Solution 1

Finding source code on the internet is not a good way to do this: particularly if you have no idea what the code you have found does or how it does it.

Seriously, you need to learn the subject and try it for yourself - you won't learn much (if anything) by "finding code" and trying to get us to do your homework for you. If you don't learn this stuff, how do you expect to understand the next assignment, or pass the exam at the end of the course?

So sit down, read your course notes, and give it a try. If you get stuck on a particular point then ask about that, but we aren't going to do it all for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 CPallini 90 0x01AA 90 OriginalGriff 65 _Asif_ 55 Pete O'Hanlon 50
 OriginalGriff 2,093 Richard Deeming 875 Dave Kreskowiak 631 Graeme_Grant 589 Richard MacCutchan 510

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900