Monday, March 23, 2009

Artificial Intelligence / Satisfiability of CNF Formulas (1)

Write a program to generate random CNF formulas. Your program should receive as parameters n, the number of variables, k, the number of literals per clause, and m, the number of clauses. Make sure your program does not generate clauses with repeated variables.

MATLAB CODE

   1: clear all; close all; clc



   2: n=input('Number of variables n=');



   3: k=input('Number of literals per clause k=');



   4: m=input('Number of clauses m=');



   5: if k>(2*n) 



   6:     errordlg('number of literals per clause must be less or equal to 2 times n');



   7: else



   8:     xn=zeros(1,k);



   9:     fprintf('\n\n');



  10:     for o=1:m



  11:         if o==1



  12:             fprintf('  (');



  13:         else



  14:             fprintf('\n^ (');



  15:         end



  16:         for p=1:k



  17:             xnt=floor(rand*n);



  18:             while any(xn==xnt)



  19:                 xnt=round(rand*n);



  20:             end



  21:             xn(p)=xnt;            



  22:             if p~=1 



  23:                 fprintf(' v ')



  24:             end



  25:             if round(rand)==0



  26:                 fprintf('!');



  27:             end



  28:             fprintf('x%i',xnt);



  29:         end        



  30:         fprintf(')')



  31:     end



  32:     fprintf('\n\n');



  33: end


No comments: