1 Sat Question Description
The satisfying problem of the combined paradigm (CNF) in propositional logic (SAT) is the core problem of contemporary theoretical computer science, and it is a typical NP-complete problem. Before you define a satisfying problem SAT, first introduce some logic symbols.
A SAT question is: Is there a set of truth assignments for a given CNF that makes a true for a propositional variable. Obviously, if a is true, there must be a proposition variable of 1 (TRUE) in each clause of CNF.
2 Genetic algorithms
Genetic algorithms are similar to natural evolution, which solves problems by finding good chromosomes by genes that act on chromosomes. Similar to nature, genetic algorithms have no knowledge of the problem itself, it only needs to evaluate each chromosome produced by the algorithm, and select chromosomes based on the adaptive values, so that the well-adapted chromosomes have more breeding opportunities. In the genetic algorithm, a number of numerical codes for solving problems are generated randomly, that is, chromosomes, which form the initial group, and a numerical evaluation of each individual by the fitness function, the elimination of low-fitness individuals, the selection of highly adaptive individuals to participate in the genetic operation, after the genetic operation of the individual set to form the next generation of new species. The next round of evolution for this new population.
Here is the idea of genetic algorithm:
(1) Initializing groups;
(2) Calculating the fitness value of each individual on the population;
(3) According to the individual fitness value determined by a rule of choice will enter the next generation of individuals;
(4) Cross operation according to the probability PC;
(5) Mutation operation according to probability PC;
(6) If a stop condition is not met, then move to step (2) or enter (7).
(7) The optimal solution of chromosome as a problem in the output population with the optimum degree of fitness value.
The program's stopping conditions are the simplest of the following two: the completion of the pre-given evolutionary algebra is stopped; the optimal individuals in the population have no improvement in successive generations or the average fitness is stopped for several successive generations without improvement.
3 Experimental Results
The sample is 1.txt, the number of arguments n=30, the number of clauses m=129, the number of clauses that can be satisfied is 127, the run time is 00.0000 seconds, the result is as follows:
4 C + + implementation
//GA3SAT.cpp: Defines the entry point of the console application. ///*********************************-----------------------------------Genetic algorithm to solve 3SAT problems (C + + Implementation code)-----------------------------------Author: Pastoral, date:2014 email:[email protected] **************************** ******/ #include "stdafx.h"#include <iostream>#include <fstream>#include <time.h>#include <math.h>using namespace STD;#define ANSSIZE //SAT clause maximum length #define Popusize //Population size #define GENERATE //Evolutionary algebra #define PM 0.02 //compile probability intBestgenes_sat;intBestgenes[anssize];intSatgenes[popusize][anssize];intScore[popusize];int**x;intn= -;//variable element numberintm=430;//number of clauses//int Randomi (int a, int b)// {//int c=rand ()% (b-a+1) +a;//return C;// }DoubleRANDOMF (DoubleADoubleb) {Doublec = (Double) (rand ()% (int) B (int) a) + A + (Double) (rand ()/(Rand_max +1.0));returnC;}voidJohnson (intN) { for(intj =0; J<popusize; J + +) { for(inti =0; I<n; i++) {if((Double) rand ()/(Rand_max) >0.5) {Satgenes[j][i] =1; }Else{Satgenes[j][i] =0; } } }}voidSatisfied (intm) {intCount =0;intI,j,k; for(k =0; K<popusize; k++) {count =0; for(i =0; I<m; i++) { for(j =0; j<3; J + +) {if(x[i][j]<0) {intTemp= (-1) *x[i][j];if(satgenes[k][temp-1]==0) {count++; Break; } }Else if(x[i][j]>0) {if(satgenes[k][x[i][j]-1]==1) {count++; Break; }}}} Score[k] = count; }}voidFindbestgene (intN) {intBestnum;intBestscore = Int_min;intI for(i =0; I<popusize; i++) {if(Bestscore<score[i]) {bestnum = i; Bestscore = Score[i]; }} Bestgenes_sat = Bestscore; for(i =0; I<n; i++) {bestgenes[i] = Satgenes[bestnum][i]; }}voidAdaptintN) {intImax,temp,i,j,k; for(i =0; i<popusize/2; i++) {imax = i; for(j = i+1; j<popusize;j++) {if(Score[j]>score[imax]) {IMAX = j; }} Temp=score[i]; Score[i]=score[imax]; Score[imax]=temp; for(intK =0; K<n; k++) {temp = satgenes[i][k]; SATGENES[I][K]=SATGENES[IMAX][K]; Satgenes[imax][k]=temp; } } for(i =0; i<popusize/2; i++) {score[popusize/2+i] = Score[i]; for(k =0; K<n; k++) {satgenes[popusize/2+I][K]=SATGENES[I][K]; } }}voidCrossintN) {intCrotype,start,length,i,j,k,t;intTemp for(k =0; k<popusize/2; k++) {i = rand ()% (popusize/2); j = rand ()% (popusize/2); while(i = = j) {j = rand ()% (popusize/2); } start = rand ()%n; length = rand ()% (N-start); Crotype = rand ()%3;Switch(Crotype) { Case 0: Case 1: for(t = start;t< (start+length); t++) {temp = satgenes[i][t]; SATGENES[I][T] = satgenes[j][t]; SATGENES[J][T] = temp; } Break; Case 2: for(t = start;t< (start+length); t++) {if(Satgenes[i][t]+satgenes[j][t] = =1) {Satgenes[i][t] =0; SATGENES[J][T] =1; }Else{Satgenes[i][t] =1; SATGENES[J][T] =0; } } Break; } }}voidMutate (intN) {intI,j; for(i =0; i<popusize;i++) { for(j =0; j<n;j++) {if(RANDOMF (0,1) <pm) {Satgenes[i][j] =1-SATGENES[I][J]; } } }}BOOLIsbetter () {intMax_temp = Int_min; for(inti =0; i<popusize; i++) {if(score[i]>max_temp) {max_temp = Score[i]; } }if(Max_temp>=bestgenes_sat) {return 1; }return 0;}voidGa3sat (intNintm) {intGenaration =0;//population of the first generationJohnson (n);//Initialize populationSatisfied (M);//Calculate the quality of the geneFindbestgene (n); Ofstream Fout; Fout.open ("Output.txt"); fout<<"section"<<genaration<<"The optimal solution in a generational population is:"<<bestGenes_sat<<endl; while((bestgenes_sat!=m) &&genaration<generate) {adapt (n);//SelectCross (n);//HybridizationMutate (n);//MutationSatisfied (M);//Calculate the quality of the gene if(!isbetter ()) {inttemp = rand ()%popusize; Score[temp] = Bestgenes_sat; for(inti =0; i<n; i++) {Satgenes[temp][i] = bestgenes[i]; }} genaration++; Findbestgene (n); fout<<"section"<<genaration<<"The optimal solution in a generational population is:"<<bestGenes_sat<<endl; }//if (bestgenes_sat==m)// {//cout<< "Yes";// }//Else// {//cout<< "No";// }Fout.close ();}int_tmain (intARGC, _tchar* argv[]) {DoubleRun_time =0.0;//Execution Timetime_t start,end; start = Clock (); Ifstream fin; Fin.open ("10.txt");intI,j,t; x =New int*[M]; for(i =0; I<m; i++) {X[i] =New int[3]; } for(i =0; I<m; i++) { for(j =0; j<3; J + +) {fin>>x[i][j]; } fin>>t; } fin.close (); Srand ((unsigned) time (NULL)); Ga3sat (N,M); end = Clock (); Run_time = (end-start)/clocks_per_sec;printf("Run Time:%f\n", run_time); System"Pause");return 0;}
Compared to SA, GA has the biggest advantage in the initial solution, and there are hybridization and mutation, so SA has a very strong ability to jump out of the local optimal solution. And simple and universal, strong robustness. But there are many parameters to be determined, and the calculation speed is slow. Selection, hybridization and selection of mutation operators are also critical.
Reference Documents
[1] Zhang. Algorithm design and Analysis (advanced tutorial) [M]. National Defense Industry Press, 2007.
[2] The simulated annealing algorithm solves the traveling quotient problem http://blog.csdn.net/lalor/article/details/7688329.2011.
My other methods for solving 3SAT problems
lasvegas+ Backtracking algorithm solves 3SAT problem (c + + implementation code):
http://blog.csdn.net/zhoubin1992/article/details/46507919
LasVegas algorithm solves 3SAT problem (c + + implementation code):
http://blog.csdn.net/zhoubin1992/article/details/46469557
Simulated annealing algorithm solves 3SAT problem (c + + implementation code):
http://blog.csdn.net/zhoubin1992/article/details/46453761
Tabu Search algorithm solves 3SAT problem (c + + code implementation):
http://blog.csdn.net/zhoubin1992/article/details/46440389
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Genetic algorithm solves 3SAT problem (c + + implementation code)