atpg-ls/ls.cpp

536 lines
15 KiB
C++
Raw Normal View History

2023-02-16 18:51:31 +08:00
#include "circuit.h"
2023-02-12 18:14:12 +08:00
2023-02-27 02:49:59 +00:00
#include <cstddef>
2023-02-19 19:42:50 +08:00
#include <queue>
#include <unordered_set>
2023-02-21 19:07:40 +08:00
#include <unordered_map>
2023-02-19 19:42:50 +08:00
#include <algorithm>
#include "assert.h"
2023-02-23 11:00:24 +08:00
bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
2023-02-12 18:14:12 +08:00
2023-02-24 08:02:14 +00:00
//STEM_INC = 0;
2023-02-23 05:42:34 +00:00
2023-02-21 19:07:40 +08:00
// 初始化并重置所有 ls 数据结构
2023-02-20 13:08:25 +08:00
ls_init_data_structs();
// 随机生成初始电路
2023-02-27 02:49:59 +00:00
ls_init_circuit(faults);
2023-02-12 18:14:12 +08:00
printf("local search!\n");
2023-02-21 19:07:40 +08:00
while(true) {
2023-02-28 08:45:13 +00:00
bool value, propagate;
Gate* picked_stem = ls_pick_good_var(value, propagate);
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
if(picked_stem == nullptr) {
ls_update_weight();
2023-02-28 08:45:13 +00:00
picked_stem = ls_pick_falsified_var(value, propagate);
2023-02-27 02:49:59 +00:00
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);
2023-03-04 10:28:39 +00:00
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);
2023-02-27 02:49:59 +00:00
} 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);
2023-02-21 19:07:40 +08:00
}
2023-02-28 08:45:13 +00:00
picked_stem->value = value;
picked_stem->propagate = propagate;
2023-02-27 02:49:59 +00:00
ls_update(picked_stem);
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
//assert(is_valid_circuit());
2023-02-27 02:49:59 +00:00
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;
2023-02-21 19:07:40 +08:00
}
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);
}
}
2023-02-27 02:49:59 +00:00
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
static int original_faults = -1;
if(original_faults == - 1) {
original_faults = faults.size();
}
static int pattern = 0;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
std::unordered_set<Fault*> tmp = faults;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
for(Fault* f : tmp) {
if(f->gate->fault_satisfied[f->type]) {
faults.erase(f);
}
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
if(tmp.size() == faults.size()) pattern--;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
printf("coverage: %.4f\tpattern: %d\tbefore: %ld\tnow: %ld\n", (double)(original_faults - faults.size()) / (original_faults), ++pattern, tmp.size(), faults.size());
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
//if(tmp.size() == faults.size()) return false;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
return true;
}
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
Gate* Circuit::ls_pick_falsified_var(bool &value, bool &propagate) {
2023-02-27 02:49:59 +00:00
std::vector<Gate*> candidates;
for(Gate *g : stems) {
if(g->po) continue;
if(g->stem_satisified) continue;
if(g->propagate_satisfied) continue;
2023-02-27 02:49:59 +00:00
candidates.push_back(g);
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
if(candidates.size() == 0) {
candidates.push_back(stems[rand()%stems.size()]);
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
Gate* pick = candidates[rand()%candidates.size()];
value = !pick->value;
propagate = !propagate;
2023-02-27 02:49:59 +00:00
return pick;
}
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
Gate* Circuit::ls_pick_good_var(bool &value, bool &propagate) {
2023-02-27 02:49:59 +00:00
Gate* stem = nullptr;
ll max_score = 0;
std::vector<Gate*> stems_random;
std::vector<Gate*> candidates;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
for(Gate* s : stems) {
if(s->CC) {
stems_random.push_back(s);
2023-02-21 19:07:40 +08:00
}
}
2023-02-27 02:49:59 +00:00
for(int i=0; i<stems_random.size(); i++) {
std::swap(stems_random[i], stems_random[rand()%stems_random.size()]);
2023-02-23 11:00:24 +08:00
}
2023-02-12 18:14:12 +08:00
const int T = 25;
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
for(int i=0; i < stems_random.size() && i < T; i++) {
2023-02-27 02:49:59 +00:00
Gate* t_stem = stems_random[i];
2023-02-28 08:45:13 +00:00
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;
}
2023-02-23 11:00:24 +08:00
}
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
if(max_score > 0) {
return stem;
} else {
return nullptr;
}
2023-02-23 11:00:24 +08:00
}
void Circuit::ls_update_weight() {
2023-02-23 05:42:34 +00:00
2023-02-24 08:02:14 +00:00
//STEM_INC += 5;
2023-02-23 05:42:34 +00:00
2023-02-27 02:49:59 +00:00
if(false) {
2023-02-21 19:07:40 +08:00
} else {
for(Gate* g : gates) {
2023-02-27 02:49:59 +00:00
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* 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;
}
}
}
2023-02-21 19:07:40 +08:00
}
2023-02-27 02:49:59 +00:00
if(g->stem && !g->stem_satisified && (g->stem_cost + STEM_INC <= STEM_COST_MAX)) {
g->stem_cost += STEM_INC;
stem_total_cost += STEM_INC;
2023-02-21 19:07:40 +08:00
if(g->stem_satisified)
2023-02-21 19:07:40 +08:00
for(Gate* suc : g->suc_stems) {
2023-02-27 02:49:59 +00:00
if(suc->stem_cost - STEM_INC >= 1) {
suc->stem_cost -= STEM_INC;
if(!suc->stem_satisified) {
stem_total_cost -= STEM_INC;
2023-02-21 19:07:40 +08:00
}
}
}
2023-02-23 05:42:34 +00:00
}
2023-02-23 11:00:24 +08:00
2023-02-27 02:49:59 +00:00
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;
2023-02-24 08:02:14 +00:00
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
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;
2023-02-21 19:07:40 +08:00
}
}
}
}
2023-02-19 19:42:50 +08:00
bool cmp(Gate* a, Gate *b) {
2023-02-20 13:08:25 +08:00
return a->id > b->id;
2023-02-19 19:42:50 +08:00
}
2023-02-20 14:41:19 +08:00
void Circuit::ls_update(Gate* stem) {
2023-02-27 02:49:59 +00:00
stem->CC = 0;
for(Gate* pre : stem->pre_stems) {
pre->CC = 1;
}
for(Gate* suc : stem->suc_stems) {
suc->CC = 1;
}
2023-02-28 08:45:13 +00:00
2023-02-20 14:41:19 +08:00
ls_block_recal(stem);
2023-02-27 02:49:59 +00:00
2023-02-28 08:45:13 +00:00
//assert(is_valid_circuit());
2023-02-20 14:41:19 +08:00
}
2023-02-28 08:45:13 +00:00
ll Circuit::ls_pick_score(Gate* stem, bool value, bool propagate) {
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
//printf("[pick: %s\n", stem->name.c_str());
2023-02-23 05:42:34 +00:00
2023-02-28 08:45:13 +00:00
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);
2023-02-28 08:45:13 +00:00
bool old_value = stem->value;
bool old_propagate = stem->propagate;
stem->value = value;
stem->propagate = propagate;
2023-02-27 02:49:59 +00:00
ls_update(stem);
2023-02-20 14:41:19 +08:00
2023-02-21 19:07:40 +08:00
ll new_score = ls_score();
2023-02-28 08:45:13 +00:00
// print_gates();
// printf("--------------");
2023-02-20 14:41:19 +08:00
//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);
2023-02-28 08:45:13 +00:00
stem->value = old_value;
stem->propagate = old_propagate;
2023-02-27 02:49:59 +00:00
ls_update(stem);
2023-02-20 14:41:19 +08:00
2023-02-28 08:45:13 +00:00
ll old_score2 = ls_score();
// print_gates();
// printf("--------------");
2023-03-02 07:03:29 +00:00
//assert(old_score1 == old_score2);
2023-02-28 08:45:13 +00:00
// if(old_score1 != old_score2) {
// printf("old1: %lld old2: %lld\n", old_score1, old_score2);
//ed_cnt);
2023-02-28 08:45:13 +00:00
// exit(-1);
// }
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
return new_score - old_score1;
2023-02-21 19:07:40 +08:00
}
ll Circuit::ls_score() {
2023-02-27 02:49:59 +00:00
ll score = - propagate_total_cost - stem_total_cost - fault_total_cost;
//ll score = - stem_total_cost - fault_total_cost;
2023-02-20 14:41:19 +08:00
return score;
}
2023-02-27 02:49:59 +00:00
void Circuit::ls_init_circuit(const std::unordered_set<Fault*> &faults) {
2023-02-28 08:45:13 +00:00
// 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;
}
2023-02-27 02:49:59 +00:00
2023-02-20 13:08:25 +08:00
for(Gate* s : stems) {
2023-03-02 07:03:29 +00:00
s->stem_cost = STEM_INC;
2023-02-27 02:49:59 +00:00
stem_total_cost += s->stem_cost;
2023-02-28 08:45:13 +00:00
stem_falsified_cnt += 1;
2023-02-27 02:49:59 +00:00
2023-03-02 07:03:29 +00:00
s->propagate_cost = PROPAGATE_INC;
2023-02-27 02:49:59 +00:00
propagate_total_cost += s->propagate_cost;
2023-02-28 08:45:13 +00:00
propagate_falsified_cnt += 1;
2023-02-20 13:08:25 +08:00
}
2023-02-19 19:42:50 +08:00
2023-02-23 11:00:24 +08:00
for(Gate* g : gates) {
2023-02-28 08:45:13 +00:00
g->CC = 1;
2023-02-23 11:00:24 +08:00
2023-02-28 08:45:13 +00:00
fault_total_cost += g->fault_cost[0];
fault_total_cost += g->fault_cost[1];
fault_falsified_cnt += 2;
}
2023-02-19 19:42:50 +08:00
for(Gate* s : stems) {
s->value = rand() % 2;
2023-02-28 08:45:13 +00:00
s->propagate = rand() % 2;
2023-02-19 19:42:50 +08:00
}
for(int i=stems.size()-1; i>=0; i--) {
2023-02-28 08:45:13 +00:00
ls_block_recal(stems[i]);
2023-02-19 19:42:50 +08:00
}
2023-02-21 19:07:40 +08:00
2023-02-28 08:45:13 +00:00
if(!is_valid_circuit()) {
print_gates();
exit(-1);
}
//assert(is_valid_circuit())
2023-02-20 13:08:25 +08:00
}
void Circuit::ls_init_data_structs() {
2023-02-27 02:49:59 +00:00
propagate_total_cost = 0;
propagate_falsified_cnt = 0;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
stem_total_cost = 0;
stem_falsified_cnt = 0;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
fault_total_cost = 0;
fault_falsified_cnt = 0;
2023-02-20 13:08:25 +08:00
2023-02-27 02:49:59 +00:00
for(Gate* g : gates) {
g->CC = 0;
2023-02-20 13:08:25 +08:00
2023-02-27 02:49:59 +00:00
g->propagate = 0;
g->propagate_cost = 0;
g->propagate_satisfied = 0;
2023-02-20 13:08:25 +08:00
2023-02-27 02:49:59 +00:00
g->stem_cost = 0;
g->stem_satisified = 0;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
g->fault_satisfied[0] = g->fault_satisfied[1] = 0;
g->fault_cost[0] = g->fault_cost[1] = 0;
2023-02-20 13:08:25 +08:00
}
2023-02-24 16:01:09 +08:00
}
2023-02-25 11:08:50 +00:00
void Circuit::ls_block_recal(Gate* stem) {
2023-02-24 16:01:09 +08:00
2023-02-28 08:45:13 +00:00
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) {
// 修改当前节点的值信息
2023-02-27 02:49:59 +00:00
if(stem->cal_value() == stem->value && !stem->stem_satisified) {
stem->stem_satisified = true;
stem_total_cost -= stem->stem_cost;
stem_falsified_cnt -= 1;
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
if(stem->cal_value() != stem->value && stem->stem_satisified) {
stem->stem_satisified = false;
stem_total_cost += stem->stem_cost;
stem_falsified_cnt += 1;
}
2023-02-24 16:01:09 +08:00
2023-02-28 08:45:13 +00:00
assert((stem->cal_value() == stem->value) == stem->stem_satisified);
2023-02-24 16:01:09 +08:00
2023-02-28 08:45:13 +00:00
// 更新后继块的值
2023-02-24 16:01:09 +08:00
std::queue<Gate*> q;
std::unordered_map<Gate*, int> used;
q.push(stem);
while(!q.empty()) {
Gate* g = q.front();
q.pop();
used[g] = false;
for(Gate* out : g->outputs) {
2023-02-27 02:49:59 +00:00
if(out->stem) continue;
2023-02-24 16:01:09 +08:00
out->value = out->cal_value();
if(!used[out]) {
used[out] = true;
q.push(out);
}
}
}
2023-02-28 08:45:13 +00:00
// 更新后继 stem 的关系
2023-02-27 02:49:59 +00:00
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;
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
if(stem->cal_value() != stem->value && stem->stem_satisified) {
stem->stem_satisified = false;
stem_total_cost += stem->stem_cost;
stem_falsified_cnt += 1;
2023-02-24 16:01:09 +08:00
}
}
2023-02-28 08:45:13 +00:00
}
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;
}
2023-02-24 16:01:09 +08:00
2023-02-28 08:45:13 +00:00
std::queue<Gate*> q;
std::unordered_map<Gate*, int> used;
q.push(stem);
// 修改之前的所有 gate 错误信息直到 stem 边界
2023-02-24 16:01:09 +08:00
while(!q.empty()) {
Gate *g = q.front();
q.pop();
used[g] = false;
2023-02-28 08:45:13 +00:00
// 遍历输入迭代
2023-02-24 16:01:09 +08:00
for(Gate* in : g->inputs) {
2023-02-28 08:45:13 +00:00
// 输入如果是 stem则更新 propagate 信息是否匹配
2023-02-27 02:49:59 +00:00
if(in->stem) {
2023-02-28 08:45:13 +00:00
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;
2023-02-27 02:49:59 +00:00
}
2023-02-24 16:01:09 +08:00
2023-02-28 08:45:13 +00:00
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;
2023-02-24 16:01:09 +08:00
}
2023-02-28 08:45:13 +00:00
}
// 如果是中间节点,直接更新值
else {
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--;
}
2023-02-27 02:49:59 +00:00
}
2023-02-24 16:01:09 +08:00
}
2023-02-28 08:45:13 +00:00
in->propagate = in->fault_satisfied[0] || in->fault_satisfied[1];
2023-02-27 02:49:59 +00:00
2023-02-28 08:45:13 +00:00
if(!used[in]) {
2023-02-27 02:49:59 +00:00
used[in] = true;
q.push(in);
2023-02-24 16:01:09 +08:00
}
}
}
}
2023-02-28 08:45:13 +00:00
2023-02-12 18:14:12 +08:00
}