Assignment 3
Assignment 3
AIM:
A. Write a program to randomly generate k-SAT problems. The program must accept values for k, m the
number of clauses and n the number of variables. Each clause of length k must contain distinct variables
or their negations. Instances generated by this algorithm belong to the fixed clause length model of SAT
and are known as the Uniform Random k-SAT problem.
B. Write programs to solve a set of Uniform Random 3-SAT problems for different combinations of m and n
and compare their performance. Try the
1. Hill Climbing algorithm,
2. Beam Search with beam width of 3 and 4 ,
3. Variable Neighbourhood Descent with 3 Neighbourhood functions and
4. Tabu Search with neighbourhood functions changing 2 bits at a time.
THEORY:
The Boolean satisfiability (SAT) problem is defined as follows:
Given a Boolean formula, check whether an assignment of Boolean values to the propositional variables in the
formula exists, such that the formula evaluates to true.
If such an assignment exists, the formula is said to be satisfiable; otherwise, it is unsatisfiable. For a formula
with m variables, there are 2m possible truth assignments. The conjunctive normal form (CNF) (X1 ∨ X2) ∧
(X3 ∨ X4) ∧ · · · ∧ (Xn−1 ∨ Xn) is most frequently used for representing Boolean formulas, where ¬∀Xi are
independent. In CNF, the variables of the formula appear in literals (e.g., x) or their negation (e.g., ¬x (logical
NOT ¬)). Literals are grouped into clauses, which represent a disjunction (logical OR ∨) of the literals they
contain. A single literal can appear in any number of clauses. The conjunction (logical AND ∧) of all clauses
represents a formula. Several algorithms are known for solving the 2 - satisfiability problem; the most efficient
of them take linear time.
The K-satisfiability (Ksat) problem deals with an ensemble of N Boolean variables, submitted to M constraints.
Each constraint is in the form of an ’OR’ function of K variables in the ensemble (or their negations), and the
problem is to know whether there exists one configuration of the variables (among the 2N possible ones) which
satisfies all constraints.
The Ksat problem for K ≥ 3 is a central problem in combinatorial optimization: it was the first problem to be
shown NP-complete, and an efficient algorithm for solving Ksat in its worst case instances would immediately
lead to other algorithms for solving efficiently thousands of different hard combinatorial problems.
1
The study of random Ksat problems, where the clauses are chosen randomly, is also interesting from the
viewpoint of optimization. In practice, algorithms which are used to solve real-world NP-complete problems
display a huge variability of running times, ranging from linear to exponential, when the parameters (e.g. the
number of clauses) are changed.
Beam Search
● The Beam Search algorithm holds k number of states at any given time. It has a hyper parameter named
beam size. At the start, these states are generated randomly. The successors of these k states are
computed with the help of objective function. If any of these successors is the maximum value of the
objective function, then the algorithm stops.
● Otherwise the (initial k states and k number of successors of the states = 2k) states are placed in a pool.
The pool is then sorted numerically. The highest k states are selected as new initial states. This process
continues until a maximum value is reached.
Tabu Search
● Tabu search is a metaheuristic local heuristic search method used for mathematical optimization and to
explore the solution space beyond local optimality. Local search methods have the tendency to be stuck
in suboptimal regions but TS enhances the performance of these techniques by prohibiting already
visited solutions. Its use of adaptive memory creates a more flexible search behavior. The master loop
2
deals with all the integer variables using TS, and the inner loop minimizes each NLP sub problems using
a gradient-based method.
● TS generates a series of different sets of integer variables which are called candidates. These candidates
differ by one or more bits from the current best solution and are not included in the tabu list. NLP
subproblems are then solved for each candidate using the gradient-based method. Among all the new
candidates, the one with the best objective value is selected and treated as a seed to generate the next
generation of candidates. To prevent being trapped into local optima, a tabu list is built to forbid the
selection of already visited solutions and their neighborhoods.
CODE:
Part A:-
#include<bits/stdc++.h>
#include <random>
using namespace std;
3
cout<<"\n-----Random k-SAT Problem Generator----\n\n";
cout<<"Enter the length of clause k = ";
cin>>k;
cout<<"Enter the number of clauses m = ";
cin>>m;
cout<<"Enter the number of variables n (max 26) = ";
cin>>n;
cout<<"Enter the number of problems to be generated = ";
cin>>p;
for(int i=0;i<p;i++){
cout<<"\nProblem "<<i+1<<endl;
generateProblem(k,m,n);
OUTPUT:
4
5
CODE:
Part B:-
kSAT.h
#include<bits/stdc++.h>
using namespace std;
#ifndef KSAT_H
#define KSAT_H
class kSAT{
private:
vector<int> createClause();
void printClause(vector<int> a);
public:
int k, n, m;
vector<vector<int> >kSATformula;
6
kSAT(int k, int n, int m);
void createFormula();
void printkSAT();
vector<int> generateAssignment();
int calScore(vector<int> a);
};
#endif
kSAT.cpp
#include<bits/stdc++.h>
#include"kSAT.h"
using namespace std;
7
void kSAT:: createFormula(){
vector<int> a;
for(int i=0;i<m;i++){
a = createClause();
kSATformula.push_back(a);
}
}
vector<int> kSAT::generateAssignment(){
vector<int> a;
for(int i=0; i<n;i++){
float random = (float) rand()/RAND_MAX;
if(random>0.5)
a.push_back(1);
else
a.push_back(0);
}
return a;
}
algorithms.h
#include<bits/stdc++.h>
#include"kSAT.h"
using namespace std;
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
#endif
algorithms.cpp
#include<bits/stdc++.h>
#include"kSAT.h"
#include"algorithms.h"
#include"assignment.h"
using namespace std;
assignment.h
#include<bits/stdc++.h>
#include"kSAT.h"
using namespace std;
#ifndef ASSIGNMENT_H
#define ASSIGNMENT_H
assignment.cpp
#include<bits/stdc++.h>
#include"kSAT.h"
#include"assignment.h"
using namespace std;
void printVector(vector<int> a){
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<"\n";
}
return 0;
}
16
OUTPUT:
17
18
19
CONCLUSION:
In this assignment, I implemented a uniform random k-SAT generator. I also understood the algorithms
hill climbing, Variable Neighborhood descent, beam search and tabu search in a better way by learning
to implement them practically to solve the random 3-SAT problem, and also by comparing their
performance on the basis of scores.
20