这是一个Sails.js和Node.js应用程序。我正在努力解决我遇到的问题。我不知道要解决它的方法。这就是为什么我的问题的标题并不真正符合它。
但是,这就是场景。我是这样的:
{
"vehicleType": "30tonne",
"waybill": "7006722988",
"obdCreationDate": "01/11/16 1:21:50 PM",
"route": "5817895991a297ccb386469d",
"source": "agbara",
"destination": "ilorin",
"batchNumber": "ebf96f3322320fb2bedb7bf126f87ab8367f5bd6061cde78ab300e464df2c7af"
},
{
"vehicleType": "30tonne",
"waybill": "7006691063",
"obdCreationDate": "01/11/16 1:21:50 PM",
"route": "5817895e91a297ccb38646d9",
"source": "agbara",
"destination": "katsina",
"batchNumber": "ebf96f3322320fb2bedb7bf126f87ab8367f5bd6061cde78ab300e464df2c7af"
},
我是这样的:
{
"vehicles": [
{
"assetIdentification": "EWEW",
"vehicleType": "na",
"transporter": "5817891891a297ccb38645c1",
"organization": "5817878612a8dce1b2e4d359",
"status": "available",
"isDeleted": false,
"createdAt": "2016-10-31T18:20:32.963Z",
"updatedAt": "2016-10-31T18:20:32.963Z",
"id": "5800c3cc391eaa0709cffee5"
},
{
"assetIdentification": "RERE",
"vehicleType": "na",
"transporter": "5817891891a297ccb38645c1",
"organization": "5817878612a8dce1b2e4d359",
"status": "available",
"isDeleted": false,
"createdAt": "2016-10-31T18:20:32.965Z",
"updatedAt": "2016-10-31T18:20:32.965Z",
"id": "5800c4d9391eaa0709cffee6"
}
],
"rates": [
{
"fixedCost": 280759,
"variableCost": 0,
"vehicleType": "30tonne",
"organization": "5817878612a8dce1b2e4d359",
"route": "5817894b91a297ccb38645f9",
"transporter": "5817891891a297ccb38645c1",
"tripType": "normal",
"active": true,
"isDeleted": false,
"totalCost": 280759,
"createdAt": "2016-10-31T18:14:33.668Z",
"updatedAt": "2016-10-31T18:14:33.668Z",
"id": "58178a093ee3f7ffb3b14a47",
"_route": "5817894b91a297ccb38645f9"
},
{
"fixedCost": 280759,
"variableCost": 0,
"vehicleType": "30tonne",
"organization": "5817878612a8dce1b2e4d359",
"route": "5817894b91a297ccb38645fa",
"transporter": "5817891891a297ccb38645c1",
"tripType": "normal",
"active": true,
"isDeleted": false,
"totalCost": 280759,
"createdAt": "2016-10-31T18:14:33.866Z",
"updatedAt": "2016-10-31T18:14:33.866Z",
"id": "58178a093ee3f7ffb3b14a66",
"_route": "5817894b91a297ccb38645fa"
}
],
"organization": "5817878612a8dce1b2e4d359",
"name": "Some Organization",
"email": "sosoos@soso.so",
"phone": "8392398293829",
"active": true,
"isDeleted": false,
"slug": "some-orgs",
"createdAt": "2016-10-31T18:10:32.623Z",
"updatedAt": "2016-10-31T18:10:32.623Z",
"id": "5817891891a297ccb38645c1"
}
上面的记录(运输商)超过9000,每个运输商的rates
超过200.而之前的那个(ORDERS)可以有任何尺寸。
所以,这就是问题所在。每个订单都有routeId
和vehicleType
。运输商有Array of vehicles with vehicleSize (s)
,Array of rates which each have rateId, vehicleType and totalCost(Cost for that route)
。我试图解决的问题是获得最便宜的费率来完成所有订单,即使为每个运输商分配1个订单会使其便宜。我写了一个我在循环中循环来做所有形式的组合,它让我最便宜。但是,对30 Orders
运行它使得应用程序无法用于任何其他请求,并且使用7GB专用RAM,我们仍然不时地耗尽内存并且由于JavaScript
是同步的,这肯定会使端口成为可能该应用程序运行无用,除非它已完成该操作。 30 Orders
需要30分钟到一个小时。
我无法想到要做什么。如果Permutation
和Combination
可以解决问题我尝试过,但是没有。我不是在寻找一个完整的解决方案。我只需要建议做什么以及如何去做。任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
这是一部古典A* pathfinding task。为了优化你应该为车辆类型和路线的每个组合以最小速率创建反向索引[vehicleType,route] => [vehicle],它需要对所有速率进行单次迭代,例如,要么)。然后通过每个订单,您可以从索引中分配相关车辆。它不考虑活动和可用性标志,并且可以为同一车辆分配多个订单,但是通过价格排序的结果车辆数组,可以很容易地改进扩展到索引之上。
反向索引最好通过map-reduce在数据库端构建。虽然你也可以在节点中构建它:
let ratesByRouteVType = []
transporters
.rates
.forEach(r => {
let cmpRate = ratesByRouteVType[r.route+r.vehicleType]
if(!cmpRate || cmpRate.totalCost>r.totalCost)
ratesByRouteVType[r.route+r.vehicleType]=r
})