The Taboo Search Algorithm solves the 3SAT problem (C ++ code implementation) and the algorithm 3sat

Source: Internet
Author: User

The Taboo Search Algorithm solves the 3SAT problem (C ++ code implementation) and the algorithm 3sat
Reprinted please indicate the source: http://blog.csdn.net/zhoubin1992/article/details/46440389
Recently, I sorted out the questions about the advanced algorithm course of the year, and the taboo search algorithm solved the 3SAT problem. Note: How can I create mathematical symbols in the editor? To retain the symbols, I just went through it. 1. SAT Problem Description

Theorem 4.4.1:

F (x1, x2 ,..., Xm) reaches the minimum value 0.

2. Taboo Search Algorithm

The taboo search algorithm introduces the greedy selection mechanism in the local search process, uses the taboo table to modify the neighborhood, and controls the selection and acceptance process by constructing the candidate neighborhood. During the search process, the taboo search algorithm selects the best solution from the candidate neighbor of the solution calculated in the previous step. Even if the solution is worse than the solution obtained in the previous step, it accepts the solution, at the same time, modify the taboo table to avoid the solution being selected again within the taboo period.

The analysis is as follows:

1 initial assignment

Random initialization variable value

2. Construction of candidate neighborhoods:

For the current value X, a variable is selected from each non-zero clause, and all the selected variables constitute a child variable set SVS. Select a variable from SVS and change its value. The other variable values remain unchanged. The resulting result is a adjacent solution of X. A set of all adjacent solutions forms a candidate neighborhood. Reduced search space and improved search efficiency.

3. Taboo table:

The taboo table records the variable elements that have been disturbed in the last iteration. These variables are taboo in the current iteration range.

Taboo Table Array iteration_age [I], I = 1, 2 ,... M indicates that the value of iteration_age [I] is the number of iterations when the variable xi is disturbed.

Whether the Yuan xi is forbidden:

Iteration_age [I] + L> = iteration


Pseudocode used by the Taboo Search algorithm to solve the 3SAT problem:

Algorithm pseudocode: initcnf (); initialiteration_age [] // initialize CNF, taboo table iteration = 1; flips = 1 // number of iterations and disturbance times initialize while (v_cnf (variable )! = 0 & iteration <itera_max) // stop criterion SVS [] // select a variable flag = 1 from each non-zero clause; I = 0 while (I <| SVS | & flag = 1) do for j I + 1 to | SVS | do if (candidate (j) -v_cnf (variable) <(candidate (s)-v_cnf (variable ))) // select the F' smallest variable selection policy from SVS. then swap SVS [I] andSVS [j] if (iteration_age [SVS [I] + L> = iteration) // if the Yuan conversion taboo if (candidate (I)-v_cnf (variable) <0) // attraction criterion candidate (I) isflipped // receives the disturbance modify iteration_age [] // modifies the taboo table flag = 0 flips ++ else I ++ else candidate (I) isflipped // receives the disturbance modify iteration_age [] // modifies 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; // number of variable elements 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 element array // int neighbor [n]; // int SVS in the neighborhood [N]; // The Sub-variable meta set int vclause [n + 5]; // The Sub-statement value int itera_ma X = 500000; int iteration_age [m]; // taboo table int t; // int v; // f (x) Target Function void initcnf () // CNF initial value {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 value 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 element is a 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 []) // f (x) value {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]); // The value of each clause} v ++ = vc Lause [I];} return v;} int candidate (int a) // adjacent 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 value {variable [I] = rand () % 2; // 0 to 1} printf ("variable Initial Value:"); for (int I = 1; I <= m; I ++) {printf ("% d", variable [I]);} initcnf (); int iterati On = 1; int flips = 1; int c = v_cnf (variable); printf ("Initial f (X) = % d", c ); printf ("\ 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-zero clause. SVS [a] = svs; // selects 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) // whether the variable element is 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 ("disturbance count: % d", flips ); printf ("\ n"); printf ("the final value of the variable is:"); for (int I = 0; I <m; I ++) {printf ("% d", variable [I]);} printf ("\ n"); int v = v_cnf (variable); printf ("final f (X) = % d \ n ", v) ;}} int _ tmain (int argc, _ TCHAR * argv []) {time_t start, end; start = clock (); tssat (); end = clock (); printf ("\ n"); printf ("Run Time: % f \ n", double (end-start) /(CLOCKS_PER_SEC); system ("pause"); return 0; return 0 ;}

Run the following example to test all the examples:

CNF (l = 3)

Average time

Successes/failures

N m

TS

TS

30 129

0.8200

20/0

40 172

0.9500

20/0

50 215

0.1500

20/0

100 430

0.2600

20/0

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

References

[1] Zhang defu. algorithm design and analysis (advanced tutorial) [M]. National Defense Industry Press, 2007.


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.