Tabu Search algorithm solves 3SAT problem (c + + code implementation)

Source: Internet
Author: User

recently combed, turned out the same year advanced algorithm courses do the topic, taboo search. Spit Groove: How mathematical symbols are typed in the editor, in order to preserve the symbols, I am direct. 1 Sat Question Description

Theorem 4.4.1:

The necessary and sufficient condition of the assignment V for the CNF to be satisfied is that F (x1,x2,..., XM) achieves a minimum value of 0.

2 Tabu search Algorithm

The Tabu search algorithm introduces the greedy selection mechanism in the process of local search, and modifies the neighborhood by using the Tabu table to control the solution selection and acceptance process by constructing the candidate neighborhood. In the process of searching, the Tabu search algorithm chooses the best solution from the candidate neighborhood of the previous calculation solution, even if the solution is worse than the one obtained in the previous step, accepts it and modifies the taboo table to avoid the solution being selected again within the taboo period.

The thinking analysis is as follows:

1 Initial Assignment

Random initialization of variable values

2 Construction of Candidate neighborhood:

For the current assignment x, a variable is selected from each non-0 clause, and all the selected elements form a sub-variable set of SVS. Choose a variable from the SVS, change its value, the other variable values remain unchanged, and the resulting solution is an adjacent solution of X. The set of all adjacent solutions constitutes the candidate neighborhood. Reduce search space and improve search efficiency.

3 Taboo Table:

The taboo table records the disturbances in the recent L iterations, which are taboo disturbances within the current iteration range.

The taboo table is represented by an array iteration_age[i],i=1,2,... m, and the value of Iteration_age[i] is the number of overlapping preface when the variable XI is disturbed

Meta XI is not taboo:

Iteration_age[i]+l>=iteration


Tabu Search algorithm solves 3SAT problem pseudo-code:

Algorithm pseudo code:  initcnf (); initialiteration_age[]  //Initialize CNF, taboo table  Iteration    =  1;flips =     1     // Number of iterations and number of disturbances initialized while  (V_CNF (variable)!=0&&iteration< Itera_max)//Stop criterion  svs[]  // Select a variable flag from each non-0 clause    = 1;i  = 0 while  (i<| svs|&&flag==1) do-       J   i+1 to | svs| Do      if ((Candidate (j)-v_cnf (variable)) < (candidate (s)-v_cnf (variable))    //select F ' Minimum variable selection strategy from SVS  Then  swap svs[i] andsvs[j] if       (iteration_age[svs[i]]+l>=iteration)  //If the variable element is taboo if                               (candidate (i )-v_cnf (variable) <0)  //Attraction Criteria  candidate (i) isflipped    //Accept the perturbation of the variable  modify iteration_age[]  //Modify Taboo table  flag=0  flips++             Else  i++       Else    candidate (i) isflipped    //Accept perturbation of the variable  Modify iteration_age[]  //Modify the Taboo table  flag=0  flips++  iteration++;


C + + Implementation code:

TS3SAT.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "stdafx.h" #include <string> #include <time.h> #include <fstream># Include <iostream> #include <iterator>using namespace std;const int n=129; Number of clauses const int L=3;CONST int m=30;       Variable number const int l=20;//taboo table Length const int N=1000;int clause[n+5][l+5]; Subscript array int sign[l*n+1];//cnf variable symbol int variable[m+1];//variable array//int neighbour[n];//neighborhood int svs[n];//sub-set int vclause[n+5];// The value of the clause int itera_max = 500000;int iteration_age[m];//Taboo table int t;//int v;//f (x) objective function void initcnf ()//cnf initial assignment {printf ("\ n");     Ifstream in ("1.txt");   for (int i =0;i<n+5;i++) {for (int j=0;j<=3;j++) {clause[i][j]=1; }} for (int i = 1; I <= N, i++) {in >> clause[i][1] >> clause[i][2] >> Clause[i]      [3] >> t; }//subscript Variable random assignment/*for (int i=0;i<n;i++) {for (int j=0;j<l;j++) {Clause[i][j]=rand ()%m+1;//1 to m}}*///each variable symbol 0 is inverse 1 is positive for ( int i=1; i<=n; i++) for (int j=1; J <= L; j + +) {//sign[i] = = clause[I][j]/abs (Clause[i][j]); if (Clause[i][j]/abs (clause[i][j]) = = 1) sign[i]=1;else sign[i]=0;} for (int i=1;i<=m;i++) {iteration_age[i]=0;} for (int i=0;i<=n;i++) {svs[i]=0;}} int v_cnf (int var[]) The value of//f (x) {int v=0;for (int i=1;i<=n;i++) {vclause[i]=1;} for (int i=1;i<=n;i++) {for (int j=1;j<=l;j++) {vclause[i] *= (sign[3* (i-1) +j]^var[abs (Clause[i][j])]);//value of individual clauses} V+=vclause[i];} return v;}     int candidate (int a)//O-Solution {int var1[m+1]; memcpy (var1,variable,m+1); for (int t = 0; t < m+1; t++) var1[t] = Variable[t];int v=0;//v=v_cnf (); var1[svs[a]]=1-var1 [Svs[a]];v=v_cnf (var1); return v;} void Tssat ()//Taboo Search {Srand (double (Time (NULL))), for (int i=1;i<=m;i++)//variable assignment {Variable[i]=rand ()%2;//0 to 1}printf (" The initial assignment of the variable is: "); for (int i=1;i<=m;i++) {printf ("%d ", Variable[i]);} INITCNF (); int iteration=1;int flips=1;int c=v_cnf (variable);p rintf ("Initial f (X) =%d", c);p rintf ("\ n"); while (V_CNF ( Variable)!=0&&iteration < Itera_max) {int a=0;for (int i=0;i<n;i++)//Select a variable from each non-0 clause {if (vclause[i]==1) {int Svs=abs (clause[i][rand ()%l]); svs[a]=svs;//to select the subscript int pos = 1;for (int i=0;i<a;i++) {if (Svs[a]==svs[i]) {pos = 0;break;}} if (pos = = 1) {a++;}} int Flag=1;int S=0;while (s<a&&flag==1) {for (int j=s+1;j<a;j++) {if (candidate (j)-v_cnf (variable)) < (Candidate (s)-v_cnf (variable))) Select f ' minimum variable {/*int temp=candidate (i), candidate (i) =candidate (j); candidate (j) =temp;*/int Temp=svs[s]; SVS[S]=SVS[J]; Svs[j]=temp;}} if (iteration_age[svs[s]]+l>=iteration)//variable is not taboo {if (candidate (s)-v_cnf (variable) <0)//attraction criterion {Variable[svs[s]] =1-variable[svs[s]];iteration_age[svs[s]]=iteration;flag=0;flips++;} else{//flag=0;s++;}} else {variable[svs[s]]=1-variable[svs[s]];iteration_age[svs[s]]=iteration;flips++;flag=0;}} iteration++;} printf ("Number of disturbances:%d", flips);p rintf ("\ n");p rintf ("The variable is ultimately evaluated as:"), for (int i=0;i<m;i++) {printf ("%d", Variable[i]);} printf ("\ n"), int v=v_cnf (variable);p rintf ("Final f (X) =%d\n", V);}} int _tmain (int argc, _tchar* argv[]) {time_t Start,end;start = Clock (), Tssat (), end = Clock ();p rintf ("\ n");p rintf ("Run Time: %f\n ", double (ENd-start)/(CLOCKS_PER_SEC)); System ("pause"); return 0;return 0;} 

Reference documents

[1] Zhang. Algorithm design and Analysis (advanced tutorial) [M]. National Defense Industry Press, 2007.


Tabu Search algorithm solves 3SAT problem (c + + code implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.