#pragma once #include #include #include #include #include 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; bool stem; bool pi; bool po; std::vector pre_stems; std::vector suc_stems; std::vector outputs; std::vector inputs; // Status Define (Two var) bool value; bool propagate; // local search bool stem_satisified; int stem_cost; // 和求得的值是否一致 bool propagate_satisfied; int propagate_cost; // 这个由 value 和 propagate 决定 int fault_satisfied[2]; int fault_cost[2]; // configuration checking bool CC; std::unordered_map> sa_by_out; int cal_value(); bool cal_sa(bool x); //bool sa(bool x); }; class Fault { public: Gate* gate; enum Type { SA0, SA1 } type; Fault(Gate* gate, Type type):gate(gate),type(type) {} }; class Circuit { public: std::vector PIs; std::vector POs; std::vector gates; std::vector stems; // PI + stems std::unordered_map name2gate; std::queue tmp; std::unordered_map tmp_used; void parse_from_file(const char *filename); void print_gates(); bool is_valid_circuit(); void init_topo_index(); void init_stems(); // local search // shared info static const int STEM_INC = 2; static const int STEM_COST_MAX = 1e9; static ll stem_total_cost; static int stem_falsified_cnt; static const int PROPAGATE_INC = 2; 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 &faults); // incremental flip struct const double SP = 0.01; void ls_init_circuit(const std::unordered_set &faults); void ls_update_weight(); void ls_init_data_structs(); void ls_block_recal(Gate* stem); void ls_block_recal_value(Gate* stem); void ls_block_recal_fault(Gate* stem); Gate* ls_pick_good_var(bool &value, bool &propagate); Gate* ls_pick_falsified_var(bool &value, bool &propagate); void ls_update(Gate* stem); ll ls_pick_score(Gate* stem, bool value, bool propagate); ll ls_score(); // simulator int** simulate(); };