javascript - Optimal MLB lineup using Knapsack variant -


i writing program find best possible mlb lineup using knapsack solution. pass in player data has players calculated value , salary. salary "weight" in terms of being knapsack problem.

my problem not able select players, rather select optimal lineup. choosing pitcher, center, first baseman, second baseman, third baseman, short stop, , 3 outfielders. can successfully. want "weight" 36,000, choosing lineup total of 21,000.

here knapsack code:

calculatelineup.prototype.findoptimallineup = function(data, capacity) {   var items = data.data;   var idxitem   = 0,       idxcapspace = 0,       idxposition = 0,       oldmax    = 0,       newmax    = 0,       numitems  = items.length,       weightmatrix  = new array(numitems+1),       keepmatrix    = new array(numitems+1),       positionarray = new array("p", "c", "1b", "2b", "3b", "ss", "of", "of", "of"),       solutionset   = [];    // setup matrices   for(idxitem = 0; idxitem < numitems + 1; idxitem++){     weightmatrix[idxitem] = new array(capacity+1);     keepmatrix[idxitem]   = new array(capacity+1);   }    // build weightmatrix [0][0] -> [numitems-1][capacity-1]   (idxitem = 0; idxitem <= numitems; idxitem++){     (idxcapspace = 0; idxcapspace <= capacity; idxcapspace++){              // fill top row , left column zeros           if (idxitem === 0 || idxcapspace === 0){             weightmatrix[idxitem][idxcapspace] = 0;           }            // if item fit, decide if there's greater value in keeping it,           // or leaving           else if (items[idxitem-1]["salary"] <= idxcapspace){             newmax = items[idxitem-1]["value"] + weightmatrix[idxitem-1][idxcapspace-items[idxitem-1]["salary"]];             oldmax = weightmatrix[idxitem-1][idxcapspace];              // update matrices             if(newmax > oldmax ){                weightmatrix[idxitem][idxcapspace]  = newmax;               keepmatrix[idxitem][idxcapspace]    = 1;              }             else{               weightmatrix[idxitem][idxcapspace]  = oldmax;                keepmatrix[idxitem][idxcapspace]    = 0;             }           }            //else, item can't fit; value , weight same before            //else              //weightmatrix[idxitem][idxcapspace] = weightmatrix[idxitem-1][idxcapspace];     }   }    // traverse through keepmatrix ([numitems][capacity] -> [1][?])   // create solutionset   idxcapspace = capacity;   idxitem   = numitems;   for(idxitem; idxitem < capacity; idxitem--){     if(keepmatrix[idxitem][idxcapspace] === 1 && !this.filledallpositions(items[idxitem - 1]["position"])){       solutionset.push(items[idxitem - 1]);       idxcapspace = idxcapspace - items[idxitem - 1]["salary"];     }   }   return {"maxvalue": weightmatrix[numitems][capacity], "set": solutionset}; }; 

am making blatant mistake not seeing, or logic off?

you checking solutionset right? logic accept positions not included in knapsack logic, means solutionset filter on top of knapsack solution. did arrive @ right knapsack solution, because, on top of solution, checking if position filled, few items eliminated solutionset (items fighting same position) , total weight decreased.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -