Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This file should be placed at the node_modules sub-directory of the directory where you're
  3.  * executing it.
  4.  *
  5.  * Written by Fernando Castor in November/2017.
  6.  */
  7.  readFormula("hole1.cnf")
  8. exports.solve = function(fileName) {
  9.   let formula = propsat.readFormula(fileName)
  10.   let result = doSolve(formula.clauses, formula.variables)
  11.   return result // two fields: isSat and satisfyingAssignment
  12. }
  13.  
  14. // Receives the current assignment and produces the next one
  15. function nextAssignment(currentAssignment) {
  16.   // implement here the code to produce the next assignment based on currentAssignment.
  17. var temparray=currentAssignment.join('')
  18. var b2=parseInt(temparray,2)
  19. b2++
  20. var b2=parseInt(b2,2)
  21. b2=b2.toString(2)
  22. b2=b2.split('')
  23. for (var f=0;b2.length>f;f++){
  24.   b2[f] = parseInt(b2[f]);
  25. }
  26. for(var jk=0;b2.length>jk;jk++){
  27.   var b2k = b2.length - 1 - jk
  28.   var temp2k = temparray.length - 1 - jk
  29.   array[temp2k]=b2[b2k]
  30. }
  31. var newAssignment = temparray
  32.  
  33.   return newAssignment
  34. }
  35.  
  36.  
  37. function doSolve(clauses, assignment) {
  38.   let isSat = false
  39.     var k=0
  40.      while((!isSat) &&  k < Math.pow(2, array2.length)-1){        
  41.            var copiaclauses=new Array()
  42.            for(var t=0;array.length>t;t++){
  43.              copiaclauses[t]=new Array()
  44.              
  45.              for(var d=0;array[t].length>d;d++){  
  46.                 for(var a=0;array2.length>a;a++)          
  47.                 if (Math.abs(array[i][j]) == array2[m])
  48.                     if (array[t][d] > 0) {
  49.                     copiaclauses[t].push(array2[a])  
  50.                 }   else
  51.                     copiaclauses[t].push(!array2[a])
  52.              }
  53.           }
  54.  
  55.     // does this assignment satisfy the formula? If so, make isSat true.
  56.  var checkSat = true;
  57.     // if not, get the next assignment and try again.
  58.  
  59.     for(var t = 0; t < copiaclauses.length; t++){
  60.       var haveTrue = false;
  61.       for(var d = 0;d < copiaclauses[i].length && !haveTrue; d++){
  62.         if(copiaclauses[t][d] == 1){
  63.           copiaclauses[t] = 1
  64.           haveTrue = true
  65.         }
  66.       }
  67.       if(!haveTrue){
  68.         copiaclauses[i] = 0
  69.         checkSat = false;
  70.       }
  71.     }
  72.  
  73.     if(checkSat){
  74.       isSat = true
  75.     } else{
  76.     assignment = nextAssignment(assignment)
  77.     }
  78.   let result = {'isSat': isSat, satisfyingAssignment: null}
  79.   if (isSat) {
  80.     result.satisfyingAssignment = array2
  81.   }
  82.   return result
  83. }
  84.  
  85. function readFormula(fileName) {
  86.   // To read the file, it is possible to use the 'fs' module.
  87.   // Use function readFileSync and not readFile.
  88.   var fs = require('fs')
  89.   // First read the lines of text of the file and only afterward use the auxiliary functions.
  90.   let text = fs.readFileSync(fileName,"utf8").split('\n') // = ...  //  an array containing lines of text extracted from the file.
  91.   let clauses = readClauses(text)
  92.   let variables = readVariables(clauses)
  93.  
  94.   // In the following line, text is passed as an argument so that the function
  95.   // is able to extract the problem specification.
  96.   let specOk = checkProblemSpecification(text, clauses, variables)
  97.  
  98.   let result = { 'clauses': [], 'variables': [] }
  99.   if (specOk) {
  100.     result.clauses = clauses
  101.     result.variables = variables
  102.   }
  103.   return result
  104. } function readClauses (text){
  105.   var array=new Array()
  106.   for(var i=0;text.length>i;i++){
  107.       if(text[i].charAt(0) != 'c' && text[i].charAt(0) !='p'){
  108.          array.push(text[i].split(" "))
  109.       }
  110.   }
  111.  
  112.   return array
  113. } function readVariables (clauses){
  114.   var quantidade=1
  115.   var array2=new Array()
  116.   for(var j=0;clauses.length>j;j++){
  117.     for(var x=0;clauses[j].length>x;x++){
  118.       if(clauses[j][x]==Math.abs(quantidade)){
  119.         quantidade++
  120.         j=0
  121.         x=0
  122.         array2.push(0)
  123.       }
  124. }
  125.     }
  126.    
  127.     return array2
  128.   } function checkProblemSpecification(text, clauses,variables){
  129.     for (int y=0;text.length>y;y++){
  130.       if(text[y].charAt(0)=='p'){
  131.       text[y].split(" ")
  132.        if(text[y][4]==array.length && text[y][3]=array2.length){
  133.         return true
  134.        }else
  135.        return false
  136.     }
  137.       }
  138.    
  139.  
  140.  
  141.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement