私は何日もこれを解読しようとしてきましたが、どんな助けでも大歓迎です。基本的に、この一連の目的地のペアリングを反復処理し、「反転した」各ペアの走行距離を再計算することで全体の距離を改善できるかどうかを確認しようとしています。インバーテッド ペアの総走行距離がオリジナルよりも優れている場合は、オリジナルのペアをインバーテッド バージョンに交換します。
私が見つけた問題は、このプロセスを実行したままにしておくとメモリが不足するか、結果が期待どおりに最適化されないことです(マイレージが得られます)。
ここでは少し大ざっぱですが、どこが間違っているのかわかりません。建設的な質問にするには: JavaScript でペアリングの配列を反復処理して最適化できる関数/アルゴリズムを作成するのを手伝ってくれる人はいますか? ...お願いします?
var firstResult = [{"from":"DE55 7RA","to":"DE55 4XJ","oNum":"3740003654","dNum":"3650645273","metres":"536"},{"from":"WF2 0XF","to":"WF2 0XE","oNum":"3740003632","dNum":"3650645256","metres":"304"},{"from":"WF9 2RH","to":"WF9 3AP","oNum":"3740003657","dNum":"3650645279","metres":"4132"},{"from":"LS10 1BG","to":"LS9 8HY","oNum":"3740003637","dNum":"3650645266","metres":"2941"},{"from":"WF10 5UA","to":"WF8 4QD","oNum":"3740003646","dNum":13,"metres":"7248"},{"from":"HD2 1UA","to":"WF15 6JL","oNum":"3740003641","dNum":"3650645270","metres":"6745"},{"from":"HU14 3HJ","to":"HU15 2PE","oNum":"3740003640","dNum":"3650645259","metres":"13309"},{"from":"WF2 0XF","to":"WF5 8NE","oNum":"3740003631","dNum":"3650645262","metres":"9862"},{"from":"WF9 2XX","to":"WF8 4QD","oNum":"3740003656","dNum":83,"metres":"13705"},{"from":"WF9 2XX","to":"WF8 4QD","oNum":"3740003647","dNum":67,"metres":"13705"},{"from":"WF9 2XR","to":"WF8 4QD","oNum":"3740003636","dNum":59,"metres":"13545"},{"from":"DN14 6XL","to":"DN14 0SB","oNum":"3740003642","dNum":"3650645268","metres":"20763"},{"from":"NG16 4HY","to":"NG15 0DR","oNum":"3740003635","dNum":"3650645258","metres":"13165"},{"from":"DN6 7FB","to":"S73 0UN","oNum":"3740003639","dNum":"3650645255","metres":"18835"},{"from":"LS25 6JH","to":"WF6 1GX","oNum":"3740003652","dNum":"3650645253","metres":"22861"},{"from":"DN3 3FF","to":"DN9 1LB","oNum":"3740003660","dNum":"3650645264","metres":"21343"},{"from":"DN6 7FB","to":"S71 3HQ","oNum":"3740003638","dNum":"3650645278","metres":"19268"},{"from":"S43 4UX","to":"S6 1LY","oNum":"3740003633","dNum":"3650645276","metres":"30365"},{"from":"S43 4UX","to":"S6 1LY","oNum":"3740003651","dNum":"3650645275","metres":"30365"},{"from":"DN14 6XL","to":"HU3 4UW","oNum":"3740003667","dNum":"3650645277","metres":"41502"},{"from":"LS25 6JH","to":"LS7 1PZ","oNum":"3740003653","dNum":"3650645274","metres":"25194"},{"from":"LS25 6JH","to":"BD4 0SG","oNum":"3740003658","dNum":"3650645269","metres":"43788"},{"from":"DN14 6XL","to":"HU7 0XS","oNum":"3740003648","dNum":"3650645265","metres":"49066"},{"from":"NG90 1BS","to":"DE6 1EX","oNum":"3740003643","dNum":"3650645261","metres":"44283"},{"from":"S80 3YY","to":"HD6 1LJ","oNum":"3740003665","dNum":"3650645260","metres":"80026"},{"from":"S80 2RZ","to":"BD8 9HQ","oNum":"3740003634","dNum":"3650645263","metres":"95649"},{"from":"NG90 1BS","to":"HU7 0YW","oNum":"3740003650","dNum":"3650645272","metres":"156190"},{"from":"NG90 1BS","to":"HU12 8TB","oNum":"3740003644","dNum":"3650645267","metres":"160285"},{"from":"NG90 1BS","to":"YO17 6YA","oNum":"3740003645","dNum":"3650645257","metres":"172388"},{"from":"NG90 1BS","to":"YO17 9HG","oNum":"3740003649","dNum":"3650645271","metres":"176868"}],
fs = require('fs'),
_ = require('underscore'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
logPostals = fs.createWriteStream('postals.txt', {'flags' : 'w'}),
db = mongoose.connect('mongodb://localhost/legs');
var Leg = new Schema({
from: {
type: String,
validate: /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i,
trim: true
},
to: {
type: String,
validate: /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i,
trim: true
},
time: Number,
distance: Number
})
var legModel = mongoose.model('Leg', Leg);
var legIndex = 0, armIndex= 0 , inverted = 0, original = 0;
var goCompare = function(){
_.each(firstResult , function(leg){
_.each(firstResult , function(arm){
if(leg.from !== arm.from){
legModel.find({
from: leg.from,
to: arm.to
}, function(err, data) {
if (data.length > 0) {
legModel.find({
from: arm.from,
to : leg.to
},function(e,d){
inverted = data[0].distance + d[0].distance;
original = (leg.metres*1) + (arm.metres*1) ;
if( original > inverted ){
legIndex = firstResult.indexOf(leg);
armIndex = firstResult.indexOf(arm);
firstResult[legIndex] ={
from : leg.from,
to : arm.to,
metres : data[0].distance,
oNum : leg.oNum,
dNum : arm.dNum
};
firstResult[armIndex] = {
from : arm.from,
to : leg.to,
metres : d[0].distance,
oNum : arm.oNum,
dNum : leg.dNum
};
_.each(firstResult , function( item ){
logPostals.write(item.from +'\t'+ item.to +'\t'+item.metres +'\r\n')
})
goCompare();
}else{
}//end of if distance comparison
})//end of function callback from search
}//end of if data...
}//end of err...data
)//end of find
}
})
})
}//end of go compare###############
goCompare();