Simulated annealing algorithm solves 3SAT problem (c + + implementation code)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhoubin1992/article/details/46453761

1 SAT Problem 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 . Introduce some logic symbols before you define the SAT for satisfying questions.


2 Simulated annealing algorithm

Simulated annealing algorithm from the solid annealing principle, the solid heating to full high, and then let it slowly cooling, heating, the solid particles with temperature rise to disorderly shape, internal energy increases, and slowly cooled particles gradually orderly, at each temperature to achieve equilibrium, finally at room temperature to reach the ground state, the internal energy is minimized. By simulating the combinatorial optimization problem with solid annealing, the internal energy E is simulated as the objective function value F, and the temperature T evolves into the control parameter T, which is the simulated annealing algorithm for solving combinatorial optimization problem: the iteration of "generating new solution → calculating the difference of the target function → accepting or discarding" from the initial solution I and the initial control parameter T and gradually attenuation t value, the current solution at the end of the algorithm is the approximate optimal solution, which is a heuristic random search process based on Monte Carlo iterative method.

the simulated annealing algorithm can be decomposed into the solution space, the objective function and the initial solution of 3 parts. The basic idea is :

(1) Initialization: initial temperature T (full Large), initial solution state S (is the beginning of the algorithm iteration), the number of iterations per t value L (Markov chain length), attenuation criterion α, stop criterion.

(2) Steps (3) to (6) of k=1,......,l;

(3) Produce new solution s′;

(4) Calculate the increment cost=cost (s′)-cost (s), where cost (s) is the evaluation function;

(5) If t′<0 accepts s′ as the new current solution, otherwise the probability exp (-t′/t) accepts s′ as the new current solution;

(6) If the termination condition is satisfied, the current solution is output as the optimal solution and the program is terminated. Otherwise t decreases gradually, and turns to the 2nd step operation.

The pseudo code of the simulated annealing algorithm is as follows:

<span style= "FONT-SIZE:14PX;" >choose an initial solution X0  randomly    //random selection An initial solution x0give an initial temperature T0, x←x0, t←t0    //initialization Temperature T0while The stop criterion is not yet satisfied    do//stop criterion does not meet the   length of the {  for i←1 to L do                          //markov chain of L
   {Pick a solution  x ' ∈n (x) randomly  //randomly Select a solution in the pro domain X '         δf←f (x ')-f (x)                                                            ifδf<0 then  x←x '         Else  X←x '  with probability exp (-δf/t)  }  //with exp (-δf/t) Accept probability X ' t←g (T)    //generally, T←at    }
   
    //Temperature Drop return x</span>
   
3 C + + implementation code

<span style= "FONT-SIZE:14PX;" >//SA3Sat.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <time.h> #include <fstream> #include <math.h >using namespace std; #define Anssize 100int ans[anssize]; int new_ans[anssize];int **x;int n=100;int m=430;int randomi (int a, int b) {int c=rand ()% (b-a+1) +a;return C;} Double Randomf (double A, double b) {double c = (double) (rand ()% ((int) b (int) a)) + A + (double) (rand ()/(Rand_max + 1.0)); RET Urn C;} void Johnson (int n) {for (int i = 0; i<n; i++) {if (double) rand ()/(Rand_max) >0.5) {ans[i] = 1;} Else{ans[i] = 0;}}}  int Satisfied_ans (int m) {int count = 0;int i,j;for (i = 0; i<m; i++) {for (j = 0; j<3; j + +) {if (x[i][j]<0) {int temp= ( -1) *x[i][j];if (ans[temp-1]==0) {count++;break;}} else if (x[i][j]>0) {if (ans[x[i][j]-1]==1) {count++;break;}}}} return count;} int Satisfied_new_ans (int m) {int count = 0;int i,j;for (i = 0; i<m; i++) {for (j = 0; j<3; j + +) {if (x[i][j]<0) {int temp= ( -1) *x[i][j];if (new_ans[temp-1]==0) {count++;break;}} else if (x[i][j]>0) {if (new_ans[x[i][j]-1]==1) {count++;break;}}}} return count;} void Disturb (int n) {for (int j = 0; j<n; j + +) {New_ans[j] = ans[j];} int i = rand ()%n;new_ans[i] = 1-new_ans[i];} BOOL Accept (int deta,float T) {if (deta>0) {return 1;} else if (((deta<0) && (exp (DETA/T) &GT;RANDOMF (0,1))) {return 1;} return 0;} void Sa3sat (int n,int m) {int i;   Johnson (n);  Initial solution Float T = 1000;    Initial temperature int L = 100*n; Float T_time=0.001;while (T>t_time&&satisfied_ans (m)!=m) {for (i= 0; i<l; i++) {disturb (n);//for (i = 0; i& Lt;n; i++)//{//cout<<ans[i]<< "";//}int Deta = Satisfied_new_ans (M)-satisfied_ans (m); if (accept (deta,t)) {for ( int j = 0; J<n; J + +) {Ans[j] = New_ans[j];}}} T = 0.98*t;}}     int _tmain (int argc, _tchar* argv[]) {//int n,m,i,j;//cin>>n>>m;  3SAT problem//x = new int*[m];//for (i = 0; i<m; i++)//{//X[i] = new int[3];//}//for (i = 0; i<m; i++)//clauses {//for (j = 0; j<3; J + +)//{//cin>>x[i][j];//}//}double run_time = 0.0;    Execution time time_t Start,end;start = Clock () ifstream fin;fin.open ("10.txt"); int i,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)); Sa3sat (N,M); cout<< "number of clauses to satisfy:" <<satisfied_ans (m) <<endl; cout<< "The final value of the variable is:"; for (i = 0; i<n; i++) {cout<<ans[i]<< "";}  if (Satisfied_ans (m) ==m)//{//cout<< "Yes";//}//else//{//cout<< "No";//}end = Clock (); run_time = (End- Start)/clocks_per_sec;printf ("Run Time:%f\n", run_time); System ("pause"); return 0;} </span>


4 Experimental results

4.1 Parameter settings

Control parameter initial value: t0=1000;

Stop criteria: When the temperature reaches the lower limit of the set, the stop algorithm runs or the maximum satisfying clause condition is reached.

The attenuation function of the control parameter T in the cooling schedule: A (t) =0.98*t;

Mapkob chain Length: fixed length 100*m.

4.2 Experimental results

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

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 128, the run time is 19 seconds, the result is as follows:


Reference documents

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


Simulated annealing algorithm solves 3SAT problem (c + + implementation code)

Related Article

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.