Compare commits

...

9 Commits
newc ... main

Author SHA1 Message Date
73ab1d168a 按照stem翻转 2023-03-04 10:39:33 +00:00
b2df76e1de 增加仿真的步骤 2023-03-04 10:28:39 +00:00
69be6704ed 更新 .gitignore 2023-03-02 07:21:46 +00:00
82ea887b6f 删除output.txt 2023-03-02 07:03:59 +00:00
93c30f5280 rm 2023-03-02 07:03:34 +00:00
be80ecc334 修改了一下权重 2023-03-02 07:03:29 +00:00
73023786dd 传播状态独立,但是效果不好 2023-03-01 07:14:23 +00:00
2dffd855c5 无错误的增量式数据结构 2023-02-28 08:45:13 +00:00
a9e6804d15 修改数据结构,通过编译 2023-02-27 02:49:59 +00:00
11 changed files with 3076 additions and 16523 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o
*.d
.vscode
.vscode
*.txt

BIN
atpg

Binary file not shown.

View File

@ -5,19 +5,29 @@
#include <unordered_set>
#include "assert.h"
ll Circuit::stem_total_cost;
int Circuit::stem_falsified_cnt;
ll Circuit::propagate_total_cost;
int Circuit::propagate_falsified_cnt;
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;
}
gate->stem = true;
if(gate->stem) {
stems.push_back(gate);
}
}
for(Gate *g : gates) {
if(g->isPI) continue;
if(g->pi) continue;
std::queue<Gate*> q;
std::unordered_map<Gate*, bool> used;
q.push(g);
@ -37,9 +47,8 @@ void Circuit::init_stems() {
//printf("pre: %s %d\n", g->name.c_str(), g->pre_stems.size());
}
for(Gate *g : gates) {
if(g->isPO) continue;
if(g->po) continue;
std::queue<Gate*> q;
std::unordered_map<Gate*, bool> used;
q.push(g);
@ -90,7 +99,7 @@ void Circuit::init_topo_index() {
void Circuit::print_gates() {
static const char* type2name[9] = {"AND", "NAND", "OR", "NOR", "XOR", "XNOR", "NOT", "BUF", "IN"};
for(Gate* gate : gates) {
printf("Gate: %3s (t:%4s v:%d pi:%d po:%d s:%d p:%d s0:%d s1:%d) Inputs:", gate->name.c_str(), type2name[gate->type], gate->value, gate->isPI, gate->isPO, gate->stem, gate->is_propagated(), gate->sa[0], gate->sa[1]);
printf("Gate: %3s (t:%4s v:%d pi:%d po:%d s:%d p:%d s0:%d s1:%d) Inputs:", gate->name.c_str(), type2name[gate->type], gate->value, gate->pi, gate->po, gate->stem, gate->propagate, gate->fault_satisfied[0], gate->fault_satisfied[1]);
for(Gate* in : gate->inputs) {
printf(" %s", in->name.c_str());
}
@ -100,50 +109,67 @@ void Circuit::print_gates() {
bool Circuit::is_valid_circuit() {
ll flip_total_weight = 0;
ll stem_total_weight = 0;
ll fault_total_weight = 0;
ll stem_total_cost = 0;
int stem_falsified_cnt = 0;
int flip_total_cnt = 0;
int stem_total_cnt = 0;
int fault_total_cnt = 0;
ll propagate_total_cost = 0;
int propagate_falsified_cnt = 0;
//printf("flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
ll fault_total_cost = 0;
int fault_falsified_cnt = 0;
//printf("propagate: %d, stem: %d, fault:%d\n", propagate_total_cost, stem_total_cost, fault_total_cost);
for(Gate* g : gates) {
if(flip_need_update[g->id]) {
flip_total_weight += flip_weight[g->id];
flip_total_cnt++;
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_weight += stem_weight[g->id];
stem_total_cost += g->stem_cost;
stem_falsified_cnt += 1;
}
if(g->stem && g->cal_value() == g->value) {
stem_total_cnt++;
if(!g->fault_satisfied[0]) {
fault_total_cost += g->fault_cost[0];
fault_falsified_cnt += 1;
}
if(g->sa[0]) {
fault_total_weight += fault_weight[g->id][0];
fault_total_cnt += 1;
}
if(g->sa[1]) {
fault_total_weight += fault_weight[g->id][1];
fault_total_cnt += 1;
if(!g->fault_satisfied[1]) {
fault_total_cost += g->fault_cost[1];
fault_falsified_cnt += 1;
}
// 检查门的赋值情况
if(g->cal_value() != g->value) {
if(!g->stem && g->cal_value() != g->value) {
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->isPO) {
if(g->sa[g->value] != 0 || g->sa[!g->value] == 0 ) {
if(g->po) {
if(g->fault_satisfied[g->value] || !g->fault_satisfied[!g->value] || !g->propagate) {
printf("WRONG-PO: %s \n", g->name.c_str());
}
continue;
@ -151,42 +177,23 @@ 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->is_propagated() && !g->value;
sa1 |= out->is_propagated() && g->value;
}
g->value = !g->value;
}
if(sa0 != g->sa[0] || sa1 != g->sa[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;
}
}
if(this->flip_total_weight != flip_total_weight || this->stem_total_weight != stem_total_weight || this->fault_total_weight != fault_total_weight) {
if(Circuit::propagate_total_cost != propagate_total_cost || Circuit::stem_total_cost != stem_total_cost || Circuit::fault_total_cost != fault_total_cost) {
printf("CIRCUIT CHECK FAILED!\n");
printf("[wrong] flip: %d, stem: %d, fault:%d\n", this->flip_total_weight, this->stem_total_weight, this->fault_total_weight);
printf("[right] flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
printf("[wrong] propagate: %lld, stem: %lld, fault:%lld\n", Circuit::propagate_total_cost, Circuit::stem_total_cost, Circuit::fault_total_cost);
printf("[right] propagate: %lld, stem: %lld, fault:%lld\n", propagate_total_cost, stem_total_cost, fault_total_cost);
return false;
}
if(this->flip_total_cnt != flip_total_cnt || this->stem_total_cnt != stem_total_cnt || this->fault_total_cnt != fault_total_cnt) {
if(Circuit::propagate_falsified_cnt != propagate_falsified_cnt || Circuit::stem_falsified_cnt != stem_falsified_cnt || Circuit::fault_falsified_cnt != fault_falsified_cnt) {
printf("CIRCUIT CHECK FAILED!\n");
printf("[wrong] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", this->flip_total_cnt, this->stem_total_cnt, this->fault_total_cnt);
printf("[right] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_cnt, stem_total_cnt, fault_total_weight);
printf("[wrong] propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", Circuit::propagate_falsified_cnt, Circuit::stem_falsified_cnt, Circuit::fault_falsified_cnt);
printf("[right] propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", propagate_falsified_cnt, stem_falsified_cnt, fault_falsified_cnt);
return false;
}

View File

@ -10,25 +10,44 @@ using ll = long long;
class Gate {
public:
// gate structral info in circuit
int id;
std::string name;
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
int value;
bool sa[2];
bool stem;
bool isPI;
bool isPO;
std::unordered_map<Gate*, std::pair<int, int>> sa_by_out;
bool pi;
bool po;
std::vector<Gate*> pre_stems;
std::vector<Gate*> suc_stems;
std::vector<Gate*> outputs;
std::vector<Gate*> inputs;
bool is_propagated();
// Status Define (Two var)
bool value;
bool propagate;
// local search
bool stem_satisified;
int stem_cost;
// 和求得的值是否一致
bool propagate_satisfied;
int propagate_cost;
// 这个由 value 和 propagate 决定
int fault_satisfied[2];
int fault_cost[2];
// configuration checking
bool CC;
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 {
@ -60,48 +79,45 @@ void init_stems();
// local search
// shared info
static const int STEM_INC = 2;
static const int STEM_COST_MAX = 1e9;
static ll stem_total_cost;
static int stem_falsified_cnt;
static const int PROPAGATE_INC = 2;
static const int PROPAGATE_COST_MAX = 1e9;
static ll propagate_total_cost;
static int propagate_falsified_cnt;
static const int FAULT_INC = 1;
static const int FAULT_COST_MAX = 20;
static ll fault_total_cost;
static int fault_falsified_cnt;
bool local_search(std::unordered_set<Fault*> &faults);
// incremental flip struct
const double SP = 0.01;
const int FLIP_INC = 1;
const int FLIP_WEIGHT_MAX = 1e9;
int* CC;
ll flip_total_weight;
int flip_total_cnt;
int* flip_weight;
int* flip_need_update;
std::vector<Gate*> flip_update_queue;
// incremental stem struct
const int STEM_INC = 10;
const int STEM_WEIGHT_MAX = 1e9;
ll stem_total_weight;
int stem_total_cnt;
int* stem_weight;
int* stem_satisfied;
const int FAULT_INC = 1;
const int FAULT_WEIGHT_MAX = 20;
ll fault_total_weight;
int fault_total_cnt;
int** fault_weight;
int** fault_detected;
void ls_init_circuit();
void ls_init_weight(const std::unordered_set<Fault*> &faults);
void ls_init_circuit(const std::unordered_set<Fault*> &faults);
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(bool &value, bool &propagate);
Gate* ls_pick_falsified_var(bool &value, bool &propagate);
void ls_flip(Gate* stem);
void ls_update(Gate* stem);
ll ls_pick_score(Gate* stem);
ll ls_pick_score(Gate* stem, bool value, bool propagate);
ll ls_score();
// simulator
int** simulate();
};

1398
cost.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,8 +2,38 @@
#include "assert.h"
bool Gate::is_propagated() {
return sa[0] || sa[1];
// bool Gate::sa(bool x) {
// if(x == 0) {
// return propagate && value;
// } else {
// return propagate && !value;
// }
// }
bool Gate::cal_sa(bool x) {
if(po) {
if(x == 0) return value;
else return !value;
}
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() {

685
ls.cpp
View File

@ -1,5 +1,6 @@
#include "circuit.h"
#include <cstddef>
#include <queue>
#include <unordered_set>
#include <unordered_map>
@ -13,105 +14,68 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
// 初始化并重置所有 ls 数据结构
ls_init_data_structs();
// 赋值初始权重
ls_init_weight(faults);
// 随机生成初始电路
ls_init_circuit();
ls_init_circuit(faults);
printf("local search!\n");
while(true) {
Gate* stem = nullptr;
ll max_score = 0;
std::vector<Gate*> stems_random;
std::vector<Gate*> candidates;
bool value, propagate;
Gate* picked_stem = ls_pick_good_var(value, propagate);
for(int i=0; i<stems.size(); i++) {
if(CC[stems[i]->id]) {
stems_random.push_back(stems[i]);
}
}
for(int i=0; i<stems_random.size(); i++) {
std::swap(stems_random[i], stems_random[rand()%stems_random.size()]);
}
const int T = 100;
int t = 0;
for(int i=0; i<stems_random.size(); 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;
}
if(t_score > 0) t++;
if(i >= T) break;
}
if(max_score > 0) {
// printf("FLIP: %s (+%lld)\n", stem->name.c_str(), max_score);
// printf("[LS] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt);
ls_flip(stem);
CC[stem->id] = 0;
for(Gate* pre : stem->pre_stems) {
CC[pre->id] = 1;
}
for(Gate* suc : stem->suc_stems) {
CC[suc->id] = 1;
}
} else {
if(picked_stem == nullptr) {
ls_update_weight();
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);
while(!flip_update_queue.empty()) {
Gate* g = flip_update_queue.back();
flip_update_queue.pop_back();
if(!flip_need_update[g->id]) continue;
flip_need_update[g->id] = false;
flip_total_weight -= flip_weight[g->id];
flip_total_cnt -= 1;
ls_update(g);
int** sa = simulate();
int cnt = 0;
static int max_cnt = 0;
for(Fault* f : faults) {
if(sa[f->gate->id][f->type]) {
cnt++;
}
}
max_cnt = std::max(max_cnt, cnt);
printf("[SOL] detect-faults: %d max-faults: %d\n", cnt, max_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);
}
if(stem_total_cnt == stems.size() && flip_total_cnt == 0) {
printf("FIND SOLUTION!\n");
printf("[SOL] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt);
break;
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);
break;
}
if(fault_total_cost == 6340 && propagate_falsified_cnt == 19 && stem_falsified_cnt == 1 && fault_falsified_cnt == 317) {
static int cnt = 0;
cnt++;
if(cnt == 100) {
print_gates();
for(Gate* s: stems) {
if(!s->propagate_satisfied) {
printf(" > propagate falsified: %s\n", s->name.c_str());
printf("s0: %d s1: %d, cal_s0: %d, cal_s1: %d\n", s->fault_satisfied[0], s->fault_satisfied[1], s->cal_sa(0), s->cal_sa(1));
printf("score: %lld\n", ls_pick_score(s, 1, 0));
}
}
exit(0);
}
std::vector<Gate*> candidates;
for(Gate *g : stems) {
if(g->isPO) continue;
if(stem_satisfied[g->id]) continue;
candidates.push_back(g);
}
if(candidates.size() == 0) {
candidates.push_back(stems[rand()%stems.size()]);
}
Gate* pick = candidates[rand()%candidates.size()];
ls_flip(pick);
CC[pick->id] = 0;
for(Gate* pre : pick->pre_stems) {
CC[pre->id] = 1;
}
for(Gate* suc : pick->suc_stems) {
CC[suc->id] = 1;
}
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);
}
}
@ -124,90 +88,133 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
std::unordered_set<Fault*> tmp = faults;
for(Fault* f : tmp) {
if(f->gate->sa[f->type]) {
if(f->gate->fault_satisfied[f->type]) {
faults.erase(f);
}
}
if(tmp.size() == faults.size()) pattern--;
printf("coverage: %.4f\tpattern: %d\tbefore: %d\tnow: %d\n", (double)(original_faults - faults.size()) / (original_faults), ++pattern, tmp.size(), faults.size());
printf("coverage: %.4f\tpattern: %d\tbefore: %ld\tnow: %ld\n", (double)(original_faults - faults.size()) / (original_faults), ++pattern, tmp.size(), faults.size());
//if(tmp.size() == faults.size()) return false;
return true;
}
Gate* Circuit::ls_pick_falsified_var(bool &value, bool &propagate) {
std::vector<Gate*> candidates;
for(Gate *g : stems) {
if(g->po) continue;
if(g->stem_satisified) continue;
if(g->propagate_satisfied) continue;
candidates.push_back(g);
}
if(candidates.size() == 0) {
candidates.push_back(stems[rand()%stems.size()]);
}
Gate* pick = candidates[rand()%candidates.size()];
value = !pick->value;
propagate = !propagate;
return pick;
}
Gate* Circuit::ls_pick_good_var(bool &value, bool &propagate) {
Gate* stem = nullptr;
ll max_score = 0;
std::vector<Gate*> stems_random;
std::vector<Gate*> candidates;
for(Gate* s : stems) {
if(s->CC) {
stems_random.push_back(s);
}
}
for(int i=0; i<stems_random.size(); i++) {
std::swap(stems_random[i], stems_random[rand()%stems_random.size()]);
}
const int T = 25;
for(int i=0; i < stems_random.size() && i < T; i++) {
Gate* t_stem = stems_random[i];
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(max_score > 0) {
return stem;
} else {
return nullptr;
}
}
void Circuit::ls_update_weight() {
//STEM_INC += 5;
if(rand() % 10000 <= SP * 10000) {
for(Gate* g : gates) {
if(g->stem && stem_satisfied[g->id] && (stem_weight[g->id] - STEM_INC >= 1)) {
stem_weight[g->id] -= STEM_INC;
for(Gate* suc : g->suc_stems) {
if(stem_weight[suc->id] + STEM_INC <= STEM_WEIGHT_MAX) {
stem_weight[suc->id] += STEM_INC;
if(!stem_satisfied[suc->id]) {
stem_total_weight += STEM_INC;
}
}
}
}
}
if(false) {
} else {
for(Gate* g : gates) {
if(flip_need_update[g->id] && (flip_weight[g->id] + FLIP_INC < FLIP_WEIGHT_MAX)) {
flip_weight[g->id] += FLIP_INC;
flip_total_weight += FLIP_INC;
}
if(g->stem && !stem_satisfied[g->id] && (stem_weight[g->id] + STEM_INC < STEM_WEIGHT_MAX)) {
stem_weight[g->id] += STEM_INC;
stem_total_weight += STEM_INC;
if(g->stem && !g->propagate_satisfied && (g->propagate_cost + PROPAGATE_INC <= PROPAGATE_COST_MAX)) {
g->propagate_cost += PROPAGATE_INC;
propagate_total_cost += PROPAGATE_INC;
for(Gate* suc : g->suc_stems) {
if(stem_weight[suc->id] - STEM_INC > 1) {
stem_weight[suc->id] -= STEM_INC;
if(!stem_satisfied[suc->id]) {
stem_total_weight -= STEM_INC;
for(Gate* pre : g->pre_stems) {
if(pre->propagate_cost - PROPAGATE_INC >= 1) {
pre->propagate_cost -= PROPAGATE_INC;
if(!pre->propagate_satisfied) {
propagate_total_cost -= PROPAGATE_INC;
}
}
}
}
for(int i=0; i<=1; i++) {
for(Gate* pre : g->suc_stems) {
// int inc = 0.2 * fault_weight[pre->id][i];
// inc = std::max(1, inc);
if(g->stem && !g->stem_satisified && (g->stem_cost + STEM_INC <= STEM_COST_MAX)) {
g->stem_cost += STEM_INC;
stem_total_cost += STEM_INC;
// if(g->type == Gate::NAND || g->type == Gate::NOR || g->type == Gate::NOT || g->type == Gate::XNOR) {
// if(fault_weight[g->id][!pre->value] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[g->id][!pre->value] += inc;
// if(g->sa[!pre->value]) fault_total_weight += inc;
// }
// } else {
// if(fault_weight[g->id][pre->value] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[g->id][pre->value] += inc;
// if(g->sa[pre->value]) fault_total_weight += inc;
// }
// }
if(g->stem_satisified)
// if(fault_weight[suc->id][1] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[suc->id][1] += inc;
// if(suc->sa[1]) fault_total_weight += inc;
// }
for(Gate* suc : g->suc_stems) {
if(suc->stem_cost - STEM_INC >= 1) {
suc->stem_cost -= STEM_INC;
if(!suc->stem_satisified) {
stem_total_cost -= STEM_INC;
}
}
}
}
if(!g->sa[0] && fault_weight[g->id][0] > 0 && (fault_weight[g->id][0] + FAULT_INC < FAULT_WEIGHT_MAX)) {
fault_weight[g->id][0] += FAULT_INC;
if(!g->fault_satisfied[0] && g->fault_cost[0] > 0 && (g->fault_cost[0] + FAULT_INC <= FAULT_COST_MAX)) {
g->fault_cost[0] += FAULT_INC;
fault_total_cost += FAULT_INC;
}
if(!g->sa[1] && fault_weight[g->id][1] > 0 && (fault_weight[g->id][1] + FAULT_INC < FAULT_WEIGHT_MAX)) {
fault_weight[g->id][1] += FAULT_INC;
if(!g->fault_satisfied[1] && g->fault_cost[1] > 0 && (g->fault_cost[1] + FAULT_INC <= FAULT_COST_MAX)) {
g->fault_cost[1] += FAULT_INC;
fault_total_cost += FAULT_INC;
}
}
}
}
@ -217,161 +224,181 @@ bool cmp(Gate* a, Gate *b) {
return a->id > b->id;
}
void Circuit::ls_flip(Gate* stem) {
stem->value = !stem->value;
ls_block_recal(stem);
}
void Circuit::ls_update(Gate* stem) {
stem->CC = 0;
for(Gate* pre : stem->pre_stems) {
pre->CC = 1;
}
for(Gate* suc : stem->suc_stems) {
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());
ls_flip(stem);
ll old_score1 = ls_score();
// print_gates();
// printf("--------------");
//printf("[before] propagate: %lld, stem: %lld, fault:%lld\n", Circuit::propagate_total_cost, Circuit::stem_total_cost, Circuit::fault_total_cost);
//printf("[before] 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("--------------");
ls_flip(stem);
//printf("[now] propagate: %lld, stem: %lld, fault:%lld\n", Circuit::propagate_total_cost, Circuit::stem_total_cost, Circuit::fault_total_cost);
//printf("[now] propagate_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", Circuit::propagate_falsified_cnt, Circuit::stem_falsified_cnt, Circuit::fault_falsified_cnt);
old_score = std::max(old_score, ls_score());
stem->value = old_value;
stem->propagate = old_propagate;
return new_score - old_score;
ls_update(stem);
ll old_score2 = ls_score();
// print_gates();
// printf("--------------");
//assert(old_score1 == old_score2);
// if(old_score1 != old_score2) {
// printf("old1: %lld old2: %lld\n", old_score1, old_score2);
//ed_cnt);
// exit(-1);
// }
return new_score - old_score1;
}
ll Circuit::ls_score() {
//ll score = -flip_total_weight -stem_total_weight + fault_total_weight;
ll score = -flip_total_weight -stem_total_weight + fault_total_weight;
ll score = - propagate_total_cost - stem_total_cost - fault_total_cost;
//ll score = - stem_total_cost - fault_total_cost;
return score;
}
void Circuit::ls_init_weight(const std::unordered_set<Fault*> &faults) {
for(Gate* s : stems) {
stem_weight[s->id] = 1;
stem_total_weight += stem_weight[s->id];
}
for(Fault* f : faults) {
fault_weight[f->gate->id][f->type] = 1;
}
for(Gate* s: stems) {
flip_weight[s->id] = 1;
}
}
void Circuit::ls_init_circuit() {
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();
for(Fault* f : faults) {
f->gate->fault_cost[f->type] = 1;
}
for(Gate* s : stems) {
s->stem_cost = STEM_INC;
stem_total_cost += s->stem_cost;
stem_falsified_cnt += 1;
s->propagate_cost = PROPAGATE_INC;
propagate_total_cost += s->propagate_cost;
propagate_falsified_cnt += 1;
}
for(Gate* g : gates) {
g->sa[0] = 0;
g->sa[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]);
}
while(!flip_update_queue.empty()) {
Gate* g = flip_update_queue.back();
flip_update_queue.pop_back();
if(!flip_need_update[g->id]) continue;
flip_need_update[g->id] = false;
flip_total_weight -= flip_weight[g->id];
flip_total_cnt -= 1;
ls_update(g);
if(!is_valid_circuit()) {
print_gates();
exit(-1);
}
//assert(is_valid_circuit())
}
void Circuit::ls_init_data_structs() {
const int MAX_LEN = gates.size() + 1;
if(flip_weight == nullptr) {
CC = new int[MAX_LEN];
propagate_total_cost = 0;
propagate_falsified_cnt = 0;
stem_total_cost = 0;
stem_falsified_cnt = 0;
flip_weight = new int[MAX_LEN];
flip_need_update = new int[MAX_LEN];
fault_total_cost = 0;
fault_falsified_cnt = 0;
stem_weight = new int[MAX_LEN];
stem_satisfied = new int[MAX_LEN];
for(Gate* g : gates) {
g->CC = 0;
fault_weight = new int*[MAX_LEN];
for(int i=0; i<MAX_LEN; i++) {
fault_weight[i] = new int[2];
}
fault_detected = new int*[MAX_LEN];
for(int i=0; i<MAX_LEN; i++) {
fault_detected[i] = new int[2];
}
g->propagate = 0;
g->propagate_cost = 0;
g->propagate_satisfied = 0;
}
g->stem_cost = 0;
g->stem_satisified = 0;
flip_total_weight = 0;
flip_total_cnt = 0;
stem_total_weight = 0;
stem_total_cnt = 0;
fault_total_weight = 0;
fault_total_cnt = 0;
for(int i=0; i<MAX_LEN; i++) {
CC[i] = 1;
flip_weight[i] = 0;
flip_need_update[i] = 0;
stem_weight[i] = 0;
stem_satisfied[i] = 0;
fault_weight[i][0] = 0;
fault_weight[i][1] = 0;
fault_detected[i][0] = 0;
fault_detected[i][1] = 0;
g->fault_satisfied[0] = g->fault_satisfied[1] = 0;
g->fault_cost[0] = g->fault_cost[1] = 0;
}
}
void Circuit::ls_block_recal(Gate* stem) {
if(flip_need_update[stem->id]) {
flip_need_update[stem->id] = false;
flip_total_weight -= flip_weight[stem->id];
flip_total_cnt -= 1;
ls_block_recal_value(stem);
std::unordered_set<Gate*> pres;
for(Gate* suc : stem->suc_stems) {
ls_block_recal_fault(suc);
}
if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){
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_satisfied[stem->id] = false;
stem_total_weight += stem_weight[stem->id];
stem_total_cnt -= 1;
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;
stem_falsified_cnt -= 1;
}
//printf("flip: %s\n", stem->name.c_str());
//stem->value = !stem->value;
if(stem->isPO) {
if(stem->sa[!stem->value] == false) {
fault_total_weight += fault_weight[stem->id][!stem->value];
fault_total_cnt += 1;
stem->sa[!stem->value] = true;
}
if(stem->sa[stem->value] == true) {
fault_total_weight -= fault_weight[stem->id][stem->value];
fault_total_cnt -= 1;
stem->sa[stem->value] = false;
}
if(stem->cal_value() != stem->value && stem->stem_satisified) {
stem->stem_satisified = false;
stem_total_cost += stem->stem_cost;
stem_falsified_cnt += 1;
}
assert((stem->cal_value() == stem->value) == stem->stem_satisified);
// 更新后继块的值
std::queue<Gate*> q;
std::unordered_map<Gate*, int> used;
std::vector<Gate*> suc_stems;
q.push(stem);
@ -380,10 +407,7 @@ void Circuit::ls_block_recal(Gate* stem) {
q.pop();
used[g] = false;
for(Gate* out : g->outputs) {
if(out->stem) {
suc_stems.push_back(out);
continue;
}
if(out->stem) continue;
out->value = out->cal_value();
if(!used[out]) {
used[out] = true;
@ -392,90 +416,121 @@ void Circuit::ls_block_recal(Gate* stem) {
}
}
assert(q.empty());
used.clear();
for(Gate* stem : suc_stems) {
q.push(stem);
if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){
stem_satisfied[stem->id] = true;
stem_total_weight -= stem_weight[stem->id];
stem_total_cnt += 1;
// 更新后继 stem 的关系
for(Gate* stem : stem->suc_stems) {
if(stem->cal_value() == stem->value && !stem->stem_satisified) {
stem->stem_satisified = true;
stem_total_cost -= stem->stem_cost;
stem_falsified_cnt -= 1;
}
if(stem->cal_value() != stem->value && stem_satisfied[stem->id]) {
stem_satisfied[stem->id] = false;
stem_total_weight += stem_weight[stem->id];
stem_total_cnt -= 1;
if(stem->cal_value() != stem->value && stem->stem_satisified) {
stem->stem_satisified = false;
stem_total_cost += stem->stem_cost;
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->sa[!g->value] && in->value;
bool sa1 = right_value && input_detected && g->sa[!g->value] && !in->value;
in->sa_by_out[g] = std::make_pair(sa0, sa1);
bool old_sa[2];
old_sa[0] = in->sa[0];
old_sa[1] = in->sa[1];
in->sa[0] = in->sa[1] = 0;
for(Gate* out : in->outputs) {
auto &p = in->sa_by_out[out];
in->sa[0] |= p.first;
in->sa[1] |= p.second;
}
if(in->stem && !in->isPI && (in->sa[0] != old_sa[0] || in->sa[1] != old_sa[1])) {
for(Gate* pre : in->pre_stems) {
if(flip_need_update[pre->id]) continue;
flip_need_update[pre->id] = true;
flip_update_queue.push_back(pre);
flip_total_weight += flip_weight[pre->id];
flip_total_cnt += 1;
// 输入如果是 stem则更新 propagate 信息是否匹配
if(in->stem) {
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(old_sa[0] != in->sa[0]) {
if(in->sa[0]) {
fault_total_weight += fault_weight[in->id][0];
fault_total_cnt += 1;
} else {
fault_total_weight -= fault_weight[in->id][0];
fault_total_cnt -= 1;
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 {
if(old_sa[1] != in->sa[1]) {
if(in->sa[1]) {
fault_total_weight += fault_weight[in->id][1];
fault_total_cnt += 1;
} else {
fault_total_weight -= fault_weight[in->id][1];
fault_total_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--;
}
}
}
in->propagate = in->fault_satisfied[0] || in->fault_satisfied[1];
if(!used[in]) {
used[in] = true;
q.push(in);
}
}
if(!in->stem && !used[in]) {
used[in] = true;
q.push(in);
}
}
}
}

1000
new.txt Normal file

File diff suppressed because it is too large Load Diff

16104
output.txt

File diff suppressed because it is too large Load Diff

View File

@ -73,10 +73,10 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[0];
gate->sa[0] = gate->sa[1] = false;
gate->propagate = false;
gate->stem = false;
gate->isPI = false;
gate->isPO = false;
gate->pi = false;
gate->po = false;
for(auto &in : ins) {
@ -113,10 +113,10 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[2];
gate->type = Gate::INPUT;
gate->sa[0] = gate->sa[1] = false;
gate->propagate = false;
gate->stem = true;
gate->isPI = true;
gate->isPO = false;
gate->pi = true;
gate->po = false;
name2gate.insert(std::make_pair(gate->name, gate));
gates.push_back(gate);
@ -141,7 +141,7 @@ void Circuit::parse_from_file(const char *filename) {
}
Gate* po = name2gate[po_name];
po->isPO = true;
po->po = true;
po->stem = true;
POs.push_back(po);
}

150
simulator.cpp Normal file
View File

@ -0,0 +1,150 @@
#include "circuit.h"
#include <assert.h>
#include <unordered_map>
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; i<g->inputs.size(); i++) {
res &= value[g->inputs[i]->id];
}
break;
case Gate::NAND:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res &= value[g->inputs[i]->id];
}
res = !res;
break;
case Gate::OR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res |= value[g->inputs[i]->id];
}
break;
case Gate::NOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res |= value[g->inputs[i]->id];
}
res = !res;
break;
case Gate::XOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res ^= value[g->inputs[i]->id];
}
break;
case Gate::XNOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.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->po) {
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; i<MAXN; i++) {
sa[i] = new int[2];
}
value = new int[MAXN];
}
// init PI
for(Gate* pi : PIs) {
value[pi->id] = pi->value;
}
for(Gate *g : gates) {
if(g->pi) continue;
value[g->id] = cal_value(g, value);
}
for(Gate *g : gates) {
assert(value[g->id] == cal_value(g, value));
}
std::queue<Gate*> q;
std::unordered_map<Gate*, int> 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;
}