Last active
July 19, 2016 20:11
-
-
Save sabotuer99/ec142f373f3ec76fcfd0ce823472e98a to your computer and use it in GitHub Desktop.
Truck Positon Dynammic Programming in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Analysis') | |
.addItem('Generate Positions', 'genPositions') | |
.addToUi(); | |
} | |
function genPositions() { | |
var data = SpreadsheetApp.getActive().getSheetByName("data").getDataRange().getValues(); | |
var positions = data.length; | |
var girders = data[0].length; | |
var state = {} | |
state.data = data; | |
state.combos = []; | |
state.max = positions; | |
runCombine(state); | |
var combos = state.combos; | |
var resultsPage = SpreadsheetApp.getActive().getSheetByName("Results"); | |
resultsPage.clear(); | |
if(combos.length * combos[0].length < 2000000){ | |
resultsPage.getRange(1,1,combos.length,combos[0].length).setValues(combos); | |
} else { | |
resultsPage.getRange(1,1,1,1).setValues([["Results saved to results(x).csv files in Drive"]]); | |
saveCSV(combos); | |
} | |
} | |
function saveCSV(combos){ | |
console.log("Started csv generation"); | |
var folder = DriveApp.getRootFolder(); | |
var content = ""; | |
var h = combos.map(function (a) { return a.join(",") + "\n";}); | |
for(var i = 0; i < h.length; i += 50000){ | |
var rows = h.slice(i, i + 50000); | |
content = rows.join(""); | |
folder.createFile("results("+ i +").csv", content); | |
} | |
} | |
function runCombine(state){ | |
var end = state.max; | |
var D = []; | |
for(var i = end; i >= 1; i--){ | |
var baseRow = state.data[i - 1].slice(); | |
baseRow.unshift(i.toString()); | |
baseRow.unshift(1); //this is the lane count, or "depth" | |
D[i] = []; | |
D[i].push(baseRow); | |
for(var j = i + 12; j <= end; j++){ | |
for(var x = 0; x < D[j].length; x++){ | |
var row = D[j][x]; | |
var name = baseRow[1] + "_" + row[1]; | |
var depth = row[0] + 1; | |
var newRow = [depth, name]; | |
for(var k = 2; k < row.length; k++) | |
newRow.push(row[k] + baseRow[k]); | |
D[i].push(newRow); | |
} | |
} | |
console.log(i + ": " + D[i].length); | |
} | |
D.forEach(function (group) { | |
group.forEach(function (combo){ | |
var name = combo[1]; | |
var mpCombo = [name]; | |
for(var m = 2; m < combo.length; m++) | |
mpCombo.push(combo[m] * mpFactor(combo[0])); | |
state.combos.push(mpCombo); | |
}); | |
}); | |
} | |
function mpFactor(depth){ | |
if(depth >= 4) | |
return 0.65; | |
if(depth == 3) | |
return 0.85; | |
if(depth == 2) | |
return 1.2; | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment