atpg-ls/ls.cpp

407 lines
11 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-27 02:49:59 +00:00
Gate* picked_stem = ls_pick_good_var();
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
if(picked_stem == nullptr) {
ls_update_weight();
picked_stem = ls_pick_falsified_var();
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("[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-27 02:49:59 +00:00
ls_update(picked_stem);
2023-02-21 19:07:40 +08:00
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
}
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-27 02:49:59 +00:00
Gate* Circuit::ls_pick_falsified_var() {
std::vector<Gate*> candidates;
for(Gate *g : stems) {
if(g->po) continue;
if(g->stem_satisified) continue;
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()];
return pick;
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
Gate* Circuit::ls_pick_good_var() {
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
2023-02-27 02:49:59 +00:00
const int T = 100;
int t = 0;
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++) {
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;
2023-02-23 11:00:24 +08:00
}
2023-02-27 02:49:59 +00:00
if(t_score > 0) t++;
if(i >= T) break;
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-21 19:07:40 +08:00
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;
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
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-20 14:41:19 +08:00
ls_block_recal(stem);
2023-02-27 02:49:59 +00:00
2023-02-20 14:41:19 +08:00
}
2023-02-21 19:07:40 +08:00
ll Circuit::ls_pick_score(Gate* stem) {
2023-02-23 05:42:34 +00:00
ll old_score = ls_score();
2023-02-27 02:49:59 +00:00
//stem->value = !stem->value;
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-20 14:41:19 +08:00
2023-02-27 02:49:59 +00:00
//stem->value = !stem->value;
ls_update(stem);
2023-02-20 14:41:19 +08:00
2023-02-23 05:42:34 +00:00
old_score = std::max(old_score, ls_score());
2023-02-21 19:07:40 +08:00
return new_score - old_score;
}
ll Circuit::ls_score() {
2023-02-27 02:49:59 +00:00
ll score = - propagate_total_cost - 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) {
fault_falsified_cnt = faults.size();
stem_falsified_cnt = stems.size();
propagate_falsified_cnt = stems.size();
2023-02-20 13:08:25 +08:00
for(Gate* s : stems) {
2023-02-27 02:49:59 +00:00
s->stem_cost = 1;
stem_total_cost += s->stem_cost;
s->propagate_cost = 1;
propagate_total_cost += s->propagate_cost;
2023-02-20 13:08:25 +08:00
}
for(Fault* f : faults) {
2023-02-27 02:49:59 +00:00
f->gate->fault_cost[f->type] = 1;
fault_total_cost += f->gate->fault_cost[f->type];
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-27 02:49:59 +00:00
g->fault_satisfied[0] = 0;
g->fault_satisfied[1] = 0;
2023-02-23 11:00:24 +08:00
}
2023-02-19 19:42:50 +08:00
for(Gate* s : stems) {
s->value = rand() % 2;
}
for(int i=stems.size()-1; i>=0; i--) {
2023-02-25 11:08:50 +00:00
ls_update(stems[i]);
2023-02-19 19:42:50 +08:00
}
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
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-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-27 02:49:59 +00:00
// 当 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;
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
if(!stem->fault_satisfied[!stem->value]) {
stem->fault_satisfied[!stem->value] = true;
fault_total_cost -= stem->fault_cost[!stem->value];
fault_falsified_cnt -= 1;
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);
}
}
}
assert(q.empty());
used.clear();
2023-02-27 02:49:59 +00:00
for(Gate* stem : stem->suc_stems) {
2023-02-24 16:01:09 +08:00
q.push(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
}
}
while(!q.empty()) {
Gate *g = q.front();
q.pop();
used[g] = false;
bool right_value = (g->cal_value() == g->value);
2023-02-27 02:49:59 +00:00
2023-02-24 16:01:09 +08:00
for(Gate* in : g->inputs) {
in->value = !in->value;
bool input_detected = (g->cal_value() != g->value);
in->value = !in->value;
2023-02-27 02:49:59 +00:00
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;
2023-02-24 16:01:09 +08:00
in->sa_by_out[g] = std::make_pair(sa0, sa1);
2023-02-27 02:49:59 +00:00
bool new_sa[2];
new_sa[0] = new_sa[1] = 0;
2023-02-24 16:01:09 +08:00
2023-02-27 02:49:59 +00:00
in->fault_satisfied[0] = in->fault_satisfied[1] = 0;
2023-02-24 16:01:09 +08:00
for(Gate* out : in->outputs) {
auto &p = in->sa_by_out[out];
2023-02-27 02:49:59 +00:00
new_sa[0] |= p.first;
new_sa[1] |= p.second;
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
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;
}
2023-02-24 16:01:09 +08:00
2023-02-27 02:49:59 +00:00
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;
}
2023-02-24 16:01:09 +08:00
2023-02-27 02:49:59 +00:00
} 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;
}
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
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;
}
2023-02-24 16:01:09 +08:00
}
2023-02-27 02:49:59 +00:00
in->fault_satisfied[0] = new_sa[0];
in->fault_satisfied[1] = new_sa[1];
if(!in->stem && !used[in]) {
used[in] = true;
q.push(in);
2023-02-24 16:01:09 +08:00
}
}
}
}
2023-02-12 18:14:12 +08:00
}