This commit is contained in:
YuhangQ 2023-05-10 10:32:52 +08:00
parent 85cd5aa62b
commit 482069812f
6 changed files with 66 additions and 132 deletions

BIN
atpg

Binary file not shown.

View File

@ -3,12 +3,10 @@
int Circuit::check_circuit() {
int sum_value_unsatisfied_cost = 0;
int sum_fault_need_update_cost = 0;
int sum_fault_propagated_weight = 0;
int sum_fault_propagated_unsatisfied_cost = 0;
int sum_fault_detected_weight = 0;
int sum_fault_detected_unsatisfied_cost = 0;
for(Gate* g : gates) {
@ -19,39 +17,31 @@ int Circuit::check_circuit() {
assert(g->value_satisfied == ( g->cal_value() == g->value ));
assert(g->fault_detected_satisfied[0] == ( g->cal_fault_detected(0) == g->fault_detected[0] ));
assert(g->fault_detected_satisfied[1] == ( g->cal_fault_detected(1) == g->fault_detected[1] ));
int changed = 0;
if(!changed && g->cal_fault_detected(0) != g->fault_detected[0]) changed = 1;
if(!changed && g->cal_fault_detected(1) != g->fault_detected[1]) changed = 1;
if(!changed && g->cal_propagate_len(0) != g->cal_propagate_len(0)) changed = 1;
if(!changed && g->cal_propagate_len(1) != g->cal_propagate_len(1)) changed = 1;
assert(g->fault_propagated_satisfied[0] == ( g->cal_propagate_len(0) == g->fault_propagated_len[0]));
// if(g->fault_propagated_satisfied[1] != ( g->cal_propagate_len(1) == g->fault_propagated_len[1])) {
// printf("pl: %s\n", g->name.c_str());
// print_gates();
// }
assert(g->fault_propagated_satisfied[1] == ( g->cal_propagate_len(1) == g->fault_propagated_len[1]));
assert(changed == g->fault_need_update);
if(!g->value_satisfied) sum_value_unsatisfied_cost += g->value_unsatisfied_cost;
if(g->fault_need_update) sum_fault_need_update_cost += g->fault_need_update_cost;
if(g->fault_detected[0]) sum_fault_detected_weight += g->fault_detected_weight[0];
if(g->fault_detected[1]) sum_fault_detected_weight += g->fault_detected_weight[1];
if(!g->fault_detected_satisfied[0]) sum_fault_detected_unsatisfied_cost += g->fault_detected_unsatisfied_cost[0];
if(!g->fault_detected_satisfied[1]) sum_fault_detected_unsatisfied_cost += g->fault_detected_unsatisfied_cost[1];
sum_fault_propagated_weight += g->fault_propagated_len[0] * g->fault_propagated_weight[0];
sum_fault_propagated_weight += g->fault_propagated_len[1] * g->fault_propagated_weight[1];
if(!g->fault_propagated_satisfied[0]) sum_fault_propagated_unsatisfied_cost += g->fault_propagated_unsatisfied_cost[0];
if(!g->fault_propagated_satisfied[1]) sum_fault_propagated_unsatisfied_cost += g->fault_propagated_unsatisfied_cost[1];
}
int circuit_score = sum_fault_propagated_weight + sum_fault_detected_weight
- sum_fault_propagated_unsatisfied_cost - sum_fault_detected_unsatisfied_cost - sum_value_unsatisfied_cost;
- sum_value_unsatisfied_cost - sum_fault_need_update_cost;
printf("svuc: %d, sfpw: %d, sfpuc: %d, sfdw: %d, sfduc: %d\n",
sum_value_unsatisfied_cost, sum_fault_propagated_weight, sum_fault_propagated_unsatisfied_cost,
sum_fault_detected_weight, sum_fault_detected_unsatisfied_cost);
sum_value_unsatisfied_cost,
sum_fault_propagated_weight, sum_fault_detected_weight);
return circuit_score;
}

View File

@ -32,14 +32,11 @@ public:
int fault_propagated_len[2];
int fault_propagated_weight[2];
int fault_propagated_satisfied[2];
int fault_propagated_unsatisfied_cost[2];
int fault_detected[2];
int fault_detected_weight[2];
int fault_detected_satisfied[2];
int fault_detected_unsatisfied_cost[2];
int fault_need_update;
int fault_need_update_cost;
int CC;
@ -68,12 +65,9 @@ public:
int score;
int score_value_unsatisfied_cost;
int score_fault_propagated_weight[2];
int score_fault_propagated_unsatisfied_cost[2];
int score_fault_detected_weight[2];
int score_fault_detected_unsatisfied_cost[2];
int score_fault_update_cost;
// score calculation function
@ -83,11 +77,9 @@ public:
int cal_score_fault_propagated_weight(int sa);
int cal_score_fault_propagated_unsatisfied_cost(int sa);
int cal_score_fault_detected_weight(int sa);
int cal_score_fault_detected_unsatisfied_cost(int sa);
int cal_score_fault_update_cost();
};
class Fault {

View File

@ -12,10 +12,6 @@ void Gate::update_gate_property() {
void Gate::update_gate_statistics() {
value_satisfied = ( cal_value() == value );
fault_detected_satisfied[0] = ( cal_fault_detected(0) == fault_detected[0] );
fault_detected_satisfied[1] = ( cal_fault_detected(1) == fault_detected[1] );
fault_propagated_satisfied[0] = ( cal_propagate_len(0) == fault_propagated_len[0] );
fault_propagated_satisfied[1] = ( cal_propagate_len(1) == fault_propagated_len[1] );
}
int Gate::cal_propagate_len(bool x) {

View File

@ -113,40 +113,17 @@ void Circuit::ls_flip(Gate* gate) {
int cal_cnt = 0;
for(Gate* g : gate->fanouts) {
// printf("cal2: %s\n", g->name.c_str());
t.insert(g);
g->score = g->cal_score();
cal_cnt++;
for(Gate* in : g->fanins) {
t.insert(in);
in->score = in->cal_score();
cal_cnt++;
for(Gate *out : in->fanouts) {
t.insert(out);
out->score = out->cal_score();
cal_cnt++;
for(Gate *in : out->fanins) {
t.insert(in);
in->score = in->cal_score();
cal_cnt++;
}
}
}
for(Gate* g : gate->reigon) {
g->score = g->cal_score();
}
for(Gate* g : gate->fanins) {
t.insert(g);
g->score = g->cal_score();
cal_cnt++;
for(Gate* out : g->fanouts) {
t.insert(out);
out->score = out->cal_score();
cal_cnt++;
for(Gate* in : out->fanins) {
t.insert(in);
in->score = in->cal_score();
cal_cnt++;
}
}
}
@ -180,11 +157,11 @@ void Circuit::ls_init_circuit(std::unordered_set<Fault*> &faults) {
// init value, weight and cost
for(Gate* g : gates) {
g->value = rand() % 2;
g->fault_need_update = 0;
g->value_unsatisfied_cost = 1;
g->fault_need_update_cost = 1;
g->fault_propagated_weight[0] = g->fault_propagated_weight[1] = 0;
g->fault_propagated_unsatisfied_cost[0] = g->fault_propagated_unsatisfied_cost[1] = 1;
g->fault_detected_weight[0] = g->fault_detected_weight[1] = 0;
g->fault_detected_unsatisfied_cost[0] = g->fault_detected_unsatisfied_cost[1] = 1;
}
for(Fault* f : faults) {

View File

@ -5,39 +5,49 @@ int Gate::cal_score(int debug) {
value ^= 1;
if(debug) {
// printf("lala: %s\n", this->name.c_str());
assert(score_value_unsatisfied_cost == cal_value_unsatisfied_cost());
assert(score_fault_propagated_weight[0] == cal_score_fault_propagated_weight(0));
assert(score_fault_propagated_weight[1] == cal_score_fault_propagated_weight(1));
assert(score_fault_propagated_unsatisfied_cost[0] == cal_score_fault_propagated_unsatisfied_cost(0));
assert(score_fault_propagated_unsatisfied_cost[1] == cal_score_fault_propagated_unsatisfied_cost(1));
assert(score_fault_detected_weight[0] == cal_score_fault_detected_weight(0));
assert(score_fault_detected_weight[1] == cal_score_fault_detected_weight(1));
assert(score_fault_detected_unsatisfied_cost[0] == cal_score_fault_detected_unsatisfied_cost(0));
assert(score_fault_detected_unsatisfied_cost[1] == cal_score_fault_detected_unsatisfied_cost(1));
}
// if(debug) {
// // printf("lala: %s\n", this->name.c_str());
// assert(score_value_unsatisfied_cost == cal_value_unsatisfied_cost());
// assert(score_fault_propagated_weight[0] == cal_score_fault_propagated_weight(0));
// assert(score_fault_propagated_weight[1] == cal_score_fault_propagated_weight(1));
// assert(score_fault_propagated_unsatisfied_cost[0] == cal_score_fault_propagated_unsatisfied_cost(0));
// assert(score_fault_propagated_unsatisfied_cost[1] == cal_score_fault_propagated_unsatisfied_cost(1));
// assert(score_fault_detected_weight[0] == cal_score_fault_detected_weight(0));
// assert(score_fault_detected_weight[1] == cal_score_fault_detected_weight(1));
// assert(score_fault_detected_unsatisfied_cost[0] == cal_score_fault_detected_unsatisfied_cost(0));
// assert(score_fault_detected_unsatisfied_cost[1] == cal_score_fault_detected_unsatisfied_cost(1));
// }
int origin_fault_detected[2];
origin_fault_detected[0] = this->fault_detected[0];
origin_fault_detected[1] = this->fault_detected[1];
this->fault_detected[0] = this->cal_fault_detected(0);
this->fault_detected[1] = this->cal_fault_detected(1);
int origin_fault_propagated_len[2];
origin_fault_propagated_len[0] = this->fault_propagated_len[0];
origin_fault_propagated_len[1] = this->fault_propagated_len[1];
this->fault_propagated_len[0] = this->cal_propagate_len(0);
this->fault_propagated_len[1] = this->cal_propagate_len(1);
score_value_unsatisfied_cost = cal_value_unsatisfied_cost();
score_fault_propagated_weight[0] = cal_score_fault_propagated_weight(0);
score_fault_propagated_weight[1] = cal_score_fault_propagated_weight(1);
score_fault_propagated_unsatisfied_cost[0] = cal_score_fault_propagated_unsatisfied_cost(0);
score_fault_propagated_unsatisfied_cost[1] = cal_score_fault_propagated_unsatisfied_cost(1);
score_fault_detected_weight[0] = cal_score_fault_detected_weight(0);
score_fault_detected_weight[1] = cal_score_fault_detected_weight(1);
score_fault_detected_unsatisfied_cost[0] = cal_score_fault_detected_unsatisfied_cost(0);
score_fault_detected_unsatisfied_cost[1] = cal_score_fault_detected_unsatisfied_cost(1);
score_fault_update_cost = cal_score_fault_update_cost();
value ^= 1;
this->fault_propagated_len[0] = origin_fault_propagated_len[0];
this->fault_propagated_len[1] = origin_fault_propagated_len[1];
this->fault_detected[0] = origin_fault_detected[0];
this->fault_detected[1] = origin_fault_detected[1];
return - score_value_unsatisfied_cost
return - score_value_unsatisfied_cost - score_fault_update_cost
+ score_fault_propagated_weight[0] + score_fault_propagated_weight[1]
- score_fault_propagated_unsatisfied_cost[0] - score_fault_propagated_unsatisfied_cost[1]
+ score_fault_detected_weight[0] + score_fault_detected_weight[1]
- score_fault_detected_unsatisfied_cost[0] - score_fault_detected_unsatisfied_cost[1];
+ score_fault_detected_weight[0] + score_fault_detected_weight[1];
}
int Gate::cal_value_unsatisfied_cost() {
@ -65,62 +75,31 @@ int Gate::cal_score_fault_propagated_weight(int sa) {
return res;
}
int Gate::cal_score_fault_propagated_unsatisfied_cost(int sa) {
int res = 0;
int origin_fault_propagated_len[2];
origin_fault_propagated_len[0] = this->fault_propagated_len[0];
origin_fault_propagated_len[1] = this->fault_propagated_len[1];
this->fault_propagated_len[0] = this->cal_propagate_len(0);
this->fault_propagated_len[1] = this->cal_propagate_len(1);
if(!this->fault_propagated_satisfied[sa]) {
res -= this->fault_propagated_unsatisfied_cost[sa];
}
for(Gate* g : this->reigon) {
if(g->fault_propagated_satisfied[sa] && g->cal_propagate_len(sa) != g->fault_propagated_len[sa]) {
res += g->fault_propagated_unsatisfied_cost[sa];
}
if(!g->fault_propagated_satisfied[sa] && g->cal_propagate_len(sa) == g->fault_propagated_len[sa]) {
res -= g->fault_propagated_unsatisfied_cost[sa];
}
}
this->fault_propagated_len[0] = origin_fault_propagated_len[0];
this->fault_propagated_len[1] = origin_fault_propagated_len[1];
return res;
}
int Gate::cal_score_fault_detected_weight(int sa) {
int res = 0;
res += ( this->cal_fault_detected(sa) - this->fault_detected[sa] ) * this->fault_detected_weight[sa];
return res;
}
int Gate::cal_score_fault_detected_unsatisfied_cost(int sa) {
int Gate::cal_score_fault_update_cost() {
int res = 0;
int origin_fault_detected[2];
origin_fault_detected[0] = this->fault_detected[0];
origin_fault_detected[1] = this->fault_detected[1];
this->fault_detected[0] = this->cal_fault_detected(0);
this->fault_detected[1] = this->cal_fault_detected(1);
if(!fault_detected_satisfied[sa]) {
res -= this->fault_detected_unsatisfied_cost[sa];
if(this->fault_need_update) {
res -= this->fault_need_update_cost;
}
for(Gate* g : this->reigon) {
if(g->fault_detected_satisfied[sa] && g->cal_fault_detected(sa) != g->fault_detected[sa]) {
res += g->fault_detected_unsatisfied_cost[sa];
}
if(!g->fault_detected_satisfied[sa] && g->cal_fault_detected(sa) == g->fault_detected[sa]) {
res -= g->fault_detected_unsatisfied_cost[sa];
}
}
for(Gate* g : fanins) {
bool changed = 0;
if(!changed && g->cal_fault_detected(0) != g->fault_detected[0]) changed = 1;
if(!changed && g->cal_fault_detected(1) != g->fault_detected[1]) changed = 1;
if(!changed && g->cal_propagate_len(0) != g->cal_propagate_len(0)) changed = 1;
if(!changed && g->cal_propagate_len(1) != g->cal_propagate_len(1)) changed = 1;
this->fault_detected[0] = origin_fault_detected[0];
this->fault_detected[1] = origin_fault_detected[1];
if(!g->fault_need_update && changed) {
res += g->fault_need_update_cost;
}
}
return res;
}