zoukankan      html  css  js  c++  java
  • 背包问题matlab程序

    clear
    clc
    a=0.95
    k=[5;10;13;4;3;11;13;10;8;16;7;4];
    k=-k;
    d=[2;5;18;3;2;5;10;4;11;7;14;6];
    restriction=46;
    num=12;
    sol_new=ones(1,num);
    E_current=inf;
    E_best=inf;
    sol_current=sol_new;
    sol_best=sol_new;
    t0=97;
    tf=3;
    t=t0;
    p=1;
    while t>=tf
        for r=1:100
            %产生随机扰动
            tmp=ceil(rand.*num);
            sol_new(1,tmp)=~sol_new(1,tmp);
            %检查是否满足约束
            while 1
                q=(sol_new*d<=restriction);
                if ~q
                    p=~p;
                    tmp=find(sol_new==1);
                    if p
                        sol_new(1,tmp)=0;
                    else
                        sol_new(1,tmp(end))=0;
                    end
                else
                    break
                end
            end
            %计算背包中的物品价值
            E_new=sol_new*k;
            if E_new<E_current
                E_current=E_new;
                sol_current=sol_new;
                if E_new<E_best
                    %把冷却过程中最好的解保存下来
                    E_best=E_new;
                    sol_best=sol_new;
                end
                else
                    if rand<exp(-(E_new-E_current)./t)
                        E_current=E_new;
                        sol_current=sol_new;
                    else
                        sol_new=sol_current;
                    end
                end
            end
            t=t.*a;
        end
        disp('最优解为:')
        sol_best
        disp('物品总价值等于:')
        val=-E_best;
        disp(val)
        disp('背包中物品重量是:')
        disp(sol_best * d)
           

  • 相关阅读:
    ajax怎么打开新窗口具体如何实现
    关于springcloud hystrix 执行 hystrix.stream 跳转失败的问题
    Zookeeper 和Eureka比较
    Maven Install报错:Perhaps you are running on a JRE rather than a JDK?
    Oracle11g卸载步骤
    Oracle数据库备份及恢复
    python是如何进行内存管理的?
    python面试题
    json模块与hashlib模块的使用
    随机验证码、打印进度条、文件copy脚本
  • 原文地址:https://www.cnblogs.com/Xbingbing/p/3269226.html
Copyright © 2011-2022 走看看