【问题标题】:Algorithm Logic Required for finding best solution寻找最佳解决方案所需的算法逻辑
【发布时间】:2015-08-06 09:59:11
【问题描述】:

场景

  1. 具有以下属性的可用车辆列表:

    车辆1 {乘客:4,行李:2,手提箱:8}

    车辆2 {乘客:5,行李:3,手提箱:10}

    车辆3 {乘客:6,行李:3,手提箱:10}

    车辆4 {乘客:8,行李:4,手提箱:10}

  2. 现在,如果用户要求旅行(13 名乘客,6 件行李,15 个手提箱),那么最高效的车辆结果将是:

    车辆 2 + 车辆 4

问题定义: 我已经能够开发出一个流程/算法(但仅限于乘客人数),但是当乘客/手提箱/行李数量超过 3 辆车或更多车辆的需求时,我无法开发算法它。

代码:

function ajax_getVehicles() {

    $vehicleType = $_POST['vehicle_type'];
    $passengers = intval($_POST['passengers']);
    $luggage = intval($_POST['luggage']);
    $suitcases = intval($_POST['suitcases']);

    $requiredVehicles = array();

    // 1. Check if all passengers fit in a single car
    if (!$requiredVehicles) {
        $vehicle = $this->common_model->get_where('fleets', array('vehicle_type' => $vehicleType, 'passengers >=' => $passengers), 'passengers ASC');
        if ($vehicle)
            array_push($requiredVehicles, $vehicle[0]);
    }

    // 2. Try sending duplicate vehicles
    $vehicles = $this->common_model->get_where('fleets', array('vehicle_type' => $vehicleType), 'passengers ASC');
    if (!$requiredVehicles) {
        foreach ($vehicles as $v) {
            if ($v['passengers'] * 2 == $passengers) {
                array_push($requiredVehicles, $v, $v);
            }
        }
    }

    // 3. Find best possible solution
    if (!$requiredVehicles) {
        $totalPermutation = gmp_fact(count($vehicles)) / (gmp_fact(count($vehicles) - 2) * gmp_fact(2));

        $total_pax_array = array();
        for ($i = 0; $i < $totalPermutation; $i++) {
            for ($count = $i + 1; $count < count($vehicles); $count++) {
                $total_pax = $vehicles[$i]['passengers'] + $vehicles[$count]['passengers'];

                if ($total_pax >= $passengers) {

                    if (count($total_pax_array) < 1) {
                        $requiredVehicles = array($vehicles[$i], $vehicles[$count]);
                    } else if ($total_pax < min($total_pax_array)) {
                        $requiredVehicles = array($vehicles[$i], $vehicles[$count]);
                    }

                    array_push($total_pax_array, $total_pax);
                }
            }
        }
    }

    // 4. check if requirement can be acheived by sending duplicate vehicles
    if (!$requiredVehicles) {

        foreach ($vehicles as $v) {
            if ($v['passengers'] * 2 > $passengers) {
                array_push($requiredVehicles, $v, $v);
            } 
        }
    }

    if (!$requiredVehicles)
        jsonOutput('ERROR', 'call for 3 vehicles required.');
    else
        jsonOutput('SUCCESS', 'criteria matching vehicles', $requiredVehicles);
}

【问题讨论】:

    标签: php algorithm codeigniter logic solution


    【解决方案1】:

    这可以通过遵循递归公式使用Dynamic Programming (DP) 来解决:

    D(p,l,s,0) =   infinity        if p>0 or l>0 or s>0
                   0               otherwise      
    D(p,l,s,i) = min { D(p,l,s,i-1), D(p-cars[i].passangers, l-cars[i].luggage, s-cars[i].suitcases) + 1}
    

    这个想法是 D(p,l,s,i) 表示汽车 1,2,3...,i 之间的最小汽车数量 - 可以乘坐 p 乘客、l 行李和 @ 987654325@手提箱。

    时间复杂度(如果应用 DP 技术):O(n*p*l*s),其中n 是可用汽车的数量,p 需要的乘客数量,l - 需要的行李数量,s - 需要手提箱的数量。


    另一种解决方案是生成汽车的所有子集,为每个子集检查它是否是可行的解决方案,并从可行的解决方案中选择最小尺寸的子集。时间复杂度:O(2^n)

    【讨论】:

    • 感谢您回复@amit。我对动态编程一无所知。您能否向我推荐一些符合此标准的其他类似解决方案。如果可能?
    • @ManishShrestha DP 非常简单,最简单的形式是记忆化,这基本上意味着 - 你不需要重新计算两次,因为你“记住”了这个值。查看我链接的维基百科页面以获取更多详细信息。我还提供了一个蛮力选项来优化解决它。
    • 好的。我会尝试找出 DP,并会回到这里。再次感谢@amit。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多