201 lines
5.1 KiB
C++
201 lines
5.1 KiB
C++
#include "circuit.h"
|
|
|
|
#include <queue>
|
|
#include <unordered_set>
|
|
#include <algorithm>
|
|
#include "assert.h"
|
|
|
|
std::vector<int> Circuit::local_search(const std::vector<Fault*> &faults) {
|
|
|
|
// 初始化并清零所有 ls 数据结构
|
|
ls_init_data_structs();
|
|
|
|
// 赋值初始权重
|
|
ls_init_weight(faults);
|
|
|
|
// 随机生成初始电路
|
|
ls_init_circuit();
|
|
|
|
printf("local search!\n");
|
|
|
|
//ls_flip(PIs[0]);
|
|
|
|
//print_gates();
|
|
|
|
return std::vector<int>();
|
|
}
|
|
|
|
bool cmp(Gate* a, Gate *b) {
|
|
return a->id > b->id;
|
|
}
|
|
|
|
void Circuit::ls_flip(Gate* stem) {
|
|
|
|
if(flip_need_update[stem->id]) {
|
|
flip_need_update[stem->id] = false;
|
|
flip_total_weight -= flip_weight[stem->id];
|
|
}
|
|
|
|
//printf("flip: %s\n", stem->name.c_str());
|
|
|
|
//stem->value = !stem->value;
|
|
|
|
if(stem->isPO) {
|
|
stem->sa[!stem->value] = true;
|
|
stem->sa[stem->value] = false;
|
|
}
|
|
|
|
std::queue<Gate*> q;
|
|
std::unordered_map<Gate*, int> used;
|
|
std::vector<Gate*> suc_stems;
|
|
|
|
q.push(stem);
|
|
|
|
while(!q.empty()) {
|
|
Gate* g = q.front();
|
|
q.pop();
|
|
used[g] = false;
|
|
for(Gate* out : g->outputs) {
|
|
if(out->stem) {
|
|
suc_stems.push_back(out);
|
|
continue;
|
|
}
|
|
out->value = out->cal_value();
|
|
if(!used[out]) {
|
|
used[out] = true;
|
|
q.push(out);
|
|
}
|
|
}
|
|
}
|
|
|
|
assert(q.empty());
|
|
used.clear();
|
|
|
|
for(Gate* stem : suc_stems) {
|
|
q.push(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;
|
|
|
|
//printf("gate: %s -> %s rv: %d id: %d p:%d sa0: %d sa1: %d\n", in->name.c_str(), g->name.c_str(), right_value, input_detected, g->is_propagated(), sa0, sa1);
|
|
|
|
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];
|
|
//printf("%d %d\n", p.first, p.second);
|
|
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])) {
|
|
|
|
bool exist = false;
|
|
for(Gate* pre : in->pre_stems) {
|
|
if(flip_need_update[pre->id]) {
|
|
exist = true;
|
|
}
|
|
}
|
|
|
|
if(!exist) {
|
|
Gate* pre = in->pre_stems[0];
|
|
flip_need_update[pre->id] = true;
|
|
flip_update_queue.push_back(pre);
|
|
|
|
flip_total_weight += flip_weight[pre->id];
|
|
}
|
|
}
|
|
|
|
//printf("gate: %s -> %s rv: %d id: %d p:%d sa0: %d sa1: %d\n", in->name.c_str(), g->name.c_str(), right_value, input_detected, g->is_propagated(), in->sa[0], in->sa[1]);
|
|
|
|
if(!in->stem && !used[in]) {
|
|
used[in] = true;
|
|
q.push(in);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Circuit::ls_init_weight(const std::vector<Fault*> &faults) {
|
|
for(Gate* s : stems) {
|
|
stem_weight[s->id] = 1;
|
|
}
|
|
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() {
|
|
// for(auto pi : PIs) {
|
|
// pi->value = rand() % 2;
|
|
// }
|
|
|
|
for(Gate* s : stems) {
|
|
s->value = rand() % 2;
|
|
}
|
|
|
|
for(int i=stems.size()-1; i>=0; i--) {
|
|
ls_flip(stems[i]);
|
|
}
|
|
|
|
while(!flip_update_queue.empty()) {
|
|
Gate* g = flip_update_queue.back();
|
|
flip_update_queue.pop_back();
|
|
flip_need_update[g->id] = false;
|
|
ls_flip(g);
|
|
}
|
|
}
|
|
|
|
void Circuit::ls_init_data_structs() {
|
|
const int MAX_LEN = gates.size() + 1;
|
|
|
|
if(flip_weight == nullptr) {
|
|
flip_weight = new int[MAX_LEN];
|
|
flip_need_update = new int[MAX_LEN];
|
|
|
|
stem_weight = new int[MAX_LEN];
|
|
stem_satisfied = new int[MAX_LEN];
|
|
|
|
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];
|
|
}
|
|
|
|
} else {
|
|
for(int i=0; i<MAX_LEN; i++) {
|
|
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;
|
|
}
|
|
}
|
|
} |