0% found this document useful (0 votes)
13 views5 pages

AI Lab10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

AI Lab10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <bits/stdc++.

h>
using namespace std;

int rouletteWheel(vector<vector<int>> &fitness, int tot_fitness_value)


{
int x = 0, randomNumber = rand() % tot_fitness_value + 1;

for (int i = 0; i < fitness.size(); i++)


{
if (x + fitness[i][1] >= randomNumber)
return i;
x += fitness[i][1];
}
return -1;
}

void print(vector<vector<bool>> &chromosomeEncoding)


{
for (int i = 0; i < chromosomeEncoding.size(); i++)
{
for (int j = 0; j < chromosomeEncoding.size(); j++)
{
cout << chromosomeEncoding[i][j] << " ";
}
cout << endl;
}
}

void Calculatefitness(vector<int> weight, vector<int> value,vector<vector<bool>>


chromosomeEncoding,vector<vector<int>> &fitness,int &tot_fitness_value, int &capacity){
cout << endl
<< "Fitness : " << endl
<< "Weight\tValue" << endl;
for (int i = 0; i < weight.size(); i++)
{
int tot_weight = 0, tot_value = 0;
for (int j = 0; j < weight.size(); j++)
{
if (chromosomeEncoding[i][j])
{
tot_weight += weight[j];
tot_value += value[j];
}
}
fitness[i] =
{
tot_weight, tot_value};
if (tot_weight <= capacity)
{
tot_fitness_value += tot_value;
}
else
{
fitness[i] =
{
0, 0};
}
cout << tot_weight << "\t" << tot_value << endl;
}
}

void calculateAns(vector<bool> &ans, vector<vector<bool>> chromosomeEncoding,


vector<vector<int>> fitness, vector<int> value){
int prev_total = 0, temp ;
for(int i = 0; i < ans.size(); i++){
if(ans[i])prev_total += value[i];
}

for(int i = 0; i < chromosomeEncoding.size(); i++){


if(fitness[i][1] > prev_total){
prev_total = fitness[i][1];
ans = chromosomeEncoding[i];
}
}
}

int main()
{
srand(time(0));
int capacity = 12, tot_fitness_value = 0;
vector<int> weight = {5, 3, 7, 2}, value =
{
12, 5, 10, 7};
vector<vector<bool>> chromosomeEncoding(weight.size(),
vector<bool>(weight.size(), false)),
tempEncoding;

vector<bool> ans(weight.size(), false);


vector<vector<int>> fitness(weight.size(), vector<int>(2, 0)); // Weight, Value
cout << "Generation 1: " << endl;

for (int i = 0; i < chromosomeEncoding.size(); i++)


{
for (int j = 0; j < chromosomeEncoding.size(); j++)
{
if (rand() % 2)
{
chromosomeEncoding[i][j] = true;
}
}
}

print(chromosomeEncoding);

Calculatefitness(weight, value, chromosomeEncoding, fitness,


tot_fitness_value,capacity);
cout << "Total Fitness Value : " << tot_fitness_value << endl;

calculateAns(ans,chromosomeEncoding,fitness,value);

cout << "Generation 2 : ";

tempEncoding.clear();
for (int i = 0; i < weight.size(); i++)
{
int num = rouletteWheel(fitness, tot_fitness_value);
tempEncoding.push_back(chromosomeEncoding[num]);
cout << num << " ";
}
cout << endl
<< endl;

chromosomeEncoding = tempEncoding;

cout << "Before Crossover: " << endl;


print(chromosomeEncoding);
cout << endl;

for (int i = 0; i < weight.size (); i += 2)


{
swap (chromosomeEncoding[i][3], chromosomeEncoding[i + 1][3]);
}

cout << "After Crossover: " << endl;


print(chromosomeEncoding);

for(int i = 0; i < weight.size(); i++){


int randomNumber = rand() % weight.size();
chromosomeEncoding[i][randomNumber] = !chromosomeEncoding[i][randomNumber];
}
cout << endl << "After Mutation: " << endl;
print(chromosomeEncoding);

tot_fitness_value = 0;
Calculatefitness(weight, value, chromosomeEncoding, fitness,
tot_fitness_value,capacity);

cout << tot_fitness_value;

calculateAns(ans,chromosomeEncoding,fitness,value);

cout << endl << "Ans : ";


tot_fitness_value = 0;
for(int i = 0; i < ans.size(); i++){
cout << ans[i] << " ";
if(ans[i])tot_fitness_value += value[i];
}

cout << "Ans: " << tot_fitness_value << endl;

return 0;
}

You might also like