Write a C program that can read any simple data file such as text.txt 3 2 // The
ID: 3883343 • Letter: W
Question
Write a C program that can read any simple data file such as
text.txt
3 2 // The 1st row indicates the number of jobs and the number of machines
0 5 1 10 //These others rows each represents a specific job combined by (machine-id, job-duration) pairs.
0 10 1 5
0 1 1 4
After 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 to an outtput file. The makespan would look like this,
makespan
21 // first line indicates the optimization criterion
2 0 0 1 1 6 //this line is the makespan computed from the schedule
2 1 0 6 1 16 // this line 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.