JavaScript实现 Simulated-Annealing算法

本文详细介绍了模拟退火算法的工作原理及其实现过程。通过设置不同的参数,如冷却因子、稳定因子等,该算法能够在复杂的搜索空间中寻找全局最优解。文章提供了一个具体的JavaScript实现示例,展示了如何初始化算法状态,并逐步进行模拟退火过程。

Simulated-Annealing算法:

var SimulatedAnnealing = (function () {
    var coolingFactor            = 0.0,
        stabilizingFactor        = 0.0,
        freezingTemperature      = 0.0,
        currentSystemEnergy      = 0.0,
        currentSystemTemperature = 0.0,
        currentStabilizer        = 0.0,

        generateNewSolution      = null,
        generateNeighbor         = null,
        acceptNeighbor           = null;

    function _init (options) {
        coolingFactor            = options.coolingFactor;
        stabilizingFactor        = options.stabilizingFactor;
        freezingTemperature      = options.freezingTemperature;
        generateNewSolution      = options.generateNewSolution;
        generateNeighbor         = options.generateNeighbor;
        acceptNeighbor           = options.acceptNeighbor;

        currentSystemEnergy      = generateNewSolution();
        currentSystemTemperature = options.initialTemperature;
        currentStabilizer        = options.initialStabilizer;
    }

    function _probabilityFunction (temperature, delta) {
        if (delta < 0) {
            return true;
        }

        var C = Math.exp(-delta / temperature);
        var R = Math.random();

        if (R < C) {
            return true;
        }

        return false;
    }

    function _doSimulationStep () {
        if (currentSystemTemperature > freezingTemperature) {
            for (var i = 0; i < currentStabilizer; i++) {
                var newEnergy = generateNeighbor(),
                    energyDelta = newEnergy - currentSystemEnergy;

                if (_probabilityFunction(currentSystemTemperature, energyDelta)) {
                    acceptNeighbor();
                    currentSystemEnergy = newEnergy;
                }
            }
            currentSystemTemperature = currentSystemTemperature - coolingFactor;
            currentStabilizer = currentStabilizer * stabilizingFactor;
            return false;
        }
        currentSystemTemperature = freezingTemperature;
        return true;
    }

    return {
        Initialize: function (options) {
            _init(options);
        },

        Step: function () {
            return _doSimulationStep();
        },

        GetCurrentEnergy: function () {
            return currentSystemEnergy;
        },

        GetCurrentTemperature: function () {
            return currentSystemTemperature;
        }
    };
})();


转载于:https://my.oschina.net/jsan/blog/222797

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值