|
1 | 1 | import LineBasedGrader from "./lineGrader"; |
| 2 | +import { DiGraph } from "jsnetworkx/node/classes"; |
| 3 | +import { hasPath } from "jsnetworkx/node/algorithms/shortestPaths/generic"; |
| 4 | + |
| 5 | +function graphToNX(answerBlocks) { |
| 6 | + var graph = new DiGraph(); |
| 7 | + for (let block of answerBlocks) { |
| 8 | + let line = block.lines[0]; // FIXME assume each block only has one line, won't work with adaptivity |
| 9 | + graph.addNode(line.tag); |
| 10 | + for (let line2 of line.depends) { |
| 11 | + // the depends graph lists the *incoming* edges of a node |
| 12 | + graph.addEdge(line2, line); |
| 13 | + } |
| 14 | + } |
| 15 | + return graph; |
| 16 | +} |
| 17 | + |
| 18 | +function isVertexCover(graph, vertexCover) { |
| 19 | + for (let edge of graph.edges()) { |
| 20 | + if (!(vertexCover.has(edge[0]) || vertexCover.has(edge[1]))) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + return true; |
| 25 | +} |
| 26 | + |
| 27 | +function allSubsets(arr) { |
| 28 | + let subsets = {}; |
| 29 | + for (let i = 0; i <= arr.length; i++) { |
| 30 | + subsets[i] = []; |
| 31 | + } |
| 32 | + for (let i = 0; i < Math.pow(2, arr.length); i++) { |
| 33 | + let bin = i.toString(2); |
| 34 | + while (bin.length < arr.length) { |
| 35 | + bin = "0" + bin; |
| 36 | + } |
| 37 | + let subset = new Set(); |
| 38 | + for (let j = 0; j < bin.length; j++) { |
| 39 | + if (bin[j] == '1') { |
| 40 | + subset.add(arr[j]); |
| 41 | + } |
| 42 | + } |
| 43 | + subsets[subset.size].push(subset); |
| 44 | + } |
| 45 | + return subsets; |
| 46 | +} |
2 | 47 |
|
3 | 48 | export default class DAGGrader extends LineBasedGrader { |
4 | 49 |
|
5 | | - inverseLISIndices(arr) { |
6 | | - // TODO implement this properly for the DAG grader so that it is the shortest edit distance |
| 50 | + |
| 51 | + inverseLISIndices(answerBlocks, solution) { |
| 52 | + // For more details and a proof of the correctness of the algorithm, see the paper: https://arxiv.org/abs/2204.04196 |
| 53 | + let graph = graphToNX(answerBlocks); |
| 54 | + console.log(allSubsets([1,2,3])) |
| 55 | + |
| 56 | + let seen = new Set(); |
| 57 | + let problematicSubgraph = new DiGraph(); |
| 58 | + let distractors = []; |
| 59 | + for (let block of solution) { |
| 60 | + |
| 61 | + if (block.distractor) { |
| 62 | + distractors.push(block); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + for (let block2 of seen) { |
| 67 | + let problematic = hasPath(graph, block, block2); |
| 68 | + if (hasPath(graph, block, block2)) { |
| 69 | + problematicSubgraph.addEdge(block, block2); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + seen.add(block); |
| 74 | + } |
| 75 | + |
| 76 | + console.log(problematicSubgraph); |
| 77 | + |
| 78 | + if (problematicSubgraph.numberOfNodes() == 0) { |
| 79 | + // just return the indices of the distractors, I guess??? |
| 80 | + } else { |
| 81 | + let mvc = null; |
| 82 | + let subsets = allSubsets(problematicSubgraph.nodes()); |
| 83 | + for (let i = 0; i <= problematicSubgraph.numberOfNodes(); i++) { |
| 84 | + for (let subset of subsets[i]) { |
| 85 | + if (isVertexCover(problematicSubgraph, subset)) { |
| 86 | + mvc = subset; |
| 87 | + console.log(mvc) |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + if (mvc != null) { |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // TODO implement the algorithm properly for the DAG grader so that it is the shortest edit distance |
7 | 98 | // to ANY of the correct solutions instead of just to the model solution |
8 | | - return super.inverseLISIndices(arr) |
| 99 | + return super.inverseLISIndices(answerBlocks, solution) |
9 | 100 | } |
10 | 101 |
|
11 | 102 | checkCorrectOrdering(solutionLines, answerLines) { |
|
0 commit comments