diff --git a/CCAnr/CCAnr b/CCAnr/CCAnr deleted file mode 100755 index caca9ce..0000000 Binary files a/CCAnr/CCAnr and /dev/null differ diff --git a/CCAnr/Makefile b/CCAnr/Makefile deleted file mode 100644 index 47f869a..0000000 --- a/CCAnr/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -CCAnr: cca.cpp cca.h basis.h cw.h preprocessor.h - g++ cca.cpp -O3 -static -o CCAnr diff --git a/CCAnr/basis.h b/CCAnr/basis.h deleted file mode 100644 index a4daedf..0000000 --- a/CCAnr/basis.h +++ /dev/null @@ -1,377 +0,0 @@ -#ifndef _BASIS_H_ -#define _BASIS_H_ - -#include -#include -#include -#include - -using namespace std; - -enum type{SAT3, SAT5, SAT7, strSAT} probtype; - -/* limits on the size of the problem. */ -#define MAX_VARS 4000010 -#define MAX_CLAUSES 20000000 - - -// Define a data structure for a literal in the SAT problem. -struct lit { - int clause_num; //clause num, begin with 0 - int var_num; //variable num, begin with 1 - int sense; //is 1 for true literals, 0 for false literals. -}; - -/*parameters of the instance*/ -int num_vars; //var index from 1 to num_vars -int num_clauses; //clause index from 0 to num_clauses-1 -int max_clause_len; -int min_clause_len; -int formula_len=0; -double avg_clause_len; -double ratio; - -/* literal arrays */ -lit* var_lit[MAX_VARS]; //var_lit[i][j] means the j'th literal of var i. -int var_lit_count[MAX_VARS]; //amount of literals of each var -lit* clause_lit[MAX_CLAUSES]; //clause_lit[i][j] means the j'th literal of clause i. -int clause_lit_count[MAX_CLAUSES]; // amount of literals in each clause - -lit* org_clause_lit[MAX_CLAUSES]; //clause_lit[i][j] means the j'th literal of clause i. -int org_clause_lit_count[MAX_CLAUSES]; // amount of literals in each clause -int simplify=0; - -/* Information about the variables. */ -int score[MAX_VARS]; -int time_stamp[MAX_VARS]; -int conf_change[MAX_VARS]; -int* var_neighbor[MAX_VARS]; -int var_neighbor_count[MAX_VARS]; -//int pscore[MAX_VARS]; -int fix[MAX_VARS]; - -/* Information about the clauses */ -int clause_weight[MAX_CLAUSES]; -int sat_count[MAX_CLAUSES]; -int sat_var[MAX_CLAUSES]; -//int sat_var2[MAX_CLAUSES]; - -//unsat clauses stack -int unsat_stack[MAX_CLAUSES]; //store the unsat clause number -int unsat_stack_fill_pointer; -int index_in_unsat_stack[MAX_CLAUSES];//which position is a clause in the unsat_stack - -int this_try_best_unsat_stack_fill_pointer; - -//variables in unsat clauses -int unsatvar_stack[MAX_VARS]; -int unsatvar_stack_fill_pointer; -int index_in_unsatvar_stack[MAX_VARS]; -int unsat_app_count[MAX_VARS]; //a varible appears in how many unsat clauses - -//configuration changed decreasing variables (score>0 and confchange=1) -int goodvar_stack[MAX_VARS]; -int goodvar_stack_fill_pointer; -int already_in_goodvar_stack[MAX_VARS]; - -//unit clauses preprocess -lit unitclause_queue[MAX_VARS]; -int unitclause_queue_beg_pointer=0; -int unitclause_queue_end_pointer=0; -int clause_delete[MAX_CLAUSES]; - -/* Information about solution */ -int cur_soln[MAX_VARS]; //the current solution, with 1's for True variables, and 0's for False variables - -//cutoff -int max_tries = 10000; -int tries; -int max_flips = 2000000000; -int step; - -void setup_datastructure(); -void free_memory(); -int build_instance(char *filename); -void build_neighbor_relation(); - -void free_memory() -{ - int i; - for (i = 0; i < num_clauses; i++) - { - delete[] clause_lit[i]; - } - - for(i=1; i<=num_vars; ++i) - { - delete[] var_lit[i]; - delete[] var_neighbor[i]; - } -} - -/* - * Read in the problem. - */ -int temp_lit[MAX_VARS]; //the max length of a clause can be MAX_VARS -int build_instance(char *filename) -{ - char line[1000000]; - char tempstr1[10]; - char tempstr2[10]; - int cur_lit; - int i,j; - int v,c;//var, clause - - ifstream infile(filename); - if(!infile.is_open()) - return 0; - - /*** build problem data structures of the instance ***/ - infile.getline(line,1000000); - while (line[0] != 'p') - infile.getline(line,1000000); - - sscanf(line, "%s %s %d %d", tempstr1, tempstr2, &num_vars, &num_clauses); - ::ratio = double(num_clauses) / num_vars; - - if(num_vars>=MAX_VARS || num_clauses>=MAX_CLAUSES) - { - cout<<"the size of instance exceeds out limitation, please enlarge MAX_VARS and (or) MAX_CLAUSES."<>cur_lit; - - while (cur_lit != 0) { - temp_lit[clause_lit_count[c]] = cur_lit; - clause_lit_count[c]++; - - infile>>cur_lit; - } - - clause_lit[c] = new lit[clause_lit_count[c]+1]; - - for(i=0; i 0) clause_lit[c][i].sense = 1; - else clause_lit[c][i].sense = 0; - - var_lit_count[clause_lit[c][i].var_num]++; - } - clause_lit[c][i].var_num=0; - clause_lit[c][i].clause_num = -1; - - //unit clause - if(clause_lit_count[c]==1) - { - unitclause_queue[unitclause_queue_end_pointer++] = clause_lit[c][0]; - clause_delete[c]=1; - } - - if(clause_lit_count[c] > max_clause_len) - max_clause_len = clause_lit_count[c]; - else if(clause_lit_count[c] < min_clause_len) - min_clause_len = clause_lit_count[c]; - - formula_len += clause_lit_count[c]; - } - infile.close(); - - avg_clause_len = (double)formula_len/num_clauses; - - if(unitclause_queue_end_pointer>0) - { - simplify = 1; - for (c = 0; c < num_clauses; c++) - { - org_clause_lit_count[c] = clause_lit_count[c]; - org_clause_lit[c] = new lit[clause_lit_count[c]+1]; - for(i=0; i -#include //these two h files are for linux -#include - -#include "ccanr.h" - -char * inst; -int seed; - -long long ls_no_improv_times; - -bool aspiration_active; - -static int pick_var(void) -{ - int i,k,c,v; - int best_var; - lit* clause_c; - - /**Greedy Mode**/ - /*CCD (configuration changed decreasing) mode, the level with configuation chekcing*/ - if(goodvar_stack_fill_pointer>0) - { - - //if(goodvar_stack_fill_pointerscore[best_var]) best_var = v; - else if(score[v]==score[best_var]) - { - //if(unsat_app_count[v]>unsat_app_count[best_var]) best_var = v; - //else if(unsat_app_count[v]==unsat_app_count[best_var]&&time_stamp[v]score[best_var]) best_var = v; - else if(score[v]==score[best_var]) - { - //if(unsat_app_count[v]>unsat_app_count[best_var]) best_var = v; - //else if(unsat_app_count[v]==unsat_app_count[best_var]&&time_stamp[v]ave_weight) - { - best_var = unsatvar_stack[i]; - break; - } - } - - for(++i; iscore[best_var]) best_var = v; - else if(score[v]==score[best_var] && time_stamp[v]score[best_var]) best_var = v; - //else if(score[v]==score[best_var]&&time_stamp[v]unsat_app_count[best_var]) best_var = v; - //else if(unsat_app_count[v]==unsat_app_count[best_var] && time_stamp[v]score[best_var]) best_var = v; - else if(score[v]==score[best_var]&&time_stamp[v] 0) preprocess(); - - build_neighbor_relation(); - - scale_ave=(threshold+1)*q_scale; // - - cout<<"c Instance: Number of variables = "<var_num)!=0; p++) - { - unsat_app_count[v]++; - if(unsat_app_count[v]==1) - { - index_in_unsatvar_stack[v] = unsatvar_stack_fill_pointer; - push(v,unsatvar_stack); - } - } -} - - -inline void sat(int clause) -{ - int index,last_unsat_clause; - - //since the clause is satisfied, its position can be reused to store the last_unsat_clause - last_unsat_clause = pop(unsat_stack); - index = index_in_unsat_stack[clause]; - unsat_stack[index] = last_unsat_clause; - index_in_unsat_stack[last_unsat_clause] = index; - - //update appreance count of each var in unsat clause and update stack of vars in unsat clauses - int v,last_unsat_var; - for(lit* p=clause_lit[clause]; (v=p->var_num)!=0; p++) - { - unsat_app_count[v]--; - if(unsat_app_count[v]==0) - { - last_unsat_var = pop(unsatvar_stack); - index = index_in_unsatvar_stack[v]; - unsatvar_stack[index] = last_unsat_var; - index_in_unsatvar_stack[last_unsat_var] = index; - } - } -} - -//initiation of the algorithm -void init() -{ - int v,c; - int i,j; - int clause; - - //Initialize edge weights - for (c = 0; c0)// && conf_change[v]==1) - { - already_in_goodvar_stack[v] = 1; - push(v,goodvar_stack); - - } - else already_in_goodvar_stack[v] = 0; - } - - //setting for the virtual var 0 - time_stamp[0]=0; - //pscore[0]=0; - - this_try_best_unsat_stack_fill_pointer = unsat_stack_fill_pointer; -} - - -void flip(int flipvar) -{ - cur_soln[flipvar] = 1 - cur_soln[flipvar]; - - int i,j; - int v,c; - - lit* clause_c; - - int org_flipvar_score = score[flipvar]; - - //update related clauses and neighbor vars - for(lit *q = var_lit[flipvar]; (c=q->clause_num)>=0; q++) - { - clause_c = clause_lit[c]; - if(cur_soln[flipvar] == q->sense) - { - ++sat_count[c]; - - if (sat_count[c] == 2) //sat_count from 1 to 2 - score[sat_var[c]] += clause_weight[c]; - else if (sat_count[c] == 1) // sat_count from 0 to 1 - { - sat_var[c] = flipvar;//record the only true lit's var - for(lit* p=clause_c; (v=p->var_num)!=0; p++) score[v] -= clause_weight[c]; - - sat(c); - } - } - else // cur_soln[flipvar] != cur_lit.sense - { - --sat_count[c]; - if (sat_count[c] == 1) //sat_count from 2 to 1 - { - for(lit* p=clause_c; (v=p->var_num)!=0; p++) - { - if(p->sense == cur_soln[v] ) - { - score[v] -= clause_weight[c]; - sat_var[c] = v; - break; - } - } - } - else if (sat_count[c] == 0) //sat_count from 1 to 0 - { - for(lit* p=clause_c; (v=p->var_num)!=0; p++) score[v] += clause_weight[c]; - unsat(c); - }//end else if - - }//end else - } - - score[flipvar] = -org_flipvar_score; - - /*update CCD */ - int index; - - conf_change[flipvar] = 0; - //remove the vars no longer goodvar in goodvar stack - for(index=goodvar_stack_fill_pointer-1; index>=0; index--) - { - v = goodvar_stack[index]; - if(score[v]<=0) - { - goodvar_stack[index] = pop(goodvar_stack); - already_in_goodvar_stack[v] = 0; - } - } - - //update all flipvar's neighbor's conf_change to be 1, add goodvar - int* p; - for(p=var_neighbor[flipvar]; (v=*p)!=0; p++) - { - conf_change[v] = 1; - - if(score[v]>0 && already_in_goodvar_stack[v] ==0) - { - push(v,goodvar_stack); - already_in_goodvar_stack[v] = 1; - } - } -} - -#endif - diff --git a/CCAnr/ccanr.h b/CCAnr/ccanr.h deleted file mode 100644 index d343031..0000000 --- a/CCAnr/ccanr.h +++ /dev/null @@ -1,9 +0,0 @@ -namespace CCAnr { - -void module_init(); -int module_pick_var(); -void module_flip_var(int var); -void module_reset(); - -int* module_cur_soln(); -} \ No newline at end of file diff --git a/CCAnr/cw.h b/CCAnr/cw.h deleted file mode 100644 index c3a252d..0000000 --- a/CCAnr/cw.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef _CW_H_ -#define _CW_H_ - -#include "basis.h" - -#define sigscore ave_weight //significant score needed for aspiration - -int ave_weight=1; -int delta_total_weight=0; - -/**************************************** clause weighting for 3sat **************************************************/ - -int threshold; -float p_scale;//w=w*p+ave_w*q -float q_scale=0; -int scale_ave;//scale_ave==ave_weight*q_scale - -int q_init=0; - -void smooth_clause_weights() -{ - int i,j,c,v; - int new_total_weight=0; - - for (v=1; v<=num_vars; ++v) - score[v] = 0; - - //smooth clause score and update score of variables - for (c = 0; c0 && conf_change[v]==1 && already_in_goodvar_stack[v] ==0) - { - push(v,goodvar_stack); - already_in_goodvar_stack[v] =1; - } - } - - delta_total_weight+=unsat_stack_fill_pointer; - if(delta_total_weight>=num_clauses) - { - ave_weight+=1; - delta_total_weight -= num_clauses; - - //smooth weights - if(ave_weight>threshold) - smooth_clause_weights(); - } -} - - -void set_clause_weighting() -{ - threshold=300; - p_scale=0.3; - if(q_init==0) - { - if(::ratio<=15) q_scale=0; - else q_scale=0.7; - } - else - { - if(q_scale<0.5) //0 - q_scale = 0.7; - else - q_scale = 0; - } - - scale_ave=(threshold+1)*q_scale; - q_init = 1; -} - -#endif diff --git a/CCAnr/preprocessor.h b/CCAnr/preprocessor.h deleted file mode 100644 index 894353c..0000000 --- a/CCAnr/preprocessor.h +++ /dev/null @@ -1,133 +0,0 @@ -#include "basis.h" -//preprocess -void unit_propagation() -{ - lit uc_lit; - int uc_clause; - int uc_var; - bool uc_sense; - - int c,v; - int i,j; - lit cur, cur_c; - - - //while (unitclause_queue_beg_pointer < unitclause_queue_end_pointer) - for(unitclause_queue_beg_pointer=0; unitclause_queue_beg_pointer < unitclause_queue_end_pointer; unitclause_queue_beg_pointer++) - { - uc_lit = unitclause_queue[unitclause_queue_beg_pointer]; - - uc_var = uc_lit.var_num; - uc_sense = uc_lit.sense; - - if(fix[uc_var]==1) {if(uc_sense!=cur_soln[uc_var])cout<<"c wants to fix a variable twice, forbid."< max_clause_len) - max_clause_len = clause_lit_count[c]; - else if(clause_lit_count[c] < min_clause_len) - min_clause_len = clause_lit_count[c]; - } - - avg_clause_len = (double)formula_len/num_clauses; - - for (v=1; v<=num_vars; ++v) - { - if(fix[v]==1) - { - fix_var_count++; - } - var_lit[v][var_lit_count[v]].clause_num=-1;//new var_lit boundary - } - - cout<<"c unit propagation fixes "<outputs.size() >= 2) { gate->stem = true; } - // gate->stem = true; - // if(rand() % 1000 <= 100) { - // gate->stem = true; - // } if(gate->stem) { stems.push_back(gate); } @@ -37,7 +33,7 @@ void Circuit::init_stems() { } } } - //printf("pre: %s %d\n", g->name.c_str(), g->pre_stems.size()); + printf("pre: %s %d\n", g->name.c_str(), g->pre_stems.size()); } for(Gate *g : gates) { @@ -179,6 +175,10 @@ bool Circuit::is_valid_circuit() { fault_total_cnt += 1; } + if(g->stem) { + assert(stem_satisfied[g->id] == (g->cal_value() == g->value)); + } + // 检查门的赋值情况 if(g->cal_value() != g->value) { printf("WRONG-ASSGIN: %s \n", g->name.c_str()); diff --git a/circuit.h b/circuit.h index d883458..bef48b5 100644 --- a/circuit.h +++ b/circuit.h @@ -117,7 +117,6 @@ ll ls_score(); int** simulate(); - // time status int flip_cnt = 0; @@ -126,4 +125,6 @@ double flip_time = 0; int update_cnt = 0; double update_time = 0; +std::unordered_set stem_unsatisfied_set; + }; \ No newline at end of file diff --git a/gate.cpp b/gate.cpp index 5b79081..c2e1b23 100644 --- a/gate.cpp +++ b/gate.cpp @@ -2,7 +2,6 @@ #include "assert.h" - int Gate::cal_propagate_len(bool x) { int fpl[2]; @@ -10,10 +9,8 @@ int Gate::cal_propagate_len(bool x) { for(Gate* out : outputs) { if(!out->is_detected(this)) continue; - fpl[!value] = std::max(fpl[!value], out->fault_propagate_len[!out->value] + 1); } - return fpl[x]; } diff --git a/ls.cpp b/ls.cpp index 4eeab07..328e35f 100644 --- a/ls.cpp +++ b/ls.cpp @@ -23,24 +23,19 @@ bool Circuit::local_search(std::unordered_set &faults) { while(true) { + // int ustem = 0; + // for(Gate* stem : stems) { + // ustem += !stem_satisfied[stem->id]; + // } + // printf("ustem: %d %d\n", ustem, stem_unsatisfied_set.size()); + // assert(ustem == stem_unsatisfied_set.size()); + auto start = std::chrono::system_clock::now(); Gate* stem = nullptr; ll max_score = 0; - // std::vector stems_random; - // std::vector candidates; - - // for(int i=0; iid]) { - // stems_random.push_back(stems[i]); - // } - // } - // for(int i=0; i &faults) { std::chrono::duration elapsed_seconds = end - start; update_cnt++; update_time += elapsed_seconds.count(); - //printf("[UP] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %lld, stem_cnt: %lld, fault_cnt:%lld\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt); } } @@ -248,11 +242,11 @@ void Circuit::ls_init_weight(const std::unordered_set &faults) { fault_weight[f->gate->id][f->type] = 1; } - int r = rand() % faults.size(); - auto it = faults.begin(); - std::advance(it, r); + // int r = rand() % faults.size(); + // auto it = faults.begin(); + // std::advance(it, r); - fault_weight[(*it)->gate->id][(*it)->type] = 1000000; + // fault_weight[(*it)->gate->id][(*it)->type] = 1000000; for(Gate* s: stems) { flip_weight[s->id] = 1; @@ -349,6 +343,8 @@ void Circuit::ls_block_recal(Gate* stem) { } if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){ + stem_unsatisfied_set.erase(stem); + stem_satisfied[stem->id] = true; stem_total_weight -= stem_weight[stem->id]; stem_total_cnt += 1; @@ -364,6 +360,8 @@ void Circuit::ls_block_recal(Gate* stem) { } if(stem->cal_value() != stem->value && stem_satisfied[stem->id]) { + stem_unsatisfied_set.insert(stem); + stem_satisfied[stem->id] = false; stem_total_weight += stem_weight[stem->id]; stem_total_cnt -= 1; @@ -459,12 +457,14 @@ void Circuit::ls_block_recal(Gate* stem) { stem->fault_propagate_len[1] = fpl1; if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){ + stem_unsatisfied_set.erase(stem); stem_satisfied[stem->id] = true; stem_total_weight -= stem_weight[stem->id]; stem_total_cnt += 1; } if(stem->cal_value() != stem->value && stem_satisfied[stem->id]) { + stem_unsatisfied_set.insert(stem); stem_satisfied[stem->id] = false; stem_total_weight += stem_weight[stem->id]; stem_total_cnt -= 1; diff --git a/show_exp.ipynb b/show_exp.ipynb deleted file mode 100644 index e946599..0000000 --- a/show_exp.ipynb +++ /dev/null @@ -1,38 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.7" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/simulator.cpp b/simulator.cpp deleted file mode 100644 index 41be410..0000000 --- a/simulator.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include "circuit.h" - -#include -#include - -int cal_value(Gate *g, int *value) { - int res; - - switch(g->type) { - case Gate::NOT: - res = !value[g->inputs[0]->id]; - break; - case Gate::BUF: - res = value[g->inputs[0]->id]; - break; - case Gate::AND: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res &= value[g->inputs[i]->id]; - } - break; - case Gate::NAND: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res &= value[g->inputs[i]->id]; - } - res = !res; - break; - case Gate::OR: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res |= value[g->inputs[i]->id]; - } - break; - case Gate::NOR: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res |= value[g->inputs[i]->id]; - } - res = !res; - break; - case Gate::XOR: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res ^= value[g->inputs[i]->id]; - } - break; - case Gate::XNOR: - res = value[g->inputs[0]->id]; - for(int i=1; iinputs.size(); i++) { - res ^= value[g->inputs[i]->id]; - } - res = !res; - break; - case Gate::INPUT: - res = value[g->id]; - break; - default: - assert(false); - break; - } - return res; -} - -bool cal_sa(Gate* g, bool x, int** sa, int *value) { - if(g->isPO) { - if(x == 0) return value[g->id]; - else return !value[g->id]; - } - - bool sa0 = 0; - bool sa1 = 0; - - for(Gate* out : g->outputs) { - if(!sa[out->id][0] && !sa[out->id][1]) continue; - - if(cal_value(out, value) != value[out->id]) continue; - - value[g->id] = !value[g->id]; - bool detect = cal_value(out, value) != value[out->id]; - value[g->id] = !value[g->id]; - if(!detect) continue; - - sa0 |= value[g->id]; - sa1 |= !value[g->id]; - } - if(x == 0) return sa0; - else return sa1; -} - - -int** Circuit::simulate() { - - static bool init = false; - static int** sa = nullptr; - static int* value = nullptr; - - if(!init) { - const int MAXN = gates.size() + 1; - init = true; - sa = new int*[MAXN]; - for(int i=0; iid] = pi->value; - } - - for(Gate *g : gates) { - if(g->isPI) continue; - value[g->id] = cal_value(g, value); - } - - for(Gate *g : gates) { - assert(value[g->id] == cal_value(g, value)); - } - - std::queue q; - std::unordered_map topo; - - // init PO - for(Gate* po : POs) { - sa[po->id][!value[po->id]] = 1; - sa[po->id][value[po->id]] = 0; - q.push(po); - } - - while(!q.empty()) { - Gate* g = q.front(); - q.pop(); - for(Gate* in : g->inputs) { - if(++topo[in] == in->outputs.size()) { - sa[in->id][0] = cal_sa(in, 0, sa, value); - sa[in->id][1] = cal_sa(in, 1, sa, value); - q.push(in); - } - } - } - - for(Gate* g : gates) { - assert(sa[g->id][0] == cal_sa(g, 0, sa, value)); - assert(sa[g->id][1] == cal_sa(g, 1, sa, value)); - } - - return sa; -} \ No newline at end of file