简单的背包问题

给定8个物品,其重量和价值如下图的表所示,现在从这些物品中挑出总重量不超过100的物品,编写程序求解所有方案中价值总和的最大值
在这里插入图片描述

#include <iostream>
#include <cstdlib>
#include <ctime>
#include<cmath>
#include<vector>
#include <fstream>
#include<algorithm>
using namespace std;
const double pc = 0.7;//交叉互换的概率
const double pv = 0.3;//变异的概率
const int number_bits = 8;//二进制表示需要
const int number_agent = 10;//每代的个数
const int Max_bear = 100;//最大承受重量
const int NC_max = 10000;
const int bag = 8;
double information[2][8] = {{30, 40, 20, 5, 15, 60, 25, 10}, {47, 30, 9, 8, 15, 66, 12, 11}};;//表示每个包的重量和价值
/*
*
*/
class Number {
public:
    vector<int> _bits;
    int _fitness;//适应度即价值
    int _is_sur;//重量
    Number() :_fitness(0), _is_sur(0)//初始化
    {
        for (int i = 0; i < number_bits; i++)
            _bits.push_back(rand() % 2);
        UpdateFit();
    }
    void Init_bits(vector<int>& x)//手动初始化
    {
        _bits.clear();
        for (int i = 0; i < number_bits; i++)
            _bits.push_back(x[i]);
        UpdateFit();
    }
    void UpdateFit()//更新适应度
    {
        _fitness = 0;
        _is_sur = 0;
        for (int i = 0; i < number_bits; i++)
        {
            _fitness += information[1][i] * _bits[i];
            _is_sur += information[0][i] * _bits[i];
        }
        if (_is_sur > Max_bear)
            _fitness = 1;//惩罚
    }
    void Mutations()//自身发生变异
    {
        int P = rand() % 100;
        if (P <= pv * 100)
        {
            int pos = rand() % number_bits;
            if (_bits[pos] == 0)
                _bits[pos] = 1;
            else
                _bits[pos] = 0;
            UpdateFit();
        }
    }
    void Print()
    {
        for(int i = 0; i < number_bits; i++)
            cout << " " << _bits[i];
    }
};
void Print(Number* n)
{
    for (int i = 0; i < number_agent; i++)
    {
        n[i].Print();
        cout << "  Fitness: " << n[i]._fitness <<  " weight: " << n[i]._is_sur;
        if (i % 2 == 0)
            cout << "            ";
        else
            cout << endl;
    }
}
bool cmp1(Number& n1, Number& n2)//比较函数
{
    return n1._fitness < n2._fitness;
}
void Cross_swap(Number& n1, Number& n2, Number& n3)//交叉互换
{
    vector<int> bit1(n1._bits), bit2(n2._bits);
    int pos1, pos2, P = rand() % 101;
    if (P <= pc * 100)
    {
        pos1 = rand() % number_bits;
        pos2 = rand() % number_bits;
        if (pos1 > pos2)
        {
            int temp = pos1;
            pos1 = pos2;
            pos2 = temp;
        }
        for (int i = 0; i < number_bits; i++)
        {
            if (i >= pos1 && i <= pos2)
            {
                bit1[i] = n2._bits[i];
                bit2[i] = n1._bits[i];
            }
        }
    }
    n3.Init_bits(bit1);
}
int main()
{
    srand((int)time(NULL));
    Number* n, * nn;
    int while_number = 1;
    n = new Number[number_agent + 2];
    int Max = 0, Max_weight = 0;
    while (while_number++ < NC_max)
    {
        sort(n, n + number_agent, cmp1);
        if (n[0]._fitness > Max)
        {
            Max = n[0]._fitness;
            Max_weight = n[0]._is_sur;
        }
        cout << "第" << while_number << "代的最大为: " << n[0]._fitness << endl;
        Print(n);

        //轮盘赌,随机选取两个父代
        int fit_sum = 0;
        for (int i = 0; i < number_agent; i++)
        {
            fit_sum += n[i]._fitness;
        }
        //生成和后代
        nn = new Number[number_agent + 2];
        int product_agent = 0;
        while (product_agent < number_agent)
        {
            int pos1 = 0, pos2 = number_agent - 1, P, sum = 0;
            P = rand() % (fit_sum);
            for (int j = 0; j < number_agent; j++)
            {
                sum += n[j]._fitness;
                if (sum > P)
                {
                    pos2 = j;
                    break;
                }
            }
            P = rand() % (fit_sum);
            for (int j = number_agent - 1; j >= 0; j--)
            {
                sum += n[j]._fitness;
                if (sum > P)
                {
                    pos1 = j;
                    break;
                }
            }
            Cross_swap(n[pos1], n[pos2], nn[product_agent]);
            nn[product_agent].Mutations();
            if (nn[product_agent]._is_sur > Max_bear) //如果不将这注销则收敛速度加快
                product_agent--;
            product_agent++;
        }
        delete []n;
        n = nn;
    }
    cout << "the lagest number is :" << Max << " Max_weight is :" << Max_weight << endl;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值