修改数据结构,通过编译
This commit is contained in:
parent
589eeb7432
commit
a9e6804d15
81
circuit.cpp
81
circuit.cpp
@ -5,6 +5,16 @@
|
||||
#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) {
|
||||
@ -17,7 +27,7 @@ void Circuit::init_stems() {
|
||||
}
|
||||
|
||||
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,48 @@ 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->propagate_satisfied) {
|
||||
propagate_total_cost += g->propagate_cost;
|
||||
propagate_falsified_cnt += 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 检查 PO 的传播设定是否正确
|
||||
if(g->isPO) {
|
||||
if(g->sa[g->value] != 0 || g->sa[!g->value] == 0 ) {
|
||||
if(g->po) {
|
||||
if(g->fault_satisfied[g->value] != 0 || g->fault_satisfied[!g->value] == 0 ) {
|
||||
printf("WRONG-PO: %s \n", g->name.c_str());
|
||||
}
|
||||
continue;
|
||||
@ -163,30 +170,30 @@ bool Circuit::is_valid_circuit() {
|
||||
g->value = !g->value;
|
||||
|
||||
if(out->cal_value() != out->value) {
|
||||
sa0 |= out->is_propagated() && !g->value;
|
||||
sa1 |= out->is_propagated() && g->value;
|
||||
sa0 |= out->propagate && !g->value;
|
||||
sa1 |= out->propagate && g->value;
|
||||
}
|
||||
|
||||
g->value = !g->value;
|
||||
}
|
||||
|
||||
if(sa0 != g->sa[0] || sa1 != g->sa[1]) {
|
||||
if(sa0 != g->fault_satisfied[0] || sa1 != 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;
|
||||
}
|
||||
|
||||
|
80
circuit.h
80
circuit.h
@ -10,24 +10,39 @@ 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;
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
@ -60,46 +75,37 @@ void init_stems();
|
||||
|
||||
// local search
|
||||
|
||||
// shared info
|
||||
static const int STEM_INC = 10;
|
||||
static const int STEM_COST_MAX = 1e9;
|
||||
static ll stem_total_cost;
|
||||
static int stem_falsified_cnt;
|
||||
|
||||
static const int PROPAGATE_INC = 10;
|
||||
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_flip(Gate* stem);
|
||||
Gate* ls_pick_good_var();
|
||||
Gate* ls_pick_falsified_var();
|
||||
|
||||
void ls_update(Gate* stem);
|
||||
ll ls_pick_score(Gate* stem);
|
||||
|
||||
|
4
gate.cpp
4
gate.cpp
@ -2,10 +2,6 @@
|
||||
|
||||
#include "assert.h"
|
||||
|
||||
bool Gate::is_propagated() {
|
||||
return sa[0] || sa[1];
|
||||
}
|
||||
|
||||
int Gate::cal_value() {
|
||||
int res;
|
||||
|
||||
|
504
ls.cpp
504
ls.cpp
@ -1,5 +1,6 @@
|
||||
#include "circuit.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <queue>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
@ -13,105 +14,29 @@ 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;
|
||||
Gate* picked_stem = ls_pick_good_var();
|
||||
|
||||
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();
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
ls_update(picked_stem);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,90 +49,113 @@ 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() {
|
||||
std::vector<Gate*> candidates;
|
||||
for(Gate *g : stems) {
|
||||
if(g->po) continue;
|
||||
if(g->stem_satisified) continue;
|
||||
candidates.push_back(g);
|
||||
}
|
||||
|
||||
if(candidates.size() == 0) {
|
||||
candidates.push_back(stems[rand()%stems.size()]);
|
||||
}
|
||||
|
||||
Gate* pick = candidates[rand()%candidates.size()];
|
||||
return pick;
|
||||
}
|
||||
|
||||
Gate* Circuit::ls_pick_good_var() {
|
||||
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 = 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) {
|
||||
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 && !g->propagate_satisfied && (g->propagate_cost + PROPAGATE_INC <= PROPAGATE_COST_MAX)) {
|
||||
g->propagate_cost += PROPAGATE_INC;
|
||||
propagate_total_cost += PROPAGATE_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->stem_satisified && (g->stem_cost + STEM_INC <= STEM_COST_MAX)) {
|
||||
g->stem_cost += STEM_INC;
|
||||
stem_total_cost += STEM_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;
|
||||
if(suc->stem_cost - STEM_INC >= 1) {
|
||||
suc->stem_cost -= STEM_INC;
|
||||
if(!suc->stem_satisified) {
|
||||
stem_total_cost -= STEM_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->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(fault_weight[suc->id][1] + inc <= FAULT_WEIGHT_MAX) {
|
||||
// fault_weight[suc->id][1] += inc;
|
||||
// if(suc->sa[1]) fault_total_weight += 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[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[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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -217,22 +165,34 @@ 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);
|
||||
|
||||
}
|
||||
ll Circuit::ls_pick_score(Gate* stem) {
|
||||
|
||||
ll old_score = ls_score();
|
||||
|
||||
ls_flip(stem);
|
||||
//stem->value = !stem->value;
|
||||
|
||||
ls_update(stem);
|
||||
|
||||
ll new_score = ls_score();
|
||||
|
||||
ls_flip(stem);
|
||||
//stem->value = !stem->value;
|
||||
|
||||
ls_update(stem);
|
||||
|
||||
old_score = std::max(old_score, ls_score());
|
||||
|
||||
@ -240,29 +200,32 @@ ll Circuit::ls_pick_score(Gate* stem) {
|
||||
}
|
||||
|
||||
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;
|
||||
return score;
|
||||
}
|
||||
|
||||
void Circuit::ls_init_weight(const std::unordered_set<Fault*> &faults) {
|
||||
|
||||
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(Gate* s : stems) {
|
||||
stem_weight[s->id] = 1;
|
||||
stem_total_weight += stem_weight[s->id];
|
||||
s->stem_cost = 1;
|
||||
stem_total_cost += s->stem_cost;
|
||||
|
||||
s->propagate_cost = 1;
|
||||
propagate_total_cost += s->propagate_cost;
|
||||
}
|
||||
for(Fault* f : faults) {
|
||||
fault_weight[f->gate->id][f->type] = 1;
|
||||
f->gate->fault_cost[f->type] = 1;
|
||||
fault_total_cost += f->gate->fault_cost[f->type];
|
||||
}
|
||||
for(Gate* s: stems) {
|
||||
flip_weight[s->id] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Circuit::ls_init_circuit() {
|
||||
|
||||
for(Gate* g : gates) {
|
||||
g->sa[0] = 0;
|
||||
g->sa[1] = 0;
|
||||
g->fault_satisfied[0] = 0;
|
||||
g->fault_satisfied[1] = 0;
|
||||
}
|
||||
|
||||
for(Gate* s : stems) {
|
||||
@ -273,105 +236,66 @@ void Circuit::ls_init_circuit() {
|
||||
ls_update(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);
|
||||
}
|
||||
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;
|
||||
|
||||
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] = 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;
|
||||
if(stem->cal_value() != stem->value && stem->stem_satisified) {
|
||||
stem->stem_satisified = false;
|
||||
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;
|
||||
// 当 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;
|
||||
}
|
||||
|
||||
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->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;
|
||||
std::vector<Gate*> suc_stems;
|
||||
|
||||
q.push(stem);
|
||||
|
||||
@ -380,10 +304,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;
|
||||
@ -395,19 +316,19 @@ void Circuit::ls_block_recal(Gate* stem) {
|
||||
assert(q.empty());
|
||||
used.clear();
|
||||
|
||||
for(Gate* stem : suc_stems) {
|
||||
for(Gate* stem : 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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -418,63 +339,68 @@ void Circuit::ls_block_recal(Gate* stem) {
|
||||
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;
|
||||
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 old_sa[2];
|
||||
old_sa[0] = in->sa[0];
|
||||
old_sa[1] = in->sa[1];
|
||||
bool new_sa[2];
|
||||
new_sa[0] = new_sa[1] = 0;
|
||||
|
||||
in->sa[0] = in->sa[1] = 0;
|
||||
in->fault_satisfied[0] = in->fault_satisfied[1] = 0;
|
||||
for(Gate* out : in->outputs) {
|
||||
auto &p = in->sa_by_out[out];
|
||||
in->sa[0] |= p.first;
|
||||
in->sa[1] |= p.second;
|
||||
new_sa[0] |= p.first;
|
||||
new_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;
|
||||
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(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->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(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;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
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->fault_satisfied[0] = new_sa[0];
|
||||
in->fault_satisfied[1] = new_sa[1];
|
||||
|
||||
if(!in->stem && !used[in]) {
|
||||
used[in] = true;
|
||||
q.push(in);
|
||||
}
|
||||
}
|
||||
|
||||
if(!in->stem && !used[in]) {
|
||||
used[in] = true;
|
||||
q.push(in);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22651
output.txt
22651
output.txt
File diff suppressed because it is too large
Load Diff
14
parser.cpp
14
parser.cpp
@ -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->fault_satisfied[0] = gate->fault_satisfied[1] = 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->fault_satisfied[0] = gate->fault_satisfied[1] = 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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user