"Advanced algorithm" Tabu search algorithm solves 3SAT problem (c + + implementation)

Source: Internet
Author: User
Tags rand

Reprint Please specify Source: http://blog.csdn.net/zhoubin1992/article/details/46440389
recently combed, turned out the current year advanced algorithm course to do the topic. Tabu Search algorithm solves 3SAT problem.

Spit Groove: How mathematical symbols are typed in the editor, in order to preserve the symbols, I am direct.

1 Sat question descriptive narrative

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

Tabu search algorithm is a greedy selection mechanism introduced in the process of local search. By using the Tabu table to modify the neighborhood, the selection and acceptance process of the solution is controlled by the constructed candidate neighborhood.

In the process of searching, the Tabu search algorithm chooses the best solution from the candidate neighborhood of the last step calculation solution, even if the solution is worse than the one obtained in the previous step, accepts it, and changes the taboo table at the same time to avoid the solution being selected again within the taboo period.

The thinking analysis is for example the following:

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. Improved 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 of iteration_age[i],i=1,2,... m. 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)  //Assuming the variable element taboo                               if (Candidate (i )-v_cnf (variable) <0)  //Attraction Criteria  candidate (i) isflipped    //Accept the perturbation of the variable  modify iteration_age[]  //Change Taboo table  flag=0  flips++             Else  i++       Else    candidate (i) isflipped    //Accept perturbation of the variable  Modify iteration_age[]  //Change Taboo table  flag=0  flips++  iteration++;


C + + Implementation code:

TS3SAT.cpp: Defines the entry point of the console application.

-----------------------------------Tabu search algorithm solves 3SAT problems (C + + code implementation)---------------- -------------------Author: Pastoral, date:2014 email:[email protected] **********************************/#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 {if (vclause[i]==1) {int svs=abs (Clause[i][rand ()%l]) from each non-0 clause; 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 finally evaluated as:"), for (int i=0;i<m;i++) {printf ("%d", Variable[i]);} printf ("\ n "), int v=v_cnf (variable);p rintf (" Finally 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 ("Execution Time: %f\n ", double (End-start)/(CLOCKS_PER_SEC)); System (" pause "); return 0;return 0;}



Test all given examples, perform 20 times available results such as the following:

CNF (l=3)

Average time

Success/Failure

n        m 

TS

TS

30     & nbsp 129

0.8200

20/0

40       172

0.9500

20/0

50       215

0.

20/0

100      43 0

0.2600

20/0

Test Example (1.txt): http://download.csdn.net/detail/zhoubin1992/8794893

References

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


"Advanced algorithm" Tabu search algorithm solves 3SAT problem (c + + 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.