0/1 knapsack problem Genetic Algorithm

Source: Internet
Author: User
Tags random seed

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>

# Define popsize 200
# Define PC 0.618
# Define PM 0.03.
# Define lchrom 50
# Define maxgen1000

Struct population
{
Int chrom [lchrom];
Double weight;
Double fitness;
} Oldpop [popsize], newpop [popsize];

Int weight [lchrom] = {80, 82, 85, 70, 72, 70, 66, 50, 55, 25,
50, 55, 40, 48, 50, 32, 22, 60, 30, 32,
40, 38, 35, 32, 25, 28, 30, 22, 50, 30,
45, 30, 60, 50, 20, 65, 20, 25, 30, 10,
20, 25, 15, 10, 10, 10, 4, 4, 2, 1 },
Profit [lchrom] ={ 220,208,198,192,180,180,165,162,160,158,
155,130,125,122,120,118,115,110,105,101,
100,100, 98, 96, 95, 90, 88, 82, 80, 77,
75, 73, 72, 70, 69, 66, 65, 63, 60, 58,
56, 50, 30, 20, 15, 10, 8, 5, 3, 1 },
Contain = 1000;

Double sumfitness, maxfitness;
Int maxpop;

Int cal_weight (struct population pop)
{
Int pop_weight = 0 ;;
Int I = 0;

For (I = 0; I <lchrom; I ++)
{
Pop_weight = pop_weight + pop. chrom [I] * weight [I];
}

Return pop_weight;
}

Int cal_fit (struct population pop)
{
Int pop_profit = 0;
Int I = 0;

For (I = 0; I <lchrom; I ++)
{
Pop_profit = pop_profit + pop. chrom [I] * profit [I];
}

Return pop_profit;
}

Void statistics (struct population pop [popsize])
{
Int I = 0;
Sumfitness = pop [0]. fitness;
Maxfitness = pop [0]. fitness;
Maxpop = 0;

For (I = 0; I <popsize; I ++)
{
Sumfitness = sumfitness + pop [I]. fitness;

If (POP [I]. Fitness> maxfitness)
{
Maxfitness = pop [I]. fitness;
Maxpop = I;
}
}
}

Void initpop (void)
{
Int I = 0, j = 0;
Int flag = 0;
Int temp;

For (I = 0; I <popsize; I ++)
{
Flag = 1;
While (FLAG)
{
For (j = 0; j <lchrom; j ++)
{
Oldpop [I]. chrom [J] = rand () % 2;
}

Temp = cal_weight (oldpop [I]);
If (temp <= contain)
{
Oldpop [I]. Weight = temp;
Oldpop [I]. Fitness = cal_fit (oldpop [I]);
Flag = 0;
}
}
}
}

Int execise (double probability)
{
If (RAND () % 20001/20000. 0) <= probability)
Return 1;
 
Return 0;
}

Int selection ()
{
Int I = 0;
Double P = 0.0;
Double rand_number;
 
Rand_number = (RAND () % 2001/2000. 0 );

Do
{
P = P + (oldpop [I]. Fitness/sumfitness );
I ++;
} While (I <popsize) & (P <rand_number ));

Return I-1;
}

Void crossover (INT chr1 [lchrom], int chr2 [lchrom], struct population pop)
{
Int I, cross_pos;

If (execise (PC ))
{
Cross_pos = rand () % (lchrom-1 );
}
Else
Cross_pos = lchrom-1;

For (I = 0; I <= cross_pos; I ++)
{
Pop. chrom [I] = chr1 [I];
}
For (I = cross_pos + 1; I <lchrom; I ++)
{
Pop. chrom [I] = chr2 [I];
}
}

Int mutation (int mut)
{
If (execise (PM ))
{
If (MUT)
Mut = 0;
Else
Mut = 1;
}
Return mut;
}

Void generation ()
{
Int I = 0, j = 0, mate1 = 0, mate2 = 0;
Double temp;
Int flag;

I = 0;
While (I <popsize)
{
Flag = 1;
While (FLAG)
{
Mate1 = selection ();
Mate2 = selection ();

Crossover (oldpop [mate1]. chrom, oldpop [mate2]. chrom, newpop [I]);

For (j = 0; j <lchrom; j ++)
{
Newpop [I]. chrom [J] = mutation (newpop [I]. chrom [J]);
}

Temp = cal_weight (newpop [I]);

If (temp <= contain)
{
Newpop [I]. Weight = temp;
Newpop [I]. Fitness = cal_fit (newpop [I]);
Flag = 0;
}
}
I ++;
}
}

Void print (struct population pop [popsize], int Gen)
{
Int J = 0;
Printf ("the generation is % d. \ n", Gen );
Printf ("the population's Max fitness is % F. \ n", pop [maxpop]. Fitness );
Printf ("the knapsack's weigth is % F. \ n", pop [maxpop]. weight );
Printf ("the population's chrom is :");
For (j = 0; j <lchrom; j ++)
{
If (0 = (J % 5 ))
Printf ("");
Printf ("% d", pop [maxpop]. chrom [J]);
}

Printf ("\ n ");
}

Int main (void)
{
Int I = 0, j = 0, K = 0;
Double oldmax = 0;
Srand (unsigned) Time (null ));
Initpop ();
Statistics (oldpop );
Print (oldpop, 0 );

For (I = 1; I <maxgen; I ++)
{
If (I % 100 = 0)
{
Srand (unsigned) Time (null); // set Random Seed
}

Generation ();
Statistics (newpop );
If (maxfitness <oldmax) continue;
Else
{
Oldmax = maxfitness;
Print (newpop, I );
For (j = 0; j <popsize; j ++)
{
Oldpop [J]. Fitness = newpop [J]. fitness;
Oldpop [J]. Weight = newpop [J]. weight;
For (k = 0; k <lchrom; k ++)
{
Oldpop [J]. chrom [k] = newpop [J]. chrom [k];
}
}
}
}
 
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.