无错误的增量式数据结构

This commit is contained in:
YuhangQ 2023-02-28 08:45:13 +00:00
parent a9e6804d15
commit 2dffd855c5
7 changed files with 8180 additions and 6676 deletions

BIN
atpg

Binary file not shown.

View File

@ -16,11 +16,11 @@ ll Circuit::fault_total_cost;
int Circuit::fault_falsified_cnt;
void Circuit::init_stems() {
for(auto& gate: gates) {
if(gate->outputs.size() >= 2) {
gate->stem = true;
}
gate->stem = true;
if(gate->stem) {
stems.push_back(gate);
}
@ -122,11 +122,23 @@ bool Circuit::is_valid_circuit() {
for(Gate* g : gates) {
if(!g->propagate_satisfied) {
if(g->stem && !g->propagate_satisfied) {
propagate_total_cost += g->propagate_cost;
propagate_falsified_cnt += 1;
}
bool pro_sat = (g->cal_sa(0) == g->fault_satisfied[0] && g->cal_sa(1) == g->fault_satisfied[1]);
if(g->stem && g->propagate_satisfied != pro_sat) {
printf("WRONG-fault_satisfied: %s \n", g->name.c_str());
return false;
}
if(g->stem && g->stem_satisified != (g->cal_value() == g->value)) {
printf("WRONG-STEM_SATIS: %s \n", g->name.c_str());
return false;
}
if(g->stem && g->cal_value() != g->value) {
stem_total_cost += g->stem_cost;
stem_falsified_cnt += 1;
@ -147,10 +159,17 @@ bool Circuit::is_valid_circuit() {
printf("WRONG-ASSGIN: %s \n", g->name.c_str());
return false;
}
// 检查 fault_satified 和 value propagate 的关系
int sa0 = g->propagate && g->value;
int sa1 = g->propagate && !g->value;
assert(g->fault_satisfied[0] == sa0);
assert(g->fault_satisfied[1] == sa1);
// 检查 PO 的传播设定是否正确
if(g->po) {
if(g->fault_satisfied[g->value] != 0 || g->fault_satisfied[!g->value] == 0 ) {
if(g->fault_satisfied[g->value] || !g->fault_satisfied[!g->value] || !g->propagate) {
printf("WRONG-PO: %s \n", g->name.c_str());
}
continue;
@ -158,26 +177,7 @@ bool Circuit::is_valid_circuit() {
// 非 PO 情况下检查故障传播是否正确
bool sa0 = false;
bool sa1 = false;
for(Gate* out : g->outputs) {
if(out->cal_value() != out->value) {
assert(out->stem);
continue;
}
g->value = !g->value;
if(out->cal_value() != out->value) {
sa0 |= out->propagate && !g->value;
sa1 |= out->propagate && g->value;
}
g->value = !g->value;
}
if(sa0 != g->fault_satisfied[0] || sa1 != g->fault_satisfied[1]) {
if(!g->stem && (g->cal_sa(0) != g->fault_satisfied[0] || g->cal_sa(1) != g->fault_satisfied[1])) {
printf("WRONG-SA: %s \n", g->name.c_str());
return false;
}

View File

@ -32,9 +32,11 @@ public:
bool stem_satisified;
int stem_cost;
// 和求得的值是否一致
bool propagate_satisfied;
int propagate_cost;
// 这个由 value 和 propagate 决定
int fault_satisfied[2];
int fault_cost[2];
@ -44,6 +46,8 @@ public:
std::unordered_map<Gate*, std::pair<int, int>> sa_by_out;
int cal_value();
bool cal_sa(bool x);
//bool sa(bool x);
};
class Fault {
@ -102,12 +106,14 @@ void ls_update_weight();
void ls_init_data_structs();
void ls_block_recal(Gate* stem);
void ls_block_recal_value(Gate* stem);
void ls_block_recal_fault(Gate* stem);
Gate* ls_pick_good_var();
Gate* ls_pick_falsified_var();
Gate* ls_pick_good_var(bool &value, bool &propagate);
Gate* ls_pick_falsified_var(bool &value, bool &propagate);
void ls_update(Gate* stem);
ll ls_pick_score(Gate* stem);
ll ls_pick_score(Gate* stem, bool value, bool propagate);
ll ls_score();
};

View File

@ -2,6 +2,35 @@
#include "assert.h"
// bool Gate::sa(bool x) {
// if(x == 0) {
// return propagate && value;
// } else {
// return propagate && !value;
// }
// }
bool Gate::cal_sa(bool x) {
bool sa0 = 0;
bool sa1 = 0;
for(Gate* out : outputs) {
if(!out->propagate) continue;
if(out->cal_value() != out->value) continue;
this->value = !this->value;
bool detect = out->cal_value() != out->value;
this->value = !this->value;
if(!detect) continue;
sa0 |= this->value;
sa1 |= !this->value;
}
if(x == 0) return sa0;
else return sa1;
}
int Gate::cal_value() {
int res;

277
ls.cpp
View File

@ -21,18 +21,25 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
while(true) {
Gate* picked_stem = ls_pick_good_var();
bool value, propagate;
Gate* picked_stem = ls_pick_good_var(value, propagate);
if(picked_stem == nullptr) {
ls_update_weight();
picked_stem = ls_pick_falsified_var();
picked_stem = ls_pick_falsified_var(value, propagate);
printf("[UP] propagate: %lld, stem: %lld, fault:%lld. propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", propagate_total_cost, stem_total_cost, fault_total_cost, propagate_falsified_cnt, stem_falsified_cnt, fault_falsified_cnt);
} else {
printf("pick: %s value: %d propagate: %d\n", picked_stem->name.c_str(), value, propagate);
printf("[LS] propagate: %lld, stem: %lld, fault:%lld. propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", propagate_total_cost, stem_total_cost, fault_total_cost, propagate_falsified_cnt, stem_falsified_cnt, fault_falsified_cnt);
}
picked_stem->value = value;
picked_stem->propagate = propagate;
ls_update(picked_stem);
//assert(is_valid_circuit());
if(stem_falsified_cnt == 0 && propagate_falsified_cnt == 0) {
printf("FIND SOLUTION!\n");
printf("[SOL] propagate: %lld, stem: %lld, fault:%lld. propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", propagate_total_cost, stem_total_cost, fault_total_cost, propagate_falsified_cnt, stem_falsified_cnt, fault_falsified_cnt);
@ -63,7 +70,7 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
return true;
}
Gate* Circuit::ls_pick_falsified_var() {
Gate* Circuit::ls_pick_falsified_var(bool &value, bool &propagate) {
std::vector<Gate*> candidates;
for(Gate *g : stems) {
if(g->po) continue;
@ -79,7 +86,8 @@ Gate* Circuit::ls_pick_falsified_var() {
return pick;
}
Gate* Circuit::ls_pick_good_var() {
Gate* Circuit::ls_pick_good_var(bool &value, bool &propagate) {
Gate* stem = nullptr;
ll max_score = 0;
@ -97,17 +105,20 @@ Gate* Circuit::ls_pick_good_var() {
}
const int T = 100;
int t = 0;
for(int i=0; i<stems_random.size(); i++) {
for(int i=0; i < stems_random.size() && i < T; i++) {
Gate* t_stem = stems_random[i];
ll t_score = ls_pick_score(t_stem);
if(t_score > max_score) {
max_score = t_score;
stem = t_stem;
for(int j=0; j<4; j++) {
if((j&1) == t_stem->value && (j&2) == t_stem->propagate) continue;
ll t_score = ls_pick_score(t_stem, j&1, j&2);
if(t_score > max_score) {
max_score = t_score;
stem = t_stem;
value = j & 1;
propagate = j & 2;
}
}
if(t_score > 0) t++;
if(i >= T) break;
}
if(max_score > 0) {
@ -115,7 +126,6 @@ Gate* Circuit::ls_pick_good_var() {
} else {
return nullptr;
}
}
void Circuit::ls_update_weight() {
@ -177,26 +187,53 @@ void Circuit::ls_update(Gate* stem) {
suc->CC = 1;
}
ls_block_recal(stem);
//assert(is_valid_circuit());
}
ll Circuit::ls_pick_score(Gate* stem) {
ll Circuit::ls_pick_score(Gate* stem, bool value, bool propagate) {
ll old_score = ls_score();
//printf("[pick: %s\n", stem->name.c_str());
//stem->value = !stem->value;
ll old_score1 = ls_score();
// print_gates();
// printf("--------------");
// printf("[wrong] propagate: %lld, stem: %lld, fault:%lld\n", Circuit::propagate_total_cost, Circuit::stem_total_cost, Circuit::fault_total_cost);
// printf("[wrong] propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", Circuit::propagate_falsified_cnt, Circuit::stem_falsified_cnt, Circuit::fault_falsified_cnt);
bool old_value = stem->value;
bool old_propagate = stem->propagate;
stem->value = value;
stem->propagate = propagate;
ls_update(stem);
ll new_score = ls_score();
// print_gates();
// printf("--------------");
//stem->value = !stem->value;
stem->value = old_value;
stem->propagate = old_propagate;
ls_update(stem);
old_score = std::max(old_score, ls_score());
ll old_score2 = ls_score();
// print_gates();
// printf("--------------");
return new_score - old_score;
assert(old_score1 == old_score2);
// if(old_score1 != old_score2) {
// printf("old1: %lld old2: %lld\n", old_score1, old_score2);
// printf("[wrong] propagate: %lld, stem: %lld, fault:%lld\n", Circuit::propagate_total_cost, Circuit::stem_total_cost, Circuit::fault_total_cost);
// printf("[wrong] propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", Circuit::propagate_falsified_cnt, Circuit::stem_falsified_cnt, Circuit::fault_falsified_cnt);
// exit(-1);
// }
return new_score - old_score1;
}
ll Circuit::ls_score() {
@ -207,36 +244,47 @@ ll Circuit::ls_score() {
void Circuit::ls_init_circuit(const std::unordered_set<Fault*> &faults) {
fault_falsified_cnt = faults.size();
stem_falsified_cnt = stems.size();
propagate_falsified_cnt = stems.size();
// fault_falsified_cnt = faults.size();
// stem_falsified_cnt = stems.size();
// propagate_falsified_cnt = stems.size();
for(Fault* f : faults) {
f->gate->fault_cost[f->type] = 1;
}
for(Gate* s : stems) {
s->stem_cost = 1;
stem_total_cost += s->stem_cost;
stem_falsified_cnt += 1;
s->propagate_cost = 1;
propagate_total_cost += s->propagate_cost;
}
for(Fault* f : faults) {
f->gate->fault_cost[f->type] = 1;
fault_total_cost += f->gate->fault_cost[f->type];
propagate_falsified_cnt += 1;
}
for(Gate* g : gates) {
g->fault_satisfied[0] = 0;
g->fault_satisfied[1] = 0;
}
g->CC = 1;
fault_total_cost += g->fault_cost[0];
fault_total_cost += g->fault_cost[1];
fault_falsified_cnt += 2;
}
for(Gate* s : stems) {
s->value = rand() % 2;
s->propagate = rand() % 2;
}
for(int i=stems.size()-1; i>=0; i--) {
ls_update(stems[i]);
ls_block_recal(stems[i]);
}
assert(is_valid_circuit());
if(!is_valid_circuit()) {
print_gates();
exit(-1);
}
//assert(is_valid_circuit())
}
void Circuit::ls_init_data_structs() {
@ -267,6 +315,20 @@ void Circuit::ls_init_data_structs() {
void Circuit::ls_block_recal(Gate* stem) {
ls_block_recal_value(stem);
std::unordered_set<Gate*> pres;
for(Gate* suc : stem->suc_stems) {
ls_block_recal_fault(suc);
}
ls_block_recal_fault(stem);
}
void Circuit::ls_block_recal_value(Gate* stem) {
// 修改当前节点的值信息
if(stem->cal_value() == stem->value && !stem->stem_satisified) {
stem->stem_satisified = true;
stem_total_cost -= stem->stem_cost;
@ -279,20 +341,9 @@ void Circuit::ls_block_recal(Gate* stem) {
stem_falsified_cnt += 1;
}
// 当 po 的值变化
if(stem->po) {
if(stem->fault_satisfied[stem->value]) {
stem->fault_satisfied[stem->value] = false;
fault_total_cost += stem->fault_cost[stem->value];
fault_falsified_cnt += 1;
}
assert((stem->cal_value() == stem->value) == stem->stem_satisified);
if(!stem->fault_satisfied[!stem->value]) {
stem->fault_satisfied[!stem->value] = true;
fault_total_cost -= stem->fault_cost[!stem->value];
fault_falsified_cnt -= 1;
}
}
// 更新后继块的值
std::queue<Gate*> q;
std::unordered_map<Gate*, int> used;
@ -313,12 +364,8 @@ void Circuit::ls_block_recal(Gate* stem) {
}
}
assert(q.empty());
used.clear();
// 更新后继 stem 的关系
for(Gate* stem : stem->suc_stems) {
q.push(stem);
if(stem->cal_value() == stem->value && !stem->stem_satisified) {
stem->stem_satisified = true;
stem_total_cost -= stem->stem_cost;
@ -331,77 +378,107 @@ void Circuit::ls_block_recal(Gate* stem) {
stem_falsified_cnt += 1;
}
}
}
void Circuit::ls_block_recal_fault(Gate* stem) {
// po 的 propagte 不可变
if(stem->po) { stem->propagate = true; }
// 根据 value 和 propagete 更新当前节点的 fault_satisfied
for(int i=0; i<=1; i++) {
int sa[2];
sa[0] = stem->propagate && stem->value == 1;
sa[1] = stem->propagate && stem->value == 0;
if(sa[i] != stem->fault_satisfied[i]) {
if(stem->fault_satisfied[i]) {
stem->fault_satisfied[i] = false;
fault_total_cost += stem->fault_cost[i];
fault_falsified_cnt++;
} else {
stem->fault_satisfied[i] = true;
fault_total_cost -= stem->fault_cost[i];
fault_falsified_cnt--;
}
}
}
// 更新 propagate_satfisfied
if(stem->cal_sa(0) == stem->fault_satisfied[0]
&& stem->cal_sa(1) == stem->fault_satisfied[1]
&& !stem->propagate_satisfied) {
stem->propagate_satisfied = true;
propagate_total_cost -= stem->propagate_cost;
propagate_falsified_cnt -= 1;
}
if((stem->cal_sa(0) != stem->fault_satisfied[0] || stem->cal_sa(1) != stem->fault_satisfied[1])
&& stem->propagate_satisfied) {
stem->propagate_satisfied = false;
propagate_total_cost += stem->propagate_cost;
propagate_falsified_cnt += 1;
}
std::queue<Gate*> q;
std::unordered_map<Gate*, int> used;
q.push(stem);
// 修改之前的所有 gate 错误信息直到 stem 边界
while(!q.empty()) {
Gate *g = q.front();
q.pop();
used[g] = false;
bool right_value = (g->cal_value() == g->value);
// 遍历输入迭代
for(Gate* in : g->inputs) {
in->value = !in->value;
bool input_detected = (g->cal_value() != g->value);
in->value = !in->value;
bool sa0 = right_value && input_detected && g->fault_satisfied[!g->value] && in->value;
bool sa1 = right_value && input_detected && g->fault_satisfied[!g->value] && !in->value;
in->sa_by_out[g] = std::make_pair(sa0, sa1);
bool new_sa[2];
new_sa[0] = new_sa[1] = 0;
in->fault_satisfied[0] = in->fault_satisfied[1] = 0;
for(Gate* out : in->outputs) {
auto &p = in->sa_by_out[out];
new_sa[0] |= p.first;
new_sa[1] |= p.second;
}
// 输入如果是 stem则更新 propagate 信息是否匹配
if(in->stem) {
if(in->propagate_satisfied && (in->fault_satisfied[0] != new_sa[0] || in->fault_satisfied[1] != new_sa[1])) {
in->propagate_satisfied = false;
propagate_falsified_cnt++;
propagate_total_cost += in->propagate_cost;
if(in->cal_sa(0) == in->fault_satisfied[0]
&& in->cal_sa(1) == in->fault_satisfied[1]
&& !in->propagate_satisfied) {
in->propagate_satisfied = true;
propagate_total_cost -= in->propagate_cost;
propagate_falsified_cnt -= 1;
}
if(!in->propagate_satisfied && (in->fault_satisfied[0] == new_sa[0] && in->fault_satisfied[1] == new_sa[1])) {
in->propagate_satisfied = true;
propagate_falsified_cnt--;
propagate_total_cost -= in->propagate_cost;
if((in->cal_sa(0) != in->fault_satisfied[0] || in->cal_sa(1) != in->fault_satisfied[1])
&& in->propagate_satisfied) {
in->propagate_satisfied = false;
propagate_total_cost += in->propagate_cost;
propagate_falsified_cnt += 1;
}
}
// 如果是中间节点,直接更新值
else {
} else {
if(!new_sa[0] != in->fault_satisfied[0]) {
if(in->fault_satisfied[0]) {
fault_total_cost += in->fault_cost[0];
fault_falsified_cnt += 1;
} else {
fault_total_cost -= in->fault_cost[0];
fault_falsified_cnt -= 1;
for(int i=0; i<=1; i++) {
int sa[2];
sa[0] = in->cal_sa(0);
sa[1] = in->cal_sa(1);
if(sa[i] != in->fault_satisfied[i]) {
if(in->fault_satisfied[i]) {
in->fault_satisfied[i] = false;
fault_total_cost += in->fault_cost[i];
fault_falsified_cnt++;
} else {
in->fault_satisfied[i] = true;
fault_total_cost -= in->fault_cost[i];
fault_falsified_cnt--;
}
}
}
if(!new_sa[1] != in->fault_satisfied[1]) {
if(in->fault_satisfied[1]) {
fault_total_cost += in->fault_cost[1];
fault_falsified_cnt += 1;
} else {
fault_total_cost -= in->fault_cost[1];
fault_falsified_cnt -= 1;
}
}
in->propagate = in->fault_satisfied[0] || in->fault_satisfied[1];
in->fault_satisfied[0] = new_sa[0];
in->fault_satisfied[1] = new_sa[1];
if(!in->stem && !used[in]) {
if(!used[in]) {
used[in] = true;
q.push(in);
}
}
}
}
}

14488
output.txt

File diff suppressed because it is too large Load Diff

View File

@ -73,7 +73,7 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[0];
gate->fault_satisfied[0] = gate->fault_satisfied[1] = false;
gate->propagate = false;
gate->stem = false;
gate->pi = false;
gate->po = false;
@ -113,7 +113,7 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[2];
gate->type = Gate::INPUT;
gate->fault_satisfied[0] = gate->fault_satisfied[1] = false;
gate->propagate = false;
gate->stem = true;
gate->pi = true;
gate->po = false;