Make a C program that can read any simple data file. The program should contain
ID: 3883615 • Letter: M
Question
Make a C program that can read any simple data file. The program should contain code that randomly initialize a genetic algorithm population (say population size = 100) and compute the makespans of the population. Select the best schedule according to makespans and output the schedule and its makespan.
For example suppose you have a file named text.txt that contains the following data.
3 2 //This first row indicates the number of jobs and the number of machines
0 5 1 10 //These other 3 rows represent a specific job combined by (machine-id, job-duration) pairs.
0 10 1 5
0 1 1 4
The output would be:
21 //This first line indicates the optimization criterion 2
0 0 1 1 6 //This second line is the makespan computed from the schedule
2 1 0 6 1 16 //This is the flowshop schedule combined by (job-id, start-time) pairs
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int calculate(bool* genetic);
int main()
{
int i,j,g; //counters
int population=100;
bool genetic[10][7]; //population
//initializing population
for(i=0;i<population;i++)
{
for(j=0;j<7;j++)
{
//randomize the genetic
genetic[i][j]=rand()%2;
}
}
for(g=0;g<100;g++)
{
printf("generation %d ",g);
//Evaluation
int best=0;
for(i=1;i<population;i++)
{
if(calculate(genetic[best])<calculate(genetic[i]))
best=i;
}
//Reproduction
for(i=0;i<population;i++)
{
if(i!=best)
{
for(j=0;j<7;j++)
{
if(rand()%2)
genetic[i][j]=genetic[best][j];
else
genetic[i][j]=genetic[i][j];
//mutation
if(rand()%100<4)
genetic[i][j]=rand()%2;
}
}
}
printf("best calculate %d ",calculate(genetic[best]));
}
getchar();
return 0;
}
int calculate(bool* genetic)
{
return ( -genetic[0] + genetic[1] + genetic[2]
-genetic[3] + genetic[4] - genetic[5]
-genetic[6] );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.