-
Notifications
You must be signed in to change notification settings - Fork 0
/
walksat.h
143 lines (143 loc) · 3.58 KB
/
walksat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
void randomvector(header head,int a[])
{
srand(time(NULL));
int temp;
for(int i=1;i<=head->numvar;i++)
{
temp=rand()%2;
a[i]=temp;
}
return ;
}
bool check1(header head,int guess[])
{
sat_information a[head->numvar+1]={0};
clausehead phead=head->nexth->nexth;
int flag=1;
while(phead)
{
clausenode pnode=phead->nextn->next;
int correctnum=0,tempvar;
while(pnode)
{
if((pnode->Var>0 && guess[abs(pnode->Var)]==1) || (pnode->Var<0 && guess[abs(pnode->Var)]==0))
{
if(!correctnum) tempvar=pnode->Var;
correctnum++;
}
else flag=0;
pnode=pnode->next;
}
if(correctnum==0)
{
pnode=phead->nextn->next;
while(pnode)
{
a[abs(pnode->Var)].usnum++;
pnode=pnode->next;
}
}
else if(correctnum==1) a[abs(tempvar)].snum++;
phead=phead->nexth;
}
if(flag==1) return true;
else
{
int maxd=0,maxvar;
for(int i=1;i<=head->numvar;i++)
{
a[i].d=a[i].usnum-a[i].snum;
if(a[i].d>maxd)
{
maxd=a[i].d;
maxvar=i;
}
}
if(maxd>0) guess[maxvar]=!guess[maxvar];
return false;
}
}
bool check2(header head,int guess[],clausehead wrongclause[],int &num)
{
clausehead phead=head->nexth->nexth;
while(phead)
{
clausenode pnode=phead->nextn->next;
int flag=0;
while(pnode)
{
if((pnode->Var>0 && guess[abs(pnode->Var)]==1) || (pnode->Var<0 && guess[abs(pnode->Var)]==0))
{
flag=1;
break;
}
pnode=pnode->next;
}
if(flag==0) wrongclause[num++]=phead;
phead=phead->nexth;
}
if(num==0) return true;
else
{
srand(time(NULL));
int choose;
do{
choose=rand()%num;
phead=wrongclause[choose];
clausenode pnode=phead->nextn->next;
while(pnode)
{
if((pnode->Var>0 && guess[(abs(pnode->Var))]==0) || pnode->Var<0 && guess[abs(pnode->Var)]==1)
break;
pnode=pnode->next;
}
if(pnode)
{
choose=pnode->Var;
break;
}
}while(1);
if(choose>0) guess[choose]=1;
else guess[-choose]=0;
}
}
bool walksat_schedule(header head, int *s)
{
int guess[head->numvar+1],pos,isright=1;
clausehead wrongclause[head->numclause]={0};
for(int i=0;i<N && isright;i++)
{
srand(time(NULL));
memset(guess,0,sizeof(guess));
randomvector(head,guess);
pos=1;
for(int j=0;j<flip;j++)
{
if(j%100==0) srand(time(NULL));
double x=rand()/double(RAND_MAX);
//printf("%lf ",x);
int temp=x>p?0:1;
//printf("%d \n",temp);
int num=0;
if(temp==0)
{
if(check1(head,guess)==true)
{
isright=0;
break;
}
}
else
{
if(check2(head,guess,wrongclause,num)==true)
{
isright=0;
break;
}
}
}
}
for(int i=1;i<=head->numvar;i++) s[i]=guess[i];
if(isright==0) return true;
else return false;
}