Skip to content

Commit

Permalink
solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Schwarzentruber committed Feb 14, 2024
1 parent 0ff00ad commit b8c913f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
9 changes: 8 additions & 1 deletion guessRhythm/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ortools.linear_solver import pywraplp
import json
import sys
import func_timeout


###
Expand Down Expand Up @@ -91,7 +92,13 @@ def main():
arg = sys.argv[1].replace('\\', '')
print(arg)
input = json.loads(arg)
print(solve(input["dhats"], input["possibleDurations"], input["signature"]))
try:
solution = func_timeout.func_timeout(2, solve, args=(input["dhats"], input["possibleDurations"], input["signature"]))
#solve(input["dhats"], input["possibleDurations"], input["signature"])
print(solution)
except Exception as e:
print(e)
print(0)

if __name__ == "__main__":
main()
39 changes: 19 additions & 20 deletions js/rhythmguess.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ function isEq(a, b) {
}


const solve = window.location.href.indexOf("github") ? solveQuickAndDirty : solveWithLP//solveQuickAndDirty;
/**
*
* @param {*} dhats
Expand All @@ -369,7 +370,7 @@ function isEq(a, b) {
* @returns array of durations, or 0 if no solution
* @description it calls the LP solver in Python (server side)
*/
async function solve(dhats, possibleDurations, signatureValue) {
async function solveWithLP(dhats, possibleDurations, signatureValue) {
var url = '/your/url';
var formData = new FormData();
const strJSON = JSON.stringify({ dhats, possibleDurations, signature: signatureValue });
Expand All @@ -381,12 +382,26 @@ async function solve(dhats, possibleDurations, signatureValue) {
console.log(txt)
const array = JSON.parse(lines[lines.length - 2]);

if(array == "0")
if (array == "0")
throw "no solution"
return array;
}


/**
* up([1, 2, 4, 5,3], [3, 4])
* */
function up(durations, bests) {
return durations.sort((a, b) => {
if ((bests.indexOf(a) >= 0) && (bests.indexOf(b) >= 0))
return bests.indexOf(a) >= bests.indexOf(b);
else if (bests.indexOf(a) >= 0)
return -1;
else if (bests.indexOf(b) >= 0)
return 1;
else
return 0;
})
}


async function solveQuickAndDirty(dhats, D, signature) {
Expand All @@ -405,28 +420,12 @@ async function solveQuickAndDirty(dhats, D, signature) {
const newD = D;
newD[i] = [...newD[i]];

function up(durations, bests) {
return durations;
return durations.sort((a, b) => {
if ((bests.indexOf(a) >= 0) && (bests.indexOf(b) >= 0))
return bests.indexOf(a) <= bests.indexOf(b);
else if (bests.indexOf(a) >= 0)
return -1;
else if (bests.indexOf(b) >= 0)
return 1;
else
return 0;
})
}



for (j = 0; j < i; j++)
if (dhats[j] == dhats[i]) {
console.log("up")
console.log(solution[j])
console.log(D[i])
newD[i] = up(newD[i], [solution[j]]);
console.log(newD[i])
}
else if (dhats[i] > dhats[j])
newD[i] = up(newD[i], D[i].filter((d) => d >= solution[j]));
Expand Down

0 comments on commit b8c913f

Please sign in to comment.