Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // g++ g.cpp -ggdb -std=c++11 -lmpfr -lgmp
- //
- #include <iostream>
- #include <cmath>
- #include <map>
- #include "mpreal.h"
- #include <string>
- #include <cstdlib>
- using namespace std;
- using namespace mpfr;
- typedef mpreal scalar;
- map<int, scalar> v,g;
- const scalar one = 1;
- class clause3 {
- clause3* next;
- clause3() {}
- public:
- int sx, sy, sz, nx, ny, nz;
- clause3(int n1, int n2, int n3, int s1, int s2, int s3, clause3* nxt = 0) :
- sx(s1), sy(s2), sz(s3), nx(n1), ny(n2), nz(n3), next(nxt) {}
- clause3(istream& is, clause3* prev = 0) { read(is, prev); }
- bool read(istream& is, clause3* prev = 0) {
- try {
- string str;
- getline(is, str);
- while (str[0] == 'c' || str[0] == 'p') { getline(is, str); }
- sscanf(str.c_str(), "%d %d %d 0", &nx, &ny, &nz);
- // if (!str.size()) return false;
- // v[nx = atoi(str.c_str())] = 0;
- if (!nx) return false;
- if (nx < 0) {
- nx = -nx;
- sx = 0;
- } else sx = 1;
- // getline(is, str,' ');
- // v[ny = atoi(str.c_str())] = 0;
- if (ny < 0) {
- ny = -ny;
- sy = 0;
- } else sy = 1;
- // getline(is, str,' ');
- // v[nz = atoi(str.c_str())] = 0;
- if (nz < 0) {
- nz = -nz;
- sz = 0;
- } else sz = 1;
- v[nx]=v[ny]=v[nz]=0;
- if (is.good()) {
- next = new clause3;
- // is.ignore(numeric_limits<std::streamsize>::max(), '\n');
- // getline(is, str);
- if (!next->read(is, this)) {
- delete next;
- next = 0;
- return true;
- }
- }
- else next = 0;
- return true;
- }
- catch (...) {
- return false;
- }
- }
- scalar operator()(bool print = false) const { // eval
- scalar x = v[nx], y = v[ny], z = v[nz];
- scalar r = one - (sx ? one - x : x) * (sy ? one - y : y) * (sz ? one - z : z);
- if (next) r *= (*next)(print);
- if (print) cout<<r<<' ';
- return r;
- }
- scalar operator()(int dv) const { // diff
- scalar r = 0;
- if (dv == nx)
- r = (sx?one:-one) * (sy ? one - v[ny] : v[ny]) * (sz ? one - v[nz] : v[nz]);
- else if (dv == ny)
- r = (sy?one:-one) * (sx ? one - v[nx] : v[nx]) * (sz ? one - v[nz] : v[nz]);
- else if (dv == nz)
- r = (sz?one:-one) * (sx ? one - v[nx] : v[nx]) * (sy ? one - v[ny] : v[ny]);
- if (!next) return r;
- if (r) return r * (*next)() + (*next)(dv) * (*this)();
- return (*next)(dv) * (*this)();
- }
- scalar operator()(int d1, int d2) const { // 2nd diff
- scalar r = 0;
- // if (d1 == nx && d2 == nx) return
- return r;
- }
- void print() const {
- cout<<'(';
- if (!sx)cout<<'!'; cout<<nx<<'|';
- if (!sy)cout<<'!'; cout<<ny<<'|';
- if (!sz)cout<<'!'; cout<<nz<<')';
- if (next) { cout<<" & "<<endl; next->print(); }
- else cout<<endl;
- }
- };
- int main(int,char**){
- mpreal::set_default_prec(512);
- const clause3& c = *new clause3(cin);
- //#define DOESNT_CONVERGE
- //c.print();
- c(true);
- cout<<endl;
- scalar d;
- for (auto x : v) v[x.first] = .5;
- for (uint n = 0; n < 3; n++) {
- #ifdef DOESNT_CONVERGE
- for (auto x : v)
- cout<<'d'<<x.first<<' '<<(g[x.first] = (c()-1)*c(x.first))<<'\t'; //
- for (auto x : v) v[x.first] -= g[x.first];
- #else
- for (auto x : v) if (rand()%3) {
- cout<<'d'<<x.first<<' '<<(d = (c()-1)*c(x.first))<<','; // to detect unsat we minimize (c-1)^2 so grad is prop to (c-1)dc
- v[x.first] -= d;
- cout<<v[x.first]<<'\t';
- }
- #endif
- cout<<endl<<c();
- cout<<"======="<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement