这是一种算法,我试图使用本地搜索来解决2-SAT问题。但是,我似乎遇到了分段错误:11。问题从for ( int i =0; i<upper_limit; i++)
之后的主代码开始。
如何解决此错误?
#include <fstream>
#include <iostream>
#include<math.h>
#include <map>
#include <vector>
#include <set>
#include <random>
#include <math.h>
using namespace std;
bool check_clause(short &logic,bool x1,bool x2)
{
switch(logic)
{
case 0:
return ( (not x1) or (not x2) );
case 1:
return ( (not x1) or x2 );
break;
case 2:
return ( x1 or (not x2) );
break;
case 3:
return( x1 or x2 );
break;
}
return false;
}
int check_logic(vector<int> & elem)
{
short logic=0;
//Creating logic flag to construct teach clause Eg. x or !x2 = (10)(bin)= (2)(decimal)
for(int j =1; j>=0;j--)
{
if (elem[j]>0)
{
logic+=pow(2,1-j);
}
}
return logic;
}
vector <int> check_assignment(map <int,bool> &assignment,map <int, vector<int> > &data)
{ int count=0;
vector <int> unsatisfied;
for (auto elem: data)
{ //Variables for both vertex indices for clause and the logic flag
int first_vertex=abs(elem.second[0]);
int second_vertex=abs(elem.second[1]);
short logic=check_logic(elem.second);
//Retrieving randomly generated boolean variables for first and second vertex of current clause
bool x1 = assignment[first_vertex];
bool x2 = assignment[second_vertex];
bool answer=check_clause(logic,x1,x2);
if (answer==false)
{
unsatisfied.push_back(count);
}
count+=1;
}
return unsatisfied;
}
bool isKeyInMap(int number, map <int,vector<int> > & dictionary)
{
if (dictionary.find(number)==dictionary.end())
{
return false;
}
else
{
return true;
}
}
int main()
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0,1);
map <int, vector<int> > data;
map<int,vector<int> >::iterator it;
vector<int> row(2,0);
ifstream file_read("2sat1.txt");
int a;
int n;
file_read>>n;
cout<<n<<endl;
map <int, vector<int> > positives;
map <int, vector<int> > negatives;
int count=0;
while (file_read>>a)
{
row[0]=a;
file_read>>a;
row[1]=a;
data[count].insert(data[count].end(),row.begin(),row.end());
for (auto element: row)
{
if (element<0)
{
negatives[ -element ].push_back(count);
}
else
{
positives[ element ].push_back(count);
}
}
count++;
}
int n_rows=positives.size();
int incl_count=0;
vector <int> deletionList;
set <int> inclset;
// cout<<isKeyInMap(200001,positives)<<endl;
for (int i =1; i<=n;i++)
{
bool pos_entry=isKeyInMap(i,positives);
bool neg_entry=isKeyInMap(i,negatives);
if ( pos_entry != neg_entry )
{
if (pos_entry==true)
{
deletionList.insert(deletionList.end(),positives[i].begin(),positives[i].end());
}
else
{
deletionList.insert(deletionList.end(),negatives[i].begin(),negatives[i].end());
}
}
else if ((pos_entry==neg_entry) and (pos_entry==true))
{
inclset.insert(i);
}
}
cout<<"Inclusion set size: "<<inclset.size()<<endl;
int incl_size=inclset.size();
set <int> deletionSet(deletionList.begin(),deletionList.end());
cout<<"Set Size: "<<deletionSet.size()<<endl;
// sort(deletionList.begin(),deletionList.end());
for (auto i=deletionSet.rbegin(); i!=deletionSet.rend();++i)
{
for (auto item: data[*i])
{
cout<<item<<" ";
}
it=data.find(*i);
if (it!=data.end())
{
data.erase(it);
}
else
{
cout<<"Not found!"<<endl;
}
}
cout<<data.size()<<endl;
map <int,bool> assignment;
int upper_limit= (int) ceil ( log2(incl_size) );
long local_iter_size= 2*pow(incl_size,2);
for (auto elem: inclset)
{
assignment[elem]=(bool) dis(gen);
}
for ( int i =0; i<upper_limit; i++)
{
for (auto elem: inclset)
{
assignment[elem]= (bool) dis(gen);
}
for (auto elem: assignment)
{
cout<<elem.first<<":"<<elem.second<<endl;
}
cout<<"Size of assignment: "<<assignment.size()<<endl;
for (int j=0; j<local_iter_size; j++)
{
cout<<"Okay "<<j<<endl;
vector <int> unsatisfied;
unsatisfied = check_assignment(assignment,data);
if (unsatisfied.empty())
{
cout<<"Yay, we win!";
return 0;
}
int pick_random= unsatisfied[rand()% unsatisfied.size()];
vector <int> record= data[pick_random];
short logic=check_logic(record);
int first_vertex=abs(record[0]);
int second_vertex=abs(record[1]);
bool x1=assignment[first_vertex];
bool x2= assignment[second_vertex];
if (check_clause(logic,not x1,x2)==true)
{
assignment[first_vertex]
= not x1;
}
else
{
assignment[second_vertex]=not x2;
}
cout<<"Number of unfulfilled clauses: "<<unsatisfied.size()<<endl;
}
}
}
答案 0 :(得分:0)
if (answer==false)
{
unsatisfied.push_back(count);
}
count+=1;
在check_clause函数中找到的此子句有错误。我们应该推动不满足条款的密钥索引,即unsatisfied.push_back(elem.first)
。由于此错误,程序尝试访问向量unsatisfied
范围之外的索引。
现在,代码工作正常。