ACEC/acec.cpp
2022-10-14 14:04:57 +08:00

147 lines
4.6 KiB
C++

#include <bits/stdc++.h>
#include "vec.hpp"
#include "circuit.hpp"
#include "sweep.hpp"
#include "libopenf4.h"
extern "C" {
#include "aiger.h"
}
#define pvar(i) ((string((i) < 0 ? "N" : "") + "A" + std::to_string(abs(i))).c_str())
Sweep* sweep_init(int argv, char* args[]) {
Sweep *S = new Sweep();
S->aig = aiger_init();
S->det_v_sz = 0;
S->file = fopen(args[1], "r");
return S;
}
int main(int argv, char* args[]) {
Sweep *S = sweep_init(argv, args);
S->solve();
using namespace std;
//using namespace F4;
// Create polynomial array.
vector<string> polynomialArray;
// Create variable name array.
vector<string> variableName;
set<string> vars;
int t = S->maxvar;
for (int i = 1; i <= S->maxvar; i++) {
circuit *c = &(S->C[i]);
if (!c->sz || S->del[i] == 2) continue;
if(c->type == And) {
char term[1024];
sprintf(term, "-%s+%s", pvar(i * (S->C[i].neg ? -1 : 1)), pvar(c->to[0]));
for(int i=1; i<c->sz; i++) {
sprintf(term, "%s*%s", term, pvar(c->to[i]));
}
polynomialArray.push_back(term);
printf("%s\n", term);
}
if(c->type == Xor) {
string last = pvar(c->to[0]);
char term[1024];
for(int i=1; i<c->sz-1; i++) {
string tmp = pvar(++t);
sprintf(term, "-%s-2*%s*%s+%s+%s", tmp.c_str(), last.c_str(), pvar(c->to[i]), last.c_str(), pvar(c->to[i]));
polynomialArray.push_back(term);
printf("%s\n", term);
last = tmp;
}
sprintf(term, "-%s-2*%s*%s+%s+%s", pvar(i * (S->C[i].neg ? -1 : 1)), last.c_str(), pvar(c->to[c->sz-1]), last.c_str(), pvar(c->to[c->sz-1]));
polynomialArray.push_back(term);
printf("%s\n", term);
}
if (c->type == HA || c->type == FA) {
char term[1024];
sprintf(term, "-%s-2*%s+%s", pvar(i * (S->C[i].neg ? -1 : 1)), pvar(c->carrier), pvar(c->to[0]));
for(int i=1; i<c->sz; i++) {
sprintf(term, "%s+%s", term, pvar(c->to[i]));
}
polynomialArray.push_back(term);
printf("%s\n", term);
}
if(c->type == Majority) {
char term[1024];
sprintf(term, "-%s-2*%s+%s", pvar(++t), pvar(i * (S->C[i].neg ? -1 : 1)), pvar(c->to[0]));
for(int i=1; i<c->sz; i++) {
sprintf(term, "%s+%s", term, pvar(c->to[i]));
}
polynomialArray.push_back(term);
printf("%s\n", term);
}
// if (c->type == HA || c->type == FA) {
// printf("( %d %d )", i * (S->C[i].neg ? -1 : 1), c->carrier);
// }
// else {
// printf("%d", i * (S->C[i].neg ? -1 : 1));
// }
// printf(" = %s ( ", gate_type[c->type].c_str());
// for (int j = 0; j < c->sz; j++) {
// printf("%d ", c->to[j]);
// }
// puts(")");
}
for(int i=t; i>=1; i--) {
variableName.push_back(pvar(i));
variableName.push_back(pvar(-i));
char term[1024];
sprintf(term, "-%s+%s^2", pvar(i), pvar(i));
polynomialArray.emplace_back(term);
printf("%s\n", term);
sprintf(term, "%s+%s", pvar(i), pvar(-i));
polynomialArray.emplace_back(term);
printf("%s\n", term);
}
char term[1024];
sprintf(term, "-%s+1", pvar(S->maxvar));
polynomialArray.emplace_back(term);
printf("%s\n", term);
// for(int i = 0; i < 6; i++)
// {
// variableName.push_back('x'+to_string(i));
// }
// // Fill the polynomial array.
// polynomialArray.emplace_back("x0+x1+x2+x3+x4+x5");
// polynomialArray.emplace_back("x0*x1+x1*x2+x2*x3+x3*x4+x0*x5+x4*x5");
// polynomialArray.emplace_back("x0*x1*x2+x1*x2*x3+x2*x3*x4+x0*x1*x5+x0*x4*x5+x3*x4*x5");
// polynomialArray.emplace_back("x0*x1*x2*x3+x1*x2*x3*x4+x0*x1*x2*x5+x0*x1*x4*x5+x0*x3*x4*x5+x2*x3*x4*x5");
// polynomialArray.emplace_back("x0*x1*x2*x3*x4+x0*x1*x2*x3*x5+x0*x1*x2*x4*x5+x0*x1*x3*x4*x5+x0*x2*x3*x4*x5+x1*x2*x3*x4*x5");
// polynomialArray.emplace_back("x0*x1*x2*x3*x4*x5-1");
// Compute a reduce groebner basis.
vector<string> basis = groebnerBasisF4(7, variableName.size(), variableName, polynomialArray, 1, 0);
for(size_t i = 0; i < basis.size(); i++)
{
cout << basis[i] << endl;
}
for(int i=t; i>=1; i--) {
printf("(declare-fun %s () Bool)", pvar(i));
printf("(declare-fun %s () Bool)", pvar(-i));
}
return 0;
}