Chris Pollett > Students > Yunxuan

    Print View

    [Bio]

    [Blog]

    [First Proposal]

    [Final Step of Shor's and Grover's Algorithms]

    [jQuantum QFT]

    [My Quantum Circuits]

    [Three Models]

    [Threshold of Error Correction]

    [jQuantum Manual]

    [Quantum State and LH]

    [QHC Scientists]

    [LH and Tensor Networks]

    [k-LH is QMA-complete]

    [Deutch Josza Algorithm]

    [Semester Report]

    [Second Proposal]

    [SolveQ Algorithm: 2-SAT]

    [Random-kSAT-Generator: Version 1]

    [Freezing Point Experiment]

    [Random-kQSAT-Generator: Version 2]

    [Random-kQSAT-Generator: Version 3]

    [Antiferromagnetic Heisenberg Model]

    [Ising Model]

    [Random-kQSAT-Generator: Version 4]

    [Random-kQSAT-Generator: Version 5]

    [Random-kQSAT-Generator: Version 6]

    [SolveQ Algorithm: 2-QSAT]

    [Thesis]

























This program generates 50 3-SAT problems. Each problem has 15 clauses and the problems use a total of 7 variables. As you can see, you can set different values of clause size, number of variables, number of clauses, and number of problems for yourself. This program will help me investigate the freezing point of barely solvable problems, as I vary the ratio between number of variables and number of clauses, and plot the number of problems that were successfully satisfied out of 50.


import java.io.*;

import static java.lang.Math.*;

import java.util.*;


class randomSatgenerator {
	private int clausesize = 3;
	private int numvariable = 7;
	private int numclause = 31;
	private int numinstances = 50;
	private ArrayList<String> chain = new ArrayList<String>();
	private ArrayList<String> stack = new ArrayList<String>();
	private int success= 0;
	
	public static void main (String [] args){
		randomSatgenerator gen = new randomSatgenerator();
		/*String line="";
		System.out.println("Enter Clause Size->");
		 try{
			  BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
			  line = in.readLine();
			  gen.clausesize=Integer.parseInt(line);
			  
		  }catch(Exception ex){
			  System.out.println("Bad Input");
		  }
		 System.out.println("Enter number of variables->");
		 try{
			  BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
			  line = in.readLine();
			  gen.numvariable=Integer.parseInt(line);
			  
		  }catch(Exception ex){
			  System.out.println("Bad Input");
		  }
		 System.out.println("Enter number of clauses in each instance->");
		 try{
			  BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
			  line = in.readLine();
			  gen.numclause=Integer.parseInt(line);
			  
		  }catch(Exception ex){
			  System.out.println("Bad Input");
		  }
		 System.out.println("Enter number of instances->");
		 try{
			  BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
			  line = in.readLine();
			  gen.numinstances=Integer.parseInt(line);
			  
		  }catch(Exception ex){
			  System.out.println("Bad Input");
		  }*/
		 
		 gen.generate();
	}
	public void generate(){
		ArrayList<Integer>temp = new ArrayList<Integer>();
		ArrayList<ArrayList<Integer>> temp2 = new ArrayList<ArrayList<Integer>>();
		boolean []taken = new boolean [numvariable];
		
		for(int i=0; i<taken.length; i++){
			taken[i] = false;
		}
		for (int k = 0; k<numinstances; k++) {
			for (int j = 0; j<numclause; j++) {
				int v;
				for(int i = 0; i<clausesize; i++){
					v = (int)(Math.random()*numvariable)+1;
					while(taken[v-1] == true){
						v = (int)(Math.random()*numvariable)+1;					
					}
					taken[v-1] = true;
					
					//int a = (int)(Math.random()*numvariable)+1;
					temp.add(new Integer(v));
				}
				for(int i = 0; i < taken.length; i++){
					taken[i] = false;
				}
				double b = Math.random()*pow(2,clausesize);
				int c = (int)b;
				temp.add(new Integer(c));
				temp2.add(temp);
				temp = new ArrayList<Integer>();
			}
			Solve(temp2);
			removesolution();
			if(chain.size()>0){
				if(chain.size()>6)
					success++;
				for(int i=0; i<chain.size(); i++){
					System.out.println(chain.get(i));
				}
				System.out.println();
				System.out.println();
			}
			
			chain = new ArrayList <String> ();
			stack = new ArrayList <String> ();
		}
		System.out.println(success);
	}
	public void Solve(ArrayList<ArrayList<Integer>> temp2){
		int max = (int)pow(2,numvariable);
		boolean noClause = false;
		for(int j = 0; j<max; j++){
			String temp3 = Tobinary(j,"");
			temp3 = reverseString(temp3);
			if (temp3.length()<numvariable) {
				temp3 = addZero(temp3, numvariable);
			}
			chain.add(temp3);
		}
		while(temp2.size()>0){
			int control = temp2.get(0).size()-1;
			int [] removepat = new int[control];
			int [] removepatb = new int[control];
			int [] a1 = new int[control];
			String[] str = new String[control];
		
			for(int i = 0; i<clausesize; i++){
				removepat[i]=Integer.parseInt(""+temp2.get(0).get(i))-1;
			}
			
			String st = Tobinary(temp2.get(0).get(control),"");
			st = reverseString(st);
			st = addZero(st,clausesize);
			for(int i = 0; i<clausesize; i++){
				//removepatb[clausesize-i-1] = Integer.parseInt(""+st.charAt(i));
				removepatb[i] = Integer.parseInt(""+st.charAt(i));
			}//get the 3 letter code in messy order
			
			sort(removepat, removepatb);//sort the code order to var index 1,2,3...
			/*for (int d = 0; d<clausesize-1; d++) {
				for(int i = 1; i<clausesize; i++){
					if(removepat[i]==removepat[d] && removepatb[i]!=removepatb[d]){
						noClause = true;
						break;
					}				
				}
				if (noClause){
					break;
				}
			}*/
			for(int k = 0; k<chain.size(); k++){
				if (noClause){
					break;
				}
				String temp = chain.get(k); 
				for(int j=0; j<clausesize; j++){
					str[j] = ""+temp.charAt(temp.length()-removepat[clausesize-j-1]-1);
				}//the solution test case put in String array.
				
				for(int i = 0; i<clausesize; i++){
					a1[i] = Integer.parseInt(str[i]);
				}// the solution test case put in int array.
				int count = 0;
				for(int i = 0; i<clausesize; i++){
					if(a1[i]==removepatb[clausesize-i-1]){
						count++;
					}
				}// if 3 letter code match with solution letter code then add to stack.
				if(count==clausesize){
					stack.add(temp); //stack contains all the bad solution entries
									// that need to be removed.
				}
			}	
			temp2.remove(0);
			noClause = false;
		}
	}
	public String addZero(String s, int l){
		int b = l-s.length();
		for(int i = 0; i<b; i++){
			s = "0"+""+s;
		}
		return s;
	}
	public String reverseString(String st){
		String temp = "";
		for(int i = st.length()-1; i>=0; i--){
			temp = temp+st.charAt(i);
		}
		return temp;
	}
	public void sort(int [] a, int [] b){
		int temp = 0;
		int temp2;
		int temp3;
		for(int i = 0; i<a.length-1; i++){
			temp = i;
			for(int j = i+1; j<a.length; j++){
				if(a[j]<a[temp]){
					temp = j;
				}
			}
			temp2 = a[temp];
			a[temp] = a[i];
			a[i] = temp2;
			
			temp3 = b[temp];
			b[temp] = b[i];
			b[i] = temp3;
		}
	}
	public String Tobinary(int decimal, String path){
		int remainder;
		if(decimal<=1){
			path = path+decimal;
			return path;
		}
		remainder = decimal%2;
		path = path+remainder;
		return Tobinary(decimal>>1, path);
	}
	
	public void removesolution(){
		for(int i=stack.size()-1; i>=0; i--){
			String a = stack.get(i);
			chain.remove(a);
		}
	}
	public void printsolution(){
		for (int i = 0; i<chain.size(); i++){
			System.out.println(chain.get(i));
		}
	}
}